MCPcopy Create free account
hub / github.com/anus-dev/ANUS / findLastSafeSplitPoint

Function findLastSafeSplitPoint

packages/cli/src/ui/utils/markdownUtilities.ts:94–126  ·  view source on GitHub ↗
(content: string)

Source from the content-addressed store, hash-verified

92};
93
94export 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};

Callers 2

useAnusStreamFunction · 0.85

Calls 2

isIndexInsideCodeBlockFunction · 0.85

Tested by

no test coverage detected