[][src]Function comrak::parse_document_with_broken_link_callback

pub fn parse_document_with_broken_link_callback<'a, 'c>(
    arena: &'a Arena<AstNode<'a>>,
    buffer: &str,
    options: &ComrakOptions,
    callback: Option<&'c mut dyn FnMut(&[u8]) -> Option<(Vec<u8>, Vec<u8>)>>
) -> &'a AstNode<'a>

Parse a Markdown document to an AST.

In case the parser encounters any potential links that have a broken reference (e.g [foo] when there is no [foo]: url entry at the bottom) the provided callback will be called with the reference name, and the returned pair will be used as the link destination and title if not None.

Note: The label provided to the callback is the normalized representation of the label as described in the GFM spec.

extern crate comrak;
use comrak::{Arena, parse_document_with_broken_link_callback, format_html, ComrakOptions};
use comrak::nodes::{AstNode, NodeValue};

// The returned nodes are created in the supplied Arena, and are bound by its lifetime.
let arena = Arena::new();

let root = parse_document_with_broken_link_callback(
    &arena,
    "# Cool input!\nWow look at this cool [link][foo]. A [broken link] renders as text.",
    &ComrakOptions::default(),
    Some(&mut |link_ref: &[u8]| match link_ref {
        b"foo" => Some((
            b"https://www.rust-lang.org/".to_vec(),
            b"The Rust Language".to_vec(),
        )),
        _ => None,
    }),
);

let mut output = Vec::new();
format_html(root, &ComrakOptions::default(), &mut output)?;
let output_str = std::str::from_utf8(&output).expect("invalid UTF-8");
assert_eq!(output_str, "<h1>Cool input!</h1>\n<p>Wow look at this cool \
                <a href=\"https://www.rust-lang.org/\" title=\"The Rust Language\">link</a>. \
                A [broken link] renders as text.</p>\n");