| 185 | * @internal |
| 186 | */ |
| 187 | export const extractFencedCode = (content: string): [examples: Array<string>, warnings: Array<string>] => { |
| 188 | // The regex now captures the closing fence (group 3) if present. |
| 189 | // If there's no closing fence, group 3 will be undefined. |
| 190 | const fenceRegex = /(?:```|~~~)(.*?)\n([\s\S]*?)(?:(```|~~~)|$)/g |
| 191 | const matches = Array.fromIterable(content.matchAll(fenceRegex)) |
| 192 | |
| 193 | const warnings: Array<string> = [] |
| 194 | |
| 195 | // Log a warning if a code fence is not properly closed. |
| 196 | for (const match of matches) { |
| 197 | if (match[3] === undefined) { |
| 198 | warnings.push(`Code block does not have a matching closing fence:\n${content}`) |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return [ |
| 203 | matches |
| 204 | .filter((match) => { |
| 205 | const meta = match[1].toLocaleLowerCase() |
| 206 | const isTypeScript = meta.startsWith("ts") || meta.startsWith("typescript") |
| 207 | const isSkipTypeChecking = meta.includes(SKIP_TYPE_CHECKING_FENCE_METADATA) |
| 208 | return isTypeScript && !isSkipTypeChecking |
| 209 | }) |
| 210 | .map((match) => match[2].trim()), |
| 211 | warnings |
| 212 | ] |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Generates example files for the given modules. |