1
0

verify-md-wrap.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * Reject Markdown prose paragraphs spanning multiple physical lines. The GFM
  3. * AST distinguishes paragraphs—including those in lists and blockquotes—from
  4. * multiline structural nodes. The checker never rewrites; symlinked instruction
  5. * files are deduped. The owning convention is in `docs/AGENTS.md`.
  6. */
  7. import { readFileSync } from 'node:fs'
  8. import { relative, resolve } from 'node:path'
  9. import type { Nodes } from 'mdast'
  10. import { parseMarkdown, visitMarkdown } from './markdown.ts'
  11. import { uniqueRepoFiles } from './repo-files.ts'
  12. const root = resolve(import.meta.dirname, '..')
  13. /** Files to check: doc-typecheck's scope, system-prompt expected outputs, and the AGENTS.md pair. */
  14. const PATTERNS = [
  15. 'README.md',
  16. 'README.zh.md',
  17. '.agents/notes/**/*.md',
  18. 'docs/**/*.md',
  19. 'packages/*/*.md',
  20. 'packages/*/*/*.md',
  21. 'examples/**/system-prompt.expected.md',
  22. 'packages/**/system-prompt.expected.md',
  23. 'AGENTS.md',
  24. 'packages/AGENTS.md',
  25. ]
  26. /** A located hard-wrap: a prose paragraph spanning more than one source line. */
  27. interface Violation {
  28. file: string
  29. /** 1-based line where the hard-wrapped paragraph starts. */
  30. line: number
  31. text: string
  32. }
  33. /** Find every hard-wrapped prose paragraph in one Markdown file via its AST. */
  34. function findViolations(absPath: string): Violation[] {
  35. const file = relative(root, absPath)
  36. const source = readFileSync(absPath, 'utf8')
  37. const tree = parseMarkdown(source)
  38. const out: Violation[] = []
  39. visitMarkdown(tree, (node: Nodes): boolean | void => {
  40. if (node.type === 'paragraph' && node.position) {
  41. const { start, end } = node.position
  42. if (end.line > start.line) {
  43. const firstLine = source.split('\n')[start.line - 1] ?? ''
  44. out.push({ file, line: start.line, text: firstLine.trim() })
  45. }
  46. // Paragraph children are inline, so no further paragraph can be nested.
  47. return false
  48. }
  49. })
  50. return out
  51. }
  52. const files = uniqueRepoFiles(root, PATTERNS)
  53. const all = files.flatMap(file => findViolations(file.abs))
  54. const checked = files.length
  55. if (all.length === 0) {
  56. console.log(`verify-md-wrap: ${checked} file(s) checked, no hard-wrapped prose paragraphs.`)
  57. process.exit(0)
  58. }
  59. console.error('verify-md-wrap: hard-wrapped prose paragraphs found (write one physical line per paragraph):')
  60. for (const v of all) {
  61. console.error(` ${v.file}:${v.line} ${v.text.slice(0, 80)}${v.text.length > 80 ? '…' : ''}`)
  62. }
  63. process.exit(1)