From 288d9f26692ebc796dbb8418d8334234dee02a06 Mon Sep 17 00:00:00 2001 From: sudoskys Date: Sat, 25 May 2024 18:54:14 +0800 Subject: [PATCH 1/2] :art: chore(playground): update use_case and show_send scripts - Update use_case.py with additional markdown symbols and formatting - Add show_send.py script for sending formatted markdown messages - Refactor telegram_exp.py for code cleanliness - Update exp1.md with improved markdown examples https://github.com/sudoskys/telegramify-markdown/issues/3 --- README.md | 5 +++-- playground/use_case.py | 3 +-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9baefd1..c81d7cc 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ or if you use `pdm`: pdm add telegramify-markdown ``` -## Supported Features +## Supported Input - [x] Headings (1-6) - [x] Links [text](url) @@ -47,7 +47,7 @@ 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 @@ -84,6 +84,7 @@ This is `inline code` """ converted = telegramify_markdown.convert(md) print(converted) +# export Markdown to Telegram MarkdownV2 style. ```` output as follows: diff --git a/playground/use_case.py b/playground/use_case.py index 2d210c4..545fe3a 100644 --- a/playground/use_case.py +++ b/playground/use_case.py @@ -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) From c4659296b74272b43c20e889f13587c644fc1a62 Mon Sep 17 00:00:00 2001 From: sudoskys Date: Sat, 25 May 2024 19:55:35 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=8C=9F=20fix:=20strict=5Fmarkdown=20f?= =?UTF-8?q?lag=20added=20to=20customize.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🚀 feat: refactor render_strong method in render.py --- README.md | 28 ++++++++++++++------------- pyproject.toml | 2 +- src/telegramify_markdown/__init__.py | 2 -- src/telegramify_markdown/customize.py | 1 + src/telegramify_markdown/render.py | 17 ++++++++++------ 5 files changed, 28 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index c81d7cc..e124bfb 100644 --- a/README.md +++ b/README.md @@ -28,16 +28,16 @@ pdm add telegramify-markdown ## 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~` @@ -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 telegram official documentation, its cant be parsed as strikethrough. +`~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** @@ -82,7 +84,7 @@ 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. ```` diff --git a/pyproject.toml b/pyproject.toml index 4e9e56a..23f781a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" }, diff --git a/src/telegramify_markdown/__init__.py b/src/telegramify_markdown/__init__.py index ffe7526..ea7b747 100644 --- a/src/telegramify_markdown/__init__.py +++ b/src/telegramify_markdown/__init__.py @@ -11,8 +11,6 @@ def markdownify(text: str): # '_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!' - # if text in ["_", "*", "[", "]", "(", ")", "~", "`", ">", "#", "+", "-", "=", "|", "{", "}", ".", "!"]: - # return text return formatting.escape_markdown(text) diff --git a/src/telegramify_markdown/customize.py b/src/telegramify_markdown/customize.py index 88d769c..f13d198 100644 --- a/src/telegramify_markdown/customize.py +++ b/src/telegramify_markdown/customize.py @@ -15,3 +15,4 @@ class Symbol(object): markdown_symbol = Symbol() +strict_markdown = True diff --git a/src/telegramify_markdown/render.py b/src/telegramify_markdown/render.py index a27aa2e..d6588c6 100644 --- a/src/telegramify_markdown/render.py +++ b/src/telegramify_markdown/render.py @@ -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): @@ -68,11 +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* but __text__ for emphasis, so we need to check the delimiter - if token.delimiter == "*": - return self.embed_span(Fragment(token.delimiter * 1), token.children) - # __ - return self.embed_span(Fragment(token.delimiter * 2), 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