diff options
Diffstat (limited to 'discord/utils.py')
| -rw-r--r-- | discord/utils.py | 42 |
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) |