Struct comrak::RenderOptions

source ·
#[non_exhaustive]
pub struct RenderOptions {
Show 14 fields pub hardbreaks: bool, pub github_pre_lang: bool, pub full_info_string: bool, pub width: usize, pub unsafe_: bool, pub escape: bool, pub list_style: ListStyleType, pub sourcepos: bool, pub experimental_inline_sourcepos: bool, pub escaped_char_spans: bool, pub ignore_setext: bool, pub ignore_empty_links: bool, pub gfm_quirks: bool, pub prefer_fenced: bool,
}
Expand description

Options for formatter functions.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§hardbreaks: bool

Soft line breaks in the input translate into hard line breaks in the output.

let mut options = Options::default();
assert_eq!(markdown_to_html("Hello.\nWorld.\n", &options),
           "<p>Hello.\nWorld.</p>\n");

options.render.hardbreaks = true;
assert_eq!(markdown_to_html("Hello.\nWorld.\n", &options),
           "<p>Hello.<br />\nWorld.</p>\n");
§github_pre_lang: bool

GitHub-style <pre lang="xyz"> is used for fenced code blocks with info tags.

let mut options = Options::default();
assert_eq!(markdown_to_html("``` rust\nfn hello();\n```\n", &options),
           "<pre><code class=\"language-rust\">fn hello();\n</code></pre>\n");

options.render.github_pre_lang = true;
assert_eq!(markdown_to_html("``` rust\nfn hello();\n```\n", &options),
           "<pre lang=\"rust\"><code>fn hello();\n</code></pre>\n");
§full_info_string: bool

Enable full info strings for code blocks

let mut options = Options::default();
assert_eq!(markdown_to_html("``` rust extra info\nfn hello();\n```\n", &options),
           "<pre><code class=\"language-rust\">fn hello();\n</code></pre>\n");

