From 31ee3fafc17c552fa972dfa9bd0fc38915538684 Mon Sep 17 00:00:00 2001 From: Edwin <60476129+Edwinexd@users.noreply.github.com> Date: Mon, 29 Mar 2021 00:38:34 +0200 Subject: Add remove_markdown helper function --- discord/utils.py | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) (limited to 'discord/utils.py') 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%s|%s)' % (_MARKDOWN_ESCAPE_SUBREGEX, _MARKDOWN_ESCAPE_COMMON), re.MULTILINE) +_URL_REGEX = r'(?P<[^: >]+:\/[^ >]+>|(?:https?|steam):\/\/[^\s<]+[^<.,:;\"\'\]\s])' + +_MARKDOWN_STOCK_REGEX = r'(?P[_\\~|\*`]|%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<[^: >]+:\/[^ >]+>|(?: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[_\\~|\*`]|%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) -- cgit v1.2.3