Skip to content

Commit 01039b8

Browse files
committed
fix(mobile): count backslash runs when escaping link labels
1 parent 53e28c7 commit 01039b8

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,20 @@ type AnchorState = { readonly pending: string[]; open: number };
6767
// Code spans keep their text; an odd trailing backslash would escape the closing "]".
6868
function escapeLinkLabel(text: string): string {
6969
return text
70-
.replace(/(`+)[\s\S]*?\1|\\?[[\]]/g, (match) =>
71-
match.startsWith("`") || match.length === 2 ? match : `\\${match}`,
70+
.replace(/(`+)[\s\S]*?\1|\\*[[\]]/g, (match) =>
71+
match.startsWith("`") || match.length % 2 === 0
72+
? match
73+
: `${match.slice(0, -1)}\\${match.slice(-1)}`,
7274
)
7375
.replace(/\\+$/, (slashes) => (slashes.length % 2 ? `${slashes}\\` : slashes));
7476
}
7577

7678
// An unescaped "!" before the inserted "[" would turn the link into an image.
7779
function bangBefore(text: string, index: number): boolean {
78-
return text[index - 1] === "!" && text[index - 2] !== "\\";
80+
if (text[index - 1] !== "!") return false;
81+
let backslashes = 0;
82+
while (text[index - 2 - backslashes] === "\\") backslashes += 1;
83+
return backslashes % 2 === 0;
7984
}
8085

8186
/**

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,19 @@ describe("HTML in native Markdown", () => {
112112
});
113113

114114
it("keeps an anchor a link after a bang or with brackets in its text", () => {
115-
for (const markdown of [
116-
"Done!<a href='https://example.com'>logs [1] ]</a>",
117-
"<p>Done!<a href='https://example.com'>logs [1] ]</a></p>",
118-
]) {
115+
for (const [markdown, text, label] of [
116+
["Done!<a href='https://example.com'>logs [1] ]</a>", "Done!", "logs [1] ]"],
117+
["<p>Done!<a href='https://example.com'>logs [1] ]</a></p>", "Done!", "logs [1] ]"],
118+
["Done\\\\!<a href='https://example.com'>logs \\\\]</a>", "Done\\!", "logs \\]"],
119+
["<p>Done\\\\!<a href='https://example.com'>logs \\\\]</a></p>", "Done\\!", "logs \\]"],
120+
] as const) {
119121
const document = parseMarkdownDocument(markdown);
120122
const paragraph = document.children[0];
121123
expect(paragraph?.type).toBe("paragraph");
122124
if (paragraph?.type !== "paragraph") continue;
123125
expect(paragraph.children).toMatchObject([
124-
{ type: "text", value: "Done!" },
125-
{ type: "link", url: "https://example.com", children: [{ value: "logs [1] ]" }] },
126+
{ type: "text", value: text },
127+
{ type: "link", url: "https://example.com", children: [{ value: label }] },
126128
]);
127129
}
128130
const document = parseMarkdownDocument(

0 commit comments

Comments
 (0)