options.render.full_info_string = true;
let html = markdown_to_html("``` rust extra info\nfn hello();\n```\n", &options);
let re = regex::Regex::new(r#"data-meta="extra info""#).unwrap();
assert!(re.is_match(&html));
§width: usize

The wrap column when outputting CommonMark.

let mut options = Options::default();
let node = parse_document(&arena, "hello hello hello hello hello hello", &options);
let mut output = vec![];
format_commonmark(node, &options, &mut output).unwrap();
assert_eq!(String::from_utf8(output).unwrap(),
           "hello hello hello hello hello hello\n");

options.render.width = 20;
let mut output = vec![];
format_commonmark(node, &options, &mut output).unwrap();
assert_eq!(String::from_utf8(output).unwrap(),
           "hello hello hello\nhello hello hello\n");
§unsafe_: bool

Allow rendering of raw HTML and potentially dangerous links.

let mut options = Options::default();
let input = "<script>\nalert('xyz');\n</script>\n\n\
             Possibly <marquee>annoying</marquee>.\n\n\
             [Dangerous](javascript:alert(document.cookie)).\n\n\
             [Safe](http://commonmark.org).\n";

assert_eq!(markdown_to_html(input, &options),
           "<!-- raw HTML omitted -->\n\
            <p>Possibly <!-- raw HTML omitted -->annoying<!-- raw HTML omitted -->.</p>\n\
            <p><a href=\"\">Dangerous</a>.</p>\n\
            <p><a href=\"http://commonmark.org\">Safe</a>.</p>\n");

options.render.unsafe_ = true;
assert_eq!(markdown_to_html(input, &options),
           "<script>\nalert(\'xyz\');\n</script>\n\
            <p>Possibly <marquee>annoying</marquee>.</p>\n\
            <p><a href=\"javascript:alert(document.cookie)\">Dangerous</a>.</p>\n\
            <p><a href=\"http://commonmark.org\">Safe</a>.</p>\n");
§escape: bool

Escape raw HTML instead of clobbering it.

let mut options = Options::default();
let input = "<i>italic text</i>";

assert_eq!(markdown_to_html(input, &options),
           "<p><!-- raw HTML omitted -->italic text<!-- raw HTML omitted --></p>\n");

options.render.escape = true;
assert_eq!(markdown_to_html(input, &options),
           "<p>&lt;i&gt;italic text&lt;/i&gt;</p>\n");
§list_style: ListStyleType

Set the type of bullet list marker to use. Options are:

let mut options = Options::default();
let input = "- one\n- two\n- three";
assert_eq!(markdown_to_commonmark(input, &options),
           "- one\n- two\n- three\n"); // default is Dash

options.render.list_style = ListStyleType::Plus;
assert_eq!(markdown_to_commonmark(input, &options),
           "+ one\n+ two\n+ three\n");

options.render.list_style = ListStyleType::Star;
assert_eq!(markdown_to_commonmark(input, &options),
           "* one\n* two\n* three\n");
§sourcepos: bool

Include source position attributes in HTML and XML output.

Sourcepos information is reliable for all core block items, and most extensions. The description lists extension still has issues; see https://github.com/kivikakk/comrak/blob/3bb6d4ce/src/tests/description_lists.rs#L60-L125.

Sourcepos information is not reliable for inlines, and is not included in HTML without also setting [experimental_inline_sourcepos]. See https://github.com/kivikakk/comrak/pull/439 for a discussion.

let mut options = Options::default();
options.render.sourcepos = true;
let input = "## Hello world!";
let xml = markdown_to_commonmark_xml(input, &options);
assert!(xml.contains("<text sourcepos=\"1:4-1:15\" xml:space=\"preserve\">"));
§experimental_inline_sourcepos: bool

Include inline sourcepos in HTML output, which is known to have issues. See https://github.com/kivikakk/comrak/pull/439 for a discussion.

let mut options = Options::default();
options.render.sourcepos = true;
let input = "Hello *world*!";
assert_eq!(markdown_to_html(input, &options),
           "<p data-sourcepos=\"1:1-1:14\">Hello <em>world</em>!</p>\n");
options.render.experimental_inline_sourcepos = true;
assert_eq!(markdown_to_html(input, &options),
           "<p data-sourcepos=\"1:1-1:14\">Hello <em data-sourcepos=\"1:7-1:13\">world</em>!</p>\n");
§escaped_char_spans: bool

Wrap escaped characters in a <span> to allow any post-processing to recognize them.

let mut options = Options::default();
let input = "Notify user \\@example";

assert_eq!(markdown_to_html(input, &options),
           "<p>Notify user @example</p>\n");

options.render.escaped_char_spans = true;
assert_eq!(markdown_to_html(input, &options),
           "<p>Notify user <span data-escaped-char>@</span>example</p>\n");
§ignore_setext: bool

Ignore setext headings in input.

let mut options = Options::default();
let input = "setext heading\n---";

assert_eq!(markdown_to_html(input, &options),
           "<h2>setext heading</h2>\n");

options.render.ignore_setext = true;
assert_eq!(markdown_to_html(input, &options),
           "<p>setext heading</p>\n<hr />\n");
§ignore_empty_links: bool

Ignore empty links in input.

let mut options = Options::default();
let input = "[]()";

assert_eq!(markdown_to_html(input, &options),
           "<p><a href=\"\"></a></p>\n");

options.render.ignore_empty_links = true;
assert_eq!(markdown_to_html(input, &options), "<p>[]()</p>\n");
§gfm_quirks: bool

Enables GFM quirks in HTML output which break CommonMark compatibility.

let mut options = Options::default();
let input = "****abcd**** *_foo_*";

assert_eq!(markdown_to_html(input, &options),
           "<p><strong><strong>abcd</strong></strong> <em><em>foo</em></em></p>\n");

options.render.gfm_quirks = true;
assert_eq!(markdown_to_html(input, &options),
           "<p><strong>abcd</strong> <em><em>foo</em></em></p>\n");
§prefer_fenced: bool

Prefer fenced code blocks when outputting CommonMark.

let arena = Arena::new();
let mut options = Options::default();
let input = "```\nhello\n```\n";
let root = parse_document(&arena, input, &options);

let mut buf = Vec::new();
format_commonmark(&root, &options, &mut buf);
assert_eq!(str::from_utf8(&buf).unwrap(), "    hello\n");

buf.clear();
options.render.prefer_fenced = true;
format_commonmark(&root, &options, &mut buf);
assert_eq!(str::from_utf8(&buf).unwrap(), "```\nhello\n```\n");

Trait Implementations§

source§

impl<'arbitrary> Arbitrary<'arbitrary> for RenderOptions

source§

fn arbitrary(u: &mut Unstructured<'arbitrary>) -> Result<Self>

Generate an arbitrary value of Self from the given unstructured data. Read more
source§

fn arbitrary_take_rest(u: Unstructured<'arbitrary>) -> Result<Self>

Generate an arbitrary value of Self from the entirety of the given unstructured data. Read more
source§

fn size_hint(depth: usize) -> (usize, Option<usize>)

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
source§

impl Clone for RenderOptions

source§

fn clone(&self) -> RenderOptions

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for RenderOptions

source§

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

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

impl Default for RenderOptions

source§

fn default() -> RenderOptions

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

impl Copy for RenderOptions

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: Copy,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

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

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

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

§

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>,

§

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.