aboutsummaryrefslogtreecommitdiff
path: root/discord/utils.py
diff options
context:
space:
mode:
authorEdwin <[email protected]>2021-03-29 00:38:34 +0200
committerGitHub <[email protected]>2021-03-28 18:38:34 -0400
commit31ee3fafc17c552fa972dfa9bd0fc38915538684 (patch)
tree56abdd0185dd99b5016f972366dbda21cd52d23b /discord/utils.py
parent[commands] Only remove top level commands on cog load failure (diff)
downloaddiscord.py-31ee3fafc17c552fa972dfa9bd0fc38915538684.tar.xz
discord.py-31ee3fafc17c552fa972dfa9bd0fc38915538684.zip
Add remove_markdown helper function
Diffstat (limited to 'discord/utils.py')
-rw-r--r--discord/utils.py42
1 files changed, 39 insertions, 3 deletions
diff --git a/discord/utils.py b/discord/utils.py
index 51031485..8049eb46 100644
--- a/discord/utils.py
+++ b/discord/utils.py
@@ -484,6 +484,43 @@ _MARKDOWN_ESCAPE_COMMON = r'^>(?:>>)?\s|\[.+\]\(.+\)'
_MARKDOWN_ESCAPE_REGEX = re.compile(r'(?P<markdown>%s|%s)' % (_MARKDOWN_ESCAPE_SUBREGEX, _MARKDOWN_ESCAPE_COMMON), re.MULTILINE)
+_URL_REGEX = r'(?P<url><[^: >]+:\/[^ >]+>|(?:https?|steam):\/\/[^\s<]+[^<.,:;\"\'\]\s])'
+
+_MARKDOWN_STOCK_REGEX = r'(?P<markdown>[_\\~|\*`]|%s)' % _MARKDOWN_ESCAPE_COMMON
+
+def remove_markdown(text, *, ignore_links=True):
+ """A helper function that removes markdown characters.
+
+ .. versionadded:: 1.7
+
+ .. note::
+ This function is not markdown aware and may remove meaning from the original text. For example,
+ if the input contains ``10 * 5`` then it will be converted into ``10 5``.
+
+ Parameters
+ -----------
+ text: :class:`str`
+ The text to remove markdown from.
+ ignore_links: :class:`bool`
+ Whether to leave links alone when removing markdown. For example,
+ if a URL in the text contains characters such as ``_`` then it will
+ be left alone. Defaults to ``True``.
+
+ Returns
+ --------
+ :class:`str`
+ The text with the markdown special characters removed.
+ """
+
+ def replacement(match):
+ groupdict = match.groupdict()
+ return groupdict.get('url', '')
+
+ regex = _MARKDOWN_STOCK_REGEX
+ if ignore_links:
+ regex = '(?:%s|%s)' % (_URL_REGEX, regex)
+ return re.sub(regex, replacement, text, 0, re.MULTILINE)
+
def escape_markdown(text, *, as_needed=False, ignore_links=True):
r"""A helper function that escapes Discord's markdown.
@@ -510,7 +547,6 @@ def escape_markdown(text, *, as_needed=False, ignore_links=True):
"""
if not as_needed:
- url_regex = r'(?P<url><[^: >]+:\/[^ >]+>|(?:https?|steam):\/\/[^\s<]+[^<.,:;\"\'\]\s])'
def replacement(match):
groupdict = match.groupdict()
is_url = groupdict.get('url')
@@ -518,9 +554,9 @@ def escape_markdown(text, *, as_needed=False, ignore_links=True):
return is_url
return '\\' + groupdict['markdown']
- regex = r'(?P<markdown>[_\\~|\*`]|%s)' % _MARKDOWN_ESCAPE_COMMON
+ regex = _MARKDOWN_STOCK_REGEX
if ignore_links:
- regex = '(?:%s|%s)' % (url_regex, regex)
+ regex = '(?:%s|%s)' % (_URL_REGEX, regex)
return re.sub(regex, replacement, text, 0, re.MULTILINE)
else:
text = re.sub(r'\\', r'\\\\', text)