diff options
| author | Tae Hyung Kim <[email protected]> | 2025-05-07 21:15:41 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-05-07 21:15:41 -0700 |
| commit | 13eadcea4506eaa84676d7db94179589f7a6f8b7 (patch) | |
| tree | 389e8527e42d6f6dab3ef84ae9cde084493571e7 /packages/cli/src/ui/utils/markdownUtilities.ts | |
| parent | d524309e3c66a0c1f2e0a5038c204735eb81683b (diff) | |
Fix bugs from useGeminiStream refactor (#284)
Diffstat (limited to 'packages/cli/src/ui/utils/markdownUtilities.ts')
| -rw-r--r-- | packages/cli/src/ui/utils/markdownUtilities.ts | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/packages/cli/src/ui/utils/markdownUtilities.ts b/packages/cli/src/ui/utils/markdownUtilities.ts index 94492b8c..96c2ba39 100644 --- a/packages/cli/src/ui/utils/markdownUtilities.ts +++ b/packages/cli/src/ui/utils/markdownUtilities.ts @@ -184,3 +184,35 @@ export const findSafeSplitPoint = ( // to keep the entire content as one piece. return content.length; }; + +export const findLastSafeSplitPoint = (content: string) => { + const enclosingBlockStart = findEnclosingCodeBlockStart( + content, + content.length, + ); + if (enclosingBlockStart !== -1) { + // The end of the content is contained in a code block. Split right before. + return enclosingBlockStart; + } + + // Search for the last double newline (\n\n) not in a code block. + let searchStartIndex = content.length; + while (searchStartIndex >= 0) { + const dnlIndex = content.lastIndexOf('\n\n', searchStartIndex); + if (dnlIndex === -1) { + // No more double newlines found after idealMaxLength + break; + } + + const potentialSplitPoint = dnlIndex + 2; + if (!isIndexInsideCodeBlock(content, potentialSplitPoint)) { + return potentialSplitPoint; + } + + searchStartIndex = potentialSplitPoint; // Continue search after the found \n\n + } + + // If no safe double newline found after idealMaxLength, return content.length + // to keep the entire content as one piece. + return content.length; +}; |
