fix: properly encode file URLs with special characters (#12424)

This commit is contained in:
Khang Ha (Kelvin)
2026-02-07 05:16:56 +07:00
committed by GitHub
parent e9a3cfc083
commit fde0b39b7c
12 changed files with 114 additions and 22 deletions

View File

@@ -72,12 +72,27 @@ export function unquoteGitPath(input: string) {
return new TextDecoder().decode(new Uint8Array(bytes))
}
export function decodeFilePath(input: string) {
try {
return decodeURIComponent(input)
} catch {
return input
}
}
export function encodeFilePath(filepath: string): string {
return filepath
.split("/")
.map((segment) => encodeURIComponent(segment))
.join("/")
}
export function createPathHelpers(scope: () => string) {
const normalize = (input: string) => {
const root = scope()
const prefix = root.endsWith("/") ? root : root + "/"
let path = unquoteGitPath(stripQueryAndHash(stripFileProtocol(input)))
let path = unquoteGitPath(decodeFilePath(stripQueryAndHash(stripFileProtocol(input))))
if (path.startsWith(prefix)) {
path = path.slice(prefix.length)
@@ -100,7 +115,7 @@ export function createPathHelpers(scope: () => string) {
const tab = (input: string) => {
const path = normalize(input)
return `file://${path}`
return `file://${encodeFilePath(path)}`
}
const pathFromTab = (tabValue: string) => {