verify-md-wrap.ts 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 { readFileSync } from 'node:fs'
  30. import { relative, resolve } from 'node:path'
  31. import type { Nodes } from 'mdast'
  32. import { parseMarkdown, visitMarkdown } from './markdown.ts'
  33. import { uniqueRepoFiles } from './repo-files.ts'
  34. const root = resolve(import.meta.dirname, '..')
  35. /** Files to check: doc-typecheck's scope, prompt goldens, and the AGENTS.md pair. */
  36. const PATTERNS = [
  37. 'README.md',
  38. 'README.zh.md',
  39. 'docs/**/*.md',
  40. 'packages/*/*.md',
  41. 'packages/*/*/*.md',
  42. 'examples/**/system-prompt.golden.md',
  43. 'packages/**/system-prompt.golden.md',
  44. 'AGENTS.md',
  45. 'packages/AGENTS.md',
  46. ]
  47. /** A located hard-wrap: a prose paragraph spanning more than one source line. */
  48. interface Violation {
  49. file: string
  50. /** 1-based line where the hard-wrapped paragraph starts. */
  51. line: number
  52. text: string
  53. }
  54. /** Find every hard-wrapped prose paragraph in one Markdown file via its AST. */
  55. function findViolations(absPath: string): Violation[] {
  56. const file = relative(root, absPath)
  57. const source = readFileSync(absPath, 'utf8')
  58. const tree = parseMarkdown(source)
  59. const out: Violation[] = []
  60. visitMarkdown(tree, (node: Nodes): boolean | void => {
  61. if (node.type === 'paragraph' && node.position) {
  62. const { start, end } = node.position
  63. if (end.line > start.line) {
  64. const firstLine = source.split('\n')[start.line - 1] ?? ''
  65. out.push({ file, line: start.line, text: firstLine.trim() })
  66. }
  67. // A paragraph's children are inline (text/emphasis/…); no nested
  68. // paragraphs to find, so don't descend.
  69. return false
  70. }
  71. })
  72. return out
  73. }
  74. const files = uniqueRepoFiles(root, PATTERNS)
  75. const all = files.flatMap(file => findViolations(file.abs))
  76. const checked = files.length
  77. if (all.length === 0) {
  78. console.log(`verify-md-wrap: ${checked} file(s) checked, no hard-wrapped prose paragraphs.`)
  79. process.exit(0)
  80. }
  81. console.error('verify-md-wrap: hard-wrapped prose paragraphs found (write one physical line per paragraph):')
  82. for (const v of all) {
  83. console.error(` ${v.file}:${v.line} ${v.text.slice(0, 80)}${v.text.length > 80 ? '…' : ''}`)
  84. }
  85. process.exit(1)