A Python library for converting Markdown to Telegram-safe MarkdownV2 formatting
Project description
Telegram Markdown Converter
A Python library for converting standard Markdown formatting to Telegram's MarkdownV2 format, with proper escaping of special characters.
Features
- ✅ Convert standard Markdown to Telegram MarkdownV2 format
- ✅ Proper escaping of special characters
- ✅ Preserve code blocks and inline code without modification
- ✅ Handle nested markdown formatting
- ✅ Support for links, bold, italic, strikethrough, underline, and spoiler text
- ✅ Recursive processing of nested markdown structures
- ✅ Type hints for better IDE support
Installation
From PyPI (coming soon)
pip install telegram-markdown-converter
From Source
git clone https://github.com/ngoldo/telegram-markdown-converter.git
cd telegram-markdown-converter
pip install -e .
Quick Start
from telegram_markdown_converter import convert_markdown
# Basic usage
text = "This is **bold** and *italic* text with a [link](https://example.com)"
converted = convert_markdown(text)
print(converted) # Output: This is *bold* and _italic_ text with a [link](https://example.com)
# Handle special characters
text = "Special chars: . ! - = + will be escaped"
converted = convert_markdown(text)
print(converted) # Output: Special chars: \. \! \- \= \+ will be escaped
# Code blocks and inline code are preserved
text = "Here's some `inline code` and a code block:\n```python\nprint('hello')\n```"
converted = convert_markdown(text)
# Code sections remain unchanged
Supported Markdown Elements
| Standard Markdown | Telegram MarkdownV2 | Description |
|---|---|---|
**bold** |
*bold* |
Bold text |
***bold italic*** |
*\_bold italic\_* |
Bold and Italic text |
*italic*/_italic_ |
_italic_ |
Italic text |
~~strikethrough~~/~strikethrough~ |
~strikethrough~ |
Strikethrough text |
__underline__ |
__underline__ |
Underlined text |
||spoiler|| |
||spoiler|| |
Spoiler text |
> blockquote |
>blockquote |
Blockquotes |
[link](url) |
[link](url) |
Hyperlinks |
`inline code` |
`inline code` |
Inline code |
```code block``` |
```code block``` |
Code blocks |
API Reference
convert_markdown(text: str) -> str
Converts standard Markdown text to Telegram MarkdownV2 format.
Parameters:
text(str): The input text with standard Markdown formatting
Returns:
str: The converted text with Telegram MarkdownV2 formatting and properly escaped special characters
Example:
result = convert_markdown("**Hello** *world*!")
# Returns: "*Hello* _world_\\!"
Development
Setting up the development environment
git clone https://github.com/ngoldo/telegram-markdown-converter.git
cd telegram-markdown-converter
pip install -e ".[dev]"
Running tests
pytest
Running tests with coverage
pytest --cov=telegram_markdown_converter --cov-report=html
Code formatting
black src/ tests/
isort src/ tests/
Type checking
mypy src/
Pre-commit hooks
pre-commit install
pre-commit run --all-files
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Add tests for your changes
- Ensure all tests pass (
pytest) - Format your code (
black . && isort .) - Commit your changes (
git commit -am 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Built for use with the Telegram Bot API
- Follows Telegram's MarkdownV2 specification
- Inspired by the need for safe markdown formatting in Telegram bots
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file telegram_markdown_converter-1.0.6.tar.gz.
File metadata
- Download URL: telegram_markdown_converter-1.0.6.tar.gz
- Upload date:
- Size: 15.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d241a69df018dc015939963670dfdd8a4b79ff41f75f29cd98e2fea3c81ef54
|
|
| MD5 |
57a13c8904db3f178c183649eb6ad8c3
|
|
| BLAKE2b-256 |
c3cc7ee83b4df7cec84b925dc8353584eaf53622f17995c851133789e116f975
|
File details
Details for the file telegram_markdown_converter-1.0.6-py3-none-any.whl.
File metadata
- Download URL: telegram_markdown_converter-1.0.6-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9103c45c02f64ecd9d597208c982b9eb88118ebf3d2f7490f44904f7209a9d1f
|
|
| MD5 |
582c92ec72a90ed9d8699b78a6e46ac5
|
|
| BLAKE2b-256 |
524756f9be86ef88214eac27f7e842f33b9b78b1f373bdced345c6fa4535899e
|