/** * Shared, fence-aware Markdown scanning used by the governance doc checkers * (check-agents-md.ts, check-skill-md.ts). Pure functions only: the CLI * wrappers own file I/O so tests can drive fixtures without touching disk. */ export interface DocIssue { file: string line?: number rule: string message: string } export interface MarkdownLine { /** 1-based line number. */ number: number text: string /** True when this line sits inside an open ``` / ~~~ fence. */ inFence: boolean } const FENCE_OPEN = /^\s{0,3}(```|~~~)/ const HEADING = /^(#{1,6})\s+(.*)$/ export function scanMarkdownLines(content: string): MarkdownLine[] { let inFence = false return content.split("\n").map((text, index) => { const line: MarkdownLine = { number: index + 1, text, inFence } if (FENCE_OPEN.test(text)) inFence = !inFence return line }) } export interface Heading { level: number text: string line: number } export function extractHeadings(content: string): Heading[] { const headings: Heading[] = [] for (const line of scanMarkdownLines(content)) { if (line.inFence) continue const match = line.text.match(HEADING) if (match) headings.push({ level: match[1]!.length, text: match[2]!.trim(), line: line.number }) } return headings } /** Exactly one H1 and it must be the first heading of the document. */ export function checkSingleLeadingH1(headings: Heading[], file: string): DocIssue[] { const h1s = headings.filter((heading) => heading.level === 1) if (h1s.length === 1 && h1s[0] === headings[0]) return [] if (h1s.length === 0) { return [{ file, rule: "h1", message: "document has no H1 heading" }] } if (h1s[0] !== headings[0]) { return [{ file, line: h1s[0]!.line, rule: "h1", message: "H1 must be the first heading" }] } return h1s.slice(1).map((heading) => ({ file, line: heading.line, rule: "h1", message: `duplicate H1 "${heading.text}" (keep a single document title)` })) } /** Heading levels may step down freely but may never jump up by more than one. */ export function checkHeadingHierarchy(headings: Heading[], file: string): DocIssue[] { const issues: DocIssue[] = [] let previous: Heading | undefined for (const heading of headings) { if (previous && heading.level > previous.level + 1) { issues.push({ file, line: heading.line, rule: "heading-hierarchy", message: `heading jumps from H${previous.level} to H${heading.level} ("${heading.text}")` }) } previous = heading } return issues } /** Trailing whitespace outside code fences, and fences that never close. */ export function checkHygiene(content: string, file: string): DocIssue[] { const issues: DocIssue[] = [] let lastFenceToggle: MarkdownLine | undefined const lines = scanMarkdownLines(content) for (const line of lines) { if (FENCE_OPEN.test(line.text)) { lastFenceToggle = line continue } if (!line.inFence && /[ \t]+$/.test(line.text)) { issues.push({ file, line: line.number, rule: "trailing-whitespace", message: "trailing whitespace" }) } } if (lines.length > 0 && lines[lines.length - 1]!.inFence && lastFenceToggle) { issues.push({ file, line: lastFenceToggle.number, rule: "unclosed-fence", message: "code fence opened here is never closed" }) } return issues }