verify-md-wrap.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 { isArchivedAgentNotePath, 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. 'snapshots/**/system-prompt.expected.md',
  23. 'packages/**/system-prompt.expected.md',
  24. 'AGENTS.md',
  25. 'packages/AGENTS.md',
  26. 'snapshots/AGENTS.md',
  27. ]
  28. /** A located hard-wrap: a prose paragraph spanning more than one source line. */
  29. interface Violation {
  30. file: string
  31. /** 1-based line where the hard-wrapped paragraph starts. */
  32. line: number
  33. text: string
  34. }
  35. function maskVitePressStructure(source: string): string {
  36. const lines = source.split('\n')
  37. if (lines[0] === '---') {
  38. const closing = lines.indexOf('---', 1)
  39. if (closing !== -1) {
  40. for (let index = 0; index <= closing; index++) lines[index] = ''
  41. }
  42. }
  43. return lines.map(line => line.trimStart().startsWith(':::') ? '' : line).join('\n')
  44. }
  45. /** Find every hard-wrapped prose paragraph in one Markdown file via its AST. */
  46. function findViolations(absPath: string): Violation[] {
  47. const file = relative(root, absPath)
  48. const source = readFileSync(absPath, 'utf8')
  49. const parsedSource = maskVitePressStructure(source)
  50. const tree = parseMarkdown(parsedSource)
  51. const out: Violation[] = []
  52. visitMarkdown(tree, (node: Nodes): boolean | void => {
  53. if (node.type === 'paragraph' && node.position) {
  54. const { start, end } = node.position
  55. if (end.line > start.line) {
  56. const firstLine = source.split('\n')[start.line - 1] ?? ''
  57. out.push({ file, line: start.line, text: firstLine.trim() })
  58. }
  59. // Paragraph children are inline, so no further paragraph can be nested.
  60. return false
  61. }
  62. })
  63. return out
  64. }
  65. const files = uniqueRepoFiles(root, PATTERNS, isArchivedAgentNotePath)
  66. const all = files.flatMap(file => findViolations(file.abs))
  67. const checked = files.length
  68. if (all.length === 0) {
  69. console.log(`verify-md-wrap: ${checked} file(s) checked, no hard-wrapped prose paragraphs.`)
  70. process.exit(0)
  71. }
  72. console.error('verify-md-wrap: hard-wrapped prose paragraphs found (write one physical line per paragraph):')
  73. for (const v of all) {
  74. console.error(` ${v.file}:${v.line} ${v.text.slice(0, 80)}${v.text.length > 80 ? '…' : ''}`)
  75. }
  76. process.exit(1)