Module code_pair

Source
Expand description

Structure similar to `code span` with configurable markers of variable length.

It allows you to define a custom structure with a variable number of markers (e.g., with % defined as a marker, the user can write %foo% or %%%foo%%% resulting in the same node).

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

  • MARKER - marker character
  • md - parser instance
  • f - function that should return your custom Node

Here is an example of a rule turning %foo% into 🦀foo🦀:

use markdown_that::generics::inline::code_pair;
use markdown_that::{MarkdownThat, 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 MarkdownThat::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 the exact rules of code span in CommonMark:

  1. Literal marker character sequence can be used inside a structure if its length doesn’t match the length of the opening/closing sequence (e.g., with % defined as a marker, %%foo%bar%% gets parsed as Node("foo%bar")).

  2. Single space inside is trimmed to allow you to write % %%foo % to be parsed as Node("%%foo").

If you define two structures with the same marker, only the first one will work.

Functions§

add_with