aboutsummaryrefslogtreecommitdiff
path: root/discord
diff options
context:
space:
mode:
Diffstat (limited to 'discord')
-rw-r--r--discord/ext/commands/converter.py9
-rw-r--r--discord/message.py6
-rw-r--r--discord/utils.py42
3 files changed, 50 insertions, 7 deletions
diff --git a/discord/ext/commands/converter.py b/discord/ext/commands/converter.py
index 850ca728..2da0e110 100644
--- a/discord/ext/commands/converter.py
+++ b/discord/ext/commands/converter.py
@@ -676,11 +676,16 @@ class clean_content(Converter):
Whether to use nicknames when transforming mentions.
escape_markdown: :class:`bool`
Whether to also escape special markdown characters.
+ remove_markdown: :class:`bool`
+ Whether to also remove special markdown characters. This option is not supported with ``escape_markdown``
+
+ .. versionadded:: 1.7
"""
- def __init__(self, *, fix_channel_mentions=False, use_nicknames=True, escape_markdown=False):
+ def __init__(self, *, fix_channel_mentions=False, use_nicknames=True, escape_markdown=False, remove_markdown=False):
self.fix_channel_mentions = fix_channel_mentions
self.use_nicknames = use_nicknames
self.escape_markdown = escape_markdown
+ self.remove_markdown = remove_markdown
async def convert(self, ctx, argument):
message = ctx.message
@@ -731,6 +736,8 @@ class clean_content(Converter):
if self.escape_markdown:
result = discord.utils.escape_markdown(result)
+ elif self.remove_markdown:
+ result = discord.utils.remove_markdown(result)
# Completely ensure no mentions escape:
return discord.utils.escape_mentions(result)
diff --git a/discord/message.py b/discord/message.py
index c681d33e..da2e9938 100644
--- a/discord/message.py
+++ b/discord/message.py
@@ -808,9 +808,9 @@ class Message(Hashable):
.. note::
- This *does not* escape markdown. If you want to escape
- markdown then use :func:`utils.escape_markdown` along
- with this function.
+ This *does not* affect markdown. If you want to escape
+ or remove markdown then use :func:`utils.escape_markdown` or :func:`utils.remove_markdown`
+ respectively, along with this function.
"""
transformations = {
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)