verify-mermaid.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * Doc-sync gate: verify every fenced ```mermaid block parses with Mermaid's
  3. * own parser. Markdown link/type/code gates can say a diagram block exists and
  4. * is linked, but only Mermaid can catch syntax errors that GitHub would fail to
  5. * render.
  6. *
  7. * Scope matches the Markdown link gate so any Mermaid diagram in repo-authored
  8. * docs is checked: README.md, README.zh.md, docs/** /*.md,
  9. * packages/* /*.md, packages/* /* /*.md, examples/** /*.md, AGENTS.md,
  10. * packages/AGENTS.md, and .agents/skills/** /*.md.
  11. *
  12. * Run: `tsx scripts/verify-mermaid.ts`.
  13. */
  14. import { globSync, readFileSync, realpathSync } from 'node:fs'
  15. import { resolve } from 'node:path'
  16. import { fromMarkdown } from 'mdast-util-from-markdown'
  17. import { gfmFromMarkdown } from 'mdast-util-gfm'
  18. import { gfm } from 'micromark-extension-gfm'
  19. import { JSDOM } from 'jsdom'
  20. import type { Nodes } from 'mdast'
  21. const root = resolve(import.meta.dirname, '..')
  22. const PATTERNS = [
  23. 'README.md',
  24. 'README.zh.md',
  25. 'docs/**/*.md',
  26. 'packages/*/*.md',
  27. 'packages/*/*/*.md',
  28. 'examples/**/*.md',
  29. 'AGENTS.md',
  30. 'packages/AGENTS.md',
  31. '.agents/skills/**/*.md',
  32. ]
  33. interface Block {
  34. file: string
  35. line: number
  36. source: string
  37. }
  38. interface Violation {
  39. file: string
  40. line: number
  41. message: string
  42. }
  43. function extractMermaidBlocks(file: string): Block[] {
  44. const source = readFileSync(resolve(root, file), 'utf8')
  45. const tree = fromMarkdown(source, { extensions: [gfm()], mdastExtensions: [gfmFromMarkdown()] })
  46. const out: Block[] = []
  47. const visit = (node: Nodes): void => {
  48. if (node.type === 'code' && node.lang === 'mermaid') {
  49. out.push({ file, line: node.position?.start.line ?? 0, source: node.value })
  50. }
  51. if ('children' in node) {
  52. for (const child of node.children) visit(child)
  53. }
  54. }
  55. visit(tree)
  56. return out
  57. }
  58. function formatError(error: unknown): string {
  59. if (error instanceof Error) return error.message.replace(/\s+/g, ' ').trim()
  60. return String(error).replace(/\s+/g, ' ').trim()
  61. }
  62. const blocks: Block[] = []
  63. const seen = new Set<string>()
  64. let checkedFiles = 0
  65. for (const pattern of PATTERNS) {
  66. for (const match of globSync(pattern, { cwd: root })) {
  67. const real = realpathSync(resolve(root, match))
  68. if (seen.has(real)) continue
  69. seen.add(real)
  70. checkedFiles++
  71. blocks.push(...extractMermaidBlocks(match))
  72. }
  73. }
  74. const violations: Violation[] = []
  75. const { window } = new JSDOM('')
  76. Object.defineProperty(globalThis, 'window', { value: window })
  77. Object.defineProperty(globalThis, 'document', { value: window.document })
  78. Object.defineProperty(globalThis, 'navigator', { value: window.navigator })
  79. const mermaid = (await import('mermaid')).default
  80. mermaid.initialize({ startOnLoad: false })
  81. for (const block of blocks) {
  82. try {
  83. await mermaid.parse(block.source, { suppressErrors: false })
  84. } catch (error: unknown) {
  85. violations.push({ file: block.file, line: block.line, message: formatError(error) })
  86. }
  87. }
  88. if (violations.length === 0) {
  89. console.log(`verify-mermaid: ${blocks.length} mermaid block(s) parsed across ${checkedFiles} file(s).`)
  90. process.exit(0)
  91. }
  92. console.error('verify-mermaid: Mermaid syntax errors found:')
  93. for (const violation of violations) {
  94. console.error(` ${violation.file}:${violation.line} ${violation.message}`)
  95. }
  96. process.exit(1)