comrak - Rust (original) (raw)

Expand description

A 100% CommonMark and GFMcompatible 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");

pub use html::[format_document](html/fn.format%5Fdocument.html "fn comrak::html::format_document") as format_html;

pub use html::[format_document_with_plugins](html/fn.format%5Fdocument%5Fwith%5Fplugins.html "fn comrak::html::format_document_with_plugins") as format_html_with_plugins;

adapters

Adapter traits for plugins.

arena_tree

Included from https://github.com/SimonSapin/rust-forest/blob/5783c8be8680b84c0438638bdee07d4e4aca40ac/arena-tree/lib.rs. MIT license (per Cargo.toml).

html

HTML renderering infrastructure for the CommonMark AST, as well as helper functions. format_document and format_document_with_pluginsuse the standard formatter. Thecreate_formatter! macro allows specialisation of formatting for specific node types.

nodes

The CommonMark AST.

plugins

Plugins for enhancing the default implementation of comrak can be defined in this module.

create_formatter

Create a formatter with specialised rules for certain node types.

Anchorizer

Converts header strings to canonical, unique, but still human-readable, anchors.

Arena

An arena of objects of type T.

BrokenLinkReference

Struct to the broken link callback, containing details on the link reference which failed to find a match.

ExtensionOptions

Options to select extensions.

ExtensionOptionsBuilder

Use builder syntax to set the inputs and finish with build().

Options

Umbrella options struct.

ParseOptions

Options for parser functions.

ParseOptionsBuilder

Use builder syntax to set the inputs and finish with build().

Plugins

Umbrella plugins struct.

PluginsBuilder

Use builder syntax to set the inputs and finish with build().

RenderOptions

Options for formatter functions.

RenderOptionsBuilder

Use builder syntax to set the inputs and finish with build().

RenderPlugins

Plugins for alternative rendering.

RenderPluginsBuilder

Use builder syntax to set the inputs and finish with build().

ResolvedReference

A reference link’s resolved details.

ListStyleType

Options for bulleted list redering in markdown. See link_style in RenderOptions for more details.

WikiLinksMode

Selects between wikilinks with the title first or the URL first.

BrokenLinkCallback

The type of the callback used when a reference link is encountered with no matching reference.

URLRewriter

Trait for link and image URL rewrite extensions.

format_commonmark

Formats an AST as CommonMark, modified by the given options.

format_commonmark_with_plugins

Formats an AST as CommonMark, modified by the given options. Accepts custom plugins.

format_xml

Formats an AST as HTML, modified by the given options.

format_xml_with_plugins

Formats an AST as HTML, modified by the given options. Accepts custom plugins.

markdown_to_commonmark

Render Markdown back to CommonMark.

markdown_to_commonmark_xml

Render Markdown to CommonMark XML. See https://github.com/commonmark/commonmark-spec/blob/master/CommonMark.dtd.

markdown_to_commonmark_xml_with_plugins

Render Markdown to CommonMark XML using plugins. See https://github.com/commonmark/commonmark-spec/blob/master/CommonMark.dtd.

markdown_to_html

Render Markdown to HTML.

markdown_to_html_with_plugins

Render Markdown to HTML using plugins.

parse_document

Parse a Markdown document to an AST.

parse_document_with_broken_link_callbackDeprecated

Parse a Markdown document to an AST, specifyingParseOptions::broken_link_callback.

version

Return the version of the crate.

ComrakExtensionOptions

Legacy naming of ExtensionOptions

ComrakOptions

Legacy naming of Options

ComrakParseOptions

Legacy naming of ParseOptions

ComrakPlugins

Legacy naming of Plugins

ComrakRenderOptions

Legacy naming of RenderOptions

ComrakRenderPlugins

Legacy naming of RenderPlugins