Files
deepseek-harness/scripts/verify-md-wrap.ts
Yichen Jiang 5833d70447 Merge remote-tracking branch 'origin/master' into worktree/docs-website
# Conflicts:
#	.agents/notes/implemented/process/2026-07-13-documentation-site-projection.md
#	docs/AGENTS.md
#	docs/rfc/INDEX.md
#	docs/user/develop/basic/index.zh.md
#	docs/user/develop/basic/tool.zh.md
#	docs/user/develop/framework/index.zh.md
#	docs/user/develop/framework/service.zh.md
#	docs/user/develop/practice/index.zh.md
#	docs/user/guide/index.zh.md
#	docs/user/guide/quickstart.zh.md
#	knip.json
#	package.json
#	pnpm-lock.yaml
#	scripts/doc-typecheck.ts
#	scripts/gen-website-api.ts
#	scripts/translation-pairing.manifest.json
#	scripts/verify-type-equiv.ts
#	website/zh-CN/api/cordis/context.md
#	website/zh-CN/api/cordis/events.md
#	website/zh-CN/api/cordis/fiber.md
#	website/zh-CN/api/cordis/registry.md
#	website/zh-CN/api/cordis/service.md
#	website/zh-CN/api/harness/agent-loop.md
#	website/zh-CN/api/harness/agents.md
#	website/zh-CN/api/harness/approval.md
#	website/zh-CN/api/harness/bash-env.md
#	website/zh-CN/api/harness/bash.md
#	website/zh-CN/api/harness/code-runtime.md
#	website/zh-CN/api/harness/compact.md
#	website/zh-CN/api/harness/events.md
#	website/zh-CN/api/harness/fs.md
#	website/zh-CN/api/harness/llm.md
#	website/zh-CN/api/harness/permission.md
#	website/zh-CN/api/harness/sandbox.md
#	website/zh-CN/api/harness/session-persistence.md
#	website/zh-CN/api/harness/session-query.md
#	website/zh-CN/api/harness/sessions.md
#	website/zh-CN/api/harness/skills.md
#	website/zh-CN/api/harness/spill-store.md
#	website/zh-CN/api/harness/subagents.md
#	website/zh-CN/api/harness/system-prompt.md
#	website/zh-CN/api/harness/tasks.md
#	website/zh-CN/api/harness/token-meter.md
#	website/zh-CN/api/harness/tools.md
#	website/zh-CN/api/harness/user-interaction.md
#	website/zh-CN/api/harness/web.md
#	website/zh-CN/api/harness/workflows.md
#	website/zh-CN/api/index.md
#	website/zh-CN/design/effects-coeffects.md
#	website/zh-CN/design/index.md
#	website/zh-CN/guide/config.md
2026-07-20 10:21:36 +08:00

86 lines
2.9 KiB
TypeScript

/**
* 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. VitePress frontmatter and custom-container delimiters are
* masked before parsing. 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
}
function maskVitePressStructure(source: string): string {
const lines = source.split('\n')
if (lines[0] === '---') {
const closing = lines.indexOf('---', 1)
if (closing !== -1) {
for (let index = 0; index <= closing; index++) lines[index] = ''
}
}
return lines.map(line => line.trimStart().startsWith(':::') ? '' : line).join('\n')
}
/** 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 parsedSource = maskVitePressStructure(source)
const tree = parseMarkdown(parsedSource)
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)