From 1cd6edbac317b6380a0b2302d417f6778538be70 Mon Sep 17 00:00:00 2001 From: Jeremy Andre Date: Sun, 27 Sep 2026 01:38:47 +0200 Subject: [PATCH] feat(sync-content): mirror docs/cli image and PDF assets into the site MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Guides in agentproto/ts can now embed screenshots and link a PDF, but sync-content.mjs only ever copied .md/meta.json — any relative image or PDF reference 404s once synced. Copy matched assets (png/jpg/gif/ svg/webp/pdf) to two places: content/docs/, where fumadocs-mdx's remarkImage plugin resolves a relative markdown image as a static import against the compiled .mdx file's own directory; and public/docs/, where Next serves it ahead of the docs catch-all route at the exact path a relative link resolves to from the page's own URL. Proved against agentproto/ts#1458's phone-pairing guide: all 5 screenshots and the PDF link render/200 through a real `next dev` render of the page. --- .gitignore | 1 + package.json | 2 +- scripts/sync-content.mjs | 66 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index bf30515..e64ad8d 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ node_modules # Synced content — always populated from agentproto/ts at build time content/docs +public/docs # Sync cache (git clone of agentproto/ts) .cache diff --git a/package.json b/package.json index 8072760..592a77a 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "lint": "next lint", "pretypecheck": "node scripts/sync-content.mjs && fumadocs-mdx", "typecheck": "tsc --noEmit", - "clean": "rm -rf .next .source content/docs .cache" + "clean": "rm -rf .next .source content/docs public/docs .cache" }, "dependencies": { "@xterm/addon-fit": "^0.11.0", diff --git a/scripts/sync-content.mjs b/scripts/sync-content.mjs index a6cd723..bb768cd 100644 --- a/scripts/sync-content.mjs +++ b/scripts/sync-content.mjs @@ -17,6 +17,27 @@ * `.mdx` so Fumadocs picks them up (Fumadocs needs `.mdx`; our source * is plain `.md` because it ships next to code, not next to a site). * + * Non-markdown assets (images, PDFs) referenced by a guide are copied + * verbatim, byte for byte, to TWO places: + * + * - `content/docs/` — fumadocs-mdx's `remarkImage` plugin turns a + * relative `![]()` src into a static `import` resolved against the + * compiled .mdx file's own directory (`fumadocs-core/dist/mdx-plugins/ + * remark-image.js`: `getImportPath` does `path.relative(dir, file)` + * where `dir` is the mdx file's dirname). Webpack/Turbopack need the + * real file sitting there at build time for that import to resolve — + * it is never fetched from `public/`. + * - `public/docs/` — a plain markdown *link* (not an image), e.g. a + * "download this as a PDF" link, is NOT rewritten by remarkImage (it + * only visits `image` nodes) and reaches the page as a literal + * relative `href`. The browser resolves that against the current page + * URL, which is `/docs/` because `docsSource` mounts + * `content/docs` at `baseUrl: "/docs"` — the same relative path a + * sibling asset has under `content/docs`. Next.js serves `public/` + * files at that same path ahead of the `[[...slug]]` catch-all route, + * so mirroring the asset there under `public/docs/` is what + * actually makes the link 200 instead of 404 through the docs route. + * * Frontmatter is synthesised from the first H1 + first non-heading * paragraph if absent, so the synced pages have title + description * without us having to retroactively add frontmatter to every doc. @@ -25,7 +46,7 @@ */ import { existsSync } from "node:fs" -import { mkdir, rm, readdir, readFile, writeFile, stat } from "node:fs/promises" +import { copyFile, mkdir, rm, readdir, readFile, writeFile, stat } from "node:fs/promises" import { execFileSync } from "node:child_process" import path from "node:path" import { fileURLToPath } from "node:url" @@ -40,6 +61,13 @@ const SIBLING_DOCS = path.join(SIBLING_DIR, "docs/cli") const CACHE_DIR = path.join(ROOT, ".cache/agentproto-ts") const CACHE_DOCS = path.join(CACHE_DIR, "docs/cli") const TARGET_DIR = path.join(ROOT, "content/docs") +const PUBLIC_DOCS_DIR = path.join(ROOT, "public/docs") + +// Binary assets a guide might reference: images (for `![]()`, resolved by +// fumadocs-mdx's remarkImage as a static import) and PDFs (for a plain +// download link). Kept to what guides actually embed — not a general +// static-file mirror. +const ASSET_RE = /\.(png|jpe?g|gif|svg|webp|pdf)$/i const REPO_URL = process.env.AGENTPROTO_TS_REPO_URL ?? "https://github.com/agentproto/ts.git" @@ -68,6 +96,33 @@ async function walkMarkdown(dir, out = []) { return out } +/** Images and PDFs a guide embeds or links to — copied verbatim, see the + * header comment for why this needs two destinations. */ +async function walkAssets(dir, out = []) { + for (const entry of await readdir(dir, { withFileTypes: true })) { + const full = path.join(dir, entry.name) + if (entry.isDirectory()) { + await walkAssets(full, out) + } else if (entry.isFile() && ASSET_RE.test(entry.name)) { + out.push(full) + } + } + return out +} + +/** Copy one asset byte-for-byte to both `content/docs/` (so + * remarkImage's static import resolves) and `public/docs/` (so a + * plain relative link to it 200s). Same relative path in both — see the + * header comment. */ +async function copyAsset(srcAbs, srcRoot) { + const rel = path.relative(srcRoot, srcAbs) + for (const base of [TARGET_DIR, PUBLIC_DOCS_DIR]) { + const dstAbs = path.join(base, rel) + await mkdir(path.dirname(dstAbs), { recursive: true }) + await copyFile(srcAbs, dstAbs) + } +} + /** * Copy a single .md file from `src` to `dst` (renamed .mdx), adding * minimal frontmatter when absent so Fumadocs has a title + @@ -173,6 +228,15 @@ async function syncTree(srcRoot, dstRoot) { await syncOne(f, srcRoot, dstRoot) } console.log(`[sync-content] synced ${files.length} file(s) to ${dstRoot}`) + + await rm(PUBLIC_DOCS_DIR, { recursive: true, force: true }) + const assets = await walkAssets(srcRoot) + for (const a of assets) { + await copyAsset(a, srcRoot) + } + if (assets.length > 0) { + console.log(`[sync-content] synced ${assets.length} asset(s) to ${dstRoot} and ${PUBLIC_DOCS_DIR}`) + } } async function syncFromSibling() {