Struct markdown::ParseOptions

source ·
pub struct ParseOptions {
    pub constructs: Constructs,
    pub gfm_strikethrough_single_tilde: bool,
    pub math_text_single_dollar: bool,
    pub mdx_expression_parse: Option<Box<ExpressionParse>>,
    pub mdx_esm_parse: Option<Box<EsmParse>>,
}
Expand description

Configuration that describes how to parse from markdown.

You can use this:

  • To control what markdown constructs are turned on and off
  • To control some of those constructs
  • To add support for certain programming languages when parsing MDX

In most cases, you will want to use the default trait or gfm method.

Examples

use markdown::ParseOptions;

// Use the default trait to parse markdown according to `CommonMark`:
let commonmark = ParseOptions::default();

// Use the `gfm` method to parse markdown according to GFM:
let gfm = ParseOptions::gfm();

Fields§

§constructs: Constructs

Which constructs to enable and disable.

The default is to follow CommonMark.

Examples

use markdown::{to_html, to_html_with_options, Constructs, Options, ParseOptions};

// `markdown-rs` follows CommonMark by default:
assert_eq!(
    to_html("    indented code?"),
    "<pre><code>indented code?\n</code></pre>"
);

// Pass `constructs` to choose what to enable and disable:
assert_eq!(
    to_html_with_options(
        "    indented code?",
        &Options {
            parse: ParseOptions {
              constructs: Constructs {
                code_indented: false,
                ..Constructs::default()
              },
              ..ParseOptions::default()
            },
            ..Options::default()
        }
    )?,
    "<p>indented code?</p>"
);
§gfm_strikethrough_single_tilde: bool

Whether to support GFM strikethrough with a single tilde

This option does nothing if gfm_strikethrough is not turned on in constructs. This option does not affect strikethrough with double tildes.

The default is true, which follows how markdown on github.com works, as strikethrough with single tildes is supported. Pass false, to follow the GFM spec more strictly, by not allowing strikethrough with single tildes.

Examples

use markdown::{to_html_with_options, Constructs, Options, ParseOptions};

// `markdown-rs` supports single tildes by default:
assert_eq!(
    to_html_with_options(
        "~a~",
        &Options {
            parse: ParseOptions {
              constructs: Constructs::gfm(),
              ..ParseOptions::default()
            },
            ..Options::default()
        }
    )?,
    "<p><del>a</del></p>"
);

// Pass `gfm_strikethrough_single_tilde: false` to turn that off:
assert_eq!(
    to_html_with_options(
        "~a~",
        &Options {
            parse: ParseOptions {
              constructs: Constructs::gfm(),
              gfm_strikethrough_single_tilde: false,
              ..ParseOptions::default()
            },
            ..Options::default()
        }
    )?,
    "<p>~a~</p>"
);
§math_text_single_dollar: bool

Whether to support math (text) with a single dollar

This option does nothing if math_text is not turned on in constructs. This option does not affect math (text) with two or more dollars.

The default is true, which is more close to how code (text) and Pandoc work, as it allows math with a single dollar to form. However, single dollars can interfere with “normal” dollars in text. Pass false, to only allow math (text) to form when two or more dollars are used.

Examples

use markdown::{to_html_with_options, Constructs, Options, ParseOptions};

// `markdown-rs` supports single dollars by default:
assert_eq!(
    to_html_with_options(
        "$a$",
        &Options {
            parse: ParseOptions {
              constructs: Constructs {
                math_text: true,
                ..Constructs::default()
              },
              ..ParseOptions::default()
            },
            ..Options::default()
        }
    )?,
    "<p><code class=\"language-math math-inline\">a</code></p>"
);

// Pass `math_text_single_dollar: false` to turn that off:
assert_eq!(
    to_html_with_options(
        "$a$",
        &Options {
            parse: ParseOptions {
              constructs: Constructs {
                math_text: true,
                ..Constructs::default()
              },
              math_text_single_dollar: false,
              ..ParseOptions::default()
            },
            ..Options::default()
        }
    )?,
    "<p>$a$</p>"
);
§mdx_expression_parse: Option<Box<ExpressionParse>>

Function to parse expressions with.

This function can be used to add support for arbitrary programming languages within expressions.

It only makes sense to pass this when compiling to a syntax tree with to_mdast().

For an example that adds support for JavaScript with SWC, see tests/test_utils/mod.rs.

§mdx_esm_parse: Option<Box<EsmParse>>

Function to parse ESM with.

This function can be used to add support for arbitrary programming languages within ESM blocks, however, the keywords (export, import) are currently hardcoded JavaScript-specific.

👉 Note: please raise an issue if you’re interested in working on MDX that is aware of, say, Rust, or other programming languages.

It only makes sense to pass this when compiling to a syntax tree with to_mdast().

For an example that adds support for JavaScript with SWC, see tests/test_utils/mod.rs.

Implementations§

source§

impl ParseOptions

source

pub fn gfm() -> Self

GFM.

GFM stands for GitHub flavored markdown. GFM extends CommonMark and adds support for autolink literals, footnotes, strikethrough, tables, and tasklists.

For more information, see the GFM specification: https://github.github.com/gfm/

source

pub fn mdx() -> Self

MDX.

This turns on CommonMark, turns off some conflicting constructs (autolinks, code (indented), and HTML), and turns on MDX (ESM, expressions, and JSX).

For more information, see the MDX website: https://mdxjs.com.

👉 Note: to support ESM, you must pass mdx_esm_parse in ParseOptions too. Otherwise, ESM is treated as normal markdown.

You can pass mdx_expression_parse to parse expressions according to a certain grammar (typically, a programming language). Otherwise, expressions are parsed with a basic algorithm that only cares about braces.

Trait Implementations§

source§

impl Debug for ParseOptions

source§

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

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

impl Default for ParseOptions

source§

fn default() -> Self

CommonMark defaults.

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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.