diff --git a/src/channels/telegram-markdown-sanitize.test.ts b/src/channels/telegram-markdown-sanitize.test.ts index 89fd8e393..076f4970b 100644 --- a/src/channels/telegram-markdown-sanitize.test.ts +++ b/src/channels/telegram-markdown-sanitize.test.ts @@ -65,4 +65,16 @@ describe('sanitizeTelegramLegacyMarkdown', () => { it('preserves indented list structure', () => { expect(sanitizeTelegramLegacyMarkdown(' - nested')).toBe(' • nested'); }); + + it('flattens Markdown horizontal rules (---, ***, ___)', () => { + const input = 'before\n---\n***\n___\nafter'; + expect(sanitizeTelegramLegacyMarkdown(input)).toBe( + 'before\n⎯⎯⎯\n⎯⎯⎯\n⎯⎯⎯\nafter', + ); + }); + + it('leaves horizontal rules inside code blocks alone', () => { + const input = '```\n---\n```'; + expect(sanitizeTelegramLegacyMarkdown(input)).toBe(input); + }); }); diff --git a/src/channels/telegram-markdown-sanitize.ts b/src/channels/telegram-markdown-sanitize.ts index be9295467..c848c1132 100644 --- a/src/channels/telegram-markdown-sanitize.ts +++ b/src/channels/telegram-markdown-sanitize.ts @@ -28,6 +28,11 @@ export function sanitizeTelegramLegacyMarkdown(input: string): string { // as prose. text = text.replace(/^(\s*)[-+]\s+/gm, '$1• '); + // Flatten Markdown horizontal rules (bare --- / *** / ___ lines) to a + // plain Unicode divider. The parser doesn't understand HR syntax and the + // `*` / `_` characters would otherwise unbalance the delimiter counts below. + text = text.replace(/^[ \t]*[-_*]{3,}[ \t]*$/gm, '⎯⎯⎯'); + text = text.replace(/\*\*([^*\n]+?)\*\*/g, '*$1*'); text = text.replace(/__([^_\n]+?)__/g, '_$1_');