verify-md-wrap.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * Doc-sync gate: enforce the repo's "Markdown is not hard-wrapped" convention
  3. * (docs/AGENTS.md § Writing rules) — prose paragraphs are written as
  4. * one physical line per paragraph and the editor soft-wraps. A hard-wrapped
  5. * paragraph (a one-word edit reflows and re-diffs the whole block) is a defect
  6. * this script catches before review.
  7. *
  8. * Detection is AST-based: we parse each file with mdast-util-from-markdown (the
  9. * CommonMark parser behind remark) plus the GFM extension, then flag any
  10. * `paragraph` node whose source span covers more than one line. The parser owns
  11. * all the structure that legitimately occupies multiple lines — fenced code
  12. * (any fence length), tables, list items, blockquotes, HTML blocks, headings,
  13. * thematic breaks, link-reference definitions — while a small preprocessing
  14. * pass masks VitePress YAML frontmatter and custom-container delimiter lines.
  15. * A hard wrap is simply "a paragraph node that starts and ends on different
  16. * lines." This is checker, not formatter: it reports and never rewrites, so it
  17. * introduces zero cosmetic churn (no emphasis-marker or table-delimiter
  18. * normalization).
  19. *
  20. * A wrapped paragraph inside a list item or blockquote is still a `paragraph`
  21. * node, so those are caught too. Scope mirrors doc-typecheck plus the two
  22. * AGENTS.md files that doc-sync does NOT otherwise cover (the convention itself
  23. * lives there), plus generated system-prompt Markdown goldens: README.md,
  24. * docs/** /*.md, packages/* /*.md, examples/** /system-prompt.golden.md,
  25. * packages/** /system-prompt.golden.md, AGENTS.md, packages/AGENTS.md. The root
  26. * and packages/ CLAUDE.md are symlinks to the AGENTS.md files, so they are
  27. * deduped by real path.
  28. *
  29. * Run: `tsx scripts/verify-md-wrap.ts`.
  30. */
  31. import { readFileSync } from 'node:fs'
  32. import { relative, resolve } from 'node:path'
  33. import type { Nodes } from 'mdast'
  34. import { parseMarkdown, visitMarkdown } from './markdown.ts'
  35. import { uniqueRepoFiles } from './repo-files.ts'
  36. const root = resolve(import.meta.dirname, '..')
  37. /** Files to check: doc-typecheck's scope, prompt goldens, and the AGENTS.md pair. */
  38. const PATTERNS = [
  39. 'README.md',
  40. 'README.zh.md',
  41. 'docs/**/*.md',
  42. 'packages/*/*.md',
  43. 'packages/*/*/*.md',
  44. 'examples/**/system-prompt.golden.md',
  45. 'packages/**/system-prompt.golden.md',
  46. 'AGENTS.md',
  47. 'packages/AGENTS.md',
  48. ]
  49. /** A located hard-wrap: a prose paragraph spanning more than one source line. */
  50. interface Violation {
  51. file: string
  52. /** 1-based line where the hard-wrapped paragraph starts. */
  53. line: number
  54. text: string
  55. }
  56. function maskVitePressStructure(source: string): string {
  57. const lines = source.split('\n')
  58. if (lines[0] === '---') {
  59. const closing = lines.indexOf('---', 1)
  60. if (closing !== -1) {
  61. for (let index = 0; index <= closing; index++) lines[index] = ''
  62. }
  63. }
  64. return lines.map(line => line.trimStart().startsWith(':::') ? '' : line).join('\n')
  65. }
  66. /** Find every hard-wrapped prose paragraph in one Markdown file via its AST. */
  67. function findViolations(absPath: string): Violation[] {
  68. const file = relative(root, absPath)
  69. const source = readFileSync(absPath, 'utf8')
  70. const parsedSource = maskVitePressStructure(source)
  71. const tree = parseMarkdown(parsedSource)
  72. const out: Violation[] = []
  73. visitMarkdown(tree, (node: Nodes): boolean | void => {
  74. if (node.type === 'paragraph' && node.position) {
  75. const { start, end } = node.position
  76. if (end.line > start.line) {
  77. const firstLine = source.split('\n')[start.line - 1] ?? ''
  78. out.push({ file, line: start.line, text: firstLine.trim() })
  79. }
  80. // A paragraph's children are inline (text/emphasis/…); no nested
  81. // paragraphs to find, so don't descend.
  82. return false
  83. }
  84. })
  85. return out
  86. }
  87. const files = uniqueRepoFiles(root, PATTERNS)
  88. const all = files.flatMap(file => findViolations(file.abs))
  89. const checked = files.length
  90. if (all.length === 0) {
  91. console.log(`verify-md-wrap: ${checked} file(s) checked, no hard-wrapped prose paragraphs.`)
  92. process.exit(0)
  93. }
  94. console.error('verify-md-wrap: hard-wrapped prose paragraphs found (write one physical line per paragraph):')
  95. for (const v of all) {
  96. console.error(` ${v.file}:${v.line} ${v.text.slice(0, 80)}${v.text.length > 80 ? '…' : ''}`)
  97. }
  98. process.exit(1)