/** * Crawls every CLI reference page on a running docs server and reports broken * internal links and anchors, empty sections, leftover Markdown and missing * Markdown twins. Usage: tsx scripts/check-cli-reference.ts [origin] */ import { getCliStaticParams } from '~/features/docs/Reference.cli' const ORIGIN = process.argv[2] ?? 'http://localhost:3001' const BASE = '/docs' const CONCURRENCY = 4 const issues = new Map() const report = (kind: string, detail: string) => issues.set(kind, [...(issues.get(kind) ?? []), detail]) /** Repeats a removal until nothing matches, so a removal cannot leave a new match behind. */ function stripUntilStable(text: string, pattern: RegExp) { let previous: string do { previous = text text = text.replace(pattern, '') } while (text !== previous) return text } const pages = new Map }>>() function fetchPage(path: string) { if (!pages.has(path)) { pages.set( path, fetch(`${ORIGIN}${path}`, { redirect: 'manual' }).then(async (res) => { const html = res.status === 200 ? await res.text() : '' const visible = stripUntilStable(html, /]*>/gi) const ids = new Set([...visible.matchAll(/ id="([^"]+)"/g)].map((m) => decode(m[1]))) return { status: res.status, html: visible, ids } }) ) } return pages.get(path)! } const decode = (text: string) => text .replace(/</g, '<') .replace(/>/g, '>') .replace(/"/g, '"') .replace(/'/g, "'") .replace(/&/g, '&') async function checkPage(path: string) { const { status, html, ids } = await fetchPage(path) if (status !== 200) return report('page not 200', `${path} (${status})`) const article = html.split('id="sb-docs-guide-main-article"')[1]?.split('')[0] ?? '' if (!article) return report('no article', path) const nav = html.match(/data-cli-nav[\s\S]*?<\/nav>/)?.[0] ?? '' const hrefs = [...`${article}${nav}`.matchAll(/href="([^"]+)"/g)].map((m) => decode(m[1])) for (const href of new Set(hrefs)) { if (/^(https?:|mailto:)/.test(href)) continue const [target, anchor] = href.split('#') if (!target) { if (anchor && !ids.has(anchor)) report('missing anchor', `${path} -> #${anchor}`) continue } if (!target.startsWith(`${BASE}/`)) { report('relative link', `${path} -> ${href}`) continue } const linked = await fetchPage(target) if (linked.status === 308 || linked.status === 301 || linked.status === 307) { report('redirected link', `${path} -> ${href}`) } else if (linked.status !== 200) { report('broken link', `${path} -> ${href} (${linked.status})`) } else if (anchor && !linked.ids.has(anchor)) { report('missing anchor', `${path} -> ${href}`) } } const text = article .replace(//g, '') .replace(//g, '') .replace(/<[^>]+>/g, ' ') if (/```|\*\*\w|\]\(/.test(text)) report('leftover markdown', path) if (/&(lt|gt|amp|quot);/.test(article)) report('double escaped', path) if (/\bundefined\b|\[object Object\]/.test(text)) report('undefined text', path) if (/
\s*<\/pre>/.test(article)) report('empty output box', path)

  // An h2 with nothing but headings before the next h2
  for (const section of article.split(/(?=])/).slice(1)) {
    const withoutHeadings = stripUntilStable(section, //gi)
    const body = stripUntilStable(withoutHeadings, /<[^>]+>/g).trim()
    if (!body) report('empty section', `${path}: ${section.match(/id="([^"]+)"/)?.[1]}`)
  }

  const lede = article.match(/

([^<]*)<\/p>/)?.[1]?.trim() ?? '' if (!lede || lede === '.') report('empty summary', path) const md = await fetch(`${ORIGIN}${path}.md`) const mdText = md.ok ? await md.text() : '' if ( !md.ok || !md.headers.get('content-type')?.includes('text/markdown') || !mdText.startsWith('# ') ) { report('markdown twin', `${path}.md (${md.status})`) } if (!/ `${BASE}/reference/${slug.join('/')}` ) let next = 0 let done = 0 await Promise.all( Array.from({ length: CONCURRENCY }, async () => { while (next < paths.length) { const path = paths[next++] await checkPage(path) if (++done % 25 === 0) console.error(`${done}/${paths.length}`) } }) ) for (const [kind, list] of issues) { console.log( `\n## ${kind} (${list.length})\n${list.slice(0, 15).join('\n')}${list.length > 15 ? '\n…' : ''}` ) } console.log( `\nChecked ${paths.length} pages, ${pages.size} distinct URLs fetched, ${[...issues.values()].flat().length} issues` ) if (issues.size > 0) process.exitCode = 1 } main()