Crate comrak

source ·
Expand description

A 100% CommonMark and GFM compatible Markdown parser.

Source repository and detailed README is at https://github.com/kivikakk/comrak.

You can use comrak::markdown_to_html directly:

use comrak::{markdown_to_html, Options};
assert_eq!(markdown_to_html("Hello, **世界**!", &Options::default()),
           "<p>Hello, <strong>世界</strong>!</p>\n");

Or you can parse the input into an AST yourself, manipulate it, and then use your desired formatter:

use comrak::{Arena, parse_document, format_html, Options};
use comrak::nodes::{AstNode, NodeValue};

let arena = Arena::new();

let root = parse_document(
    &arena,
    "This is my input.\n\n1. Also [my](#) input.\n2. Certainly *my* input.\n",
    &Options::default());

for node in root.descendants() {
    if let NodeValue::Text(ref mut text) = node.data.borrow_mut().value {
        *text = text.replace("my", "your");
    }
}

let mut html = vec![];
format_html(root, &Options::default(), &mut html).unwrap();

assert_eq!(
    String::from_utf8(html).unwrap(),
    "<p>This is your input.</p>\n\
     <ol>\n\
     <li>Also <a href=\"#\">your</a> input.</li>\n\
     <li>Certainly <em>your</em> input.</li>\n\
     </ol>\n");

Re-exports§

Modules§

Structs§

Enums§

Functions§

Type Aliases§