Skip to main content

EscapeOptions

Struct EscapeOptions 

Source
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 (**) and underline (__) escape a run two characters at a time, italic a 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_content or inline_code_content leaves 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:// or https://, 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

Source

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.

Source

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).

Source

pub const fn code_block(self, on: bool) -> Self

Escapes runs of three or more backticks (code blocks). Default: on.

Source

pub const fn inline_code(self, on: bool) -> Self

Escapes runs of one or two backticks (inline code). Default: on.

Source

pub const fn bold(self, on: bool) -> Self

Escapes **. Default: on.

Source

pub const fn italic(self, on: bool) -> Self

Escapes a single * or _. Default: on.

Source

pub const fn underline(self, on: bool) -> Self

Escapes __. Default: on.

Source

pub const fn strikethrough(self, on: bool) -> Self

Escapes ~~. Default: on.

Source

pub const fn spoiler(self, on: bool) -> Self

Escapes ||. Default: on.

Source

pub const fn code_block_content(self, on: bool) -> Self

Escapes the text inside code blocks too; off leaves it as written. Default: on.

Source

pub const fn inline_code_content(self, on: bool) -> Self

Escapes the text inside inline code too; off leaves it as written. Default: on.

Source

pub const fn backslash(self, on: bool) -> Self

Doubles backslashes. Default: on. (discord.js: the escape option.)

Source

pub const fn heading(self, on: bool) -> Self

Escapes the # of a heading at the start of a line. Default: off.

Source

pub const fn bulleted_list(self, on: bool) -> Self

Escapes a - or * bullet at the start of a line. Default: off.

Source

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.

Escapes the [ of a masked link [text](url). Default: off.

Source

pub fn escape<'a>(&self, text: &'a str) -> Cow<'a, str>

Escapes the enabled kinds of markdown in text, borrowing it when nothing needs escaping. See the type docs for the rules.

discord.js counterpart: escapeMarkdown(text, options).

Trait Implementations§

Source§

impl Clone for EscapeOptions

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for EscapeOptions

Source§

impl Debug for EscapeOptions

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for EscapeOptions

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for EscapeOptions

Source§

impl Hash for EscapeOptions

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for EscapeOptions

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for EscapeOptions

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.