Skip to content

Commit b9228e1

Browse files
fix(client-runtime): keep POSIX file link containment case-sensitive
1 parent 7a885a5 commit b9228e1

3 files changed

Lines changed: 28 additions & 11 deletions

File tree

‎apps/web/src/filePathDisplay.ts‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,12 @@ import {
44
splitFilePathPosition,
55
stripSlashPrefixedWindowsDrive,
66
} from "@t3tools/client-runtime/markdown-links";
7+
import { isWindowsAbsolutePath } from "@t3tools/shared/path";
78

89
function normalizePathSeparators(path: string): string {
910
return path.replaceAll("\\", "/");
1011
}
1112

12-
function canonicalizeWindowsDrivePath(path: string): string {
13-
return /^\/[A-Za-z]:\//.test(path) ? path.slice(1) : path;
14-
}
15-
16-
export function isWindowsFilesystemPath(path: string): boolean {
17-
const normalizedPath = canonicalizeWindowsDrivePath(normalizePathSeparators(path));
18-
return /^[A-Za-z]:(?:\/|$)/.test(normalizedPath) || path.startsWith("\\\\");
19-
}
20-
2113
function trimTrailingPathSeparators(path: string): string {
2214
return path.replace(/[\\/]+$/, "");
2315
}
@@ -39,7 +31,7 @@ export function formatWorkspaceRelativePath(
3931
normalizePathSeparators(trimTrailingPathSeparators(workspaceRoot)),
4032
);
4133
const workspaceLabel = fileBasename(normalizedWorkspaceRoot);
42-
const caseInsensitive = isWindowsFilesystemPath(workspaceRoot);
34+
const caseInsensitive = isWindowsAbsolutePath(stripSlashPrefixedWindowsDrive(workspaceRoot));
4335
const pathForCompare = caseInsensitive ? normalizedPath.toLowerCase() : normalizedPath;
4436
const workspaceForCompare = caseInsensitive
4537
? normalizedWorkspaceRoot.toLowerCase()

‎apps/web/src/markdown-links.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,21 @@ describe("resolveMarkdownFileLinkTarget", () => {
294294
});
295295
});
296296

297+
it.each([
298+
["/tmp/repo/file.ts", "/", "tmp/repo/file.ts"],
299+
["C:/Users/MIKE/file.ts", "c:/", "Users/MIKE/file.ts"],
300+
["\\\\server\\SHARE\\file.ts", "\\\\Server\\Share\\", "file.ts"],
301+
["/tmp/repo/file.ts%20", "/tmp/repo", "file.ts "],
302+
])("preserves the preview target for %s in workspace %s", (href, cwd, workspaceRelativePath) => {
303+
expect(resolveMarkdownFileLinkMeta(href, cwd)).toMatchObject({ workspaceRelativePath });
304+
});
305+
306+
it("keeps an encoded final space in the absolute target", () => {
307+
expect(resolveMarkdownFileLinkTarget("/tmp/repo/file.ts%20", "/tmp/repo")).toBe(
308+
"/tmp/repo/file.ts ",
309+
);
310+
});
311+
297312
it("normalizes slash-prefixed windows drive paths before resolving", () => {
298313
expect(
299314
resolveMarkdownFileLinkTarget(

‎packages/client-runtime/src/markdownLinks.test.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,17 @@ describe("workspaceRelativeFilePath", () => {
160160
["/repo/project/src/main.ts", "/repo/project/", "src/main.ts"],
161161
["C:\\Users\\mike\\t3code\\apps\\web\\a.ts", "C:/Users/mike/t3code", "apps/web/a.ts"],
162162
["/C:/Users/mike/t3code/apps/web/a.ts", "C:/Users/mike/t3code", "apps/web/a.ts"],
163-
["/Repo/Project/src/main.ts", "/repo/project", "src/main.ts"],
163+
["/Repo/Project/src/main.ts", "/repo/project", null],
164+
["/tmp/case/project/probe.txt", "/tmp/case/Project", null],
165+
["//tmp/case/project/probe.txt", "//tmp/case/Project", null],
166+
["/tmp/case/Project/probe.txt", "/tmp/case/Project", "probe.txt"],
167+
["C:/USERS/mike/t3code/main.ts", "c:/users/MIKE/t3code", "main.ts"],
168+
["/C:/USERS/mike/t3code/main.ts", "/c:/users/MIKE/t3code", "main.ts"],
169+
["\\\\server\\share\\PROJECT\\main.ts", "\\\\Server\\Share\\Project", "main.ts"],
170+
["/tmp/repo/file.ts", "/", "tmp/repo/file.ts"],
171+
["C:/Users/MIKE/main.ts", "c:/", "Users/MIKE/main.ts"],
172+
["\\\\server\\SHARE\\file.ts", "\\\\Server\\Share\\", "file.ts"],
173+
["/tmp/repo/file.ts ", "/tmp/repo", "file.ts "],
164174
["/tmp/report.ts", "/repo/project", null],
165175
["/repo/project-two/a.ts", "/repo/project", null],
166176
["/repo/project/a.ts", undefined, null],

0 commit comments

Comments
 (0)