-
-
Notifications
You must be signed in to change notification settings - Fork 130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(util): add markdown format functions to util #2314
Draft
HTGAzureX1212
wants to merge
4
commits into
twilight-rs:main
Choose a base branch
from
TeamHarTex:HTGAzureX1212/fmt-fns
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
//! Provides the Format trait for specifying formatting with Discord markdown for strings. | ||
|
||
/// Format is a trait specifying formatting with Discord markdown for strings. | ||
pub trait Format { | ||
/// Returns the block quote formatting for a string. | ||
#[must_use] | ||
fn block_quote(self) -> Self; | ||
|
||
/// Returns the bold formatting for a string. | ||
#[must_use] | ||
fn bold(self) -> Self; | ||
|
||
/// Returns the codeblock formatting for a string. | ||
#[must_use] | ||
fn codeblock(self, language: &str) -> Self; | ||
|
||
/// Returns the H1 formatting for a string. | ||
#[must_use] | ||
fn h1(self) -> Self; | ||
|
||
/// Returns the H2 formatting for a string. | ||
#[must_use] | ||
fn h2(self) -> Self; | ||
|
||
/// Returns the H3 formatting for a string. | ||
#[must_use] | ||
fn h3(self) -> Self; | ||
|
||
/// Returns the inline code formatting for a string. | ||
#[must_use] | ||
fn inline_code(self) -> Self; | ||
|
||
/// Returns the italic formatting for a string. | ||
#[must_use] | ||
fn italic(self) -> Self; | ||
|
||
/// Returns the quote formatting for a string. | ||
#[must_use] | ||
fn line_quote(self) -> Self; | ||
|
||
/// Returns the masked links formatting for a string. | ||
/// | ||
/// This assumes `self` being the URL to be masked. | ||
#[must_use] | ||
fn masked_links(self, text: &str) -> Self; | ||
|
||
/// Returns the relative timestamp formatting for a string. | ||
#[must_use] | ||
fn relative_timestamp(self) -> Self; | ||
|
||
/// Returns the underline formatting for a string. | ||
#[must_use] | ||
fn underline(self) -> Self; | ||
|
||
/// Returns the spoiler formatting for a string. | ||
#[must_use] | ||
fn spoiler(self) -> Self; | ||
|
||
/// Returns the strikethrough formatting for a string. | ||
#[must_use] | ||
fn strikethrough(self) -> Self; | ||
} | ||
|
||
impl Format for String { | ||
fn block_quote(self) -> Self { | ||
format!(">>> {self}") | ||
} | ||
|
||
fn bold(self) -> Self { | ||
format!("**{self}**") | ||
} | ||
|
||
fn codeblock(self, language: &str) -> Self { | ||
format!("```{language}\n{self}```") | ||
} | ||
|
||
fn h1(self) -> Self { | ||
format!("# {self}") | ||
} | ||
|
||
fn h2(self) -> Self { | ||
format!("## {self}") | ||
} | ||
|
||
fn h3(self) -> Self { | ||
format!("### {self}") | ||
} | ||
|
||
fn inline_code(self) -> Self { | ||
format!("`{self}`") | ||
} | ||
|
||
fn italic(self) -> Self { | ||
format!("*{self}*") | ||
} | ||
|
||
fn line_quote(self) -> Self { | ||
format!("> {self}") | ||
} | ||
|
||
fn masked_links(self, text: &str) -> Self { | ||
format!("[{text}]({self})") | ||
} | ||
|
||
fn relative_timestamp(self) -> Self { | ||
format!("<t:{self}:R>") | ||
} | ||
HTGAzureX1212 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
fn underline(self) -> Self { | ||
format!("__{self}__") | ||
} | ||
|
||
fn spoiler(self) -> Self { | ||
format!("||{self}||") | ||
} | ||
|
||
fn strikethrough(self) -> Self { | ||
format!("~~{self}~~") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -15,6 +15,9 @@ | |||||
#[cfg(feature = "builder")] | ||||||
pub mod builder; | ||||||
|
||||||
#[cfg(feature = "format")] | ||||||
pub mod format; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
#[cfg(feature = "link")] | ||||||
pub mod link; | ||||||
|
||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer formatters like
twilight_mention::fmt::MentionFormat
andtwilight_model::user::DiscriminatorDisplay
over extension methods.