fix(telegram): prevent truncation of valid URLs with entity-marker characters in MarkdownV2 links#610
Open
merrcury wants to merge 1 commit into
Open
Conversation
…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.
Contributor
|
@merrcury is attempting to deploy a commit to the Vercel Team on Vercel. A member of the Team first needs to authorize it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes
@chat-adapter/telegramsilently truncating valid MarkdownV2 messages whose link URLs contain an odd number of entity-marker characters (_,*,~).trimToMarkdownV2SafeBoundarycounted 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 withcan'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:inFence/inInlinetracking: it opens when an unescaped](is consumed outside code and closes at the first unescaped). Markers inside that span are never recorded.]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
packages/adapter-telegram/src/markdown.test.ts:*bold*entities — unchanged*and~in the query string — unchangedrenderPostable+truncateForTelegramon a message ending with a multi-query-param link[*bold*still trims at the orphan*(existing behavior)_outside a link URL is still stripped (under-limit pass from fix(telegram): apply MarkdownV2 entity safety trim to streaming chunks #446 preserved)@chat-adapter/telegramtests passpnpm validatepasses (knip, lint, typecheck, test, build)Checklist
pnpm validatepasses.changeset/loud-falcons-wave.md, patch for@chat-adapter/telegram