(content: string)
| 92 | }; |
| 93 | |
| 94 | export const findLastSafeSplitPoint = (content: string) => { |
| 95 | const enclosingBlockStart = findEnclosingCodeBlockStart( |
| 96 | content, |
| 97 | content.length, |
| 98 | ); |
| 99 | if (enclosingBlockStart !== -1) { |
| 100 | // The end of the content is contained in a code block. Split right before. |
| 101 | return enclosingBlockStart; |
| 102 | } |
| 103 | |
| 104 | // Search for the last double newline (\n\n) not in a code block. |
| 105 | let searchStartIndex = content.length; |
| 106 | while (searchStartIndex >= 0) { |
| 107 | const dnlIndex = content.lastIndexOf('\n\n', searchStartIndex); |
| 108 | if (dnlIndex === -1) { |
| 109 | // No more double newlines found. |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | const potentialSplitPoint = dnlIndex + 2; |
| 114 | if (!isIndexInsideCodeBlock(content, potentialSplitPoint)) { |
| 115 | return potentialSplitPoint; |
| 116 | } |
| 117 | |
| 118 | // If potentialSplitPoint was inside a code block, |
| 119 | // the next search should start *before* the \n\n we just found to ensure progress. |
| 120 | searchStartIndex = dnlIndex - 1; |
| 121 | } |
| 122 | |
| 123 | // If no safe double newline is found, return content.length |
| 124 | // to keep the entire content as one piece. |
| 125 | return content.length; |
| 126 | }; |
no test coverage detected