1
0

verify-md-wrap.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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, prompt goldens, and the AGENTS.md pair. */
  15. const PATTERNS = [
  16. 'README.md',
  17. 'README.zh.md',
  18. 'docs/**/*.md',
  19. 'packages/*/*.md',
  20. 'packages/*/*/*.md',
  21. 'examples/**/system-prompt.golden.md',
  22. 'packages/**/system-prompt.golden.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. function maskVitePressStructure(source: string): string {
  34. const lines = source.split('\n')
  35. if (lines[0] === '---') {
  36. const closing = lines.indexOf('---', 1)
  37. if (closing !== -1) {
  38. for (let index = 0; index <= closing; index++) lines[index] = ''
  39. }
  40. }
  41. return lines.map(line => line.trimStart().startsWith(':::') ? '' : line).join('\n')
  42. }
  43. /** Find every hard-wrapped prose paragraph in one Markdown file via its AST. */
  44. function findViolations(absPath: string): Violation[] {
  45. const file = relative(root, absPath)
  46. const source = readFileSync(absPath, 'utf8')
  47. const parsedSource = maskVitePressStructure(source)
  48. const tree = parseMarkdown(parsedSource)
  49. const out: Violation[] = []
  50. visitMarkdown(tree, (node: Nodes): boolean | 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. // Paragraph children are inline, so no further paragraph can be nested.
  58. return false
  59. }
  60. })
  61. return out
  62. }
  63. const files = uniqueRepoFiles(root, PATTERNS)
  64. const all = files.flatMap(file => findViolations(file.abs))
  65. const checked = files.length
  66. if (all.length === 0) {
  67. console.log(`verify-md-wrap: ${checked} file(s) checked, no hard-wrapped prose paragraphs.`)
  68. process.exit(0)
  69. }
  70. console.error('verify-md-wrap: hard-wrapped prose paragraphs found (write one physical line per paragraph):')
  71. for (const v of all) {
  72. console.error(` ${v.file}:${v.line} ${v.text.slice(0, 80)}${v.text.length > 80 ? '…' : ''}`)
  73. }
  74. process.exit(1)