diff --git a/src/share-formatters.test.ts b/src/share-formatters.test.ts index 4a114d1c..fd657014 100644 --- a/src/share-formatters.test.ts +++ b/src/share-formatters.test.ts @@ -240,6 +240,14 @@ describe("preset: telegram (MarkdownV2)", () => { expect(out).not.toContain("`node 1\\.0\\-x`"); }); + it("escapes a marker that never closes instead of looping forever", () => { + expect(transform("a*b", "telegram")).toBe("a\\*b"); + expect(transform("* one\n* two", "telegram")).toBe("\\* one\n\\* two"); + expect(transform("arr[0] and `x", "telegram")).toBe("arr\\[0\\] and \\`x"); + expect(transform("**abc ~~d", "telegram")).toBe("\\*\\*abc \\~\\~d"); + expect(transform("**abc *def*", "telegram")).toBe("\\*\\*abc _def_"); + }); + it("converts tables to ``` pre blocks (no escapes inside)", () => { const out = transform(SAMPLE_MD, "telegram"); expect(out).toMatch(/```\n[\s\S]+?\n```/); diff --git a/src/share-formatters.ts b/src/share-formatters.ts index 50521643..3940f5bd 100644 --- a/src/share-formatters.ts +++ b/src/share-formatters.ts @@ -342,7 +342,11 @@ function applyTelegramText(text: string): string { continue; } } - let j = i; + // Always take the marker at i as text: it failed to close above, and a + // scan that starts on it would stop at once and never advance i. An + // unclosed `**` or `~~` is taken whole, so its second character cannot + // pair with a later marker. + let j = peek("**") || peek("~~") ? i + 2 : i + 1; while ( j < len && !text.startsWith("**", j) &&