Skip to content

Preserve literal backslashes in Markdown code blocks - #2652

Open
sinzbtw wants to merge 1 commit into
eternnoir:masterfrom
sinzbtw:sinzbtw/preserve-markdown-code-backslashes
Open

sinzbtw wants to merge 1 commit into
eternnoir:masterfrom
sinzbtw:sinzbtw/preserve-markdown-code-backslashes

Conversation

@sinzbtw

@sinzbtw sinzbtw commented Nov 2, 2026

Copy link
Copy Markdown

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.py loses 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=False and the language argument unchanged. The general escape_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).
  • Wheel and sdist build via uv build: passed.
  • Original line endings in telebot/formatting.py are 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

  • My changes preserve the formatting API and its escape=False option.
  • Added regression tests for the changed behavior.
  • The fix is in the shared formatting helper, so it applies equally to synchronous and asynchronous bot usage.
  • No example update is needed for this existing helper's bug fix.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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=False behavior, and general escape_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

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.

2 participants