1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use crate::{MarkdownIt, Node, NodeValue};
use crate::common::ErasedSet;
use crate::parser::core::{CoreRule, Root};
use crate::parser::block::builtin::BlockParserRule;
#[derive(Debug)]
pub struct InlineRoot {
pub content: String,
pub mapping: Vec<(usize, usize)>,
}
impl NodeValue for InlineRoot {}
pub fn add(md: &mut MarkdownIt) {
md.add_rule::<InlineParserRule>()
.after::<BlockParserRule>()
.before_all();
}
pub struct InlineParserRule;
impl CoreRule for InlineParserRule {
fn run(root: &mut Node, md: &MarkdownIt) {
fn walk_recursive(node: &mut Node, md: &MarkdownIt, env: &mut ErasedSet) {
let mut idx = 0;
while idx < node.children.len() {
let child = &mut node.children[idx];
if let Some(data) = child.cast_mut::<InlineRoot>() {
let content = std::mem::take(&mut data.content);
let mapping = std::mem::take(&mut data.mapping);
let mut root = std::mem::take(child);
root.children = Vec::new();
root = md.inline.parse(content, mapping, root, md, env);
let len = root.children.len();
node.children.splice(idx..=idx, root.children);
idx += len;
} else {
walk_recursive(child, md, env);
idx += 1;
}
}
}
let data = root.cast_mut::<Root>().unwrap();
let mut env = std::mem::take(&mut data.env);
walk_recursive(root, md, &mut env);
let data = root.cast_mut::<Root>().unwrap();
data.env = env;
}
}