diff --git a/aiogram/utils/markdown.py b/aiogram/utils/markdown.py
index e934fe14..7b217b4f 100644
--- a/aiogram/utils/markdown.py
+++ b/aiogram/utils/markdown.py
@@ -1,4 +1,4 @@
-from .text_decorations import html, markdown
+from .text_decorations import html_decoration, markdown_decoration
LIST_MD_SYMBOLS = "*_`["
@@ -41,7 +41,7 @@ def bold(*content, sep=" "):
:param sep:
:return:
"""
- return markdown.bold.format(value=html.quote(_join(*content, sep=sep)))
+ return markdown_decoration.bold.format(value=html_decoration.quote(_join(*content, sep=sep)))
def hbold(*content, sep=" "):
@@ -52,7 +52,7 @@ def hbold(*content, sep=" "):
:param sep:
:return:
"""
- return html.bold.format(value=html.quote(_join(*content, sep=sep)))
+ return html_decoration.bold.format(value=html_decoration.quote(_join(*content, sep=sep)))
def italic(*content, sep=" "):
@@ -63,7 +63,7 @@ def italic(*content, sep=" "):
:param sep:
:return:
"""
- return markdown.italic.format(value=html.quote(_join(*content, sep=sep)))
+ return markdown_decoration.italic.format(value=html_decoration.quote(_join(*content, sep=sep)))
def hitalic(*content, sep=" "):
@@ -74,7 +74,7 @@ def hitalic(*content, sep=" "):
:param sep:
:return:
"""
- return html.italic.format(value=html.quote(_join(*content, sep=sep)))
+ return html_decoration.italic.format(value=html_decoration.quote(_join(*content, sep=sep)))
def code(*content, sep=" "):
@@ -85,7 +85,7 @@ def code(*content, sep=" "):
:param sep:
:return:
"""
- return markdown.code.format(value=html.quote(_join(*content, sep=sep)))
+ return markdown_decoration.code.format(value=html_decoration.quote(_join(*content, sep=sep)))
def hcode(*content, sep=" "):
@@ -96,7 +96,7 @@ def hcode(*content, sep=" "):
:param sep:
:return:
"""
- return html.code.format(value=html.quote(_join(*content, sep=sep)))
+ return html_decoration.code.format(value=html_decoration.quote(_join(*content, sep=sep)))
def pre(*content, sep="\n"):
@@ -107,7 +107,7 @@ def pre(*content, sep="\n"):
:param sep:
:return:
"""
- return markdown.pre.format(value=html.quote(_join(*content, sep=sep)))
+ return markdown_decoration.pre.format(value=html_decoration.quote(_join(*content, sep=sep)))
def hpre(*content, sep="\n"):
@@ -118,7 +118,7 @@ def hpre(*content, sep="\n"):
:param sep:
:return:
"""
- return html.pre.format(value=html.quote(_join(*content, sep=sep)))
+ return html_decoration.pre.format(value=html_decoration.quote(_join(*content, sep=sep)))
def underline(*content, sep=" "):
@@ -129,7 +129,9 @@ def underline(*content, sep=" "):
:param sep:
:return:
"""
- return markdown.underline.format(value=markdown.quote(_join(*content, sep=sep)))
+ return markdown_decoration.underline.format(
+ value=markdown_decoration.quote(_join(*content, sep=sep))
+ )
def hunderline(*content, sep=" "):
@@ -140,7 +142,7 @@ def hunderline(*content, sep=" "):
:param sep:
:return:
"""
- return html.underline.format(value=html.quote(_join(*content, sep=sep)))
+ return html_decoration.underline.format(value=html_decoration.quote(_join(*content, sep=sep)))
def strikethrough(*content, sep=" "):
@@ -151,7 +153,9 @@ def strikethrough(*content, sep=" "):
:param sep:
:return:
"""
- return markdown.strikethrough.format(value=markdown.quote(_join(*content, sep=sep)))
+ return markdown_decoration.strikethrough.format(
+ value=markdown_decoration.quote(_join(*content, sep=sep))
+ )
def hstrikethrough(*content, sep=" "):
@@ -162,7 +166,9 @@ def hstrikethrough(*content, sep=" "):
:param sep:
:return:
"""
- return html.strikethrough.format(value=html.quote(_join(*content, sep=sep)))
+ return html_decoration.strikethrough.format(
+ value=html_decoration.quote(_join(*content, sep=sep))
+ )
def link(title: str, url: str) -> str:
@@ -173,7 +179,7 @@ def link(title: str, url: str) -> str:
:param url:
:return:
"""
- return markdown.link.format(value=html.quote(title), link=url)
+ return markdown_decoration.link.format(value=html_decoration.quote(title), link=url)
def hlink(title: str, url: str) -> str:
@@ -184,7 +190,7 @@ def hlink(title: str, url: str) -> str:
:param url:
:return:
"""
- return html.link.format(value=html.quote(title), link=url)
+ return html_decoration.link.format(value=html_decoration.quote(title), link=url)
def hide_link(url: str) -> str:
diff --git a/aiogram/utils/text_decorations.py b/aiogram/utils/text_decorations.py
index 3ca73bc9..5b9502b7 100644
--- a/aiogram/utils/text_decorations.py
+++ b/aiogram/utils/text_decorations.py
@@ -6,7 +6,13 @@ from typing import AnyStr, Callable, Generator, Iterable, List, Optional
from aiogram.api.types import MessageEntity
-__all__ = ("TextDecoration", "html", "markdown", "add_surrogate", "remove_surrogate")
+__all__ = (
+ "TextDecoration",
+ "html_decoration",
+ "markdown_decoration",
+ "add_surrogate",
+ "remove_surrogate",
+)
@dataclass
@@ -82,7 +88,7 @@ class TextDecoration:
yield self.quote(text[offset:length])
-html = TextDecoration(
+html_decoration = TextDecoration(
link='{value}',
bold="{value}",
italic="{value}",
@@ -93,18 +99,18 @@ html = TextDecoration(
quote=html.escape,
)
-markdown = TextDecoration(
+MARKDOWN_QUOTE_PATTERN = re.compile(r"([_*\[\]()~`>#+\-|{}.!])")
+
+markdown_decoration = TextDecoration(
link="[{value}]({link})",
bold="*{value}*",
- italic="_{value}_",
+ italic="_{value}_\r",
code="`{value}`",
pre="```{value}```",
- underline="--{value}--", # Is not supported
- strikethrough="~~{value}~~", # Is not supported
- quote=lambda text: re.sub(
- pattern=r"([*_`\[])", repl=r"\\\1", string=text
- ), # Is not always helpful
-) # Markdown is not recommended for usage. Use HTML instead
+ underline="__{value}__",
+ strikethrough="~{value}~",
+ quote=lambda text: re.sub(pattern=MARKDOWN_QUOTE_PATTERN, repl=r"\\\1", string=text),
+)
def add_surrogate(text: str) -> str:
diff --git a/tests/test_utils/test_markdown.py b/tests/test_utils/test_markdown.py
index 79b22c97..b9da8f46 100644
--- a/tests/test_utils/test_markdown.py
+++ b/tests/test_utils/test_markdown.py
@@ -14,15 +14,15 @@ class TestMarkdown:
[markdown.text, ("test", "test"), None, "test test"],
[markdown.bold, ("test", "test"), " ", "*test test*"],
[markdown.hbold, ("test", "test"), " ", "test test"],
- [markdown.italic, ("test", "test"), " ", "_test test_"],
+ [markdown.italic, ("test", "test"), " ", "_test test_\r"],
[markdown.hitalic, ("test", "test"), " ", "test test"],
[markdown.code, ("test", "test"), " ", "`test test`"],
[markdown.hcode, ("test", "test"), " ", "test test"],
[markdown.pre, ("test", "test"), " ", "```test test```"],
[markdown.hpre, ("test", "test"), " ", "
test test"], - [markdown.underline, ("test", "test"), " ", "--test test--"], + [markdown.underline, ("test", "test"), " ", "__test test__"], [markdown.hunderline, ("test", "test"), " ", "test test"], - [markdown.strikethrough, ("test", "test"), " ", "~~test test~~"], + [markdown.strikethrough, ("test", "test"), " ", "~test test~"], [markdown.hstrikethrough, ("test", "test"), " ", "
test"],
- [html, MessageEntity(type="pre", offset=0, length=5), "test"], - [html, MessageEntity(type="underline", offset=0, length=5), "test"], - [html, MessageEntity(type="strikethrough", offset=0, length=5), "
test"],
+ [html_decoration, MessageEntity(type="pre", offset=0, length=5), "test"], + [html_decoration, MessageEntity(type="underline", offset=0, length=5), "test"], [ - html, + html_decoration, + MessageEntity(type="strikethrough", offset=0, length=5), + "