1212 * @returns A mention-friendly path
1313 */
1414export 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 ( / \\ E S C A P E D _ S P A C E / 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 ( / \\ E S C A P E D _ S P A C E / 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 ( / \\ E S C A P E D _ S P A C E / g , "\\\\ " )
61+ return processedPath . replace ( / / g, "\\ " ) ;
6662}
0 commit comments