MCPcopy
hub / github.com/thesysdev/openui / stripFences

Function stripFences

packages/lang-core/src/parser/parser.ts:267–358  ·  view source on GitHub ↗
(input: string)

Source from the content-addressed store, hash-verified

265/** Extract code from markdown fences, or return as-is if no fences found.
266 * String-context-aware: skips ``` inside double-quoted strings. */
267export function stripFences(input: string): string {
268 const blocks: string[] = [];
269 let i = 0;
270
271 while (i < input.length) {
272 // Scan for opening ``` while tracking string context
273 let fenceStart = -1;
274 while (i < input.length) {
275 const nextI = skipString(input, i);
276 if (nextI > i) {
277 i = nextI;
278 continue;
279 }
280
281 const c = input[i];
282 if (
283 c === "`" &&
284 i + 1 < input.length &&
285 input[i + 1] === "`" &&
286 i + 2 < input.length &&
287 input[i + 2] === "`"
288 ) {
289 fenceStart = i;
290 break;
291 }
292 i++;
293 }
294
295 if (fenceStart === -1) break;
296
297 // Skip language tag until newline
298 let j = fenceStart + 3;
299 while (j < input.length && input[j] !== "\n") j++;
300 if (j >= input.length) {
301 // No newline after opening fence (streaming) — take everything after fence marker + lang tag
302 blocks.push(input.slice(fenceStart + 3).replace(/^[^\n]*\n?/, ""));
303 i = input.length;
304 break;
305 }
306 j++; // skip the newline
307
308 // Scan for closing ``` while tracking string context
309 let closePos = -1;
310 let k = j;
311 while (k < input.length) {
312 const nextK = skipString(input, k);
313 if (nextK > k) {
314 k = nextK;
315 continue;
316 }
317 const c = input[k];
318 if (
319 c === "`" &&
320 k + 1 < input.length &&
321 input[k + 1] === "`" &&
322 k + 2 < input.length &&
323 input[k + 2] === "`"
324 ) {

Callers 2

mergeStatementsFunction · 0.90
preprocessFunction · 0.85

Calls 2

skipStringFunction · 0.85
pushMethod · 0.80

Tested by

no test coverage detected