| 1 | /** |
| 2 | * Extracts file paths from markdown code blocks |
| 3 | */ |
| 4 | export function extractPathsFromCodeBlocks(content: string): string[] { |
| 5 | const paths: string[] = []; |
| 6 | |
| 7 | // Match code block opening patterns: |
| 8 | // 1. ```language filepath |
| 9 | // 2. ```filepath |
| 10 | // 3. ```language filepath (range) |
| 11 | |
| 12 | // First match all code block starts |
| 13 | const codeBlockStarts = content.match(/```[^\n]+/g) || []; |
| 14 | |
| 15 | for (const blockStart of codeBlockStarts) { |
| 16 | // Try to extract a valid filename with extension |
| 17 | const filenameMatches = blockStart.match(/([^\s()```]+\.[a-zA-Z0-9]+)/); |
| 18 | |
| 19 | if (filenameMatches && filenameMatches[1]) { |
| 20 | const filename = filenameMatches[1]; |
| 21 | |
| 22 | // Verify this is a legitimate filename (not part of something else) |
| 23 | if ( |
| 24 | // Check if valid extension |
| 25 | /\.[a-zA-Z0-9]+$/.test(filename) && |
| 26 | // Make sure it's not a URL |
| 27 | !filename.includes("://") && |
| 28 | // Avoid duplicates |
| 29 | !paths.includes(filename) |
| 30 | ) { |
| 31 | paths.push(filename); |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | return paths; |
| 36 | } |