continue-custom/core/llm/utils/extractContentFromCodeBlocks.ts
2025-06-20 09:27:33 -07:00

29 lines
No EOL
837 B
TypeScript

/**
* Extracts content from code blocks with given path
* @param content The full message content
* @param path The file path to look for
* @returns The content of the code block if found, undefined otherwise
*/
export function extractContentFromCodeBlock(
content: string,
path: string
): string | undefined {
// Escape special characters in the path for regex
const escapedPath = path.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
// Match the code block with this path
// This regex looks for:
// 1. Code block start with the path (```[language] path)
// 2. The content until the closing ```
const regex = new RegExp(
`\`\`\`[^\\n]*${escapedPath}[^\\n]*\\n([\\s\\S]*?)\\n\`\`\``,
"m"
);
const match = content.match(regex);
if (match && match[1]) {
return match[1];
}
return undefined;
}