* Extract code blocks from markdown/mdx files * @param {string} filePath - Path to the markdown file * @returns {Array} Array of code block objects
(filePath)
| 118 | * @returns {Array} Array of code block objects |
| 119 | */ |
| 120 | function extractCodeBlocks(filePath) { |
| 121 | const content = fs.readFileSync(filePath, 'utf8'); |
| 122 | const blocks = []; |
| 123 | |
| 124 | // Check if page has testable: true in frontmatter |
| 125 | const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---/); |
| 126 | const isTestablePage = frontmatterMatch && /testable:\s*true/i.test(frontmatterMatch[1]); |
| 127 | |
| 128 | // Regex to match code blocks with optional metadata |
| 129 | const codeBlockRegex = /```(\w+)([^\n]*)\n([\s\S]*?)```/g; |
| 130 | |
| 131 | let match; |
| 132 | let blockIndex = 0; |
| 133 | |
| 134 | while ((match = codeBlockRegex.exec(content)) !== null) { |
| 135 | const language = match[1]; |
| 136 | const metadata = match[2]; |
| 137 | const code = match[3]; |
| 138 | |
| 139 | // Test if: |
| 140 | // 1. Page has testable: true in frontmatter, OR |
| 141 | // 2. Individual block has test=true or testable marker (legacy) |
| 142 | // 3. BUT NOT if block has test=false marker (explicit opt-out) |
| 143 | const isExplicitlyDisabled = /\btest=false\b/.test(metadata); |
| 144 | const shouldTest = !isExplicitlyDisabled && ( |
| 145 | isTestablePage || |
| 146 | /\btest=true\b/.test(metadata) || |
| 147 | /\btestable\b/.test(metadata)); |
| 148 | |
| 149 | blocks.push({ |
| 150 | file: filePath, |
| 151 | language, |
| 152 | shouldTest, |
| 153 | code: code.trim(), |
| 154 | index: blockIndex++, |
| 155 | line: content.substring(0, match.index).split('\n').length |
| 156 | }); |
| 157 | } |
| 158 | |
| 159 | return blocks; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Find all documentation files |