Crate pulldown_cmark

Source
Expand description

Pull parser for CommonMark. This crate provides a Parser struct which is an iterator over Events. This iterator can be used directly, or to output HTML using the HTML module.

By default, only CommonMark features are enabled. To use extensions like tables, footnotes or task lists, enable them by setting the corresponding flags in the Options struct.

§Example

use pulldown_cmark::{Parser, Options};

let markdown_input = "Hello world, this is a ~~complicated~~ *very simple* example.";

// Set up options and parser. Strikethroughs are not part of the CommonMark standard
// and we therefore must enable it explicitly.
let mut options = Options::empty();
options.insert(Options::ENABLE_STRIKETHROUGH);
let parser = Parser::new_ext(markdown_input, options);

// Write to String buffer.
let mut html_output = String::new();
pulldown_cmark::html::push_html(&mut html_output, parser);

// Check that the output is what we expected.
let expected_html = "<p>Hello world, this is a <del>complicated</del> <em>very simple</em> example.</p>\n";
assert_eq!(expected_html, &html_output);

Note that consecutive text events can happen due to the manner in which the parser evaluates the source. A utility TextMergeStream exists to improve the comfort of iterating the events:

use pulldown_cmark::{Event, Parser, TextMergeStream};

let markdown_input = "Hello world, this is a ~~complicated~~ *very simple* example.";

let iterator = TextMergeStream::new(Parser::new(markdown_input));

for event in iterator {
    match event {
        Event::Text(text) => println!("{}", text),
        _ => {}
    }
}

Re-exports§

pub use crate::utils::*;

Modules§

html
HTML renderer that takes an iterator of events as input.
utils
Miscellaneous utilities to increase comfort. Special thanks to:

Structs§

BrokenLink
DefaultBrokenLinkCallback
Broken link callback that does nothing.
InlineStr
An inline string that can contain almost three words of utf-8 text.
InvalidHeadingLevel
Returned when trying to convert a usize into a Heading but it fails because the usize isn’t a valid heading level
OffsetIter
Markdown event and source range iterator.
Options
Option struct containing flags for enabling extra features that are not part of the CommonMark spec.
Parser
Markdown event iterator.
RefDefs
Keeps track of the reference definitions defined in the document.

Enums§

Alignment
Table column text alignment.
BlockQuoteKind
BlockQuote kind (Note, Tip, Important, Warning, Caution).
CodeBlockKind
Codeblock kind.
CowStr
A copy-on-write string that can be owned, borrowed or inlined.
Event
Markdown events that are generated in a preorder traversal of the document tree, with additional End events whenever all of an inner node’s children have been visited.
HeadingLevel
LinkType
Type specifier for inline links. See the Tag::Link for more information.
MetadataBlockKind
Metadata block kind.
Tag
Tags for elements that can contain other elements.
TagEnd
The end of a Tag.

Traits§

BrokenLinkCallback
Trait for broken link callbacks.