Struct Render

pub struct Render { 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 escaped_char_spans: bool, pub ignore_empty_links: bool, pub gfm_quirks: bool, pub prefer_fenced: bool, pub figure_with_caption: bool, pub tasklist_classes: bool, pub alert_style: AlertStyleType, pub ol_width: usize, pub experimental_minimize_commonmark: bool, pub compact_html: bool }

Options for formatter functions.

Fields

hardbreaks: bool

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

# use comrak::{markdown_to_html, Options};
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.

# use comrak::{markdown_to_html, Options};
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

# use comrak::{markdown_to_html, Options};
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);
eprintln!("{}", html);
assert!(html.contains(r#"data-meta="extra info""#));
width: usize

The wrap column when outputting CommonMark.

# use comrak::{Arena, parse_document, Options, format_commonmark};
# fn main() {
# let arena = Arena::new();
let mut options = Options::default();
let node = parse_document(&arena, "hello hello hello hello hello hello", &options);
let mut output = String::new();
format_commonmark(node, &options, &mut output).unwrap();
assert_eq!(output,
           "hello hello hello hello hello hello\n");

options.render.width = 20;
let mut output = String::new();
format_commonmark(node, &options, &mut output).unwrap();
assert_eq!(output,
           "hello hello hello\nhello hello hello\n");
# }
unsafe: bool

Allow rendering of raw HTML and potentially dangerous links.

# use comrak::{markdown_to_html, Options};
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.r#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.

# use comrak::{markdown_to_html, Options};
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:

# use comrak::{markdown_to_commonmark, Options, options::ListStyleType};
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 core block items excluding lists and list items, all inlines, 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.

# use comrak::{markdown_to_html, Options};
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 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.

# use comrak::{markdown_to_html, Options};
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");

Enabling this option will cause the escaped_char_spans parse option to be enabled.

ignore_empty_links: bool

Ignore empty links in input.

# use comrak::{markdown_to_html, Options};
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.

# use comrak::{markdown_to_html, Options};
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.

# use std::str;
# use comrak::{Arena, Options, format_commonmark, parse_document};
let arena = Arena::new();
let mut options = Options::default();
let input = "```\nhello\n```\n";
let root = parse_document(&arena, input, &options);

let mut buf = String::new();
format_commonmark(&root, &options, &mut buf);
assert_eq!(buf, "    hello\n");

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

Render the image as a figure element with the title as its caption.

# use comrak::{markdown_to_html, Options};
let mut options = Options::default();
let input = "![image](https://example.com/image.png \"this is an image\")";

assert_eq!(markdown_to_html(input, &options),
           "<p><img src=\"https://example.com/image.png\" alt=\"image\" title=\"this is an image\" /></p>\n");

options.render.figure_with_caption = true;
assert_eq!(markdown_to_html(input, &options),
           "<p><figure><img src=\"https://example.com/image.png\" alt=\"image\" title=\"this is an image\" /><figcaption>this is an image</figcaption></figure></p>\n");
tasklist_classes: bool

Add classes to the output of the tasklist extension. This allows tasklists to be styled.

# use comrak::{markdown_to_html, Options};
let mut options = Options::default();
options.extension.tasklist = true;
let input = "- [ ] Foo";

assert_eq!(markdown_to_html(input, &options),
           "<ul>\n<li><input type=\"checkbox\" disabled=\"\" /> Foo</li>\n</ul>\n");

options.render.tasklist_classes = true;
assert_eq!(markdown_to_html(input, &options),
           "<ul class=\"contains-task-list\">\n<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" disabled=\"\" /> Foo</li>\n</ul>\n");
alert_style: AlertStyleType

How to render alert blocks. Options are:

# use comrak::{markdown_to_html, Options, options::AlertStyleType};
let mut options = Options::default();
options.extension.alerts = true;
options.render.alert_style = AlertStyleType::Semantic;
assert_eq!(markdown_to_html("> [!note]\n> Something of note", &options),
           "<aside class=\"admonition note\">\n<p class=\"admonition-title\">Note</p>\n<p>Something of note</p>\n</aside>\n");
ol_width: usize

Render ordered list with a minimum marker width. Having a width lower than 3 doesn't do anything.

# use comrak::{markdown_to_commonmark, Options};
let mut options = Options::default();
let input = "1. Something";

assert_eq!(markdown_to_commonmark(input, &options),
           "1. Something\n");

options.render.ol_width = 5;
assert_eq!(markdown_to_commonmark(input, &options),
           "1.   Something\n");
experimental_minimize_commonmark: bool

Minimise escapes used in CommonMark output (-t commonmark) by removing each individually and seeing if the resulting document roundtrips. Brute-force and expensive, but produces nicer output. Note that the result may not in fact be minimal.

# use comrak::{markdown_to_commonmark, Options};
let mut options = Options::default();
let input = "__hi";

assert_eq!(markdown_to_commonmark(input, &options),
           "\\_\\_hi\n");

options.render.experimental_minimize_commonmark = true;
assert_eq!(markdown_to_commonmark(input, &options),
           "__hi\n");
compact_html: bool

Suppress pretty-printing newlines between block-level HTML elements.

Normally comrak puts a \n after closing tags like </p>, </li>, etc. With this option on, those newlines are omitted.

# use comrak::{markdown_to_html, Options};
let mut options = Options::default();
assert_eq!(markdown_to_html("# Hello\n\nWorld.\n", &options),
           "<h1>Hello</h1>\n<p>World.</p>\n");

options.render.compact_html = true;
assert_eq!(markdown_to_html("# Hello\n\nWorld.\n", &options),
           "<h1>Hello</h1><p>World.</p>");

Trait Implementations

impl Clone for Render

fn clone(&self) -> Self

impl Copy for Render

impl Debug for Render

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

impl Default for Render

fn default() -> Self

Auto Trait Implementations

impl Freeze for Render

impl RefUnwindSafe for Render

impl Send for Render

impl Sync for Render

impl Unpin for Render

impl UnsafeUnpin for Render

impl UnwindSafe for Render

Blanket Implementations

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

fn type_id(&self) -> TypeId

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

fn borrow(&self) -> &T

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

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

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

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

impl<T> From<T> for Render

fn from(t: T) -> T

Returns the argument unchanged.

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

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

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

fn into(self) -> U

Calls U::from(self).

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

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

type Error = never;
fn try_from(value: U) -> Result<T, never>

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

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