Skip to content
TypeScript

fix(clients): keep backslashes in copied Codex citations (#12243)

Simone2efb8178d83f
diff --git a/packages/client-runtime/src/codexFileCitations.ts b/packages/client-runtime/src/codexFileCitations.ts
index b5042f0806e..d29996501ea 100644
--- a/packages/client-runtime/src/codexFileCitations.ts
+++ b/packages/client-runtime/src/codexFileCitations.ts
@@ -44,6 +44,7 @@ function markdownLabel(value: string): string {
function markdownDestination(value: string): string {
return value
+ .replaceAll("\\", "%5C")
.replaceAll("<", "%3C")
.replaceAll(">", "%3E")
.replaceAll("\r", "%0D")
diff --git a/packages/client-runtime/src/codexMarkdownDirectives.test.ts b/packages/client-runtime/src/codexMarkdownDirectives.test.ts
index b2a22901eac..efd5262d073 100644
--- a/packages/client-runtime/src/codexMarkdownDirectives.test.ts
+++ b/packages/client-runtime/src/codexMarkdownDirectives.test.ts
@@ -8,6 +8,7 @@ import {
renderCodexFileCitationsAsMarkdown,
splitCodexArtifactTemplateMarkdown,
} from "./codexMarkdownDirectives.js";
+import { parseMarkdownFileLink } from "./markdownLinks.js";
interface TestNode {
readonly type: string;
@@ -89,6 +90,24 @@ describe("remarkCodexDirectives", () => {
});
});
+describe.each([
+ { name: "renderCodexDirectivesForCopy", render: renderCodexDirectivesForCopy },
+ { name: "renderCodexFileCitationsAsMarkdown", render: renderCodexFileCitationsAsMarkdown },
+])("$name file citation round trips", ({ render }) => {
+ it.each([
+ "C:\\Users\\test\\[draft]\\report.md",
+ "\\\\server\\share\\report.md",
+ "outputs/report.md",
+ "/tmp/report%5C.md",
+ ])("preserves the literal path and line: %s", (path) => {
+ const markdown = render(`:codex-file-citation{path="${path}" line_range_start="7"}`);
+ const link = parseOrdinaryMarkdown(markdown).children?.[0]?.children?.[0];
+
+ expect(link?.type).toBe("link");
+ expect(parseMarkdownFileLink(link?.url ?? "")).toEqual({ path, line: 7 });
+ });
+});
+
describe("native Markdown adapters", () => {
it("uses the same parser to render file citations as portable links", () => {
expect(renderCodexFileCitationsAsMarkdown(`Created ${FILE_CITATION}.`)).toBe(