Skip to main content

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.

Python Version License: MIT Tests

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

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Add tests for your changes
  5. Ensure all tests pass (pytest)
  6. Format your code (black . && isort .)
  7. Commit your changes (git commit -am 'Add some amazing feature')
  8. Push to the branch (git push origin feature/amazing-feature)
  9. 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

telegram_markdown_converter-1.0.6.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

telegram_markdown_converter-1.0.6-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file telegram_markdown_converter-1.0.6.tar.gz.

File metadata

File hashes

Hashes for telegram_markdown_converter-1.0.6.tar.gz
Algorithm Hash digest
SHA256 3d241a69df018dc015939963670dfdd8a4b79ff41f75f29cd98e2fea3c81ef54
MD5 57a13c8904db3f178c183649eb6ad8c3
BLAKE2b-256 c3cc7ee83b4df7cec84b925dc8353584eaf53622f17995c851133789e116f975

See more details on using hashes here.

File details

Details for the file telegram_markdown_converter-1.0.6-py3-none-any.whl.

File metadata

File hashes

Hashes for telegram_markdown_converter-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 9103c45c02f64ecd9d597208c982b9eb88118ebf3d2f7490f44904f7209a9d1f
MD5 582c92ec72a90ed9d8699b78a6e46ac5
BLAKE2b-256 524756f9be86ef88214eac27f7e842f33b9b78b1f373bdced345c6fa4535899e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page