verify-md-wrap.ts 4.0 KB

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