|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * check-nav-coverage.mjs |
| 4 | + * |
| 5 | + * Verifies that every .mdx page in the shipped taxonomy is registered in the |
| 6 | + * docs.json navigation tree, and that every docs.json navigation entry |
| 7 | + * resolves to a real file. Run it after adding, renaming, or removing pages: |
| 8 | + * |
| 9 | + * node scripts/check-nav-coverage.mjs |
| 10 | + * |
| 11 | + * Wired into CI via the "Compile docs snippets" job in |
| 12 | + * .github/workflows/snippets.yml, so a PR that adds an .mdx page without |
| 13 | + * registering it in docs.json fails the build. |
| 14 | + */ |
| 15 | +import { readFile, readdir } from "node:fs/promises"; |
| 16 | +import path from "node:path"; |
| 17 | +import process from "node:process"; |
| 18 | + |
| 19 | +const repoRoot = process.cwd(); |
| 20 | +const docsJsonPath = path.join(repoRoot, "docs.json"); |
| 21 | + |
| 22 | +/** Top-level directories whose pages ship in the navigation. */ |
| 23 | +const shippedDirs = [ |
| 24 | + "api-reference", |
| 25 | + "architecture", |
| 26 | + "concepts", |
| 27 | + "contracts", |
| 28 | + "guides", |
| 29 | + "reference", |
| 30 | + "sdk", |
| 31 | +]; |
| 32 | + |
| 33 | +async function main() { |
| 34 | + const docsJson = JSON.parse(stripComments(await readFile(docsJsonPath, "utf8"))); |
| 35 | + const navEntries = collectNavEntries(docsJson.navigation); |
| 36 | + |
| 37 | + const pagePaths = await collectPagePaths(); |
| 38 | + const missingFromNav = [...pagePaths] |
| 39 | + .filter((page) => !navEntries.has(page)) |
| 40 | + .sort(); |
| 41 | + const missingOnDisk = [...navEntries] |
| 42 | + .filter((entry) => !isExternal(entry) && !pagePaths.has(entry)) |
| 43 | + .sort(); |
| 44 | + |
| 45 | + const failures = []; |
| 46 | + if (missingFromNav.length > 0) { |
| 47 | + failures.push( |
| 48 | + [ |
| 49 | + "Pages exist on disk but are missing from docs.json navigation:", |
| 50 | + ...missingFromNav.map((page) => ` - ${page}`), |
| 51 | + ].join("\n"), |
| 52 | + ); |
| 53 | + } |
| 54 | + if (missingOnDisk.length > 0) { |
| 55 | + failures.push( |
| 56 | + [ |
| 57 | + "docs.json navigation entries with no matching .mdx file on disk:", |
| 58 | + ...missingOnDisk.map((entry) => ` - ${entry}`), |
| 59 | + ].join("\n"), |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + if (failures.length > 0) { |
| 64 | + console.error( |
| 65 | + [ |
| 66 | + "Nav coverage check failed.", |
| 67 | + "", |
| 68 | + ...failures, |
| 69 | + "", |
| 70 | + "Register new pages in their natural group in docs.json, and remove nav", |
| 71 | + "entries that point at files that no longer exist.", |
| 72 | + ].join("\n"), |
| 73 | + ); |
| 74 | + process.exit(1); |
| 75 | + } |
| 76 | + |
| 77 | + console.log( |
| 78 | + `Nav coverage passed: ${pagePaths.size} pages checked, ` + |
| 79 | + `${navEntries.size} nav entries verified.`, |
| 80 | + ); |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Collect every page path referenced anywhere in the navigation tree. |
| 85 | + * Handles nested groups, per-locale variants (e.g. guides/foo.es), and |
| 86 | + * object entries with a `page` field. External links are collected too and |
| 87 | + * filtered out by the on-disk check. |
| 88 | + */ |
| 89 | +function collectNavEntries(navigation) { |
| 90 | + const entries = new Set(); |
| 91 | + const walk = (value, inPages) => { |
| 92 | + if (typeof value === "string") { |
| 93 | + if (inPages) entries.add(value); |
| 94 | + return; |
| 95 | + } |
| 96 | + if (Array.isArray(value)) { |
| 97 | + value.forEach((item) => walk(item, inPages)); |
| 98 | + return; |
| 99 | + } |
| 100 | + if (value && typeof value === "object") { |
| 101 | + if (typeof value.page === "string") entries.add(value.page); |
| 102 | + for (const [key, child] of Object.entries(value)) { |
| 103 | + walk(child, inPages || key === "pages"); |
| 104 | + } |
| 105 | + } |
| 106 | + }; |
| 107 | + walk(navigation, false); |
| 108 | + return entries; |
| 109 | +} |
| 110 | + |
| 111 | +/** Collect the nav path of every .mdx page in the shipped taxonomy. */ |
| 112 | +async function collectPagePaths() { |
| 113 | + const pages = new Set(); |
| 114 | + |
| 115 | + const walk = async (dir) => { |
| 116 | + let entries; |
| 117 | + try { |
| 118 | + entries = await readdir(dir, { withFileTypes: true }); |
| 119 | + } catch { |
| 120 | + return; // Directory does not exist (e.g. concepts/). |
| 121 | + } |
| 122 | + for (const entry of entries) { |
| 123 | + const fullPath = path.join(dir, entry.name); |
| 124 | + if (entry.isDirectory()) { |
| 125 | + await walk(fullPath); |
| 126 | + } else if (entry.isFile() && entry.name.endsWith(".mdx")) { |
| 127 | + pages.add(toNavPath(fullPath)); |
| 128 | + } |
| 129 | + } |
| 130 | + }; |
| 131 | + |
| 132 | + for (const dir of shippedDirs) { |
| 133 | + await walk(path.join(repoRoot, dir)); |
| 134 | + } |
| 135 | + |
| 136 | + // Root-level pages live next to docs.json, outside the shipped dirs. |
| 137 | + const rootEntries = await readdir(repoRoot, { withFileTypes: true }); |
| 138 | + for (const entry of rootEntries) { |
| 139 | + if (entry.isFile() && entry.name.endsWith(".mdx")) { |
| 140 | + pages.add(entry.name.replace(/\.mdx$/, "")); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return pages; |
| 145 | +} |
| 146 | + |
| 147 | +/** Absolute path -> navigation path (relative, forward slashes, no .mdx). */ |
| 148 | +function toNavPath(filePath) { |
| 149 | + return path.relative(repoRoot, filePath).split(path.sep).join("/").replace(/\.mdx$/, ""); |
| 150 | +} |
| 151 | + |
| 152 | +function isExternal(entry) { |
| 153 | + return /^[a-z][a-z0-9+.-]*:\/\//i.test(entry) || entry.startsWith("//"); |
| 154 | +} |
| 155 | + |
| 156 | +/** |
| 157 | + * Strip line comments (//) and block comments (slash-star ... star-slash) |
| 158 | + * from JSONC so docs.json can carry the audit-script comment header. |
| 159 | + * Respects string literals so URLs like "https://..." are untouched. |
| 160 | + */ |
| 161 | +function stripComments(source) { |
| 162 | + let result = ""; |
| 163 | + let inString = false; |
| 164 | + let i = 0; |
| 165 | + |
| 166 | + while (i < source.length) { |
| 167 | + const char = source[i]; |
| 168 | + const next = source[i + 1]; |
| 169 | + |
| 170 | + if (inString) { |
| 171 | + result += char; |
| 172 | + if (char === "\\" && next !== undefined) { |
| 173 | + result += next; |
| 174 | + i += 2; |
| 175 | + continue; |
| 176 | + } |
| 177 | + if (char === '"') inString = false; |
| 178 | + i += 1; |
| 179 | + continue; |
| 180 | + } |
| 181 | + |
| 182 | + if (char === '"') { |
| 183 | + inString = true; |
| 184 | + result += char; |
| 185 | + i += 1; |
| 186 | + continue; |
| 187 | + } |
| 188 | + |
| 189 | + if (char === "/" && next === "/") { |
| 190 | + while (i < source.length && source[i] !== "\n") i += 1; |
| 191 | + continue; |
| 192 | + } |
| 193 | + |
| 194 | + if (char === "/" && next === "*") { |
| 195 | + i += 2; |
| 196 | + while (i < source.length && !(source[i] === "*" && source[i + 1] === "/")) i += 1; |
| 197 | + i += 2; |
| 198 | + continue; |
| 199 | + } |
| 200 | + |
| 201 | + result += char; |
| 202 | + i += 1; |
| 203 | + } |
| 204 | + |
| 205 | + return result; |
| 206 | +} |
| 207 | + |
| 208 | +main().catch((error) => { |
| 209 | + console.error(error); |
| 210 | + process.exit(1); |
| 211 | +}); |
0 commit comments