( lines: LineStream, filename: string, )
| 37 | * Handles nested markdown blocks in markdown files. |
| 38 | */ |
| 39 | export async function* stopAtLinesWithMarkdownSupport( |
| 40 | lines: LineStream, |
| 41 | filename: string, |
| 42 | ): LineStream { |
| 43 | if (!isMarkdownFile(filename)) { |
| 44 | for await (const line of lines) { |
| 45 | if (line.trim() === "```") { |
| 46 | return; |
| 47 | } |
| 48 | yield line; |
| 49 | } |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | const allLines: string[] = []; |
| 54 | for await (const line of lines) { |
| 55 | allLines.push(line); |
| 56 | } |
| 57 | |
| 58 | const source = allLines.join("\n"); |
| 59 | if (!source.match(/```(\w*|.*)(md|markdown|gfm|github-markdown)/)) { |
| 60 | // No nested markdown blocks detected, check for simple ``` stopping condition |
| 61 | let foundStandaloneBackticks = false; |
| 62 | for (let i = 0; i < allLines.length; i++) { |
| 63 | if (allLines[i].trim() === "```") { |
| 64 | // Found standalone backticks, yield lines up to this point |
| 65 | for (let j = 0; j < i; j++) { |
| 66 | yield allLines[j]; |
| 67 | } |
| 68 | foundStandaloneBackticks = true; |
| 69 | return; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // No standalone backticks found, yield all lines |
| 74 | if (!foundStandaloneBackticks) { |
| 75 | for (const line of allLines) { |
| 76 | yield line; |
| 77 | } |
| 78 | } |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | // Use optimized state tracker for markdown block analysis |
| 83 | const stateTracker = new MarkdownBlockStateTracker(allLines); |
| 84 | |
| 85 | for (let i = 0; i < allLines.length; i++) { |
| 86 | if (stateTracker.shouldStopAtPosition(i)) { |
| 87 | for (let j = 0; j < i; j++) { |
| 88 | yield allLines[j]; |
| 89 | } |
| 90 | return; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // If we get here, yield all lines |
| 95 | for (const line of allLines) { |
| 96 | yield line; |
no test coverage detected