Skip to content

Commit

Permalink
Merge pull request #40 from sudoskys/dev
Browse files Browse the repository at this point in the history
feat: work with super long markdown
  • Loading branch information
sudoskys authored Dec 11, 2024
2 parents 3ff01f2 + 3b1d467 commit a5d462a
Show file tree
Hide file tree
Showing 15 changed files with 1,061 additions and 58 deletions.
Binary file added .github/result-9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 31 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@
[![PyPI version](https://badge.fury.io/py/telegramify-markdown.svg)](https://badge.fury.io/py/telegramify-markdown)
[![Downloads](https://pepy.tech/badge/telegramify-markdown)](https://pepy.tech/project/telegramify-markdown)

**Effortlessly Convert Raw Markdown to Telegram's [MarkdownV2](https://core.telegram.org/bots/api#markdownv2-style) Style**
**Effortlessly Convert Raw Markdown to Telegram's [MarkdownV2](https://core.telegram.org/bots/api#markdownv2-style)
Style**

Say goodbye to formatting issues! You no longer need to write parsers to convert raw Markdown text into Telegram's
MarkdownV2 format.

This library is designed to make reading and sending Markdown content on Telegram easier and more efficient. It is ideal
for LLM responses, GitHub README files, and more.

Forget about formatting errors from the API. Our custom renderer has been tested in a real server environment to ensure
its effectiveness.

We also support Latex Visualization(escape) and Expanded Citation.
- No matter the format or length, it can be easily handled!
- Forget about formatting errors from the API. Our custom renderer has been tested in a real server environment to
ensure
its effectiveness.
- We also support Latex Visualization(escape) and Expanded Citation.

> [!NOTE]
> If you're interested, there's also a Node.js version of the library
> available: [npm:telegramify-markdown](https://www.npmjs.com/package/telegramify-markdown).
## Installation
## 🪄 Quick Start

To install the library, run:

Expand All @@ -35,11 +37,21 @@ or, if you use `pdm`:
pdm add telegramify-markdown
```

## Use case
### 🤔 What you want to do?

- If you just want to send *static text* and don't want to worry about formatting,
check: **[playground/markdownify_case.py](https://github.com/sudoskys/telegramify-markdown/blob/main/playground/markdownify_case.py)**

- If you are developing an *LLM application* and need to send potentially **super-long text**, please
check: **[playground/telegramify_case.py](https://github.com/sudoskys/telegramify-markdown/blob/main/playground/telegramify_case.py)**

| Exp 1 | Exp 2 |
|---------------------------------|---------------------------------|
| ![result](.github/result-7.png) | ![result](.github/result-8.png) |
## 👀 Use case

| markdownify_case 1 | markdownify_case 2 | telegramify_case |
|---------------------------------|---------------------------------|---------------------------------|
| ![result](.github/result-7.png) | ![result](.github/result-8.png) | ![result](.github/result-9.png) |

### `markdownify`

````python3
import telegramify_markdown
Expand Down Expand Up @@ -115,15 +127,6 @@ Text
> This is the fourth line of the quote.
> `This is the fifth line of the quote.`
```python
print("Hello, World!")
```
This is `inline code`
1. First ordered list item
2. Another item
- Unordered sub-list.
- Another item.
1. Actual numbers don't matter, just that it's a number
"""
converted = telegramify_markdown.markdownify(
markdown_text,
Expand All @@ -134,7 +137,11 @@ print(converted)
# export Markdown to Telegram MarkdownV2 style.
````

## Supported Input
### `telegramify_case`

please check: **[playground/telegramify_case.py](https://github.com/sudoskys/telegramify-markdown/blob/main/playground/telegramify_case.py)**

## 🔨 Supported Input

- [x] Headings (Levels 1-6)
- [x] `Links [text](url)`
Expand All @@ -151,14 +158,14 @@ print(converted)
- [x] Spoilers `||Spoiler||`
- [x] Task Lists
- [x] Expanded Citation
- [ ] Strikethrough `~Strikethrough~`
- [x] Telegram Emojis
- [ ] Telegram User Mentions
- [ ] Strikethrough `~Strikethrough~`

> [!NOTE]
> Despite `~Strikethrough~` being mentioned in Telegram's official documentation, it can't be parsed as strikethrough.
## Proper Usage
## 🔭 Proper Usage

```python
import textwrap
Expand Down Expand Up @@ -186,13 +193,13 @@ TeleBot("TOKEN").send_message(
)
```

## Acknowledgement
## 🧸 Acknowledgement

This library is inspired by [npm:telegramify-markdown](https://www.npmjs.com/package/telegramify-markdown).

latex escape is inspired by [latex2unicode](https://github.com/tomtung/latex2unicode) and @yym68686.

## License
## 📜 License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

Expand Down
File renamed without changes.
8 changes: 0 additions & 8 deletions playground/convert.py

This file was deleted.

Empty file removed playground/escape.py
Empty file.
21 changes: 16 additions & 5 deletions playground/show_send.py → playground/markdownify_case.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import os
import textwrap

from dotenv import load_dotenv
from telebot import TeleBot

import telegramify_markdown

# Customize the markdownify
telegramify_markdown.customize.strict_markdown = False # we need send underline text
telegramify_markdown.customize.latex_escape = True # we need to escape latex
run_1 = telegramify_markdown.markdownify(

# Test html tags
html_t = telegramify_markdown.markdownify(
"Hello, World! HTML: <strong>Hello, World!</strong>"
)
print(run_1)
md = r"""
print(html_t)

# Use textwrap.dedent to remove the leading whitespace from the text.
md = textwrap.dedent(r"""
# Title
## Subtitle
### Subsubtitle
Expand Down Expand Up @@ -99,11 +105,16 @@
- Unordered sub-list.
- Another item.
1. Actual numbers don't matter, just that it's a number
"""
```
print("```")
```
""")

emoji_md = r"""
![👍](tg://emoji?id=5368324170671202286)
"""

# export Markdown to Telegram MarkdownV2 style.
converted = telegramify_markdown.markdownify(
md,
max_line_length=None, # If you want to change the max line length for links, images, set it to the desired value.
Expand All @@ -118,5 +129,5 @@
bot.send_message(
chat_id,
converted,
parse_mode="MarkdownV2"
parse_mode="MarkdownV2" # IMPORTANT: Need Send in MarkdownV2 Mode.
)
File renamed without changes.
Loading

0 comments on commit a5d462a

Please sign in to comment.