verify-md-wrap.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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, prompt goldens, and the AGENTS.md pair. */
  14. const PATTERNS = [
  15. 'README.md',
  16. 'README.zh.md',
  17. 'docs/**/*.md',
  18. 'packages/*/*.md',
  19. 'packages/*/*/*.md',
  20. 'examples/**/system-prompt.golden.md',
  21. 'packages/**/system-prompt.golden.md',
  22. 'AGENTS.md',
  23. 'packages/AGENTS.md',
  24. ]
  25. /** A located hard-wrap: a prose paragraph spanning more than one source line. */
  26. interface Violation {
  27. file: string
  28. /** 1-based line where the hard-wrapped paragraph starts. */
  29. line: number
  30. text: string
  31. }
  32. /** Find every hard-wrapped prose paragraph in one Markdown file via its AST. */
  33. function findViolations(absPath: string): Violation[] {
  34. const file = relative(root, absPath)
  35. const source = readFileSync(absPath, 'utf8')
  36. const tree = parseMarkdown(source)
  37. const out: Violation[] = []
  38. visitMarkdown(tree, (node: Nodes): boolean | void => {
  39. if (node.type === 'paragraph' && node.position) {
  40. const { start, end } = node.position
  41. if (end.line > start.line) {
  42. const firstLine = source.split('\n')[start.line - 1] ?? ''
  43. out.push({ file, line: start.line, text: firstLine.trim() })
  44. }
  45. // Paragraph children are inline, so no further paragraph can be nested.
  46. return false
  47. }
  48. })
  49. return out
  50. }
  51. const files = uniqueRepoFiles(root, PATTERNS)
  52. const all = files.flatMap(file => findViolations(file.abs))
  53. const checked = files.length
  54. if (all.length === 0) {
  55. console.log(`verify-md-wrap: ${checked} file(s) checked, no hard-wrapped prose paragraphs.`)
  56. process.exit(0)
  57. }
  58. console.error('verify-md-wrap: hard-wrapped prose paragraphs found (write one physical line per paragraph):')
  59. for (const v of all) {
  60. console.error(` ${v.file}:${v.line} ${v.text.slice(0, 80)}${v.text.length > 80 ? '…' : ''}`)
  61. }
  62. process.exit(1)