From 7df3c05973765a744c51fecc74997c1ddb131611 Mon Sep 17 00:00:00 2001 From: goranai Date: Thu, 26 Nov 2026 10:17:10 +0800 Subject: [PATCH 1/2] fix(share): stop the Telegram formatter looping forever on an unclosed marker --- src/share-formatters.test.ts | 7 +++++++ src/share-formatters.ts | 5 ++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/share-formatters.test.ts b/src/share-formatters.test.ts index 4a114d1c..f989a57a 100644 --- a/src/share-formatters.test.ts +++ b/src/share-formatters.test.ts @@ -240,6 +240,13 @@ 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"); + }); + 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..521d0f00 100644 --- a/src/share-formatters.ts +++ b/src/share-formatters.ts @@ -342,7 +342,10 @@ function applyTelegramText(text: string): string { continue; } } - let j = i; + // Always take the character at i as text: it is a marker that failed to + // close above, and a scan that starts on it would stop at once and never + // advance i. + let j = i + 1; while ( j < len && !text.startsWith("**", j) && From 7951e1568963f8034da97517baa4cd0b4189d235 Mon Sep 17 00:00:00 2001 From: goranai Date: Thu, 26 Nov 2026 10:58:03 +0800 Subject: [PATCH 2/2] fix(share): take an unclosed ** or ~~ as text whole so it cannot pair with a later marker --- src/share-formatters.test.ts | 1 + src/share-formatters.ts | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/share-formatters.test.ts b/src/share-formatters.test.ts index f989a57a..fd657014 100644 --- a/src/share-formatters.test.ts +++ b/src/share-formatters.test.ts @@ -245,6 +245,7 @@ describe("preset: telegram (MarkdownV2)", () => { 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)", () => { diff --git a/src/share-formatters.ts b/src/share-formatters.ts index 521d0f00..3940f5bd 100644 --- a/src/share-formatters.ts +++ b/src/share-formatters.ts @@ -342,10 +342,11 @@ function applyTelegramText(text: string): string { continue; } } - // Always take the character at i as text: it is a marker that failed to - // close above, and a scan that starts on it would stop at once and never - // advance i. - let j = i + 1; + // 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) &&