Expand description
Structure similar to `code span` with configurable markers of variable length.
It allows you to define a custom structure with variable number of markers
(e.g. with % defined as a marker, user can write %foo% or %%%foo%%%
resulting in the same node).
You add a custom structure by using add_with function, which takes following arguments:
MARKER- marker charactermd- parser instancef- function that should return your custom Node
Here is an example of a rule turning %foo% into 🦀foo🦀:
use markdown_it::generics::inline::code_pair;
use markdown_it::{MarkdownIt, Node, NodeValue, Renderer};
#[derive(Debug)]
struct Ferris;
impl NodeValue for Ferris {
fn render(&self, node: &Node, fmt: &mut dyn Renderer) {
fmt.text("🦀");
fmt.contents(&node.children);
fmt.text("🦀");
}
}
let md = &mut MarkdownIt::new();
code_pair::add_with::<'%'>(md, |_| Node::new(Ferris));
let html = md.parse("hello %world%").render();
assert_eq!(html.trim(), "hello 🦀world🦀");This generic structure follows exact rules of code span in CommonMark:
-
Literal marker character sequence can be used inside of structure if its length doesn’t match length of the opening/closing sequence (e.g. with
%defined as a marker,%%foo%bar%%gets parsed asNode("foo%bar")). -
Single space inside is trimmed to allow you to write
% %%foo %to be parsed asNode("%%foo").
If you define two structures with the same marker, only the first one will work.