(input: string)
| 289 | * Extracts image file paths from user input using @path syntax and auto-detection |
| 290 | */ |
| 291 | export function extractImagePaths(input: string): string[] { |
| 292 | const paths: string[] = [] |
| 293 | |
| 294 | // Skip paths inside code blocks |
| 295 | const cleanInput = input.replace(/```[\s\S]*?```|`[^`]*`/g, ' ') |
| 296 | |
| 297 | const addPath = (p: string) => { |
| 298 | const cleaned = p.replace(/[.,!?;)\]}>">]+$/, '') // Remove trailing punctuation |
| 299 | if (isImageFile(cleaned) && !paths.includes(cleaned)) { |
| 300 | paths.push(cleaned) |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | // @path syntax |
| 305 | for (const match of cleanInput.matchAll(/@([^\s]+)/g)) { |
| 306 | addPath(match[1]) |
| 307 | } |
| 308 | |
| 309 | // Path patterns to detect |
| 310 | const patterns = [ |
| 311 | `(?:^|\\s)((?:[~/]|[A-Za-z]:\\\\)[^\\s"']*\\.(?:${IMAGE_EXTENSIONS_PATTERN}))(?=\\s|$|[.,!?;)\\]}>])`, // Absolute paths |
| 312 | `(?:^|\\s)(\\.\\.?[\\/\\\\][^\\s"']*\\.(?:${IMAGE_EXTENSIONS_PATTERN}))(?=\\s|$|[.,!?;)\\]}>])`, // ./path, ../path |
| 313 | `(?:^|\\s)((?![^\\s]*:\\/\\/|@)[^\\s"':]*[\\/\\\\][^\\s"']*\\.(?:${IMAGE_EXTENSIONS_PATTERN}))(?=\\s|$|[.,!?;)\\]}>])`, // relative/path |
| 314 | `["']([^"']*[\\/\\\\][^"']*\\.(?:${IMAGE_EXTENSIONS_PATTERN}))["']`, // Quoted paths |
| 315 | ] |
| 316 | |
| 317 | for (const pattern of patterns) { |
| 318 | const regex = new RegExp(pattern, 'gi') |
| 319 | for (const match of cleanInput.matchAll(regex)) { |
| 320 | addPath(match[1]) |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | return paths |
| 325 | } |
no test coverage detected