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

        def replacement(match):
            groupdict = match.groupdict()
            is_url = groupdict.get('url')
            if is_url:
                return is_url
            return ('\\' + groupdict['markdown'])
        regex = _MARKDOWN_STOCK_REGEX
        if ignore_links:
            regex = f'(?:{_URL_REGEX}|{regex})'
        return re.sub(regex, replacement, text, 0, re.MULTILINE)
    else:
        text = re.sub('\\\\', '\\\\\\\\', text)
        return _MARKDOWN_ESCAPE_REGEX.sub('\\\\\\1', text)
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.
def escape_markdown(text: str, *, as_needed: bool=False, ignore_links: bool=True) -> str:
    "A helper function that escapes Discord's markdown.\n\n    Parameters\n    -----------\n    text: :class:`str`\n        The text to escape markdown from.\n    as_needed: :class:`bool`\n        Whether to escape the markdown characters as needed. This\n        means that it does not escape extraneous characters if it's\n        not necessary, e.g. ``**hello**`` is escaped into ``\\*\\*hello**``\n        instead of ``\\*\\*hello\\*\\*``. Note however that this can open\n        you up to some clever syntax abuse. Defaults to ``False``.\n    ignore_links: :class:`bool`\n        Whether to leave links alone when escaping markdown. For example,\n        if a URL in the text contains characters such as ``_`` then it will\n        be left alone. This option is not supported with ``as_needed``.\n        Defaults to ``True``.\n\n    Returns\n    --------\n    :class:`str`\n        The text with the markdown special characters escaped with a slash.\n    "
    if (not as_needed):

        def replacement(match):
            groupdict = match.groupdict()
            is_url = groupdict.get('url')
            if is_url:
                return is_url
            return ('\\' + groupdict['markdown'])
        regex = _MARKDOWN_STOCK_REGEX
        if ignore_links:
            regex = f'(?:{_URL_REGEX}|{regex})'
        return re.sub(regex, replacement, text, 0, re.MULTILINE)
    else:
        text = re.sub('\\\\', '\\\\\\\\', text)
        return _MARKDOWN_ESCAPE_REGEX.sub('\\\\\\1', text)