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
//! Port of rules_block/lheading.ts: lheading (---, ===).
//!
//! Markdoc's allowIndentation patch leaves this rule alone (Markdoc disables it instead).
use super::state_block::StateBlock;
use crate::markdown_it::common::utils::ascii_trim;
use crate::markdown_it::js;
/// `lheading(state, startLine, endLine)`. JS ignores `silent` (the rule is in no alt chain);
/// so does this.
pub fn lheading(state: &mut StateBlock, start_line: usize, end_line: usize, _silent: bool) -> bool {
// Lines past the state's text (where JS reads `undefined` marks and goes on with NaN;
// the parsers never call a rule there): no rule matches there.
if start_line >= state.line_max {
return false;
}
let end_line = end_line.min(state.line_max);
let terminator_rules = state.md.block.ruler.get_rules("paragraph");
// if it's indented more than 3 spaces, it should be a code block
if state.s_count[start_line] - state.blk_indent >= 4 {
return false;
}
let old_parent_type = state.parent_type;
state.parent_type = "paragraph"; // use paragraph to match terminatorRules
// jump line-by-line until empty one or EOF
let mut level = 0;
// JS `let marker` (undefined until a candidate underline is seen; read only once `level`
// is set, when it holds that underline's character)
let mut marker: u16 = 0;
let mut next_line = start_line + 1;
// for (; nextLine < endLine && !state.isEmpty(nextLine); nextLine++)
while next_line < end_line && !state.is_empty(next_line) {
// this would be a code block normally, but after paragraph
// it's considered a lazy continuation regardless of what's there
if state.s_count[next_line] - state.blk_indent > 3 {
next_line += 1;
continue;
}
//
// Check for underline in setext header
//
if state.s_count[next_line] >= state.blk_indent {
let mut pos = state.b_marks[next_line] + state.t_shift[next_line];
let max = state.e_marks[next_line];
if pos < max {
marker = state.src[pos];
// - =
if marker == 0x2D || marker == 0x3D {
pos = state.skip_chars(pos, marker);
pos = state.skip_spaces(pos);
if pos >= max {
level = if marker == 0x3D { 1 } else { 2 };
break;
}
}
}
}
// quirk for blockquotes, this line should already be checked by that rule
if state.s_count[next_line] < 0 {
next_line += 1;
continue;
}
// Some tags can terminate paragraph without empty line.
let mut terminate = false;
for &rule in terminator_rules {
if rule(state, next_line, end_line, true) {
terminate = true;
break;
}
}
if terminate {
break;
}
next_line += 1;
}
if level == 0 {
// Didn't find valid underline
state.parent_type = old_parent_type;
return false;
}
let content = js::to_string(ascii_trim(&state.get_lines(
start_line,
next_line,
state.blk_indent,
false,
)));
state.line = next_line + 1;
// `token_*` borrow `state`: what JS reads from `state` after a push is read first.
let line = state.line;
let tag = format!("h{level}");
let markup = js::to_string(&[marker]);
let token_o = state.push("heading_open", &tag, 1);
token_o.markup = markup.clone();
token_o.map = Some([start_line, line]);
let token_i = state.push("inline", "", 0);
token_i.content = content;
token_i.map = Some([start_line, line - 1]);
token_i.children = Some(Vec::new());
let token_c = state.push("heading_close", &tag, -1);
token_c.markup = markup;
state.parent_type = old_parent_type;
true
}
#[cfg(test)]
mod tests {
use crate::markdown_it::rules_block::paragraph::test_util::{Case, check};
#[test]
fn lheading_tokens_match_node() {
// node: see `test_util` (block tokens, then env.references)
let cases: &[Case] = &[
(
"a\nb\n===\n",
"{}",
&[],
&[
"heading_open/h1 [0,3] L0 markup=\"=\"",
"inline/ [0,2] L1 content=\"a\\nb\"",
"heading_close/h1 L0 markup=\"=\"",
],
),
(
" a \n --- \nc\n- - -\n",
"{}",
&[],
&[
"heading_open/h2 [0,2] L0 markup=\"-\"",
"inline/ [0,1] L1 content=\"a\"",
"heading_close/h2 L0 markup=\"-\"",
"paragraph_open/p [2,3] L0",
"inline/ [2,3] L1 content=\"c\"",
"paragraph_close/p L0",
"hr/hr [3,4] L0 markup=\"---\"",
],
),
(
"a\n ===\n",
"{}",
&[],
&[
"paragraph_open/p [0,2] L0",
"inline/ [0,2] L1 content=\"a\\n ===\"",
"paragraph_close/p L0",
],
),
(
"> a\n---\n",
"{}",
&[],
&[
"blockquote_open/blockquote [0,1] L0 markup=\">\"",
"paragraph_open/p [0,1] L1",
"inline/ [0,1] L2 content=\"a\"",
"paragraph_close/p L1",
"blockquote_close/blockquote L0 markup=\">\"",
"hr/hr [1,2] L0 markup=\"---\"",
],
),
(
"a\n> b\n===\n",
"{}",
&[],
&[
"paragraph_open/p [0,1] L0",
"inline/ [0,1] L1 content=\"a\"",
"paragraph_close/p L0",
"blockquote_open/blockquote [1,3] L0 markup=\">\"",
"paragraph_open/p [1,3] L1",
"inline/ [1,3] L2 content=\"b\\n===\"",
"paragraph_close/p L1",
"blockquote_close/blockquote L0 markup=\">\"",
],
),
];
check(cases);
}
}