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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use ox_content_allocator::Vec;
use ox_content_ast::{List, ListItem, Node, Paragraph, Span};
use super::Parser;
use crate::error::ParseResult;
#[allow(unused_imports)]
use crate::profile_span;
impl<'a> Parser<'a> {
pub(super) fn parse_list(&mut self, start: usize) -> ParseResult<Option<Node<'a>>> {
profile_span!("parser::parse_list");
let baseline_indent = self.calc_indentation(start);
// Determine list type from the first line (already verified by try_parse_list)
let first_line_start = self.position;
let Some(first_item) = self.parse_list_item_line(first_line_start) else {
return Ok(None);
};
let ordered = first_item.ordered;
let list_start = first_item.start;
let mut children: Vec<'a, ListItem<'a>> = self.allocator.new_vec();
let mut list_spread = false;
loop {
if self.is_at_end() {
break;
}
let line_start = self.position;
self.skip_whitespace();
if self.peek() == Some('\n') || self.is_at_end() {
self.position = line_start; // Backtrack to handle end of block
break;
}
// Check indentation
let current_indent = self.calc_indentation(line_start);
// If less indented, list ends
if current_indent < baseline_indent {
self.position = line_start;
break;
}
// If indented more, check if it's a nested list
if current_indent > baseline_indent {
// Peek to see if it's a list marker
self.position = line_start; // Reset position to check marker properly
if self.try_parse_list() {
// Parse nested list
if let Some(Node::List(nested_list)) = self.parse_list(line_start)? {
// Add to the LAST item's children
if let Some(last_item) = children.last_mut() {
last_item.span = last_item.span.merge(nested_list.span);
last_item.children.push(Node::List(nested_list));
}
}
} else {
// Continuation content?
// For now, we only support simple lists.
// Just skip line to avoid infinite loop
while let Some(ch) = self.peek() {
self.advance();
if ch == '\n' {
break;
}
}
}
continue;
}
// Same indentation (or close enough? Standard is complex, we use strict >= baseline)
self.position = line_start; // Reset
// Check if it's a list item
let remaining = self.remaining();
let line = remaining.lines().next().unwrap_or("");
let Some(item) = self.parse_list_item_line_from_line(line_start, line) else {
break;
};
if item.ordered != ordered {
// Not a list item, break
break;
}
// Consume line
self.position += line.len();
let consumed_newline = self.peek() == Some('\n');
if consumed_newline {
self.advance();
}
let content_indent = item.content_offset.saturating_sub(line_start);
let mut item_source = None;
let mut item_end = self.position;
let mut item_spread = false;
loop {
if self.is_at_end() {
break;
}
let continuation_start = self.position;
let continuation_line = self.line_at(continuation_start);
let continuation_next = self.next_line_start(continuation_start);
if continuation_line.trim().is_empty() {
let mut lookahead = continuation_next;
let mut blank_count = 1;
while lookahead < self.source.len() {
let line = self.line_at(lookahead);
if !line.trim().is_empty() {
break;
}
blank_count += 1;
lookahead = self.next_line_start(lookahead);
}
if lookahead >= self.source.len() {
break;
}
let next_indent = self.calc_indentation(lookahead);
let next_item = self.parse_list_item_line(lookahead);
if next_indent == baseline_indent
&& next_item.as_ref().is_some_and(|next| next.ordered == ordered)
{
self.position = lookahead;
item_spread = true;
list_spread = true;
break;
}
if next_indent >= content_indent {
if item_source.is_none() {
item_source =
Some(self.init_list_item_source(item.content, consumed_newline));
}
let item_source = item_source.as_mut().expect("item source initialized");
for _ in 0..blank_count {
item_source.push('\n');
}
self.position = lookahead;
item_spread = true;
list_spread = true;
item_end = self.position;
continue;
}
break;
}
let current_indent = self.calc_indentation(continuation_start);
if current_indent < baseline_indent {
break;
}
if current_indent == baseline_indent {
// Reuse the line already scanned at the top of the loop
// (`continuation_line`) instead of re-finding the newline;
// only `next.ordered` is read, which a trailing `\r` cannot
// affect.
if self
.parse_list_item_line_from_line(continuation_start, continuation_line)
.is_some_and(|next| next.ordered == ordered)
{
break;
}
break;
}
if item_source.is_none() {
item_source = Some(self.init_list_item_source(item.content, consumed_newline));
}
let item_source = item_source.as_mut().expect("item source initialized");
let stripped = Self::strip_indent_columns(continuation_line, content_indent);
item_source.push_str(stripped);
item_source.push('\n');
self.position = continuation_next;
item_end = self.position;
}
let item_children = if item_source.is_none()
&& Self::can_inline_parse_list_item(item.content)
{
self.parse_inline_list_item_children(item.content, item.content_offset, item_end)?
} else {
let item_source = item_source
.unwrap_or_else(|| self.init_list_item_source(item.content, consumed_newline))
.into_bump_str();
let sub_parser =
Parser::with_options(self.allocator, item_source, self.options.clone());
let sub_doc = sub_parser.parse()?;
let mut item_children = sub_doc.children;
for child in &mut item_children {
Self::offset_node_spans(child, item.content_offset as u32);
}
item_children
};
let list_item = ListItem {
checked: item.checked,
spread: item_spread,
children: item_children,
span: Span::new(line_start as u32, item_end as u32),
};
children.push(list_item);
}
let span = Span::new(start as u32, self.position as u32);
Ok(Some(Node::List(List {
ordered,
start: list_start,
spread: list_spread,
children,
span,
})))
}
/// Builds the synthetic source used when a list item needs block parsing.
///
/// Simple single-line list items take `parse_inline_list_item_children`
/// instead and never allocate this buffer. When continuation lines, nested
/// blocks, or block-looking inline text require the historical sub-parser
/// path, the buffer is allocated directly in the bump arena so ownership
/// matches the resulting AST and we avoid a system `String` followed by an
/// arena copy.
fn init_list_item_source(
&self,
content: &'a str,
consumed_newline: bool,
) -> ox_content_allocator::String<'a> {
// Bump-allocate the per-item buffer so we don't go System → arena.
// This is now only paid for list items that actually need block
// parsing: multi-line items, nested blocks, or block-looking
// single-line contents such as `# heading`.
let mut source = ox_content_allocator::String::with_capacity_in(
content.len() + usize::from(consumed_newline),
self.allocator.bump(),
);
source.push_str(content);
if consumed_newline {
source.push('\n');
}
source
}
/// Returns true when a single-line list item can bypass the sub-parser.
///
/// A list item like `- hello **world**` has exactly one paragraph child in
/// the old implementation: the sub-parser parsed the item source as a
/// document, then offset the paragraph spans back into the parent source.
/// Reconstructing that paragraph directly removes a parser allocation and
/// a second block-dispatch pass. Items whose first non-space byte can open
/// a block stay on the old path so cases such as `- # heading`, nested
/// lists, fenced code, thematic breaks, and HTML blocks keep their AST.
fn can_inline_parse_list_item(content: &str) -> bool {
let Some(&first) = content.trim_start().as_bytes().first() else {
return true;
};
// These are the same leading-byte families that `parse_block` may
// treat as block syntax in a freshly spawned sub-parser. Keep those
// on the old path so `- # heading`, nested lists, fenced code, raw
// HTML blocks, etc. preserve their current AST.
!matches!(first, b'#' | b'-' | b'*' | b'_' | b'>' | b'`' | b'~' | b'<' | b'+' | b'0'..=b'9')
}
/// Creates the direct AST for the single-paragraph list-item fast path.
///
/// `content_offset` is the byte position of `content` in the original
/// document, so inline spans are produced with their final coordinates on
/// the first parse. `item_end` remains the list-item line end to preserve
/// the paragraph span that callers observed when this went through the
/// sub-parser.
fn parse_inline_list_item_children(
&self,
content: &'a str,
content_offset: usize,
item_end: usize,
) -> ParseResult<Vec<'a, Node<'a>>> {
let mut children = self.allocator.new_vec();
let inline = content.trim();
if inline.is_empty() {
return Ok(children);
}
let paragraph_children = self.parse_inline(inline, content_offset)?;
children.push(Node::Paragraph(Paragraph {
children: paragraph_children,
span: Span::new(content_offset as u32, item_end as u32),
}));
Ok(children)
}
}