| 1 | import { LineStream } from "../diff/util"; |
| 2 | import { isMarkdownFile, MarkdownBlockStateTracker } from "./markdownUtils"; |
| 3 | |
| 4 | /** |
| 5 | * Determines if we should stop at a markdown block based on nested markdown logic. |
| 6 | * This handles the complex case where markdown blocks contain other markdown blocks. |
| 7 | * Uses optimized state tracking to avoid redundant computation. |
| 8 | */ |
| 9 | export function shouldStopAtMarkdownBlock( |
| 10 | stateTracker: MarkdownBlockStateTracker, |
| 11 | currentIndex: number, |
| 12 | ): boolean { |
| 13 | return stateTracker.shouldStopAtPosition(currentIndex); |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Processes block nesting logic and returns updated state. |
| 18 | */ |
| 19 | export function processBlockNesting( |
| 20 | line: string, |
| 21 | seenFirstFence: boolean, |
| 22 | shouldRemoveLineBeforeStart: (line: string) => boolean, |
| 23 | ): { newSeenFirstFence: boolean; shouldSkip: boolean } { |
| 24 | if (!seenFirstFence && shouldRemoveLineBeforeStart(line)) { |
| 25 | return { newSeenFirstFence: false, shouldSkip: true }; |
| 26 | } |
| 27 | |
| 28 | if (!seenFirstFence) { |
| 29 | return { newSeenFirstFence: true, shouldSkip: false }; |
| 30 | } |
| 31 | |
| 32 | return { newSeenFirstFence: seenFirstFence, shouldSkip: false }; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Stream transformation that stops when encountering a markdown code block ending. |
| 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; |
| 97 | } |
| 98 | } |