| 48 | } |
| 49 | |
| 50 | function parseMarkdownCodeBlocks( |
| 51 | content: string, |
| 52 | filePath: string |
| 53 | ): CodeBlock[] { |
| 54 | const blocks: CodeBlock[] = []; |
| 55 | const lines = content.split("\n"); |
| 56 | |
| 57 | let inCodeBlock = false; |
| 58 | let currentLang = ""; |
| 59 | let currentCode: string[] = []; |
| 60 | let blockStartLine = 0; |
| 61 | let skipNext = false; |
| 62 | let wrapAsync = false; |
| 63 | let inHiddenBlock = false; |
| 64 | |
| 65 | for (let i = 0; i < lines.length; i++) { |
| 66 | const line = lines[i]; |
| 67 | |
| 68 | // Check for validation directives |
| 69 | if (line.includes("<!-- docs-validate: skip -->")) { |
| 70 | skipNext = true; |
| 71 | continue; |
| 72 | } |
| 73 | if (line.includes("<!-- docs-validate: wrap-async -->")) { |
| 74 | wrapAsync = true; |
| 75 | continue; |
| 76 | } |
| 77 | if (line.includes("<!-- docs-validate: hidden -->")) { |
| 78 | inHiddenBlock = true; |
| 79 | continue; |
| 80 | } |
| 81 | if (line.includes("<!-- /docs-validate: hidden -->")) { |
| 82 | inHiddenBlock = false; |
| 83 | // Skip the next visible code block since the hidden one replaces it |
| 84 | skipNext = true; |
| 85 | continue; |
| 86 | } |
| 87 | |
| 88 | // Start of code block |
| 89 | if (!inCodeBlock && line.startsWith("```")) { |
| 90 | const lang = line.slice(3).trim().toLowerCase(); |
| 91 | if (lang && LANGUAGE_MAP[lang]) { |
| 92 | inCodeBlock = true; |
| 93 | currentLang = LANGUAGE_MAP[lang]; |
| 94 | currentCode = []; |
| 95 | blockStartLine = i + 1; // 1-indexed line number |
| 96 | } |
| 97 | continue; |
| 98 | } |
| 99 | |
| 100 | // End of code block |
| 101 | if (inCodeBlock && line.startsWith("```")) { |
| 102 | blocks.push({ |
| 103 | language: currentLang, |
| 104 | code: currentCode.join("\n"), |
| 105 | file: filePath, |
| 106 | line: blockStartLine, |
| 107 | skip: skipNext, |