|
2 | 2 | * Utilities for handling path-related operations in mentions |
3 | 3 | */ |
4 | 4 |
|
| 5 | +/** |
| 6 | + * Helper function to escape backslashes and spaces in a path. |
| 7 | + * This encapsulates the multi-level escaping strategy for file paths. |
| 8 | + * @param inputPath The path to escape |
| 9 | + * @returns The escaped path |
| 10 | + */ |
| 11 | +function escapePath(inputPath: string): string { |
| 12 | + return inputPath.replace(/\\/g, '\\\\').replace(/ /g, '\\ '); |
| 13 | +} |
| 14 | + |
5 | 15 | /** |
6 | 16 | * Converts an absolute path to a mention-friendly path |
7 | 17 | * If the provided path starts with the current working directory, |
|
13 | 23 | */ |
14 | 24 | export function convertToMentionPath(path: string, cwd?: string): string { |
15 | 25 | // First, handle Windows path separators and convert to forward slashes |
16 | | - let processedPath = path.replace(/\\\\/g, "//").replace(/\\/g, "/"); |
17 | | - let normalizedCwd = cwd ? cwd.replace(/\\\\/g, "//").replace(/\\/g, "/") : ""; |
| 26 | + let processedPath = path.replace(/\\\\\\/g, "//").replace(/\\/g, "/"); |
| 27 | + let normalizedCwd = cwd ? cwd.replace(/\\\\\\/g, "//").replace(/\\/g, "/") : ""; |
18 | 28 |
|
19 | 29 | if (!normalizedCwd) { |
20 | 30 | // If no CWD, just escape spaces in the original path |
21 | | - return processedPath.replace(/\\/g, '\\\\').replace(/ /g, '\\ '); |
| 31 | + return escapePath(processedPath); |
22 | 32 | } |
23 | 33 |
|
24 | 34 | // Remove trailing slash from cwd if it exists |
@@ -53,11 +63,11 @@ export function convertToMentionPath(path: string, cwd?: string): string { |
53 | 63 | * will undergo a second round of escaping, resulting in double backslashes. |
54 | 64 | * This is necessary to preserve the escapes through the entire text processing pipeline. |
55 | 65 | */ |
56 | | - // Escape backslashes first, then spaces (single backslash for space) |
57 | | - return mentionPath.replace(/\\/g, '\\\\').replace(/ /g, '\\ '); |
| 66 | + // Use the helper function to escape the path |
| 67 | + return escapePath(mentionPath); |
58 | 68 | } |
59 | 69 |
|
60 | 70 | // If path doesn't start with CWD, escape spaces in the processed path |
61 | | - // Escape backslashes first, then spaces (single backslash for space) |
62 | | - return processedPath.replace(/\\/g, '\\\\').replace(/ /g, '\\ '); |
| 71 | + // Use the helper function to escape the path |
| 72 | + return escapePath(processedPath); |
63 | 73 | } |
0 commit comments