Skip to main content

Convert Markdown to a format usable by Telegram.

Project description

telegramify-markdown

PyPI version
Downloads

🪄 Python Telegram Markdown Converter | No more formatting headaches.

Convert Raw Markdown to Telegram's MarkdownV2 Style

Introducing a robust Python library that enhances Markdown formatting support in Telegram. This library automatically processes various Markdown inputs, preserving the original formatting without requiring manual escaping. It simplifies your Markdown interactions on Telegram with seamless auto-processing.

Before this library, sending and rendering unknown Markdown content (like GitHub's README files) required complex parsing and reconstruction. Now, you can easily handle this and customize it for better results!

Utilizing a custom renderer, the library has been tested in a real server environment to ensure its efficacy.

For those interested, there is also a Node.js version of the library with the same name: npm:telegramify-markdown

Installation

To install the library, run:

pip install telegramify-markdown

or, if you use pdm:

pdm add telegramify-markdown

Supported Input

  • Headings (Levels 1-6)
  • Links [text](url)
  • Images ![alt](url)
  • Lists (Ordered and Unordered)
  • Tables |-|-|
  • Horizontal Rules ----
  • Text Styles *Italic* and **Bold**
  • Underline __Underline__ (if customize.strict_markdown is False)
  • Code Blocks
  • Inline Code
  • Block Quotes >
  • Strikethrough ~~Strikethrough~~
  • Task Lists
  • Strikethrough ~Strikethrough~
  • Spoilers ||Spoiler||
  • Telegram Emojis
  • Telegram User Mentions

[!NOTE]
Since mistletoe doesn't parse - [ ] TODO or Spoilers, we can't implement them.
Despite ~Strikethrough~ being mentioned in Telegram's official documentation, it can't be parsed as strikethrough.

Use case

import telegramify_markdown
from telegramify_markdown import customize

customize.markdown_symbol.head_level_1 = "📌"  # If you want, Customizing the head level 1 symbol
customize.markdown_symbol.link = "🔗"  # If you want, Customizing the link symbol
customize.strict_markdown = True  # If you want to use __underline__ as underline, set it to False or it will be converted to bold.
markdown_text = """
'\_', '\*', '\[', '\]', '\(', '\)', '\~', '\`', '\>', '\#', '\+', '\-', '\=', '\|', '\{', '\}', '\.', '\!'
_ , * , [ , ] , ( , ) , ~ , ` , > , # , + , - , = , | , { , } , . , !
**bold text**
*bold text*
_italic text_
__underline__
~no valid strikethrough~
~~strikethrough~~
||spoiler||
*bold _italic bold ~~italic bold strikethrough ||italic bold strikethrough spoiler||~~ __underline italic bold___ bold*
__underline italic bold__
[link](https://www.google.com)
- [ ] Uncompleted task list item
- [x] Completed task list item
> Quote
```python
print("Hello, World!")
```
This is `inline code`
1. First ordered list item
2. Another item
    - Unordered sub-list.
1. Actual numbers don't matter, just that it's a number
"""
converted = telegramify_markdown.markdownify(
    markdown_text,
    max_line_length=None,  # If you want to change the max line length for links, images, set it to the desired value.
    normalize_whitespace=False
)
print(converted)
# export Markdown to Telegram MarkdownV2 style.

output as follows:

.github/result.png

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

telegramify_markdown-0.1.8.tar.gz (6.8 kB view hashes)

Uploaded Source

Built Distribution

telegramify_markdown-0.1.8-py3-none-any.whl (6.8 kB view hashes)

Uploaded Python 3

Supported by

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