pub struct EscapeOptions { /* private fields */ }Expand description
Which kinds of markdown escape neutralizes.
new (and Default) gives discord.js’ defaults: everything except
headings, lists and masked links; none starts from nothing. Each setter
turns one kind on or off.
§Rules
Escaping puts a backslash before the characters that would start formatting. The rules:
- Asterisks and underscores are escaped by run:
bold(**) andunderline(__) escape a run two characters at a time,italica single one — a run of one, or the first of an odd run. With both (the default) every character of the run is escaped. - Tildes and bars are escaped in pairs (
~~for strikethrough,||for spoilers); a single~or|formats nothing and is kept. - Backticks: runs of one or two are escaped for inline code,
runs of three or more for code blocks. Turning off
code_block_contentorinline_code_contentleaves the text between two delimiters untouched. - Backslashes are doubled (
backslash). - At the start of a line, on request: a heading’s
#, a bullet-or*, a numbered list’s.; and anywhere on request the[of a masked link[text](url). - Links and tokens are copied as they are: a link (
http://orhttps://, up to the next whitespace) and a<…>token on one line — a mention, emoji, timestamp, guild navigation, e-mail, phone or hidden link: content starting with@,#,+or containing:or@without whitespace, or a command mention</name …:id>. Escaping them would break them.
For ordinary text the result is the same as discord.js’. In unusual input it differs on
purpose: every backtick of a long run is escaped (discord.js leaves the fourth of four
alone), every masked link of a line is escaped (discord.js only the first), text after a
custom emoji on the same line is still escaped (discord.js stops escaping _ there), and
nothing inside a link is touched (discord.js escapes ** and backslashes in links).
discord.js counterpart: EscapeMarkdownOptions.
§Examples
use djs_formatters::EscapeOptions;
let options = EscapeOptions::new().heading(true).code_block_content(false);
assert_eq!(options.escape("# **hi**\n```**raw**```"), "\\# \\*\\*hi\\*\\*\n\\`\\`\\`**raw**\\`\\`\\`");Implementations§
Source§impl EscapeOptions
impl EscapeOptions
Sourcepub const fn new() -> Self
pub const fn new() -> Self
discord.js’ defaults: code, emphasis, strikethrough, spoilers and backslashes are escaped, also inside code; headings, lists and masked links are not.
Sourcepub const fn none() -> Self
pub const fn none() -> Self
Nothing escaped, but code content not skipped: a start for escaping a few kinds.
discord.js counterpart: EscapeMarkdownOptions with every option false (@discordjs/formatters).
Sourcepub const fn code_block(self, on: bool) -> Self
pub const fn code_block(self, on: bool) -> Self
Escapes runs of three or more backticks (code blocks). Default: on.
Sourcepub const fn inline_code(self, on: bool) -> Self
pub const fn inline_code(self, on: bool) -> Self
Escapes runs of one or two backticks (inline code). Default: on.
Sourcepub const fn strikethrough(self, on: bool) -> Self
pub const fn strikethrough(self, on: bool) -> Self
Escapes ~~. Default: on.
Sourcepub const fn code_block_content(self, on: bool) -> Self
pub const fn code_block_content(self, on: bool) -> Self
Escapes the text inside code blocks too; off leaves it as written. Default: on.
Sourcepub const fn inline_code_content(self, on: bool) -> Self
pub const fn inline_code_content(self, on: bool) -> Self
Escapes the text inside inline code too; off leaves it as written. Default: on.
Sourcepub const fn backslash(self, on: bool) -> Self
pub const fn backslash(self, on: bool) -> Self
Doubles backslashes. Default: on. (discord.js: the escape option.)
Sourcepub const fn heading(self, on: bool) -> Self
pub const fn heading(self, on: bool) -> Self
Escapes the # of a heading at the start of a line. Default: off.
Sourcepub const fn bulleted_list(self, on: bool) -> Self
pub const fn bulleted_list(self, on: bool) -> Self
Escapes a - or * bullet at the start of a line. Default: off.
Sourcepub const fn numbered_list(self, on: bool) -> Self
pub const fn numbered_list(self, on: bool) -> Self
Escapes the . of a numbered list item (1.) at the start of a line. Default: off.
Sourcepub const fn masked_link(self, on: bool) -> Self
pub const fn masked_link(self, on: bool) -> Self
Escapes the [ of a masked link [text](url). Default: off.