diff --git a/changelog/1058.bugfix.rst b/changelog/1058.bugfix.rst new file mode 100644 index 0000000000..e6498926b1 --- /dev/null +++ b/changelog/1058.bugfix.rst @@ -0,0 +1 @@ +Update markdown utilities to escape headers and unordered lists. diff --git a/disnake/utils.py b/disnake/utils.py index 54781da834..285d17886f 100644 --- a/disnake/utils.py +++ b/disnake/utils.py @@ -10,6 +10,7 @@ import os import pkgutil import re +import string import sys import unicodedata import warnings @@ -776,7 +777,8 @@ def resolve_template(code: Union[Template, str]) -> str: r"\{0}(?=([\s\S]*((?(?:>>)?\s|\[.+\]\(.+\)" +# todo: fix issue with lists including the preceeding spaces more sustainably +_MARKDOWN_ESCAPE_COMMON = r"^>(?:>>)?\s|^#{1,3}\s|^(?P\s*?)?(?P[-*])\s|\[.+\]\(.+\)" _MARKDOWN_ESCAPE_REGEX = re.compile( rf"(?P{_MARKDOWN_ESCAPE_SUBREGEX}|{_MARKDOWN_ESCAPE_COMMON})", re.MULTILINE @@ -813,7 +815,7 @@ def remove_markdown(text: str, *, ignore_links: bool = True) -> str: def replacement(match): groupdict = match.groupdict() - return groupdict.get("url", "") + return groupdict.get("url", "") or groupdict.get("keep", "") regex = _MARKDOWN_STOCK_REGEX if ignore_links: @@ -847,12 +849,12 @@ def escape_markdown(text: str, *, as_needed: bool = False, ignore_links: bool = """ if not as_needed: - def replacement(match): + def replacement(match: re.Match[str]) -> str: groupdict = match.groupdict() is_url = groupdict.get("url") if is_url: return is_url - return "\\" + groupdict["markdown"] + return "".join(c if c in string.whitespace else "\\" + c for c in groupdict["markdown"]) regex = _MARKDOWN_STOCK_REGEX if ignore_links: diff --git a/tests/test_utils.py b/tests/test_utils.py index 8c5ee4ec38..fdc930c7de 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -474,10 +474,32 @@ def test_resolve_template(url, expected) -> None: "hi a aaa\npy x uwu y", r"\*hi\* \~\~a\~ \|aaa\~\*\\\`\`" "\n" r"\`py x\`\`\` \_\_uwu\_\_ y", ), + ( + r"## disnake", + "disnake", + r"\#\# disnake", + ), + ( + r"""Inside is a long list of why markdown is an amazing tool +- markdown supports lists + - honestly its a great tool that markdown supports said lists + - this is wrong but uh we'll get to that +""", + r"""Inside is a long list of why markdown is an amazing tool +markdown supports lists + honestly its a great tool that markdown supports said lists + this is wrong but uh we'll get to that +""", + r"""Inside is a long list of why markdown is an amazing tool +\- markdown supports lists + \- honestly its a great tool that markdown supports said lists + \- this is wrong but uh we'll get to that +""", + ), ( "aaaaa\n> h\n>> abc \n>>> te*st_", "aaaaa\nh\n>> abc \ntest", - "aaaaa\n\\> h\n>> abc \n\\>>> te\\*st\\_", + "aaaaa\n\\> h\n>> abc \n\\>\\>\\> te\\*st\\_", ), ( "*h*\n> [li|nk](~~url~~) xyz **https://google.com/stuff?uwu=owo",