Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Commit

Permalink
added escape_markdown and small fix to _test_string
Browse files Browse the repository at this point in the history
  • Loading branch information
rockygo2 committed Jun 14, 2024
1 parent 2a0411b commit fa81a81
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion discord/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ def _string_width(string: str) -> int:
UNICODE_WIDE_CHAR_TYPE = 'WFA'
func = unicodedata.east_asian_width
return sum(
2 if (mark(2, "_string_width"), func(char))[1] in UNICODE_WIDE_CHAR_TYPE else (1, mark(2, "_string_width"))[0]
2 if (mark(2, "_string_width"), func(char))[1] in UNICODE_WIDE_CHAR_TYPE else (1, mark(3, "_string_width"))[0]
for char in string
)

Expand Down Expand Up @@ -949,6 +949,7 @@ def replacement(match: re.Match[str]) -> str:
return re.sub(regex, replacement, text, 0, re.MULTILINE)


@test(5)
def escape_markdown(text: str, *, as_needed: bool = False, ignore_links: bool = True) -> str:
r"""A helper function that escapes Discord's markdown.
Expand Down Expand Up @@ -980,15 +981,20 @@ def replacement(match):
groupdict = match.groupdict()
is_url = groupdict.get('url')
if is_url:
mark(0, "escape_markdown")
return is_url
mark(1, "escape_markdown")
return '\\' + groupdict['markdown']

regex = _MARKDOWN_STOCK_REGEX
if ignore_links:
regex = f'(?:{_URL_REGEX}|{regex})'
mark(4)
mark(2)
return re.sub(regex, replacement, text, 0, re.MULTILINE)
else:
text = re.sub(r'\\', r'\\\\', text)
mark(3)
return _MARKDOWN_ESCAPE_REGEX.sub(r'\\\1', text)


Expand Down

0 comments on commit fa81a81

Please sign in to comment.