TypeScript
fix(web): respect case in POSIX file links (#9309)
Michel Liao8faf031c2dac
diff --git a/apps/web/src/filePathDisplay.test.ts b/apps/web/src/filePathDisplay.test.tsindex ecceea09ca1..4c49133a313 100644--- a/apps/web/src/filePathDisplay.test.ts+++ b/apps/web/src/filePathDisplay.test.ts@@ -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",+ );+ });});diff --git a/apps/web/src/filePathDisplay.ts b/apps/web/src/filePathDisplay.tsindex fc197a4092b..36f5873cf8e 100644--- a/apps/web/src/filePathDisplay.ts+++ b/apps/web/src/filePathDisplay.ts@@ -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("\\", "/");@@ -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;diff --git a/apps/web/src/markdown-links.test.ts b/apps/web/src/markdown-links.test.tsindex e3b1cc5b224..5421c0d8781 100644--- a/apps/web/src/markdown-links.test.ts+++ b/apps/web/src/markdown-links.test.ts@@ -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(diff --git a/packages/client-runtime/src/markdownLinks.test.ts b/packages/client-runtime/src/markdownLinks.test.tsindex cf8f9ae2d16..42cd7a35e47 100644--- a/packages/client-runtime/src/markdownLinks.test.ts+++ b/packages/client-runtime/src/markdownLinks.test.ts@@ -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],diff --git a/packages/client-runtime/src/markdownLinks.ts b/packages/client-runtime/src/markdownLinks.tsindex 81cce1fd7ee..2e455655d00 100644--- a/packages/client-runtime/src/markdownLinks.ts+++ b/packages/client-runtime/src/markdownLinks.ts@@ -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);}