#4249: fix(telegram): properly nest link tags inside bold/italic formatting
channel: telegram
Cluster:
Telegram Message Handling Fixes
## 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
#17629: fix(telegram): fall back to plain text when HTML formatter produces...
by Glucksberg · 2026-02-16
81.8%
#7395: fix(whatsapp): strip markdown bold/italic from URLs before sending
by lailoo · 2026-02-02
79.5%
#17769: fix(telegram): preserve reply text in threaded mode dispatch
by Glucksberg · 2026-02-16
79.5%
#20795: fix(markdown): prevent triple newlines after blockquotes
by novalis133 · 2026-02-19
77.8%
#2716: Fix #2678: markdown horizontal rules not rendering in web chat
by Ambar-13 · 2026-01-27
77.7%
#16733: fix(ui): avoid injected newlines when tool output is hidden
by jp117 · 2026-02-15
76.3%
#12936: fix(telegram): omit message_thread_id for private DM chats
by omair445 · 2026-02-09
76.1%
#20419: fix(webchat): explicitly pass gfm and breaks options to marked.parse()
by Limitless2023 · 2026-02-18
76.1%
#3368: fix: sessions navigation, DM thread display, and DM thread delivery...
by Lukavyi · 2026-01-28
75.4%
#19213: Telegram: preserve DM topic thread in direct replies
by Kemalau · 2026-02-17
75.3%