Skip to content

fix(telegram): prevent truncation of valid URLs with entity-marker characters in MarkdownV2 links#610

Open
merrcury wants to merge 1 commit into
vercel:mainfrom
merrcury:fix/telegram-trimToMarkdownV2SafeBoundary
Open

fix(telegram): prevent truncation of valid URLs with entity-marker characters in MarkdownV2 links#610
merrcury wants to merge 1 commit into
vercel:mainfrom
merrcury:fix/telegram-trimToMarkdownV2SafeBoundary

Conversation

@merrcury

Copy link
Copy Markdown

Summary

Fixes @chat-adapter/telegram silently truncating valid MarkdownV2 messages whose link URLs contain an odd number of entity-marker characters (_, *, ~).

trimToMarkdownV2SafeBoundary counted unescaped markers anywhere outside code spans and treated an odd total as an unterminated entity. But per the MarkdownV2 spec, only ) and \ are special inside the (...) URL part of an inline link — so a message ending with e.g. [Read more](https://example.com/page?utm_campaign=a&utm_source=b&utm_channel=c) (3 raw underscores in the URL) was sliced mid-URL, rejected by Telegram with can't parse entities: Can't find end of a URL, and degraded to plain text by the markdown fallback — links and formatting silently stripped.

Changes in findUnescapedPositionsOutsideCode:

  • Tracks a link-URL state alongside the existing inFence/inInline tracking: it opens when an unescaped ]( is consumed outside code and closes at the first unescaped ). Markers inside that span are never recorded.
  • A link's ] only counts toward bracket pairing once its URL closes, so hard truncation (4096-char limit) that slices mid-URL now leaves the [ unmatched and trims back to before the link — previously the cut left an unterminated ( that Telegram rejected.

The defensive under-limit safety pass from #446 (streaming chunks) is intentionally kept; it's now link-aware.

Test plan

  • Added unit tests in packages/adapter-telegram/src/markdown.test.ts:
    • URLs with 1 and 3 (odd) underscores pass through unchanged
    • URL with raw underscores alongside surrounding *bold* entities — unchanged
    • URL with * and ~ in the query string — unchanged
    • Full-pipeline regression: renderPostable + truncateForTelegram on a message ending with a multi-query-param link
    • Hard truncation inside a link URL trims back to before the [
    • Hard truncation inside *bold* still trims at the orphan * (existing behavior)
    • Unpaired _ outside a link URL is still stripped (under-limit pass from fix(telegram): apply MarkdownV2 entity safety trim to streaming chunks #446 preserved)
  • All 216 @chat-adapter/telegram tests pass
  • pnpm validate passes (knip, lint, typecheck, test, build)

Checklist

  • All commits are signed and verified
  • pnpm validate passes
  • Changeset added (or N/A — see CONTRIBUTING.md) — .changeset/loud-falcons-wave.md, patch for @chat-adapter/telegram
  • Documentation updated (or N/A) — N/A, internal truncation behavior, no user-facing API or docs change

…aracters in MarkdownV2 links

This update ensures that URLs containing `_`, `*`, and `~` are preserved during truncation, aligning with the MarkdownV2 specification. It also includes tests to verify the correct handling of such URLs and the behavior of truncation near link boundaries.
@merrcury merrcury requested a review from a team as a code owner June 11, 2026 17:57
@vercel

vercel Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

@merrcury is attempting to deploy a commit to the Vercel Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant