verify-md-wrap.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. VitePress frontmatter and custom-container delimiters are
  6. * masked before parsing. The owning convention is in `docs/AGENTS.md`.
  7. */
  8. import { readFileSync } from 'node:fs'
  9. import { relative, resolve } from 'node:path'
  10. import type { Nodes } from 'mdast'
  11. import { parseMarkdown, visitMarkdown } from './markdown.ts'
  12. import { uniqueRepoFiles } from './repo-files.ts'
  13. const root = resolve(import.meta.dirname, '..')
  14. /** Files to check: doc-typecheck's scope, system-prompt expected outputs, and the AGENTS.md pair. */
  15. const PATTERNS = [
  16. 'README.md',
  17. 'README.zh.md',
  18. '.agents/notes/**/*.md',
  19. 'docs/**/*.md',
  20. 'packages/*/*.md',
  21. 'packages/*/*/*.md',
  22. 'examples/**/system-prompt.expected.md',
  23. 'packages/**/system-prompt.expected.md',
  24. 'AGENTS.md',
  25. 'packages/AGENTS.md',
  26. ]
  27. /** A located hard-wrap: a prose paragraph spanning more than one source line. */
  28. interface Violation {
  29. file: string
  30. /** 1-based line where the hard-wrapped paragraph starts. */
  31. line: number
  32. text: string
  33. }
  34. function maskVitePressStructure(source: string): string {
  35. const lines = source.split('\n')
  36. if (lines[0] === '---') {
  37. const closing = lines.indexOf('---', 1)
  38. if (closing !== -1) {
  39. for (let index = 0; index <= closing; index++) lines[index] = ''
  40. }
  41. }
  42. return lines.map(line => line.trimStart().startsWith(':::') ? '' : line).join('\n')
  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 parsedSource = maskVitePressStructure(source)
  49. const tree = parseMarkdown(parsedSource)
  50. const out: Violation[] = []
  51. visitMarkdown(tree, (node: Nodes): boolean | void => {
  52. if (node.type === 'paragraph' && node.position) {
  53. const { start, end } = node.position
  54. if (end.line > start.line) {
  55. const firstLine = source.split('\n')[start.line - 1] ?? ''
  56. out.push({ file, line: start.line, text: firstLine.trim() })
  57. }
  58. // Paragraph children are inline, so no further paragraph can be nested.
  59. return false
  60. }
  61. })
  62. return out
  63. }
  64. const files = uniqueRepoFiles(root, PATTERNS)
  65. const all = files.flatMap(file => findViolations(file.abs))
  66. const checked = files.length
  67. if (all.length === 0) {
  68. console.log(`verify-md-wrap: ${checked} file(s) checked, no hard-wrapped prose paragraphs.`)
  69. process.exit(0)
  70. }
  71. console.error('verify-md-wrap: hard-wrapped prose paragraphs found (write one physical line per paragraph):')
  72. for (const v of all) {
  73. console.error(` ${v.file}:${v.line} ${v.text.slice(0, 80)}${v.text.length > 80 ? '…' : ''}`)
  74. }
  75. process.exit(1)