verify-md-wrap.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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): README.md, docs/** /*.md, packages/* /*.md, AGENTS.md,
  22. * packages/AGENTS.md. The root and packages/ CLAUDE.md are symlinks to the
  23. * AGENTS.md files, so they are deduped by real path.
  24. *
  25. * Run: `tsx scripts/verify-md-wrap.ts`.
  26. */
  27. import { globSync, readFileSync, realpathSync } from 'node:fs'
  28. import { relative, resolve } from 'node:path'
  29. import { fromMarkdown } from 'mdast-util-from-markdown'
  30. import { gfmFromMarkdown } from 'mdast-util-gfm'
  31. import { gfm } from 'micromark-extension-gfm'
  32. import type { Nodes } from 'mdast'
  33. const root = resolve(import.meta.dirname, '..')
  34. /** Files to check: doc-typecheck's scope plus the AGENTS.md pair. */
  35. const PATTERNS = ['README.md', 'README.zh.md', 'docs/**/*.md', 'packages/*/*.md', 'packages/*/*/*.md', 'AGENTS.md', 'packages/AGENTS.md']
  36. /** A located hard-wrap: a prose paragraph spanning more than one source line. */
  37. interface Violation {
  38. file: string
  39. /** 1-based line where the hard-wrapped paragraph starts. */
  40. line: number
  41. text: string
  42. }
  43. /** Find every hard-wrapped prose paragraph in one Markdown file via its AST. */
  44. function findViolations(absPath: string): Violation[] {
  45. const file = relative(root, absPath)
  46. const source = readFileSync(absPath, 'utf8')
  47. const tree = fromMarkdown(source, { extensions: [gfm()], mdastExtensions: [gfmFromMarkdown()] })
  48. const out: Violation[] = []
  49. const visit = (node: Nodes): void => {
  50. if (node.type === 'paragraph' && node.position) {
  51. const { start, end } = node.position
  52. if (end.line > start.line) {
  53. const firstLine = source.split('\n')[start.line - 1] ?? ''
  54. out.push({ file, line: start.line, text: firstLine.trim() })
  55. }
  56. // A paragraph's children are inline (text/emphasis/…); no nested
  57. // paragraphs to find, so don't descend.
  58. return
  59. }
  60. if ('children' in node) {
  61. for (const child of node.children) visit(child)
  62. }
  63. }
  64. visit(tree)
  65. return out
  66. }
  67. const seen = new Set<string>()
  68. const all: Violation[] = []
  69. let checked = 0
  70. for (const pattern of PATTERNS) {
  71. for (const match of globSync(pattern, { cwd: root })) {
  72. const abs = resolve(root, match)
  73. // CLAUDE.md symlinks resolve onto AGENTS.md; dedupe by real path so a file
  74. // matched twice (or via symlink) is checked once.
  75. const real = realpathSync(abs)
  76. if (seen.has(real)) continue
  77. seen.add(real)
  78. checked++
  79. all.push(...findViolations(abs))
  80. }
  81. }
  82. if (all.length === 0) {
  83. console.log(`verify-md-wrap: ${checked} file(s) checked, no hard-wrapped prose paragraphs.`)
  84. process.exit(0)
  85. }
  86. console.error('verify-md-wrap: hard-wrapped prose paragraphs found (write one physical line per paragraph):')
  87. for (const v of all) {
  88. console.error(` ${v.file}:${v.line} ${v.text.slice(0, 80)}${v.text.length > 80 ? '…' : ''}`)
  89. }
  90. process.exit(1)