Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: escape headers and unordered lists in markdown utils #1058

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/1058.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update markdown utilities to escape headers and unordered lists.
10 changes: 6 additions & 4 deletions disnake/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import os
import pkgutil
import re
import string
import sys
import unicodedata
import warnings
Expand Down Expand Up @@ -776,7 +777,8 @@ def resolve_template(code: Union[Template, str]) -> str:
r"\{0}(?=([\s\S]*((?<!\{0})\{0})))".format(c) for c in ("*", "`", "_", "~", "|")
)

_MARKDOWN_ESCAPE_COMMON = r"^>(?:>>)?\s|\[.+\]\(.+\)"
# todo: fix issue with lists including the preceeding spaces more sustainably
_MARKDOWN_ESCAPE_COMMON = r"^>(?:>>)?\s|^#{1,3}\s|^(?P<keep>\s*?)?(?P<char>[-*])\s|\[.+\]\(.+\)"

_MARKDOWN_ESCAPE_REGEX = re.compile(
rf"(?P<markdown>{_MARKDOWN_ESCAPE_SUBREGEX}|{_MARKDOWN_ESCAPE_COMMON})", re.MULTILINE
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
24 changes: 23 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading