diff options
Diffstat (limited to 'packages/cli/src/ui/utils/markdownUtilities.ts')
| -rw-r--r-- | packages/cli/src/ui/utils/markdownUtilities.ts | 51 |
1 files changed, 15 insertions, 36 deletions
diff --git a/packages/cli/src/ui/utils/markdownUtilities.ts b/packages/cli/src/ui/utils/markdownUtilities.ts index d16b7a51..94492b8c 100644 --- a/packages/cli/src/ui/utils/markdownUtilities.ts +++ b/packages/cli/src/ui/utils/markdownUtilities.ts @@ -163,45 +163,24 @@ export const findSafeSplitPoint = ( } // idealMaxLength is NOT inside a code block. - // Search backwards from idealMaxLength for a double newline (\n\n) not in a code block. - for (let i = Math.min(idealMaxLength, content.length) - 1; i > 0; i--) { - if (content[i] === '\n' && content[i - 1] === '\n') { - const potentialSplitPoint = i + 1; - if (potentialSplitPoint <= idealMaxLength) { - if (!isIndexInsideCodeBlock(content, potentialSplitPoint)) { - return potentialSplitPoint; - } - } + // Search forwards from idealMaxLength for the next double newline (\n\n) not in a code block. + let searchStartIndex = idealMaxLength; + while (searchStartIndex < content.length) { + const dnlIndex = content.indexOf('\n\n', searchStartIndex); + if (dnlIndex === -1) { + // No more double newlines found after idealMaxLength + break; } - } - // If no safe double newline, look for a single newline (\n) - for (let i = Math.min(idealMaxLength, content.length) - 1; i >= 0; i--) { - if (content[i] === '\n') { - const potentialSplitPoint = i + 1; - if (potentialSplitPoint <= idealMaxLength) { - if (!isIndexInsideCodeBlock(content, potentialSplitPoint)) { - return potentialSplitPoint; - } - } + const potentialSplitPoint = dnlIndex + 2; + if (!isIndexInsideCodeBlock(content, potentialSplitPoint)) { + return potentialSplitPoint; } - } - // Fallback logic if no prior safe split was found - if (!isIndexInsideCodeBlock(content, idealMaxLength)) { - return idealMaxLength; - } else { - // This should ideally not be reached frequently if prior logic is sound. - // enclosingBlockStartForIdealMax would have been set if idealMaxLength was in a block. - // If somehow it's still in a block, attempt to use the start of that block. - const lastResortBlockStart = findEnclosingCodeBlockStart( - content, - idealMaxLength, - ); // Re-check - if (lastResortBlockStart !== -1) { - return lastResortBlockStart; - } - // Absolute fallback: if idealMaxLength is in a block and we can't find its start (very unlikely) - return 0; + 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; }; |
