// Fast checks of the docs pages, without a build: frontmatter, MDX syntax, imports, links, // anchors of API and source pages, and ranges. // node scripts/check-pages.mjs all pages // node scripts/check-pages.mjs src/content/docs/concepts/states.mdx … // Exit code 1 if a page has an error. Run `npm run build` and `npm run check` for the full check. import { readFileSync, readdirSync, statSync, existsSync } from 'node:fs'; import path from 'node:path'; import { parse as parseYaml } from 'yaml'; import { mdxToJs } from 'satteri'; import GithubSlugger from 'github-slugger'; import { getModel } from './api.mjs'; import { extractSnippet } from './snippet.mjs'; import { deviceOptions, readOptions } from './options.mjs'; const docsDir = path.resolve(new URL('..', import.meta.url).pathname); const repoRoot = path.resolve(docsDir, '..'); const contentDir = path.join(docsDir, 'src/content/docs'); const walk = (dir, out = []) => { for (const name of readdirSync(dir)) { const p = path.join(dir, name); if (statSync(p).isDirectory()) walk(p, out); else if (/\.mdx?$/.test(name)) out.push(p); } return out; }; const allPages = walk(contentDir); const files = process.argv.slice(2).length ? process.argv.slice(2).map((f) => path.resolve(f)) : allPages; const model = getModel({ xmlDir: path.join(docsDir, 'build/doxygen/xml'), repoRoot, base: '' }); const api = new Map(); const addAnchors = (url, recs) => { if (!api.has(url)) api.set(url, new Set()); for (const x of recs) if (x?.anchor) api.get(url).add(x.anchor); }; for (const t of model.types()) addAnchors(t.url, t.visible ?? []); for (const ns of model.namespaces()) addAnchors(ns.url, ns.visible ?? []); for (const { e } of model.namespaceEnums()) addAnchors(e.page, e.values ?? []); api.set('/api/', new Set()); const sources = new Set(model.sourceFiles); const lineCount = (f) => readFileSync(path.join(repoRoot, f), 'utf8').replace(/\n$/, '').split('\n').length; const pageUrl = (abs) => { const rel = path.relative(contentDir, abs).replace(/\.mdx?$/, ''); return rel === 'index' ? '/' : '/' + rel.replace(/\/index$/, '') + '/'; }; const pages = new Map(allPages.map((p) => [pageUrl(p), p])); const slugCache = new Map(); const slugsOf = (abs) => { if (slugCache.has(abs)) return slugCache.get(abs); const src = readFileSync(abs, 'utf8').replace(/```[\s\S]*?```/g, ''); const slugger = new GithubSlugger(); const set = new Set(['_top']); for (const m of src.matchAll(/^#{2,6}\s+(.+?)\s*$/gm)) set.add(slugger.slug(m[1].replace(/`/g, '').replace(/\[([^\]]+)\]\([^)]*\)/g, '$1'))); for (const m of src.matchAll(/\sid="([^"]+)"/g)) set.add(m[1]); // Rows of : #-