/** * "Edit this page on GitHub" links must point to files that exist. * * Every route that renders pages with `createDocsPage(source, contentDir)` * must pass the content directory that source reads from. A wrong directory * (for example `content/docs` for the examples collection) sends every edit * link on that route to a 404. */ import { describe, expect, test } from "bun:test"; import { existsSync } from "fs"; import { readdir, readFile } from "fs/promises"; import { join, relative } from "path"; const DOCS_ROOT = join(import.meta.dir, "../.."); const CALL_RE = /createDocsPage\(\s*(\w+)\s*(?:,\s*['"]([^'"]+)['"]\s*)?\)/g; async function findRouteFiles(dir: string): Promise { const out: string[] = []; for (const entry of await readdir(dir, { withFileTypes: true })) { const path = join(dir, entry.name); if (entry.isDirectory()) out.push(...(await findRouteFiles(path))); else if (/\.tsx?$/.test(entry.name)) out.push(path); } return out; } async function sourceDirs(): Promise> { // Map loader export names (examplesSource, ...) to the content dir of the // collection they load, via source.config.ts and lib/source.ts. const config = await readFile(join(DOCS_ROOT, "source.config.ts"), "utf8"); const collections = new Map(); for (const m of config.matchAll(/export const (\w+) = defineDocs\(\{\s*dir:\s*'([^']+)'/g)) { collections.set(m[1], m[2]); } const loaders = await readFile(join(DOCS_ROOT, "lib/source.ts"), "utf8"); const dirs = new Map(); for (const m of loaders.matchAll(/export const (\w+) = loader\(\{[^}]*?source:\s*(\w+)\.toFumadocsSource\(\)/gs)) { const dir = collections.get(m[2]); if (dir) dirs.set(m[1], dir); } return dirs; } describe("Edit on GitHub paths", () => { test("every createDocsPage call passes its source's content directory", async () => { const dirs = await sourceDirs(); const problems: string[] = []; let calls = 0; for (const file of await findRouteFiles(join(DOCS_ROOT, "app"))) { const text = await readFile(file, "utf8"); for (const m of text.matchAll(CALL_RE)) { calls++; const [, sourceName, contentDir] = m; const where = relative(DOCS_ROOT, file); if (!contentDir) { problems.push(`${where}: createDocsPage(${sourceName}) has no content directory`); continue; } if (!existsSync(join(DOCS_ROOT, contentDir))) { problems.push(`${where}: ${contentDir} does not exist`); } const expected = dirs.get(sourceName); if (expected && expected !== contentDir) { problems.push(`${where}: ${sourceName} reads ${expected}, but the edit link uses ${contentDir}`); } } } expect(calls).toBeGreaterThan(0); expect(problems).toEqual([]); }); }); describe("Edit on GitHub on generated API reference pages", () => { test("OpenAPI-generated reference pages don't link to a missing .mdx", async () => { const routes = [ "app/(home)/reference/(v31)/[[...slug]]/page.tsx", "app/(home)/reference/v3/[[...slug]]/page.tsx", ]; for (const route of routes) { const text = await readFile(join(DOCS_ROOT, route), "utf8"); const openApiBranch = text.slice( text.indexOf("openApiReferencePageDataSchema.safeParse"), text.indexOf("referenceMdxPageDataSchema.parse"), ); expect(openApiBranch.length).toBeGreaterThan(0); expect(openApiBranch).not.toContain("EditOnGitHub"); } }); });