Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions apps/web/src/filePathDisplay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ describe("formatWorkspaceRelativePath", () => {
),
).toBe("t3code/apps/web/src/session-logic.ts:501:9");
});

it("keeps double-slash POSIX paths case-sensitive", () => {
expect(formatWorkspaceRelativePath("//tmp/project/probe.txt", "//tmp/Project")).toBe(
"//tmp/project/probe.txt",
);
});
});
10 changes: 7 additions & 3 deletions apps/web/src/filePathDisplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
splitFilePathPosition,
stripSlashPrefixedWindowsDrive,
} from "@t3tools/client-runtime/markdown-links";
import { isWindowsAbsolutePath } from "@t3tools/shared/path";

function normalizePathSeparators(path: string): string {
return path.replaceAll("\\", "/");
Expand All @@ -30,10 +31,13 @@ export function formatWorkspaceRelativePath(
normalizePathSeparators(trimTrailingPathSeparators(workspaceRoot)),
);
const workspaceLabel = fileBasename(normalizedWorkspaceRoot);
const pathForCompare = normalizedPath.toLowerCase();
const workspaceForCompare = normalizedWorkspaceRoot.toLowerCase();
const caseInsensitive = isWindowsAbsolutePath(stripSlashPrefixedWindowsDrive(workspaceRoot));
const pathForCompare = caseInsensitive ? normalizedPath.toLowerCase() : normalizedPath;
const workspaceForCompare = caseInsensitive
? normalizedWorkspaceRoot.toLowerCase()
: normalizedWorkspaceRoot;
const workspaceWithSeparator = `${workspaceForCompare}/`;
const workspaceLabelWithSeparator = `${workspaceLabel.toLowerCase()}/`;
const workspaceLabelWithSeparator = `${caseInsensitive ? workspaceLabel.toLowerCase() : workspaceLabel}/`;

if (pathForCompare === workspaceForCompare) {
displayPath = workspaceLabel;
Expand Down
55 changes: 55 additions & 0 deletions apps/web/src/markdown-links.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,61 @@ describe("resolveMarkdownFileLinkTarget", () => {
});
});

it("does not classify a case-distinct POSIX sibling as a workspace file", () => {
expect(
resolveMarkdownFileLinkMeta(
"/tmp/t3code-case-test/project/probe.txt",
"/tmp/t3code-case-test/Project",
),
).toMatchObject({
displayPath: "/tmp/t3code-case-test/project/probe.txt",
workspaceRelativePath: null,
});
});

it("keeps Windows workspace comparisons case-insensitive", () => {
expect(
resolveMarkdownFileLinkMeta("C:/Users/MIKE/Project/src/main.ts", "c:/users/mike/project"),
).toMatchObject({
displayPath: "project/src/main.ts",
workspaceRelativePath: "src/main.ts",
});
});

it("keeps drive-root workspace comparisons case-insensitive", () => {
expect(resolveMarkdownFileLinkMeta("C:/Users/MIKE/project.ts", "c:/")).toMatchObject({
displayPath: "c:/Users/MIKE/project.ts",
workspaceRelativePath: "Users/MIKE/project.ts",
});
});

it("keeps backslash UNC workspace comparisons case-insensitive", () => {
expect(
resolveMarkdownFileLinkMeta(
"\\\\server\\share\\PROJECT\\src\\main.ts",
"\\\\Server\\Share\\Project",
),
).toMatchObject({
displayPath: "Project/src/main.ts",
workspaceRelativePath: "src/main.ts",
});
});

it.each([
["/tmp/repo/file.ts", "/", "tmp/repo/file.ts"],
["C:/Users/MIKE/file.ts", "c:/", "Users/MIKE/file.ts"],
["\\\\server\\SHARE\\file.ts", "\\\\Server\\Share\\", "file.ts"],
["/tmp/repo/file.ts%20", "/tmp/repo", "file.ts "],
])("preserves the preview target for %s in workspace %s", (href, cwd, workspaceRelativePath) => {
expect(resolveMarkdownFileLinkMeta(href, cwd)).toMatchObject({ workspaceRelativePath });
});

it("keeps an encoded final space in the absolute target", () => {
expect(resolveMarkdownFileLinkTarget("/tmp/repo/file.ts%20", "/tmp/repo")).toBe(
"/tmp/repo/file.ts ",
);
});

it("normalizes slash-prefixed windows drive paths before resolving", () => {
expect(
resolveMarkdownFileLinkTarget(
Expand Down
12 changes: 11 additions & 1 deletion packages/client-runtime/src/markdownLinks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,17 @@ describe("workspaceRelativeFilePath", () => {
["/repo/project/src/main.ts", "/repo/project/", "src/main.ts"],
["C:\\Users\\mike\\t3code\\apps\\web\\a.ts", "C:/Users/mike/t3code", "apps/web/a.ts"],
["/C:/Users/mike/t3code/apps/web/a.ts", "C:/Users/mike/t3code", "apps/web/a.ts"],
["/Repo/Project/src/main.ts", "/repo/project", "src/main.ts"],
["/Repo/Project/src/main.ts", "/repo/project", null],
["/tmp/case/project/probe.txt", "/tmp/case/Project", null],
["//tmp/case/project/probe.txt", "//tmp/case/Project", null],
["/tmp/case/Project/probe.txt", "/tmp/case/Project", "probe.txt"],
["C:/USERS/mike/t3code/main.ts", "c:/users/MIKE/t3code", "main.ts"],
["/C:/USERS/mike/t3code/main.ts", "/c:/users/MIKE/t3code", "main.ts"],
["\\\\server\\share\\PROJECT\\main.ts", "\\\\Server\\Share\\Project", "main.ts"],
["/tmp/repo/file.ts", "/", "tmp/repo/file.ts"],
["C:/Users/MIKE/main.ts", "c:/", "Users/MIKE/main.ts"],
["\\\\server\\SHARE\\file.ts", "\\\\Server\\Share\\", "file.ts"],
["/tmp/repo/file.ts ", "/tmp/repo", "file.ts "],
["/tmp/report.ts", "/repo/project", null],
["/repo/project-two/a.ts", "/repo/project", null],
["/repo/project/a.ts", undefined, null],
Expand Down
5 changes: 4 additions & 1 deletion packages/client-runtime/src/markdownLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@ export function workspaceRelativeFilePath(
const normalizedRoot = stripSlashPrefixedWindowsDrive(
workspaceRoot.replaceAll("\\", "/"),
).replace(/\/+$/, "");
if (!normalizedPath.toLowerCase().startsWith(`${normalizedRoot.toLowerCase()}/`)) return null;
const caseInsensitive = isWindowsAbsolutePath(stripSlashPrefixedWindowsDrive(workspaceRoot));
const pathForCompare = caseInsensitive ? normalizedPath.toLowerCase() : normalizedPath;
const rootForCompare = caseInsensitive ? normalizedRoot.toLowerCase() : normalizedRoot;
if (!pathForCompare.startsWith(`${rootForCompare}/`)) return null;
return normalizedPath.slice(normalizedRoot.length + 1);
}
Loading