Skip to content

Commit

Permalink
Merge pull request #7 from sudoskys/dev
Browse files Browse the repository at this point in the history
Correct Stron Rendering(Compatible)
  • Loading branch information
sudoskys authored May 25, 2024
2 parents c538856 + 9b8c630 commit 654ec6e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 23 deletions.
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ or if you use `pdm`:
pdm add telegramify-markdown
```

## Supported Features
## Supported Input

- [x] Headings (1-6)
- [x] Links [text](url)
- [x] Images ![alt]
- [x] `Links [text](url)`
- [x] `Images ![alt]`
- [x] Lists (Ordered, Unordered)
- [x] Tables |-|-|
- [x] Horizontal Rule ----
- [x] *Text* **Styles**
- [x] __Underline__
- [x] `Tables |-|-|`
- [x] `Horizontal Rule ----`
- [x] `*Text* **Styles**`
- [x] `__Underline__` (if `customize.strict_markdown` is False)
- [x] Code Blocks
- [x] `Inline Code`
- [x] Block Quotes
- [x] `Block Quotes >`
- [x] `~~Strikethrough~~`
- [ ] Task Lists
- [ ] `~Strikethrough~`
Expand All @@ -47,17 +47,19 @@ pdm add telegramify-markdown

> [!NOTE]
> Since mistletoe doesn't parse `- [] TODO` and Spoiler, we can't apply it.
`~Strikethrough~` is incorrect, even if it comes from official documentation, please use `~~Strikethrough~~` format.
`~Strikethrough~` is incorrect, even if it comes from telegram official documentation, its cant be parsed as
> strikethrough.
## Use case

````python3
import telegramify_markdown
from telegramify_markdown.customize import markdown_symbol
from telegramify_markdown import customize

markdown_symbol.head_level_1 = "📌" # If you want, Customizing the head level 1 symbol
markdown_symbol.link = "🔗" # If you want, Customizing the link symbol
md = """
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**
Expand All @@ -82,8 +84,9 @@ This is `inline code`
- Unordered sub-list.
1. Actual numbers don't matter, just that it's a number
"""
converted = telegramify_markdown.convert(md)
converted = telegramify_markdown.convert(markdown_text)
print(converted)
# export Markdown to Telegram MarkdownV2 style.
````

output as follows:
Expand Down
3 changes: 1 addition & 2 deletions playground/use_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

markdown_symbol.head_level_1 = "📌" # If you want, Customizing the head level 1 symbol
markdown_symbol.link = "🔗" # If you want, Customizing the link symbol
md = """
*bold _italic bold ~italic bold strikethrough ||italic bold strikethrough spoiler||~ __underline italic bold___ bold*
md = """*bold _italic bold ~italic bold strikethrough ||italic bold strikethrough spoiler||~ __underline italic bold___ bold*
~strikethrough~
"""
converted = telegramify_markdown.convert(md)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "telegramify-markdown"
version = "0.1.3"
version = "0.1.4"
description = "Convert Markdown to a format usable by Telegram."
authors = [
{ name = "sudoskys", email = "coldlando@hotmail.com" },
Expand Down
2 changes: 0 additions & 2 deletions src/telegramify_markdown/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

def markdownify(text: str):
# '_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!'
# if text in ["_", "*", "[", "]", "(", ")", "~", "`", ">", "#", "+", "-", "=", "|", "{", "}", ".", "!"]:
# return text
return formatting.escape_markdown(text)


Expand Down
1 change: 1 addition & 0 deletions src/telegramify_markdown/customize.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ class Symbol(object):


markdown_symbol = Symbol()
strict_markdown = True
15 changes: 11 additions & 4 deletions src/telegramify_markdown/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from mistletoe.markdown_renderer import MarkdownRenderer, LinkReferenceDefinition, Fragment
from telebot import formatting

from .customize import markdown_symbol
from .customize import markdown_symbol, strict_markdown


class TelegramMarkdownRenderer(MarkdownRenderer):
Expand Down Expand Up @@ -68,9 +68,16 @@ def render_emphasis(self, token: span_token.Emphasis) -> Iterable[Fragment]:
return super().render_emphasis(token)

def render_strong(self, token: span_token.Strong) -> Iterable[Fragment]:
# Telegram strong: *text*
# Markdown strong: **text** or __text__
return self.embed_span(Fragment('*'), token.children)
if strict_markdown:
# Telegram strong: *text*
# Markdown strong: **text** or __text__
return self.embed_span(Fragment('*'), token.children)
else:
# bold
if token.delimiter == "*":
return self.embed_span(Fragment(token.delimiter * 1), token.children)
# underline
return self.embed_span(Fragment(token.delimiter * 2), token.children)

def render_strikethrough(
self, token: span_token.Strikethrough
Expand Down

0 comments on commit 654ec6e

Please sign in to comment.