path.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. export function stripFileProtocol(input: string) {
  2. if (!input.startsWith("file://")) return input
  3. return input.slice("file://".length)
  4. }
  5. export function stripQueryAndHash(input: string) {
  6. const hashIndex = input.indexOf("#")
  7. const queryIndex = input.indexOf("?")
  8. if (hashIndex !== -1 && queryIndex !== -1) {
  9. return input.slice(0, Math.min(hashIndex, queryIndex))
  10. }
  11. if (hashIndex !== -1) return input.slice(0, hashIndex)
  12. if (queryIndex !== -1) return input.slice(0, queryIndex)
  13. return input
  14. }
  15. export function unquoteGitPath(input: string) {
  16. if (!input.startsWith('"')) return input
  17. if (!input.endsWith('"')) return input
  18. const body = input.slice(1, -1)
  19. const bytes: number[] = []
  20. for (let i = 0; i < body.length; i++) {
  21. const char = body[i]!
  22. if (char !== "\\") {
  23. bytes.push(char.charCodeAt(0))
  24. continue
  25. }
  26. const next = body[i + 1]
  27. if (!next) {
  28. bytes.push("\\".charCodeAt(0))
  29. continue
  30. }
  31. if (next >= "0" && next <= "7") {
  32. const chunk = body.slice(i + 1, i + 4)
  33. const match = chunk.match(/^[0-7]{1,3}/)
  34. if (!match) {
  35. bytes.push(next.charCodeAt(0))
  36. i++
  37. continue
  38. }
  39. bytes.push(parseInt(match[0], 8))
  40. i += match[0].length
  41. continue
  42. }
  43. const escaped =
  44. next === "n"
  45. ? "\n"
  46. : next === "r"
  47. ? "\r"
  48. : next === "t"
  49. ? "\t"
  50. : next === "b"
  51. ? "\b"
  52. : next === "f"
  53. ? "\f"
  54. : next === "v"
  55. ? "\v"
  56. : next === "\\" || next === '"'
  57. ? next
  58. : undefined
  59. bytes.push((escaped ?? next).charCodeAt(0))
  60. i++
  61. }
  62. return new TextDecoder().decode(new Uint8Array(bytes))
  63. }
  64. export function decodeFilePath(input: string) {
  65. try {
  66. return decodeURIComponent(input)
  67. } catch {
  68. return input
  69. }
  70. }
  71. export function encodeFilePath(filepath: string): string {
  72. // Normalize Windows paths: convert backslashes to forward slashes
  73. let normalized = filepath.replace(/\\/g, "/")
  74. // Handle Windows absolute paths (D:/path -> /D:/path for proper file:// URLs)
  75. if (/^[A-Za-z]:/.test(normalized)) {
  76. normalized = "/" + normalized
  77. }
  78. // Encode each path segment (preserving forward slashes as path separators)
  79. return normalized
  80. .split("/")
  81. .map((segment) => encodeURIComponent(segment))
  82. .join("/")
  83. }
  84. export function createPathHelpers(scope: () => string) {
  85. const normalize = (input: string) => {
  86. const root = scope()
  87. const prefix = root.endsWith("/") ? root : root + "/"
  88. let path = unquoteGitPath(decodeFilePath(stripQueryAndHash(stripFileProtocol(input))))
  89. if (path.startsWith(prefix)) {
  90. path = path.slice(prefix.length)
  91. }
  92. if (path.startsWith(root)) {
  93. path = path.slice(root.length)
  94. }
  95. if (path.startsWith("./")) {
  96. path = path.slice(2)
  97. }
  98. if (path.startsWith("/")) {
  99. path = path.slice(1)
  100. }
  101. return path
  102. }
  103. const tab = (input: string) => {
  104. const path = normalize(input)
  105. return `file://${encodeFilePath(path)}`
  106. }
  107. const pathFromTab = (tabValue: string) => {
  108. if (!tabValue.startsWith("file://")) return
  109. return normalize(tabValue)
  110. }
  111. const normalizeDir = (input: string) => normalize(input).replace(/\/+$/, "")
  112. return {
  113. normalize,
  114. tab,
  115. pathFromTab,
  116. normalizeDir,
  117. }
  118. }