(text, preferredLanguage)
| 363 | } |
| 364 | |
| 365 | function 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 | |
| 385 | function extractJsonCandidate(text) { |
| 386 | const code = extractFirstCodeBlock(text, 'json') || extractFirstCodeBlock(text); |
no outgoing calls
no test coverage detected