Repository navigation
Conversation
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the code-block escaping change.
No open issues 🎉
What changed in this PR
This PR fixes mcode() in the shared formatting helper to preserve literal backslashes in MarkdownV2 code blocks.
Changes:
- Escape only backslashes and backticks within code-block content.
- Preserve the language argument,
escape=Falsebehavior, and generalescape_markdown()helper. - Add seven regression/control cases covering paths, punctuation, backticks, trailing backslashes, multiline text, and disabled escaping.
| File | Description |
|---|---|
tests/test_formatting.py |
Adds targeted tests for code-block escaping and escape=False. |
telebot/formatting.py |
Replaces general Markdown escaping in mcode() with code-block-specific escaping. |
Review effort: Balanced
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.
Description
formatting.mcode()currently uses the general Markdown escaper, which treats some backslash-prefixed punctuation as already escaped. Inside a code block, those backslashes can be part of the literal code: for example,C:\code\_file.pyloses the backslash before_, and\*becomes*after MarkdownV2 unescaping.Escape code-block content according to the Telegram MarkdownV2 rules: preserve each literal backslash by escaping it, and escape backticks. Leave
escape=Falseand the language argument unchanged. The generalescape_markdown()helper is unchanged.Describe your tests
Add seven parameterized/control cases for a Windows path, backslashes before punctuation/backticks, ordinary backticks, a trailing backslash, multiline content, and
escape=False. Three regression cases fail on the original implementation.python -m pytest tests -q: 329 passed, 73 skipped. The original suite had the same 73 skips (322 passed).uv build: passed.telebot/formatting.pyare preserved.Python version: 3.13.15
OS: Windows
These are local formatting tests. No live Telegram requests were made; rendered behavior follows the documented MarkdownV2 escaping rules.
Checklist
escape=Falseoption.