1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-12-17 23:24:20 +00:00
royalnet/royalnet_discordpy/formatting.py

49 lines
1.6 KiB
Python
Raw Normal View History

2021-04-26 02:39:14 +00:00
def ds_markdown_format(string: str) -> str:
"""
Format a string as Discord Markdown.
The following characters will be considered control characters:
- ``\uE01B``: start bold
- ``\uE00B``: end bold
- ``\uE011``: start italic
- ``\uE001``: end italic
- ``\uE012``: start underline
- ``\uE002``: end underline
- ``\uE015``: start strike
- ``\uE005``: end strike
2022-02-22 03:49:04 +00:00
- ``\uE01F``: start spoiler
- ``\uE00F``: end spoiler
2021-04-26 02:39:14 +00:00
- ``\uE01C``: start single-line code
- ``\uE00C``: end single-line code
- ``\uE01D``: start multi-line code
- ``\uE00D``: end multi-line code
:param string: The string to format.
:return: The formatted string.
.. warning:: For now, this is a Discordpy implementation detail.
.. todo:: This may cause denial of service attacks from users!
"""
string = string.replace("*", "\\*")
string = string.replace("_", "\\_")
string = string.replace("`", "\\`")
string = string.replace("~", "\\~")
string = string.replace("\uE01B", "**")
string = string.replace("\uE00B", "**")
string = string.replace("\uE011", "_")
string = string.replace("\uE001", "_")
string = string.replace("\uE015", "~~")
string = string.replace("\uE005", "~~")
string = string.replace("\uE012", "__")
string = string.replace("\uE002", "__")
2022-02-22 03:49:04 +00:00
string = string.replace("\uE01F", "||")
string = string.replace("\uE00F", "||")
2021-04-26 02:39:14 +00:00
string = string.replace("\uE01C", "`")
string = string.replace("\uE00C", "`")
string = string.replace("\uE01D", "```")
string = string.replace("\uE00D", "```")
return string