Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/share-formatters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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```/);
Expand Down
6 changes: 5 additions & 1 deletion src/share-formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) &&
Expand Down
Loading