← Back to PRs

#4249: fix(telegram): properly nest link tags inside bold/italic formatting

by pradeeppeddineni open 2026-01-29 23:36 View on GitHub →
channel: telegram
## Summary Fixes a bug where bold/italic formatting breaks when containing auto-linked URLs in Telegram messages. ### Problem When a URL appears inside bold/italic text (e.g., `**Check https://example.com**`), the HTML tags were being closed in the wrong order: ```html <!-- Before (broken) --> <b>Check <a href="https://example.com">https://example.com</b></a> <!-- After (fixed) --> <b>Check <a href="https://example.com">https://example.com</a></b> ``` The misnested tags caused Telegram to fail to render the bold formatting when URLs were present inside styled spans. ### Root Cause In `src/markdown/render.ts`, the `renderMarkdownWithMarkers` function was closing style tags (bold/italic) before link tags at the same position. Since links are nested inside styles, the inner element (link) should close first. ### Solution Reordered the closing logic to close links before styles, maintaining proper HTML nesting order. ### Changes - `src/markdown/render.ts`: Swapped order of closing links and styles - `src/telegram/format.test.ts`: Added 3 test cases for the fix ## Test Plan - [x] All existing tests pass (292 tests across markdown, telegram, discord, slack, whatsapp) - [x] New test cases added: - `properly nests link tags inside bold when URL is at end of bold span` - `properly nests link tags inside italic` - `renders emoji before bold correctly` ## Testing ```typescript // Before fix markdownToTelegramHtml("**Check https://example.com**") // => '<b>Check <a href="https://example.com">https://example.com</b></a>' (broken) // After fix markdownToTelegramHtml("**Check https://example.com**") // => '<b>Check <a href="https://example.com">https://example.com</a></b>' (correct) ``` Fixes #4174 <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR updates the shared markdown renderer (`renderMarkdownWithMarkers`) to close link tags before closing style markers at the same boundary, fixing mis-nested HTML when Telegram auto-linking occurs inside bold/italic spans. It also adds Telegram formatting regression tests covering bold/italic spans that include linkified URLs. Because `renderMarkdownWithMarkers` is used by other formatters (e.g., Slack mrkdwn and markdown table conversion), the behavioral change applies across those outputs too; if the “links are always nested inside styles” invariant isn’t guaranteed by the IR, it may need to be documented or encoded more explicitly. <h3>Confidence Score: 4/5</h3> - This PR is likely safe to merge and addresses the reported Telegram HTML nesting bug. - The change is small and directly targets tag-nesting order, with targeted Telegram regression tests added. Main residual risk is that `renderMarkdownWithMarkers` is a shared renderer, so the changed close ordering could subtly affect other outputs if they rely on different nesting assumptions at identical boundaries. - src/markdown/render.ts (shared renderer behavior); src/telegram/format.test.ts (test relevance/naming) <!-- greptile_other_comments_section --> <sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub> <!-- /greptile_comment -->

Most Similar PRs