Skip to content

Commit 1173de5

Browse files
fix(web): copy Windows file links with native separators
Copy Markdown file-link paths with native separators for Windows drive and UNC targets, including relative path copies. Use the target path to choose separators so remote POSIX workspaces keep their original clipboard paths. Fixes pingdotgg#19142 Verified with 156 focused tests, the web typecheck, formatting, and scoped lint. Lint reports only pre-existing warnings outside the changed code.
1 parent a01c48f commit 1173de5

2 files changed

Lines changed: 109 additions & 7 deletions

File tree

‎apps/web/src/components/ChatMarkdown.test.tsx‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { getSyntaxHighlighterPromise } from "../lib/syntaxHighlighting";
1616
import { GitHubIcon } from "./Icons";
1717
import { Button } from "./ui/button";
1818
import { setMarkdownTaskChecked } from "./files/filePreviewMode";
19+
import { readLocalApi } from "../localApi";
1920

2021
vi.mock("../hooks/useFileMetadata", () => ({ useFileMetadata: () => null }));
2122
vi.mock("@effect/atom-react", () => ({ useAtomValue: () => null }));
@@ -1046,6 +1047,105 @@ describe("ChatMarkdown Windows file links", () => {
10461047
});
10471048
});
10481049

1050+
describe.each(["Win32", "MacIntel"])(
1051+
"ChatMarkdown file path clipboard actions on %s",
1052+
(platform) => {
1053+
it.each([
1054+
{
1055+
name: "full Windows drive path with spaces",
1056+
cwd: "C:/Users/me/project",
1057+
href: "C:/Users/me/project/reports/Device Users.csv",
1058+
action: "copy-full",
1059+
expected: String.raw`C:\Users\me\project\reports\Device Users.csv`,
1060+
},
1061+
{
1062+
name: "relative Windows drive path",
1063+
cwd: "C:/Users/me/project",
1064+
href: "C:/Users/me/project/reports/Device Users.csv",
1065+
action: "copy-relative",
1066+
expected: String.raw`project\reports\Device Users.csv`,
1067+
},
1068+
{
1069+
name: "relative link in a Windows workspace",
1070+
cwd: String.raw`C:\Users\me\project`,
1071+
href: "reports/Device Users.csv",
1072+
action: "copy-full",
1073+
expected: String.raw`C:\Users\me\project\reports\Device Users.csv`,
1074+
},
1075+
{
1076+
name: "full UNC path",
1077+
cwd: String.raw`\\server\share\project`,
1078+
href: "file://server/share/project/reports/Device%20Users.csv",
1079+
action: "copy-full",
1080+
expected: String.raw`\\server\share\project\reports\Device Users.csv`,
1081+
},
1082+
{
1083+
name: "relative UNC path",
1084+
cwd: String.raw`\\server\share\project`,
1085+
href: "file://server/share/project/reports/Device%20Users.csv",
1086+
action: "copy-relative",
1087+
expected: String.raw`project\reports\Device Users.csv`,
1088+
},
1089+
{
1090+
name: "full POSIX path",
1091+
cwd: "/home/me/project",
1092+
href: "/home/me/project/reports/Device Users.csv",
1093+
action: "copy-full",
1094+
expected: "/home/me/project/reports/Device Users.csv",
1095+
},
1096+
{
1097+
name: "relative POSIX path",
1098+
cwd: "/home/me/project",
1099+
href: "/home/me/project/reports/Device Users.csv",
1100+
action: "copy-relative",
1101+
expected: "project/reports/Device Users.csv",
1102+
},
1103+
{
1104+
name: "Windows path with line and column",
1105+
cwd: "C:/Users/me/project",
1106+
href: "C:/Users/me/project/src/main.ts:12:3",
1107+
action: "copy-full",
1108+
expected: String.raw`C:\Users\me\project\src\main.ts:12:3`,
1109+
},
1110+
])("copies $name", async ({ cwd, href, action, expected }) => {
1111+
const writeText = vi.fn(async (_text: string) => {});
1112+
const container = document.createElement("div");
1113+
document.body.append(container);
1114+
const root = createRoot(container);
1115+
1116+
try {
1117+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
1118+
vi.stubGlobal("navigator", { platform, clipboard: { writeText } });
1119+
const api = readLocalApi();
1120+
if (!api) throw new Error("Local API unavailable");
1121+
vi.spyOn(api.contextMenu, "show").mockResolvedValue(action);
1122+
await act(async () => {
1123+
root.render(
1124+
<ChatMarkdown
1125+
cwd={cwd}
1126+
environmentId={EnvironmentId.make("env-file-copy")}
1127+
text={`[File](<${href}>)`}
1128+
/>,
1129+
);
1130+
});
1131+
const link = container.querySelector(".chat-markdown-file-link");
1132+
if (!link) throw new Error("File link unavailable");
1133+
await act(async () => {
1134+
link.dispatchEvent(
1135+
new MouseEvent("contextmenu", { bubbles: true, clientX: 10, clientY: 10 }),
1136+
);
1137+
});
1138+
expect(writeText).toHaveBeenCalledWith(expected);
1139+
} finally {
1140+
await act(async () => root.unmount());
1141+
container.remove();
1142+
vi.unstubAllGlobals();
1143+
vi.restoreAllMocks();
1144+
}
1145+
});
1146+
},
1147+
);
1148+
10491149
it("opens a disclosure only when find selects a match inside it", async () => {
10501150
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
10511151
vi.stubGlobal(

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,8 +2077,8 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
20772077
})();
20782078
}, [onReveal, targetPath]);
20792079

2080-
const handleCopy = useCallback(
2081-
(value: string, title: string) => {
2080+
const handleCopyFilePath = useCallback(
2081+
(path: string, title: string) => {
20822082
if (typeof window === "undefined" || !navigator.clipboard?.writeText) {
20832083
toastManager.add(
20842084
stackedThreadToast({
@@ -2090,12 +2090,14 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
20902090
return;
20912091
}
20922092

2093-
void navigator.clipboard.writeText(value).then(
2093+
// A remote file's host OS can differ from the browser's OS.
2094+
const clipboardPath = isWindowsAbsolutePath(targetPath) ? path.replaceAll("/", "\\") : path;
2095+
void navigator.clipboard.writeText(clipboardPath).then(
20942096
() => {
20952097
toastManager.add({
20962098
type: "success",
20972099
title: `${title} copied`,
2098-
description: value,
2100+
description: clipboardPath,
20992101
});
21002102
},
21012103
(error) => {
@@ -2153,11 +2155,11 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
21532155
return;
21542156
}
21552157
if (clicked === "copy-relative") {
2156-
handleCopy(displayPath, "Relative path");
2158+
handleCopyFilePath(displayPath, "Relative path");
21572159
return;
21582160
}
21592161
if (clicked === "copy-full") {
2160-
handleCopy(targetPath, "Full path");
2162+
handleCopyFilePath(targetPath, "Full path");
21612163
}
21622164
} catch (cause) {
21632165
reportMarkdownActionFailure(
@@ -2168,7 +2170,7 @@ const MarkdownFileLink = memo(function MarkdownFileLink({
21682170
},
21692171
[
21702172
displayPath,
2171-
handleCopy,
2173+
handleCopyFilePath,
21722174
handleOpenInBrowser,
21732175
handleOpenInEditor,
21742176
handleRevealInFileManager,

0 commit comments

Comments
 (0)