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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use crate::{MarkdownIt, Node};
use crate::parser::extset::{InlineRootExt, MarkdownItExt};
use crate::parser::inline::{InlineRule, InlineState, Text};
#[derive(Debug, Default)]
struct CodePairCache<const MARKER: char> {
scanned: bool,
max: Vec<usize>,
}
impl<const MARKER: char> InlineRootExt for CodePairCache<MARKER> {}
#[derive(Debug)]
struct CodePairConfig<const MARKER: char>(fn (usize) -> Node);
impl<const MARKER: char> MarkdownItExt for CodePairConfig<MARKER> {}
pub fn add_with<const MARKER: char>(md: &mut MarkdownIt, f: fn (length: usize) -> Node) {
md.ext.insert(CodePairConfig::<MARKER>(f));
md.inline.add_rule::<CodePairScanner<MARKER>>();
}
#[doc(hidden)]
pub struct CodePairScanner<const MARKER: char>;
impl<const MARKER: char> InlineRule for CodePairScanner<MARKER> {
const MARKER: char = MARKER;
fn run(state: &mut InlineState) -> Option<(Node, usize)> {
let mut chars = state.src[state.pos..state.pos_max].chars();
if chars.next().unwrap() != MARKER { return None; }
if state.trailing_text_get().ends_with(MARKER) { return None; }
let mut pos = state.pos + 1;
while Some(MARKER) == chars.next() {
pos += 1;
}
let backticks = state.inline_ext.get_or_insert_default::<CodePairCache<MARKER>>();
let opener_len = pos - state.pos;
if backticks.scanned && backticks.max[opener_len] <= state.pos {
return None;
}
let mut match_start;
let mut match_end = pos;
while let Some(p) = state.src[match_end..state.pos_max].find(MARKER) {
match_start = match_end + p;
match_end = match_start + 1;
chars = state.src[match_end..state.pos_max].chars();
while Some(MARKER) == chars.next() {
match_end += 1;
}
let closer_len = match_end - match_start;
if closer_len == opener_len {
let mut content = state.src[pos..match_start].to_owned().replace('\n', " ");
if content.starts_with(' ') && content.ends_with(' ') && content.len() > 2 {
content = content[1..content.len() - 1].to_owned();
pos += 1;
match_start -= 1;
}
let f = state.md.ext.get::<CodePairConfig<MARKER>>().unwrap().0;
let mut node = f(opener_len);
let mut inner_node = Node::new(Text { content });
inner_node.srcmap = state.get_map(pos, match_start);
node.children.push(inner_node);
return Some((node, match_end - state.pos));
}
let backticks = state.inline_ext.get_mut::<CodePairCache<MARKER>>().unwrap();
while backticks.max.len() <= closer_len { backticks.max.push(0); }
backticks.max[closer_len] = match_start;
}
let mut backticks = state.inline_ext.get_mut::<CodePairCache<MARKER>>().unwrap();
backticks.scanned = true;
None
}
}