(response: string)
| 43 | * If no code block markers are found, returns the original content |
| 44 | */ |
| 45 | export function extractCode(response: string): string { |
| 46 | // 1. Try to extract content strictly within ``` fences |
| 47 | // Regex captures content between ```language and ``` |
| 48 | const codeBlockMatch = response.match(/```(?:tsx|typescript|react|js|javascript|json|css|less|scss)?\s*\n([\s\S]*?)```/); |
| 49 | |
| 50 | if (codeBlockMatch && codeBlockMatch[1]) { |
| 51 | return codeBlockMatch[1].trim(); |
| 52 | } |
| 53 | |
| 54 | // 2. Fallback: If no clear block structure is found (or fences are missing/malformed), |
| 55 | // try to strip loose fences just in case, but usually method 1 catches the block. |
| 56 | // If the model returned JUST code without fences, this preserves it. |
| 57 | // If the model returned "Here is code: code", this returns the whole string (which might be bad, but safest fallback). |
| 58 | |
| 59 | // Removing loose fences if any remain (unlikely if method 1 failed but good for cleanup) |
| 60 | const cleaned = response |
| 61 | .replace(/```(tsx|typescript|react|js|javascript|json|css|less|scss)?/g, '') |
| 62 | .replace(/```/g, '') |
| 63 | .trim(); |
| 64 | |
| 65 | return cleaned; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Extract multiple files from content with file headers |
no outgoing calls
no test coverage detected