(input: ParserInput)
| 27 | */ |
| 28 | export class ParserHandler implements ParserSpec { |
| 29 | execute(input: ParserInput): ParserResult { |
| 30 | try { |
| 31 | const { content } = input; |
| 32 | const processor = unified() |
| 33 | .use(remarkParse) |
| 34 | .use(remarkFrontmatter, ['yaml']); |
| 35 | |
| 36 | const ast = processor.parse(content) as Root; |
| 37 | const blocks: Array<{ yaml: string; block: 'frontmatter' | number; startLine: number }> = []; |
| 38 | const sections: string[] = []; |
| 39 | const headingsWithLines: Array<{ text: string; line: number }> = []; |
| 40 | let blockIndex = 0; |
| 41 | |
| 42 | visit(ast, (node) => { |
| 43 | if (node.type === 'yaml') { |
| 44 | const yamlNode = node as Yaml; |
| 45 | blocks.push({ |
| 46 | yaml: yamlNode.value, |
| 47 | block: 'frontmatter', |
| 48 | startLine: node.position?.start.line ?? 1 |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | if (node.type === 'code') { |
| 53 | const codeNode = node as Code; |
| 54 | if (codeNode.lang === 'yaml' || codeNode.lang === 'yml') { |
| 55 | blocks.push({ |
| 56 | yaml: codeNode.value, |
| 57 | block: blockIndex, |
| 58 | startLine: node.position?.start.line ?? 1 |
| 59 | }); |
| 60 | blockIndex++; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if (node.type === 'heading') { |
| 65 | const heading = node as Heading; |
| 66 | if (heading.depth === 2) { |
| 67 | const text = this.extractHeadingText(heading.children); |
| 68 | if (text) { |
| 69 | sections.push(text); |
| 70 | headingsWithLines.push({ text, line: node.position?.start.line ?? 1 }); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | }); |
| 75 | |
| 76 | // Slice content into sections |
| 77 | const contentLines = content.split('\n'); |
| 78 | const documentSections: Array<{ heading: string; content: string }> = []; |
| 79 | |
| 80 | const firstHeading = headingsWithLines[0]; |
| 81 | if (firstHeading) { |
| 82 | // Prelude (content before first H2) |
| 83 | const firstHeadingLine = firstHeading.line; |
| 84 | if (firstHeadingLine > 1) { |
| 85 | documentSections.push({ |
| 86 | heading: '', |
no test coverage detected