Markdown

RecipeCratesCategories
comrakcomrakcat-parser-implementations
markdownmarkdowncat-parser-implementations
pulldown-cmarkpulldown-cmarkcat-parser-implementations

pulldown-cmark is CommonMark compliant. comrak is another popular choice.

pulldown-cmark

pulldown-cmark pulldown-cmark-crates.io pulldown-cmark-github pulldown-cmark-lib.rs cat-text-processing

pulldown-cmark is a pull parser for CommonMark.

//! Example demonstrating the use of `pulldown-cmark` to parse Markdown and
//! convert it to HTML.
//!
//! `pulldown-cmark` is a parser for CommonMark, a standard dialect of Markdown.
//! It uses an event-based parsing model. This means it generates a stream of
//! events representing the Markdown structure, which can then be used to
//! generate the HTML.

use pulldown_cmark::Options;
/// The `Parser` struct is the core component of `pulldown-cmark`. It takes
/// a Markdown string as input and produces a stream of events representing
/// the parsed Markdown structure.
use pulldown_cmark::Parser;
use pulldown_cmark::html;

fn main() {
    let markdown_input = r#"
Hello, Pulldown CMark!

This is a simple paragraph with **bold** and *italic* text.

* List item 1
* List item 2

```rust
fn example() {
    println!("Hello from Rust!");
}

"#; // Set up parser options. // // You can enable or disable various Markdown extensions through the // Options struct, giving you fine-grained control over the parsing // process. Here, we enable all common extensions. let options = Options::all();

// `Parser::new_ext(markdown_input, options)` creates a `Parser` instance.
let parser = Parser::new_ext(markdown_input, options);

// Render the parser output to HTML.
// `html::push_html(&mut html_output, parser)` takes the parser and an empty
// `String` (`html_output`) and renders the Markdown content as HTML. The
// `push_html` function iterates through the parser's events and appends the
// corresponding HTML to the output string.
let mut html_output = String::new();
html::push_html(&mut html_output, parser);

println!("{}", html_output);

}


## `markdown` {#markdown}

[![markdown][c-markdown-badge]][c-markdown] [![markdown-crates.io][c-markdown-crates.io-badge]][c-markdown-crates.io] [![markdown-github][c-markdown-github-badge]][c-markdown-github] [![markdown-lib.rs][c-markdown-lib.rs-badge]][c-markdown-lib.rs]<a name="a008"></a><a name="a009"></a><a name="a010"></a><a name="a011"></a><a name="a012"></a><a name="a013"></a> [![cat-compilers][cat-compilers-badge]][cat-compilers]<a name="a014"></a> [![cat-encoding][cat-encoding-badge]][cat-encoding]<a name="a015"></a> [![cat-parser-implementations][cat-parser-implementations-badge]][cat-parser-implementations]<a name="a016"></a> [![cat-parsing][cat-parsing-badge]][cat-parsing]<a name="a017"></a> [![cat-text-processing][cat-text-processing-badge]][cat-text-processing]<a name="a018"></a>

[`markdown`][c-markdown]⮳<a name="a019"></a> is a CommonMark compliant markdown parser in Rust with ASTs and extensions.

```rust,editable
//! This example demonstrates how to use the `markdown` crate to parse Markdown
//! text and convert it to HTML.
//!
//! `markdown` is a simple native Rust library for parsing Markdown and
//! outputting HTML.
//!
//! Add to your `Cargo.toml`:
//! ```toml
//! [dependencies]
//! markdown = "0.3" # You may also try 1.0.0-alpha.xx
//! ```
use markdown::to_html;

fn main() {
    let markdown_text = r#"
# Hello, Markdown!

This is a simple paragraph with **bold** and *italic* text.

* List item 1
* List item 2
"#;

    let html_output = to_html(markdown_text);
    // Also consider using: `markdown::file_to_html`, `markdown::tokenize`.

    println!("{}", html_output);
}

comrak

comrak comrak-crates.io comrak-github comrak-lib.rs cat-command-line-utilities cat-parsing cat-text-processing

comrak is a 100% CommonMark-compatible GitHub Flavored Markdown parser and formatter.

//! `comrak` is a CommonMark and GitHub-flavored Markdown compatible parser.
use comrak::Arena;
use comrak::ComrakOptions;
use comrak::Options;
use comrak::format_html;
use comrak::markdown_to_html;
use comrak::nodes::NodeValue;
use comrak::parse_document;

/// Converts markdown to HTML.
fn to_html() {
    let markdown_input =
        "# Hello world\n\nThis is **bold** text and *italic* text.";
    let html_output =
        markdown_to_html(markdown_input, &ComrakOptions::default());

    println!("{}", html_output);
}

/// Replaces all instances of `orig_string` with `replacement` in the
/// markdown document.
///
/// # Arguments
/// * `document` - The markdown document to modify.
/// * `orig_string` - The string to replace.
fn replace_text(
    document: &str,
    orig_string: &str,
    replacement: &str,
) -> String {
    // The returned nodes are created in the supplied `Arena`, and are bound by
    // its lifetime.
    let arena = Arena::new();

    // Parse the document into a root `AstNode`.
    let root = parse_document(&arena, document, &Options::default());

    // Iterate over all the descendants of root, looking for text nodes.
    for node in root.descendants() {
        if let NodeValue::Text(ref mut text) = node.data.borrow_mut().value {
            // If the node is a text node, perform the string replacement.
            *text = text.replace(orig_string, replacement);
        }
    }

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

    String::from_utf8(html).unwrap()
}

fn main() {
    to_html();

    let doc = "This is my input.\n\n1. Also [my](#) input.\n2. Certainly *my* input.\n";
    let orig = "my";
    let repl = "your";
    let html = replace_text(doc, orig, repl);

    println!("{}", html);
}