mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-04-20 16:00:29 +00:00
parent
743a5a218f
commit
9833ea82e2
@ -474,14 +474,10 @@ class clean_content(Converter):
|
||||
result = pattern.sub(repl, argument)
|
||||
|
||||
if self.escape_markdown:
|
||||
result = re.sub(r'\\', r'\\\\', result)
|
||||
for c in ('*', '`', '_', '~', '|'):
|
||||
regex = r'\{0}(?=([\s\S]*((?<!\{0})\{0})))'.format(c)
|
||||
replace = '\{0}'.format(c)
|
||||
result = re.sub(regex, replace, result)
|
||||
result = discord.utils.escape_markdown(result)
|
||||
|
||||
# Completely ensure no mentions escape:
|
||||
return re.sub(r'@(everyone|here|[!&]?[0-9]{17,21})', '@\u200b\\1', result)
|
||||
return discord.utils.escape_mentions(result)
|
||||
|
||||
class _Greedy:
|
||||
__slots__ = ('converter',)
|
||||
|
@ -366,3 +366,43 @@ def resolve_invite(invite):
|
||||
return m.group(1)
|
||||
return invite
|
||||
|
||||
_MARKDOWN_ESCAPE_SUBREGEX = '|'.join(r'\{0}(?=([\s\S]*((?<!\{0})\{0})))'.format(c)
|
||||
for c in ('*', '`', '_', '~', '|'))
|
||||
|
||||
_MARKDOWN_ESCAPE_REGEX = re.compile('(%s)' % _MARKDOWN_ESCAPE_SUBREGEX)
|
||||
|
||||
def escape_markdown(text):
|
||||
"""A helper function that escapes Discord's markdown.
|
||||
|
||||
Parameters
|
||||
-----------
|
||||
text: :class:`str`
|
||||
The text to escape markdown from.
|
||||
|
||||
Returns
|
||||
--------
|
||||
:class:`str`
|
||||
The text with the markdown special characters escaped with a slash.
|
||||
"""
|
||||
|
||||
text = re.sub(r'\\', r'\\\\', text)
|
||||
return _MARKDOWN_ESCAPE_REGEX.sub(r'\\\1', text)
|
||||
|
||||
def escape_mentions(text):
|
||||
"""A helper function that escapes everyone, here, role, and user mentions.
|
||||
|
||||
.. note::
|
||||
|
||||
This does not include channel mentions.
|
||||
|
||||
Parameters
|
||||
-----------
|
||||
text: :class:`str`
|
||||
The text to escape mentions from.
|
||||
|
||||
Returns
|
||||
--------
|
||||
:class:`str`
|
||||
The text with the mentions removed.
|
||||
"""
|
||||
return re.sub(r'@(everyone|here|[!&]?[0-9]{17,21})', r'@\u200b\1', text)
|
||||
|
@ -583,6 +583,11 @@ Utility Functions
|
||||
|
||||
.. autofunction:: discord.utils.oauth_url
|
||||
|
||||
.. autofunction:: discord.utils.escape_markdown
|
||||
|
||||
.. autofunction:: discord.utils.escape_mentions
|
||||
|
||||
|
||||
Application Info
|
||||
------------------
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user