34 lines
977 B
Rust
34 lines
977 B
Rust
pub trait EscapeDiscordMarkdown {
|
|
fn escape(self) -> String;
|
|
}
|
|
|
|
impl<T> EscapeDiscordMarkdown for T
|
|
where
|
|
T: ToString,
|
|
{
|
|
fn escape(self) -> String {
|
|
let input = self.to_string();
|
|
|
|
let mut escaped = String::with_capacity(input.len());
|
|
|
|
for ch in input.chars() {
|
|
match ch {
|
|
'\\' | '*' | '_' | '~' | '`' | '>' | '|' | '[' | ']' | '(' | ')' | '#' | '+'
|
|
| '-' | '=' | '.' | '!' => {
|
|
escaped.push('\\');
|
|
escaped.push(ch);
|
|
}
|
|
_ => escaped.push(ch),
|
|
}
|
|
}
|
|
|
|
// Now block ping attempts — break mentions
|
|
escaped
|
|
.replace("@everyone", "@\u{200B}everyone")
|
|
.replace("@here", "@\u{200B}here")
|
|
.replace("<@", "<@\u{200B}") // user mentions
|
|
.replace("<@&", "<@&\u{200B}") // role mentions
|
|
.replace("<#", "<#\u{200B}") // channel mentions
|
|
}
|
|
}
|