MCPcopy Create free account
hub / github.com/MigoXLab/coderio / extractCode

Function extractCode

src/utils/parser.ts:45–66  ·  view source on GitHub ↗
(response: string)

Source from the content-addressed store, hash-verified

43 * If no code block markers are found, returns the original content
44 */
45export 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

Callers 3

saveGeneratedCodeFunction · 0.90
injectRootComponentToAppFunction · 0.90
parser.test.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected