MCPcopy
hub / github.com/zxlie/FeHelper / extractFirstCodeBlock

Function extractFirstCodeBlock

apps/aiagent/fh.ai-inline.js:365–383  ·  view source on GitHub ↗
(text, preferredLanguage)

Source from the content-addressed store, hash-verified

363}
364
365function extractFirstCodeBlock(text, preferredLanguage) {
366 const source = String(text || '');
367 const blocks = [];
368 const reg = /```([a-zA-Z0-9_-]*)\s*\n([\s\S]*?)```/g;
369 let match;
370 while ((match = reg.exec(source))) {
371 blocks.push({
372 lang: (match[1] || '').toLowerCase(),
373 code: (match[2] || '').trim()
374 });
375 }
376 if (!blocks.length) return '';
377 if (preferredLanguage) {
378 const target = preferredLanguage.toLowerCase();
379 const found = blocks.find(block => block.lang === target);
380 if (found) return found.code;
381 }
382 return blocks[0].code;
383}
384
385function extractJsonCandidate(text) {
386 const code = extractFirstCodeBlock(text, 'json') || extractFirstCodeBlock(text);

Callers 3

index.jsFile · 0.90
extractJsonCandidateFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected