1
0

verify-md-wrap.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 — so a hard wrap is simply "a
  14. * paragraph node that starts and ends on different lines." This is checker, not
  15. * formatter: it reports and never rewrites, so it introduces zero cosmetic
  16. * churn (no emphasis-marker or table-delimiter normalization).
  17. *
  18. * A wrapped paragraph inside a list item or blockquote is still a `paragraph`
  19. * node, so those are caught too. Scope mirrors doc-typecheck plus the two
  20. * AGENTS.md files that doc-sync does NOT otherwise cover (the convention itself
  21. * lives there), plus generated system-prompt Markdown goldens: README.md,
  22. * docs/** /*.md, packages/* /*.md, examples/** /system-prompt.golden.md,
  23. * packages/** /system-prompt.golden.md, AGENTS.md, packages/AGENTS.md. The root
  24. * and packages/ CLAUDE.md are symlinks to the AGENTS.md files, so they are
  25. * deduped by real path.
  26. *
  27. * Run: `tsx scripts/verify-md-wrap.ts`.
  28. */
  29. import { globSync, readFileSync, realpathSync } from 'node:fs'
  30. import { relative, resolve } from 'node:path'
  31. import { fromMarkdown } from 'mdast-util-from-markdown'
  32. import { gfmFromMarkdown } from 'mdast-util-gfm'
  33. import { gfm } from 'micromark-extension-gfm'
  34. import type { Nodes } from 'mdast'
  35. const root = resolve(import.meta.dirname, '..')
  36. /** Files to check: doc-typecheck's scope, prompt goldens, and the AGENTS.md pair. */
  37. const PATTERNS = [
  38. 'README.md',
  39. 'README.zh.md',
  40. 'docs/**/*.md',
  41. 'packages/*/*.md',
  42. 'packages/*/*/*.md',
  43. 'examples/**/system-prompt.golden.md',
  44. 'packages/**/system-prompt.golden.md',
  45. 'AGENTS.md',
  46. 'packages/AGENTS.md',
  47. ]
  48. /** A located hard-wrap: a prose paragraph spanning more than one source line. */
  49. interface Violation {
  50. file: string
  51. /** 1-based line where the hard-wrapped paragraph starts. */
  52. line: number
  53. text: string
  54. }
  55. /** Find every hard-wrapped prose paragraph in one Markdown file via its AST. */
  56. function findViolations(absPath: string): Violation[] {
  57. const file = relative(root, absPath)
  58. const source = readFileSync(absPath, 'utf8')
  59. const tree = fromMarkdown(source, { extensions: [gfm()], mdastExtensions: [gfmFromMarkdown()] })
  60. const out: Violation[] = []
  61. const visit = (node: Nodes): void => {
  62. if (node.type === 'paragraph' && node.position) {
  63. const { start, end } = node.position
  64. if (end.line > start.line) {
  65. const firstLine = source.split('\n')[start.line - 1] ?? ''
  66. out.push({ file, line: start.line, text: firstLine.trim() })
  67. }
  68. // A paragraph's children are inline (text/emphasis/…); no nested
  69. // paragraphs to find, so don't descend.
  70. return
  71. }
  72. if ('children' in node) {
  73. for (const child of node.children) visit(child)
  74. }
  75. }
  76. visit(tree)
  77. return out
  78. }
  79. const seen = new Set<string>()
  80. const all: Violation[] = []
  81. let checked = 0
  82. for (const pattern of PATTERNS) {
  83. for (const match of globSync(pattern, { cwd: root })) {
  84. const abs = resolve(root, match)
  85. // CLAUDE.md symlinks resolve onto AGENTS.md; dedupe by real path so a file
  86. // matched twice (or via symlink) is checked once.
  87. const real = realpathSync(abs)
  88. if (seen.has(real)) continue
  89. seen.add(real)
  90. checked++
  91. all.push(...findViolations(abs))
  92. }
  93. }
  94. if (all.length === 0) {
  95. console.log(`verify-md-wrap: ${checked} file(s) checked, no hard-wrapped prose paragraphs.`)
  96. process.exit(0)
  97. }
  98. console.error('verify-md-wrap: hard-wrapped prose paragraphs found (write one physical line per paragraph):')
  99. for (const v of all) {
  100. console.error(` ${v.file}:${v.line} ${v.text.slice(0, 80)}${v.text.length > 80 ? '…' : ''}`)
  101. }
  102. process.exit(1)