(text: string, cwd: string)
| 358 | * Returns the resolved absolute path if valid, null otherwise. |
| 359 | */ |
| 360 | export function getImageFilePathFromText(text: string, cwd: string): string | null { |
| 361 | // Must be single line (no internal newlines, including Windows \r\n) |
| 362 | if (text.includes('\n') || text.includes('\r')) return null |
| 363 | |
| 364 | // Must not be empty or have only whitespace |
| 365 | let trimmed = text.trim() |
| 366 | if (!trimmed) return null |
| 367 | |
| 368 | // Handle file:// URLs that some systems use for dragged files |
| 369 | if (trimmed.startsWith('file://')) { |
| 370 | trimmed = decodeURIComponent(trimmed.slice(7)) |
| 371 | } |
| 372 | |
| 373 | // Skip if it looks like a URL (but not file:// which we already handled) |
| 374 | if (trimmed.includes('://')) return null |
| 375 | |
| 376 | // Remove surrounding quotes that some terminals add |
| 377 | if ((trimmed.startsWith('"') && trimmed.endsWith('"')) || |
| 378 | (trimmed.startsWith("'") && trimmed.endsWith("'"))) { |
| 379 | trimmed = trimmed.slice(1, -1) |
| 380 | } |
| 381 | |
| 382 | try { |
| 383 | // Try to resolve the path |
| 384 | const resolvedPath = resolveFilePath(trimmed, cwd) |
| 385 | |
| 386 | // Check if file exists |
| 387 | if (!existsSync(resolvedPath)) return null |
| 388 | |
| 389 | // Check if it's a supported image format |
| 390 | if (!isImageFile(resolvedPath)) return null |
| 391 | |
| 392 | return resolvedPath |
| 393 | } catch { |
| 394 | return null |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Read file URL/path from clipboard when a file has been copied (e.g., from Finder). |
no test coverage detected