Skip to content

Commit 042c2d0

Browse files
committed
fix: Resolve path space escaping and add missing path-browserify dependency
- Refined space escaping logic in `context-mentions.ts` and `path-mentions.ts` to handle potential edge cases and improve robustness, addressing concerns about escaped characters. - Added `path-browserify` and `@types/path-browserify` to `webview-ui/package.json` to resolve missing dependency and type errors.
1 parent 718fb82 commit 042c2d0

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

webview-ui/package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webview-ui/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"knuth-shuffle-seeded": "^1.0.6",
4646
"lucide-react": "^0.475.0",
4747
"mermaid": "^11.4.1",
48+
"path-browserify": "^1.0.1",
4849
"posthog-js": "^1.227.2",
4950
"react": "^18.3.1",
5051
"react-dom": "^18.3.1",
@@ -75,6 +76,7 @@
7576
"@testing-library/user-event": "^14.6.1",
7677
"@types/jest": "^27.5.2",
7778
"@types/node": "^18.0.0",
79+
"@types/path-browserify": "^1.0.2",
7880
"@types/react": "^18.3.18",
7981
"@types/react-dom": "^18.3.5",
8082
"@types/shell-quote": "^1.7.5",

webview-ui/src/utils/context-mentions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ export function insertMention(
6565
*/
6666
// Escape spaces, handling already escaped spaces
6767
const formattedValue = value
68+
.replace(/\\\\/g, "\\DOUBLE_BACKSLASH") // First preserve actual double backslashes
6869
.replace(/\\ /g, "\\ESCAPED_SPACE") // Temporarily replace already escaped spaces
6970
.replace(/ /g, "\\ ") // Escape all normal spaces
7071
.replace(/\\ESCAPED_SPACE/g, "\\\\ ") // Restore escaped spaces with proper escaping
72+
.replace(/\\DOUBLE_BACKSLASH/g, "\\\\") // Restore actual double backslashes
7173

7274
if (lastAtIndex !== -1) {
7375
// If there's an '@' symbol, replace text after it up to the next space/end

webview-ui/src/utils/path-mentions.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,28 @@
1212
* @returns A mention-friendly path
1313
*/
1414
export function convertToMentionPath(path: string, cwd?: string): string {
15-
// First, detect and temporarily replace already escaped spaces
16-
// We need to be careful with Windows paths that use backslashes
17-
const detectedPath = path.replace(/\\/g, "/")
18-
// Temporarily replace escaped spaces
19-
let processedPath = detectedPath.replace(/\\ /g, "\\ESCAPED_SPACE")
20-
21-
let normalizedCwd = cwd ? cwd.replace(/\\/g, "/") : ""
15+
// 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, "/") : "";
2218

2319
if (!normalizedCwd) {
24-
// If no CWD, just escape spaces in the original path and restore escaped spaces
25-
return processedPath.replace(/ /g, "\\ ").replace(/\\ESCAPED_SPACE/g, "\\\\ ")
20+
// If no CWD, just escape spaces in the original path
21+
return processedPath.replace(/ /g, "\\ ");
2622
}
2723

2824
// Remove trailing slash from cwd if it exists
2925
if (normalizedCwd.endsWith("/")) {
30-
normalizedCwd = normalizedCwd.slice(0, -1)
26+
normalizedCwd = normalizedCwd.slice(0, -1);
3127
}
3228

3329
// Always use case-insensitive comparison for path matching
34-
const lowerPath = processedPath.toLowerCase()
35-
const lowerCwd = normalizedCwd.toLowerCase()
30+
const lowerPath = processedPath.toLowerCase();
31+
const lowerCwd = normalizedCwd.toLowerCase();
3632

3733
if (lowerPath.startsWith(lowerCwd)) {
38-
const relativePath = processedPath.substring(normalizedCwd.length)
34+
const relativePath = processedPath.substring(normalizedCwd.length);
3935
// Ensure there's a slash after the @ symbol when we create the mention path
40-
let mentionPath = "@" + (relativePath.startsWith("/") ? relativePath : "/" + relativePath)
36+
let mentionPath = "@" + (relativePath.startsWith("/") ? relativePath : "/" + relativePath);
4137

4238
/**
4339
* Space escaping logic for file paths
@@ -57,10 +53,10 @@ export function convertToMentionPath(path: string, cwd?: string): string {
5753
* will undergo a second round of escaping, resulting in double backslashes.
5854
* This is necessary to preserve the escapes through the entire text processing pipeline.
5955
*/
60-
// Escape regular spaces and restore already escaped spaces
61-
return mentionPath.replace(/ /g, "\\ ").replace(/\\ESCAPED_SPACE/g, "\\\\ ")
56+
// Escape spaces
57+
return mentionPath.replace(/ /g, "\\ ");
6258
}
6359

6460
// If path doesn't start with CWD, escape spaces in the processed path
65-
return processedPath.replace(/ /g, "\\ ").replace(/\\ESCAPED_SPACE/g, "\\\\ ")
61+
return processedPath.replace(/ /g, "\\ ");
6662
}

0 commit comments

Comments
 (0)