(path: string, cwd?: string)
| 23 | * @returns A mention-friendly path |
| 24 | */ |
| 25 | export function convertToMentionPath(path: string, cwd?: string): string { |
| 26 | // Strip file:// or vscode-remote:// protocol if present |
| 27 | let pathWithoutProtocol = path |
| 28 | |
| 29 | if (path.startsWith("file://")) { |
| 30 | pathWithoutProtocol = path.substring(7) |
| 31 | } else if (path.startsWith("vscode-remote://")) { |
| 32 | const protocolStripped = path.substring("vscode-remote://".length) |
| 33 | const firstSlashIndex = protocolStripped.indexOf("/") |
| 34 | if (firstSlashIndex !== -1) { |
| 35 | pathWithoutProtocol = protocolStripped.substring(firstSlashIndex + 1) |
| 36 | } else { |
| 37 | pathWithoutProtocol = "" |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | try { |
| 42 | pathWithoutProtocol = decodeURIComponent(pathWithoutProtocol) |
| 43 | // Fix: Remove leading slash for Windows paths like /d:/... |
| 44 | if (pathWithoutProtocol.startsWith("/") && pathWithoutProtocol[2] === ":") { |
| 45 | pathWithoutProtocol = pathWithoutProtocol.substring(1) |
| 46 | } |
| 47 | } catch (e) { |
| 48 | // Log error if decoding fails, but continue with the potentially problematic path |
| 49 | console.error("Error decoding URI component in convertToMentionPath:", e, pathWithoutProtocol) |
| 50 | } |
| 51 | |
| 52 | const normalizedPath = pathWithoutProtocol.replace(/\\/g, "/") |
| 53 | let normalizedCwd = cwd ? cwd.replace(/\\/g, "/") : "" |
| 54 | |
| 55 | if (!normalizedCwd) { |
| 56 | return pathWithoutProtocol |
| 57 | } |
| 58 | |
| 59 | // Remove trailing slash from cwd if it exists |
| 60 | if (normalizedCwd.endsWith("/")) { |
| 61 | normalizedCwd = normalizedCwd.slice(0, -1) |
| 62 | } |
| 63 | |
| 64 | // Always use case-insensitive comparison for path matching |
| 65 | const lowerPath = normalizedPath.toLowerCase() |
| 66 | const lowerCwd = normalizedCwd.toLowerCase() |
| 67 | |
| 68 | if (lowerPath.startsWith(lowerCwd)) { |
| 69 | let relativePath = normalizedPath.substring(normalizedCwd.length) |
| 70 | // Ensure there's a slash after the @ symbol when we create the mention path |
| 71 | relativePath = relativePath.startsWith("/") ? relativePath : "/" + relativePath |
| 72 | |
| 73 | // Escape any spaces in the path with backslashes |
| 74 | const escapedRelativePath = escapeSpaces(relativePath) |
| 75 | |
| 76 | return "@" + escapedRelativePath |
| 77 | } |
| 78 | |
| 79 | return pathWithoutProtocol |
| 80 | } |
no test coverage detected