Module emph_pair

Source
Expand description

Structure similar to *emphasis* with configurable markers of fixed length.

There are many structures in various Markdown flavors that can be implemented with this, namely:

  • *emphasis* or _emphasis_ -> <em>emphasis</em>
  • **strong** or __strong__ -> <strong>strong</strong>
  • ~~strikethrough~~ -> <s>strikethrough</s>
  • ==marked== -> <mark>marked</mark>
  • ++inserted++ -> <ins>inserted</ins>
  • ~subscript~ -> <sub>subscript</sub>
  • ^superscript^ -> <sup>superscript</sup>

You add a custom structure by using add_with function, which takes following arguments:

  • MARKER - marker character
  • LENGTH - length of the opening/closing marker (can be 1, 2 or 3)
  • CAN_SPLIT_WORD - whether this structure can be found in the middle of the word (for example, note the difference between foo*bar*baz and foo_bar_baz in CommonMark - first one is an emphasis, second one isn’t)
  • md - parser instance
  • f - function that should return your custom Node

Here is an example of implementing superscript in your custom code:

use markdown_that::generics::inline::emph_pair;
use markdown_that::{MarkdownThat, Node, NodeValue, Renderer};

#[derive(Debug)]
struct Superscript;
impl NodeValue for Superscript {
    fn render(&self, node: &Node, fmt: &mut dyn Renderer) {
        fmt.open("sup", &node.attrs);
        fmt.contents(&node.children);
        fmt.close("sup");
    }
}

let md = &mut MarkdownThat::new();
emph_pair::add_with::<'^', 1, true>(md, || Node::new(Superscript));

let html = md.parse("e^iπ^+1=0").render();
assert_eq!(html.trim(), "e<sup>iπ</sup>+1=0");

Note that these structures have lower priority than the rest of the rules, e.g. *foo`bar*baz` is parsed as *foo<code>bar*baz</code>.

Functions§

add_with