Add header, hyperlink and subtext utilities (#6102)

Co-authored-by: Kowlin <10947836+Kowlin@users.noreply.github.com>
This commit is contained in:
Kreusada 2024-09-01 22:40:12 +01:00 committed by GitHub
parent 907a3f7561
commit 05cf9b7f39
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,7 +5,7 @@ import itertools
import math import math
import textwrap import textwrap
from io import BytesIO from io import BytesIO
from typing import Iterator, List, Optional, Sequence, SupportsInt, Union from typing import Iterator, List, Literal, Optional, Sequence, SupportsInt, Union
import discord import discord
from babel.lists import format_list as babel_list from babel.lists import format_list as babel_list
@ -21,11 +21,14 @@ __all__ = (
"question", "question",
"bold", "bold",
"box", "box",
"header",
"hyperlink",
"inline", "inline",
"italics", "italics",
"spoiler", "spoiler",
"pagify", "pagify",
"strikethrough", "strikethrough",
"subtext",
"underline", "underline",
"quote", "quote",
"escape", "escape",
@ -39,6 +42,69 @@ __all__ = (
_ = Translator("UtilsChatFormatting", __file__) _ = Translator("UtilsChatFormatting", __file__)
def hyperlink(text: str, url: str) -> str:
"""Create hyperlink markdown with text and a URL.
Parameters
----------
text : str
The text which will contain the link.
url : str
The URL used for the hyperlink.
Returns
-------
str
The new message.
"""
return f"[{text}]({url})"
def header(text: str, size: Literal["small", "medium", "large"]) -> str:
"""Formats a header.
Parameters
----------
text : str
The text for the header.
url : Literal['small', 'medium', 'large']
The size of the header ('small', 'medium' or 'large')
Returns
-------
str
The new message.
"""
if size == "small":
multiplier = 3
elif size == "medium":
multiplier = 2
elif size == "large":
multiplier = 1
else:
raise ValueError(f"Invalid size '{size}'")
return "#" * multiplier + " " + text
def subtext(text: str) -> str:
"""Formats subtext from the given text.
Parameters
----------
text : str
The text to format as subtext.
Returns
-------
str
The new message.
"""
return "-# " + text
def error(text: str) -> str: def error(text: str) -> str:
"""Get text prefixed with an error emoji. """Get text prefixed with an error emoji.