diff --git a/core/llm/utils/extractContentFromCodeBlocks.test.ts b/core/llm/utils/extractContentFromCodeBlocks.test.ts new file mode 100644 index 000000000..b6a612ed3 --- /dev/null +++ b/core/llm/utils/extractContentFromCodeBlocks.test.ts @@ -0,0 +1,71 @@ +import { extractContentFromCodeBlock } from "./extractContentFromCodeBlocks"; + +describe("extractContentFromCodeBlock", () => { + it("should extract content from code block with language and filepath", () => { + const content = "```typescript src/main.ts\nconst x = 1;\n```"; + expect(extractContentFromCodeBlock(content, "src/main.ts")).toBe("const x = 1;"); + }); + + it("should extract content from code block with filepath only (no language)", () => { + const content = "```test.js\nclass Calculator { }\n```"; + expect(extractContentFromCodeBlock(content, "test.js")).toBe("class Calculator { }"); + }); + + it("should extract content from code block with line ranges", () => { + const content = "```js test.js (19-25)\nfunction test() {}\n```"; + expect(extractContentFromCodeBlock(content, "test.js")).toBe("function test() {}"); + }); + + it("should handle multiple code blocks and find the right one", () => { + const content = + "```typescript src/main.ts\nconst x = 1;\n```\n" + + "```test.js\nclass Calculator { }\n```\n" + + "```js utils.js (19-25)\nfunction test() {}\n```"; + + expect(extractContentFromCodeBlock(content, "src/main.ts")).toBe("const x = 1;"); + expect(extractContentFromCodeBlock(content, "test.js")).toBe("class Calculator { }"); + expect(extractContentFromCodeBlock(content, "utils.js")).toBe("function test() {}"); + }); + + it("should handle paths with special regex characters", () => { + const content = "```typescript src/special+chars.ts[123]\nconst special = true;\n```"; + expect(extractContentFromCodeBlock(content, "src/special+chars.ts[123]")).toBe("const special = true;"); + }); + + it("should handle multiline content", () => { + const content = + "```typescript src/multi-line.ts\n" + + "function example() {\n" + + " const x = 1;\n" + + " return x + 2;\n" + + "}\n" + + "```"; + + const expected = + "function example() {\n" + + " const x = 1;\n" + + " return x + 2;\n" + + "}"; + + expect(extractContentFromCodeBlock(content, "src/multi-line.ts")).toBe(expected); + }); + + it("should return undefined when path doesn't exist in any code block", () => { + const content = "```typescript src/main.ts\nconst x = 1;\n```"; + expect(extractContentFromCodeBlock(content, "nonexistent.ts")).toBeUndefined(); + }); + + it("should extract content when the path has directory structure", () => { + const content = "```js src/components/Button.jsx\nconst Button = () => ;\n```"; + expect(extractContentFromCodeBlock(content, "src/components/Button.jsx")).toBe("const Button = () => ;"); + }); + + it("should match exact paths only", () => { + const content = + "```typescript src/utils.ts\nconst util = {};\n```\n" + + "```typescript src/utils/index.ts\nconst index = {};\n```"; + + expect(extractContentFromCodeBlock(content, "src/utils.ts")).toBe("const util = {};"); + expect(extractContentFromCodeBlock(content, "src/utils/index.ts")).toBe("const index = {};"); + }); +}); \ No newline at end of file diff --git a/core/llm/utils/extractContentFromCodeBlocks.ts b/core/llm/utils/extractContentFromCodeBlocks.ts index 00abc49cf..c67ab2f7d 100644 --- a/core/llm/utils/extractContentFromCodeBlocks.ts +++ b/core/llm/utils/extractContentFromCodeBlocks.ts @@ -6,24 +6,28 @@ */ export function extractContentFromCodeBlock( content: string, - path: 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 ``` + // The path can be: + // - With language: ```typescript src/main.ts + // - Without language: ```src/main.ts + // - With line ranges: ```js src/main.ts (1-10) const regex = new RegExp( - `\`\`\`[^\\n]*${escapedPath}[^\\n]*\\n([\\s\\S]*?)\\n\`\`\``, - "m" + `\`\`\`[^\\n]*\\b${escapedPath}(?:\\s+\\([\\d-]+\\))?[^\\n]*\\n([\\s\\S]*?)\\n\`\`\``, + "m", ); - + const match = content.match(regex); if (match && match[1]) { return match[1]; } - + return undefined; -} \ No newline at end of file +}