Skip to content

Commit d28eb04

Browse files
committed
fix(mobile): inline file links keep backslashes and ampersands in their path
1 parent e00b2ad commit d28eb04

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

‎apps/mobile/src/lib/markdownDocument.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export function markdownWithInlineFileLinks(markdown: string, fileLink: RegExp):
150150
if (node.type === "inlineCode") {
151151
const start = node.position?.start.offset;
152152
const end = node.position?.end.offset;
153+
const escaped = node.value.replace(/[\\[\]&]/g, "\\$&");
153154
if (
154155
start !== undefined &&
155156
end !== undefined &&
@@ -159,7 +160,7 @@ export function markdownWithInlineFileLinks(markdown: string, fileLink: RegExp):
159160
edits.push({
160161
start,
161162
end,
162-
content: `[${node.value.replace(/[[\]\\]/g, "\\$&")}](<${node.value}>)`,
163+
content: `[${escaped}](<${escaped}>)`,
163164
});
164165
} else if (node.type !== "link" && node.type !== "linkReference" && "children" in node)
165166
node.children.forEach(visit);

‎apps/mobile/src/lib/markdownInlineFileLinks.test.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from "vite-plus/test";
22
import { ENRICHED_INLINE_FILE_LINK_REGEX } from "./enrichedLinkPresentation";
3-
import { markdownWithInlineFileLinks } from "./markdownDocument";
3+
import { markdownWithInlineFileLinks, parseMarkdownDocument } from "./markdownDocument";
44

55
const rewrite = (markdown: string) =>
66
markdownWithInlineFileLinks(markdown, ENRICHED_INLINE_FILE_LINK_REGEX);
@@ -25,4 +25,15 @@ describe("inline code that names a file", () => {
2525
it("handles double-backtick spans", () => {
2626
expect(rewrite("Open ``src/math.ts`` now.")).toBe("Open [src/math.ts](<src/math.ts>) now.");
2727
});
28+
29+
it("links to the exact path when it holds backslashes or ampersands", () => {
30+
for (const path of ["C:\\repo\\_private\\file.ts", "./src/a&copy;.ts", "./src/[id].ts"]) {
31+
const paragraph = parseMarkdownDocument(rewrite(`\`${path}\``)).children[0];
32+
const link = paragraph?.type === "paragraph" ? paragraph.children[0] : undefined;
33+
expect(link?.type === "link" && link.url).toBe(path);
34+
expect(
35+
link?.type === "link" && link.children[0]?.type === "text" && link.children[0].value,
36+
).toBe(path);
37+
}
38+
});
2839
});

0 commit comments

Comments
 (0)