string_to_string/
string-to-string.rs

1use pulldown_cmark::{html, Options, Parser};
2
3fn main() {
4    let markdown_input: &str = "Hello world, this is a ~~complicated~~ *very simple* example.";
5    println!("Parsing the following markdown string:\n{}", markdown_input);
6
7    // Set up options and parser. Strikethroughs are not part of the CommonMark standard
8    // and we therefore must enable it explicitly.
9    let mut options = Options::empty();
10    options.insert(Options::ENABLE_STRIKETHROUGH);
11    let parser = Parser::new_ext(markdown_input, options);
12
13    // Write to String buffer.
14    let mut html_output: String = String::with_capacity(markdown_input.len() * 3 / 2);
15    html::push_html(&mut html_output, parser);
16
17    // Check that the output is what we expected.
18    let expected_html: &str =
19        "<p>Hello world, this is a <del>complicated</del> <em>very simple</em> example.</p>\n";
20    assert_eq!(expected_html, &html_output);
21
22    // Write result to stdout.
23    println!("\nHTML output:\n{}", &html_output);
24}