Skip to content

Commit

Permalink
fix: support extended markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
onerandomusername committed Jun 24, 2023
1 parent c38f6ff commit 242e5a5
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
12 changes: 7 additions & 5 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,15 +777,16 @@ 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
)

_URL_REGEX = r"(?P<url><[^: >]+:\/[^ >]+>|(?:https?|steam):\/\/[^\s<]+[^<.,:;\"\'\]\s])"

_MARKDOWN_STOCK_REGEX = rf"(?P<markdown>[_\\~|\*`#\-]|{_MARKDOWN_ESCAPE_COMMON})"
_MARKDOWN_STOCK_REGEX = rf"(?P<markdown>[_\\~|\*`]|{_MARKDOWN_ESCAPE_COMMON})"


def remove_markdown(text: str, *, ignore_links: bool = True) -> str:
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
58 changes: 58 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,64 @@ def test_markdown_links(text: str, expected, expected_ignore) -> None:
assert utils.remove_markdown(text, ignore_links=True) == expected_ignore


@pytest.mark.parametrize(
("text", "expected"),
[
(
r"# disnake",
r"disnake",
),
(
r"## woah this should be removed\n ### but not this one",
r"woah this should be removed\n ### but not this one",
),
(
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
""",
),
],
)
def test_remove_markdown(text: str, expected: str) -> None:
assert utils.remove_markdown(text) == expected


@pytest.mark.parametrize(
("text", "expected"),
[
(
r"# disnake",
r"\# disnake",
),
(
"## escape these quotes\n ### but not these",
"\\#\\# escape these quotes\n ### but not these",
),
(
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
""",
),
],
)
def test_escape_markdown(text: str, expected: str) -> None:
assert utils.escape_markdown(text) == expected


@pytest.mark.parametrize(
("text", "expected"),
[
Expand Down

0 comments on commit 242e5a5

Please sign in to comment.