Module markdown_it::examples::ferris
source · [−]Expand description
This is an example of how you can make your own plugins in markdown-it.
What is it
There are 3 different plugins here:
-
inline rule - turns
(\/)into🦀in inline context (i.e. inside other text) -
block rule - turns
(\/)-------(\/)into ferris.svg in block context (i.e. it has to occupy the entire line) -
core rule - counts the number of nodes created by the above two plugins and writes that number at the end of the document
It represents three stages of markdown processing (block elements, inline elements and AST post-processing).
How to use
cargo run --example ferris
Implementation
See core_rule, block_rule and inline_rule below for implementations.
// Adds three custom plugins and runs them.
mod block_rule;
mod core_rule;
mod inline_rule;
fn main() {
// create markdown parser
let md = &mut markdown_it::MarkdownIt::new();
// add commonmark syntax, you almost always want to do that
markdown_it::plugins::cmark::add(md);
// add custom three rules described above
inline_rule::add(md);
block_rule::add(md);
core_rule::add(md);
// and now you can use it
let html = md.parse(r#"
(\/) hello world (\/)
(\/)-------------(\/)
"#).render();
print!("{}", html);
assert_eq!(html.trim(), r#"
<p><span class="ferris-inline">🦀</span> hello world <span class="ferris-inline">🦀</span></p>
<div class="ferris-block"><img src="https://upload.wikimedia.org/wikipedia/commons/0/0f/Original_Ferris.svg"></div>
<footer class="ferris-counter">There are 3 crabs lurking in this document.</footer>
"#.trim());
}Modules
Replaces (\/)-------(\/) with a nice picture.
Counts the number of crabs lurking around.
Replaces (\/) with 🦀.