| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- /**
- * Reject Markdown prose paragraphs spanning multiple physical lines. The GFM
- * AST distinguishes paragraphs—including those in lists and blockquotes—from
- * multiline structural nodes. The checker never rewrites; symlinked instruction
- * files are deduped. The owning convention is in `docs/AGENTS.md`.
- */
- import { readFileSync } from 'node:fs'
- import { relative, resolve } from 'node:path'
- import type { Nodes } from 'mdast'
- import { parseMarkdown, visitMarkdown } from './markdown.ts'
- import { uniqueRepoFiles } from './repo-files.ts'
- const root = resolve(import.meta.dirname, '..')
- /** Files to check: doc-typecheck's scope, system-prompt expected outputs, and the AGENTS.md pair. */
- const PATTERNS = [
- 'README.md',
- 'README.zh.md',
- '.agents/notes/**/*.md',
- 'docs/**/*.md',
- 'packages/*/*.md',
- 'packages/*/*/*.md',
- 'examples/**/system-prompt.expected.md',
- 'packages/**/system-prompt.expected.md',
- 'AGENTS.md',
- 'packages/AGENTS.md',
- ]
- /** A located hard-wrap: a prose paragraph spanning more than one source line. */
- interface Violation {
- file: string
- /** 1-based line where the hard-wrapped paragraph starts. */
- line: number
- text: string
- }
- /** Find every hard-wrapped prose paragraph in one Markdown file via its AST. */
- function findViolations(absPath: string): Violation[] {
- const file = relative(root, absPath)
- const source = readFileSync(absPath, 'utf8')
- const tree = parseMarkdown(source)
- const out: Violation[] = []
- visitMarkdown(tree, (node: Nodes): boolean | void => {
- if (node.type === 'paragraph' && node.position) {
- const { start, end } = node.position
- if (end.line > start.line) {
- const firstLine = source.split('\n')[start.line - 1] ?? ''
- out.push({ file, line: start.line, text: firstLine.trim() })
- }
- // Paragraph children are inline, so no further paragraph can be nested.
- return false
- }
- })
- return out
- }
- const files = uniqueRepoFiles(root, PATTERNS)
- const all = files.flatMap(file => findViolations(file.abs))
- const checked = files.length
- if (all.length === 0) {
- console.log(`verify-md-wrap: ${checked} file(s) checked, no hard-wrapped prose paragraphs.`)
- process.exit(0)
- }
- console.error('verify-md-wrap: hard-wrapped prose paragraphs found (write one physical line per paragraph):')
- for (const v of all) {
- console.error(` ${v.file}:${v.line} ${v.text.slice(0, 80)}${v.text.length > 80 ? '…' : ''}`)
- }
- process.exit(1)
|