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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//! Port of parser_inline.ts: tokenizes paragraph content.
use super::ruler::Ruler;
use super::rules_inline;
use super::rules_inline::state_inline::StateInline;
use super::token::Token;
use super::{Env, JsError, MarkdownIt, js};
/// An inline rule: `(state, silent) => boolean`.
pub type InlineRule = fn(&mut StateInline, bool) -> bool;
/// A post-processing inline rule (`ruler2`): `(state) => void`.
pub type InlineRule2 = fn(&mut StateInline);
// Parser rules
const RULES: [(&str, InlineRule); 12] = [
("text", rules_inline::text::text),
("linkify", rules_inline::linkify::linkify),
("newline", rules_inline::newline::newline),
("escape", rules_inline::escape::escape),
("backticks", rules_inline::backticks::backtick),
("strikethrough", rules_inline::strikethrough::tokenize),
("emphasis", rules_inline::emphasis::tokenize),
("link", rules_inline::link::link),
("image", rules_inline::image::image),
("autolink", rules_inline::autolink::autolink),
("html_inline", rules_inline::html_inline::html_inline),
("entity", rules_inline::entity::entity),
];
// `rule2` ruleset was created specifically for emphasis/strikethrough
// post-processing and may be changed in the future.
//
// Don't use this for anything except pairs (plugins working with `balance_pairs`).
//
const RULES2: [(&str, InlineRule2); 4] = [
("balance_pairs", rules_inline::balance_pairs::link_pairs),
("strikethrough", rules_inline::strikethrough::post_process),
("emphasis", rules_inline::emphasis::post_process),
// rules for pairs separate '**' into its own text tokens, which may be left unused,
// rule below merges unused segments back with the rest of the text
(
"fragments_join",
rules_inline::fragments_join::fragments_join,
),
];
/// `ParserInline` (`md.inline`).
#[derive(Debug, Clone)]
pub struct ParserInline {
/// Keeps the configuration of inline rules.
pub ruler: Ruler<InlineRule>,
/// Second ruler used for post-processing (e.g. in emphasis-like rules).
pub ruler2: Ruler<InlineRule2>,
}
impl Default for ParserInline {
fn default() -> Self {
Self::new()
}
}
impl ParserInline {
pub fn new() -> Self {
let mut ruler = Ruler::new();
let mut ruler2 = Ruler::new();
for (name, rule) in RULES {
ruler.push(name, rule, &[]);
}
for (name, rule) in RULES2 {
ruler2.push(name, rule, &[]);
}
ParserInline { ruler, ruler2 }
}
/// `skipToken(state)`: skip a single token by running all rules in validation mode
/// (moves `state.pos` past it, and caches where it ends).
///
/// Throws (see [`crate::markdown_it::JsError`]) where JS throws: a rule that matched
/// without moving `state.pos`, and nesting past [`crate::markdown_it::MAX_DEPTH`]. Once
/// anything has thrown it moves `state.pos` to `state.posMax`, so that the scan calling it
/// ends.
pub fn skip_token(&self, state: &mut StateInline) {
if !state.env.enter() {
state.pos = state.pos.max(state.pos_max);
return;
}
self.skip_token_inner(state);
state.env.leave();
if state.env.has_thrown() {
state.pos = state.pos.max(state.pos_max);
}
}
fn skip_token_inner(&self, state: &mut StateInline) {
let pos = state.pos;
let rules = self.ruler.get_rules("");
let len = rules.len();
let max_nesting = state.md.options.max_nesting;
if let Some(&cached) = state.cache.get(&pos) {
state.pos = cached;
return;
}
let mut ok = false;
if f64::from(state.level) < max_nesting {
for i in 0..len {
// Increment state.level and decrement it later to limit recursion.
// It's harmless to do here, because no tokens are created. But ideally,
// we'd need a separate private state variable for this purpose.
//
state.level += 1;
ok = rules[i](state, true);
state.level -= 1;
if state.env.has_thrown() {
return;
}
if ok {
if pos >= state.pos {
state
.env
.throw(JsError::error("inline rule didn't increment state.pos"));
return;
}
break;
}
}
} else {
// Too much nesting, just skip until the end of the paragraph.
//
// NOTE: this will cause links to behave incorrectly in the following case,
// when an amount of `[` is exactly equal to `maxNesting + 1`:
//
// [[[[[[[[[[[[[[[[[[[[[foo]()
//
// TODO: remove this workaround when CM standard will allow nested links
// (we can replace it by preventing links from being parsed in
// validation mode)
//
state.pos = state.pos_max;
}
if !ok {
state.pos += 1;
}
state.cache.insert(pos, state.pos);
}
/// `tokenize(state)`: generate tokens for input range.
///
/// Throws (see [`crate::markdown_it::JsError`]) where JS throws: a rule that matched
/// without moving `state.pos`, and nesting past [`crate::markdown_it::MAX_DEPTH`]. Stops
/// once anything has thrown.
pub fn tokenize(&self, state: &mut StateInline) {
if !state.env.enter() {
return;
}
self.tokenize_inner(state);
state.env.leave();
}
fn tokenize_inner(&self, state: &mut StateInline) {
let rules = self.ruler.get_rules("");
let len = rules.len();
let end = state.pos_max;
let max_nesting = state.md.options.max_nesting;
while state.pos < end {
// Try all possible rules.
// On success, rule should:
//
// - update `state.pos`
// - update `state.tokens`
// - return true
let prev_pos = state.pos;
let mut ok = false;
if f64::from(state.level) < max_nesting {
for i in 0..len {
ok = rules[i](state, false);
if state.env.has_thrown() {
return;
}
if ok {
if prev_pos >= state.pos {
state
.env
.throw(JsError::error("inline rule didn't increment state.pos"));
return;
}
break;
}
}
}
if ok {
if state.pos >= end {
break;
}
continue;
}
// `state.pending += state.src[state.pos++]`: one UTF-16 code unit. Past the end
// of `src` (only if a plugin set `posMax` beyond it) JS appends "undefined".
match state.src.get(state.pos) {
Some(&ch) => state.pending.push(ch),
None => state.pending.extend(js::to_utf16("undefined")),
}
state.pos += 1;
}
if !state.pending.is_empty() {
state.push_pending();
}
}
/// `parse(str, md, env, outTokens)`: process input string and push inline tokens into
/// `out_tokens`.
pub fn parse(&self, src: &[u16], md: &MarkdownIt, env: &mut Env, out_tokens: &mut Vec<Token>) {
let mut state = StateInline::new(src.to_vec(), md, env, std::mem::take(out_tokens));
self.tokenize(&mut state);
let rules = self.ruler2.get_rules("");
let len = rules.len();
for i in 0..len {
if state.env.has_thrown() {
break;
}
rules[i](&mut state);
}
*out_tokens = state.tokens;
}
}