|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | + |
| 4 | +type SourceDoc = { |
| 5 | + id: string; |
| 6 | + relPath: string; |
| 7 | + ext: "md" | "mdx"; |
| 8 | + size: number; |
| 9 | + sha256: string; |
| 10 | + sha256_normalized: string; |
| 11 | + frontmatter_keys: string[]; |
| 12 | + title?: string; |
| 13 | + headings: Array<{ depth: number; text: string }>; |
| 14 | + text: string; |
| 15 | +}; |
| 16 | + |
| 17 | +type TopicsIndex = { |
| 18 | + schema: 1; |
| 19 | + library: string; |
| 20 | + version: string; // for now: ref (main) or commit short sha |
| 21 | + docs_hash: string; |
| 22 | + revision: any; |
| 23 | + topics: Array<{ |
| 24 | + slug: string; |
| 25 | + title: string; |
| 26 | + kind: "page" | "section"; |
| 27 | + relPath: string; |
| 28 | + anchor?: string; |
| 29 | + sourceIds: string[]; // doc ids from sources.jsonl |
| 30 | + }>; |
| 31 | +}; |
| 32 | + |
| 33 | +function slugify(s: string) { |
| 34 | + return s |
| 35 | + .toLowerCase() |
| 36 | + .replace(/['"]/g, "") |
| 37 | + .replace(/[^a-z0-9]+/g, "-") |
| 38 | + .replace(/^-+|-+$/g, "") |
| 39 | + .slice(0, 80); |
| 40 | +} |
| 41 | + |
| 42 | +function makeAnchor(text: string) { |
| 43 | + // Keep consistent with GitHub-ish anchors (roughly) |
| 44 | + return slugify(text); |
| 45 | +} |
| 46 | + |
| 47 | +export function buildTopicsIndex(args: { |
| 48 | + library: string; |
| 49 | + sourceRoot: string; // build/sources/<lib>/<owner>__<repo>/<ref> |
| 50 | + indexRoot: string; // build/index/<lib>/<owner>__<repo>/<ref> |
| 51 | + version: string; // e.g. "main" |
| 52 | + outDir: string; // registry/skills/<lib>/<version> |
| 53 | +}) { |
| 54 | + const docsIndexPath = path.join(args.indexRoot, "docs.index.json"); |
| 55 | + const sourcesPath = path.join(args.indexRoot, "docs.sources.jsonl"); |
| 56 | + const revisionPath = path.join(args.sourceRoot, "REVISION.json"); |
| 57 | + |
| 58 | + if (!fs.existsSync(docsIndexPath)) throw new Error(`Missing ${docsIndexPath}`); |
| 59 | + if (!fs.existsSync(sourcesPath)) throw new Error(`Missing ${sourcesPath}`); |
| 60 | + if (!fs.existsSync(revisionPath)) throw new Error(`Missing ${revisionPath}`); |
| 61 | + |
| 62 | + const docsIndex = JSON.parse(fs.readFileSync(docsIndexPath, "utf8")); |
| 63 | + const revision = JSON.parse(fs.readFileSync(revisionPath, "utf8")); |
| 64 | + const docs_hash: string = docsIndex.docs_hash; |
| 65 | + |
| 66 | + const lines = fs.readFileSync(sourcesPath, "utf8").split("\n").filter(Boolean); |
| 67 | + const docs: SourceDoc[] = lines.map((l) => JSON.parse(l)); |
| 68 | + |
| 69 | + const topics: TopicsIndex["topics"] = []; |
| 70 | + |
| 71 | + for (const doc of docs) { |
| 72 | + const pageTitle = doc.title ?? doc.relPath.replace(/^docs\//, ""); |
| 73 | + topics.push({ |
| 74 | + slug: `page:${slugify(doc.relPath)}`, |
| 75 | + title: pageTitle, |
| 76 | + kind: "page", |
| 77 | + relPath: doc.relPath, |
| 78 | + sourceIds: [doc.id], |
| 79 | + }); |
| 80 | + |
| 81 | + // Add section topics for h2/h3 only (keeps index tight) |
| 82 | + for (const h of doc.headings) { |
| 83 | + if (h.depth !== 2 && h.depth !== 3) continue; |
| 84 | + const anchor = makeAnchor(h.text); |
| 85 | + topics.push({ |
| 86 | + slug: `sec:${slugify(doc.relPath)}#${anchor}`, |
| 87 | + title: `${pageTitle} → ${h.text}`, |
| 88 | + kind: "section", |
| 89 | + relPath: doc.relPath, |
| 90 | + anchor, |
| 91 | + sourceIds: [doc.id], |
| 92 | + }); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // stable order |
| 97 | + topics.sort((a, b) => a.slug.localeCompare(b.slug)); |
| 98 | + |
| 99 | + const out: TopicsIndex = { |
| 100 | + schema: 1, |
| 101 | + library: args.library, |
| 102 | + version: args.version, |
| 103 | + docs_hash, |
| 104 | + revision, |
| 105 | + topics, |
| 106 | + }; |
| 107 | + |
| 108 | + fs.mkdirSync(args.outDir, { recursive: true }); |
| 109 | + fs.writeFileSync(path.join(args.outDir, "index.json"), JSON.stringify(out, null, 2) + "\n"); |
| 110 | +} |
0 commit comments