def escape_markdown(text, *, as_needed=False, ignore_links=True):
if not as_needed:<EOL><INDENT>url_regex = r'<STR_LIT>'<EOL>def replacement(match):<EOL><INDENT>groupdict = match.groupdict()<EOL>is_url = groupdict.get('<STR_LIT:url>')<EOL>if is_url:<EOL><INDENT>return is_url<EOL><DEDENT>return '<STR_LIT:\\>' + groupdict['<STR_LIT>']<EOL><DEDENT>regex = r'<STR_LIT>'<EOL>if ignore_links:<EOL><INDENT>regex = '<STR_LIT>' % (url_regex, regex)<EOL><DEDENT>return re.sub(regex, replacement, text)<EOL><DEDENT>else:<EOL><INDENT>text = re.sub(r'<STR_LIT:\\>', r'<STR_LIT>', text)<EOL>return _MARKDOWN_ESCAPE_REGEX.sub(r'<STR_LIT>', text)<EOL><DEDENT>
r"""A helper function that escapes Discord's markdown.

    Parameters
    -----------
    text: :class:`str`
        The text to escape markdown from.
    as_needed: :class:`bool`
        Whether to escape the markdown characters as needed. This
        means that it does not escape extraneous characters if it's
        not necessary, e.g. ``**hello**`` is escaped into ``\*\*hello**``
        instead of ``\*\*hello\*\*``. Note however that this can open
        you up to some clever syntax abuse. Defaults to ``False``.
    ignore_links: :class:`bool`
        Whether to leave links alone when escaping markdown. For example,
        if a URL in the text contains characters such as ``_`` then it will
        be left alone. This option is not supported with ``as_needed``.
        Defaults to ``True``.

    Returns
    --------
    :class:`str`
        The text with the markdown special characters escaped with a slash.