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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
use crate::config::WrapMode;
use crate::formatter::indent_utils::{calculate_list_item_indent, is_alignable_marker};
use crate::formatter::inline_layout::{self, WrapStrategy};
use crate::syntax::{AstNode, BlockQuote, FencedDiv, SyntaxKind, SyntaxNode};
use rowan::NodeOrToken;
use super::Formatter;
impl Formatter {
fn has_open_only_fenced_div(item: &SyntaxNode) -> bool {
item.descendants().any(|node| {
let Some(fenced_div) = FencedDiv::cast(node) else {
return false;
};
!fenced_div.has_closing_fence()
&& !fenced_div
.body_blocks()
.any(|body_child| body_child.kind() != SyntaxKind::BLANK_LINE)
})
}
fn has_continuation_eligible_predecessor(node: &SyntaxNode) -> bool {
let mut prev = node.prev_sibling();
while let Some(sibling) = prev {
match sibling.kind() {
SyntaxKind::BLANK_LINE => prev = sibling.prev_sibling(),
SyntaxKind::LIST_ITEM | SyntaxKind::PARAGRAPH | SyntaxKind::CODE_BLOCK => {
return true;
}
_ => return false,
}
}
false
}
fn normalize_task_checkbox(checkbox: &str) -> String {
if checkbox == "[X]" {
"[x]".to_string()
} else {
checkbox.to_string()
}
}
/// Extract the marker text from a ListItem node
/// Standardizes bullet list markers to "-" for consistency
pub(super) fn extract_list_marker(node: &SyntaxNode) -> Option<String> {
for el in node.children_with_tokens() {
if let NodeOrToken::Token(t) = el
&& t.kind() == SyntaxKind::LIST_MARKER
{
let marker = t.text().to_string();
// Standardize bullet list markers: convert *, +, - to "-"
if marker.len() == 1 && matches!(marker.as_str(), "-" | "*" | "+") {
return Some("-".to_string());
}
return Some(marker);
}
}
None
}
/// Check if a nested list is empty (contains only one item with no text content)
fn is_empty_nested_list(list_node: &SyntaxNode) -> bool {
let items: Vec<_> = list_node
.children()
.filter(|c| c.kind() == SyntaxKind::LIST_ITEM)
.collect();
// Must have exactly one item
if items.len() != 1 {
return false;
}
let item = &items[0];
// Check if item has any text content or nested structures
for child in item.children_with_tokens() {
match child {
NodeOrToken::Token(t) => {
// Has text content beyond marker/whitespace/newline
if matches!(t.kind(), SyntaxKind::TEXT | SyntaxKind::ESCAPED_CHAR) {
return false;
}
}
NodeOrToken::Node(n) => {
// Has nested blocks (PLAIN/PARAGRAPH/LIST/etc)
if !matches!(
n.kind(),
SyntaxKind::LIST_MARKER | SyntaxKind::WHITESPACE | SyntaxKind::NEWLINE
) {
return false;
}
}
}
}
true
}
/// Calculate the maximum marker width for all direct ListItem children of a List
/// Returns 0 if markers shouldn't be aligned
pub(super) fn calculate_max_marker_width(list_node: &SyntaxNode) -> usize {
let markers: Vec<String> = list_node
.children()
.filter(|child| child.kind() == SyntaxKind::LIST_ITEM)
.filter_map(|item| Self::extract_list_marker(&item))
.collect();
// Check if any marker is alignable
if !markers.iter().any(|m| is_alignable_marker(m)) {
return 0;
}
// Return max width of alignable markers
markers
.iter()
.filter(|m| is_alignable_marker(m))
.map(|m| m.len())
.max()
.unwrap_or(0)
}
/// Calculate the content indentation offset for a list item (marker + padding + space)
/// This is the column where the list item's content starts relative to the list's base indent
pub(super) fn calculate_list_item_content_indent(
item_node: &SyntaxNode,
max_marker_width: usize,
) -> usize {
let marker = Self::extract_list_marker(item_node).unwrap_or_default();
// Check for task checkbox (adds 4 more characters: "[x] ")
let has_checkbox = item_node.children_with_tokens().any(|el| {
if let NodeOrToken::Token(t) = el {
t.kind() == SyntaxKind::TASK_CHECKBOX
} else {
false
}
});
let indent = calculate_list_item_indent(&marker, max_marker_width, has_checkbox);
indent.content_offset()
}
/// Format a paragraph that is a continuation of a list item.
/// Strips existing indentation from the text and applies the correct list item indentation.
pub(super) fn format_list_continuation_paragraph(&mut self, node: &SyntaxNode, indent: usize) {
let text = node.text().to_string();
let line_width = self.config.line_width.saturating_sub(indent);
let wrap_mode = self.config.wrap.clone().unwrap_or(WrapMode::Reflow);
match wrap_mode {
WrapMode::Preserve => {
// Strip existing indentation and apply list item indentation
for line in text.lines() {
self.output.push_str(&" ".repeat(indent));
self.output.push_str(line.trim_start());
self.output.push('\n');
}
}
WrapMode::Reflow => {
// Wrap with list item indentation
let lines = self.wrapped_lines_for_paragraph(node, line_width);
for line in lines {
self.output.push_str(&" ".repeat(indent));
self.output.push_str(&line);
self.output.push('\n');
}
}
WrapMode::Sentence => {
let lines = self.sentence_lines_for_paragraph(node);
for line in lines {
self.output.push_str(&" ".repeat(indent));
self.output.push_str(&line);
self.output.push('\n');
}
}
}
}
/// Format a List node
pub(super) fn format_list(&mut self, node: &SyntaxNode, indent: usize) {
// Add blank line before top-level lists (indent == 0) that follow content.
// Keep one normalized separator between adjacent top-level lists to match Pandoc output.
if indent == 0
&& self.fenced_div_depth == 0
&& !self.output.is_empty()
&& !self.output.ends_with("\n\n")
{
self.output.push('\n');
}
// Calculate max marker width for right-alignment
let max_marker_width = Self::calculate_max_marker_width(node);
self.max_marker_widths.push(max_marker_width);
// Decide loose/tight at the *list* level.
// Parser may emit PLAIN for most list item text; we treat lists as loose
// if there are explicit blank lines between items in the CST.
let list_children: Vec<_> = node.children().collect();
let has_blank_between_items = list_children.iter().enumerate().any(|(idx, child)| {
if child.kind() != SyntaxKind::BLANK_LINE {
return false;
}
let prev_is_item = idx > 0 && list_children[idx - 1].kind() == SyntaxKind::LIST_ITEM;
let next_is_item = idx + 1 < list_children.len()
&& list_children[idx + 1].kind() == SyntaxKind::LIST_ITEM;
prev_is_item && next_is_item
});
let has_nested_lists = list_children.iter().any(|child| {
child.kind() == SyntaxKind::LIST_ITEM
&& child
.children()
.any(|item_child| item_child.kind() == SyntaxKind::LIST)
});
let has_blockquote_children = list_children.iter().any(|child| {
child.kind() == SyntaxKind::LIST_ITEM
&& child
.children()
.any(|item_child| matches!(item_child.kind(), SyntaxKind::BLOCK_QUOTE))
});
let is_loose = (has_blank_between_items || has_blockquote_children) && !has_nested_lists;
log::debug!("Formatting list: is_loose={}", is_loose);
let mut item_count = 0;
let total_items = node
.children()
.filter(|c| c.kind() == SyntaxKind::LIST_ITEM)
.count();
let mut last_item_content_indent = 0;
for child in node.children() {
if child.kind() == SyntaxKind::LIST_ITEM {
let prev_is_fenced_div = child
.prev_sibling()
.map(|n| n.kind() == SyntaxKind::FENCED_DIV)
.unwrap_or(false);
if prev_is_fenced_div && self.output.ends_with("\n\n") {
self.output.pop();
}
item_count += 1;
// Calculate content indent for this list item (marker + space)
last_item_content_indent =
indent + Self::calculate_list_item_content_indent(&child, max_marker_width);
self.format_node_sync(&child, indent);
// Add blank line after each item for loose lists (except last)
if is_loose
&& item_count < total_items
&& !self.output.ends_with("\n\n")
&& !Self::has_open_only_fenced_div(&child)
{
let mut next = child.next_sibling();
while let Some(sibling) = next.clone() {
if sibling.kind() == SyntaxKind::BLANK_LINE {
next = sibling.next_sibling();
} else {
break;
}
}
let next_non_blank_is_list_item = next
.map(|n| n.kind() == SyntaxKind::LIST_ITEM)
.unwrap_or(false);
if next_non_blank_is_list_item {
self.output.push('\n');
}
}
} else if child.kind() == SyntaxKind::BLANK_LINE {
// Preserve explicit separators when not treating this list as globally loose.
let prev_is_item = child
.prev_sibling()
.map(|n| n.kind() == SyntaxKind::LIST_ITEM)
.unwrap_or(false);
let next_is_item = child
.next_sibling()
.map(|n| n.kind() == SyntaxKind::LIST_ITEM)
.unwrap_or(false);
if !is_loose && prev_is_item && next_is_item && !self.output.ends_with("\n\n") {
self.output.push('\n');
}
continue;
} else if child.kind() == SyntaxKind::PARAGRAPH {
if Self::has_continuation_eligible_predecessor(&child) {
// Paragraphs that are siblings of ListItems are continuation content.
self.format_list_continuation_paragraph(&child, last_item_content_indent);
} else {
self.format_node_sync(&child, indent);
}
} else if child.kind() == SyntaxKind::CODE_BLOCK {
if Self::has_continuation_eligible_predecessor(&child) {
// Code blocks that are siblings of ListItems are also continuation content.
self.format_indented_code_block(&child, last_item_content_indent);
} else {
self.format_node_sync(&child, indent);
}
} else {
self.format_node_sync(&child, indent);
}
}
// Pop the max marker width off the stack
self.max_marker_widths.pop();
if !self.output.ends_with('\n') {
self.output.push('\n');
}
}
/// Find Plain or PARAGRAPH child in a ListItem node.
/// These nodes wrap the text content in Pandoc-style AST.
/// For nested lists, skip Plain nodes that appear before the ListMarker
/// (these contain only indentation whitespace).
fn find_content_node(node: &SyntaxNode) -> Option<SyntaxNode> {
let mut seen_marker = false;
for el in node.children_with_tokens() {
match el {
rowan::NodeOrToken::Token(t) if t.kind() == SyntaxKind::LIST_MARKER => {
seen_marker = true;
}
rowan::NodeOrToken::Node(n)
if matches!(n.kind(), SyntaxKind::PLAIN | SyntaxKind::PARAGRAPH) =>
{
// Only return Plain/PARAGRAPH nodes that come after the marker
if seen_marker {
return Some(n);
}
}
_ => {}
}
}
None
}
/// Format a ListItem node
pub(super) fn format_list_item(&mut self, node: &SyntaxNode, indent: usize) {
// Pre-pass: Process any directive comments to update tracker state
for child in node.children() {
if matches!(child.kind(), SyntaxKind::HTML_BLOCK | SyntaxKind::COMMENT)
&& let Some(directive) = crate::directives::extract_directive_from_node(&child)
{
self.directive_tracker.process_directive(&directive);
}
}
// Compute indent, marker, and checkbox from leading tokens
let mut marker = String::new();
let mut checkbox = None;
// NOTE: We ignore WHITESPACE tokens for list indentation calculation.
// The WHITESPACE tokens are emitted by the parser for losslessness, but the
// formatter should use the `indent` parameter (which represents nesting level)
// to determine output indentation, not the source indentation from WHITESPACE tokens.
for el in node.children_with_tokens() {
if let NodeOrToken::Token(t) = el {
match t.kind() {
SyntaxKind::WHITESPACE => {
// Skip - we don't accumulate source indentation
// The `indent` parameter determines output indentation
}
SyntaxKind::LIST_MARKER => {
let raw_marker = t.text().to_string();
// Standardize bullet list markers to "-"
marker = if raw_marker.len() == 1
&& matches!(raw_marker.as_str(), "-" | "*" | "+")
{
"-".to_string()
} else {
raw_marker
};
}
SyntaxKind::TASK_CHECKBOX => {
checkbox = Some(Self::normalize_task_checkbox(t.text()));
}
_ => {}
}
}
}
// Get max marker width for this list level
let max_marker_width = self.max_marker_widths.last().copied().unwrap_or(0);
// Calculate indentation using the utility
let list_indent = calculate_list_item_indent(&marker, max_marker_width, checkbox.is_some());
let total_indent = indent;
let hanging = list_indent.hanging_indent(total_indent);
let available_width = self.config.line_width.saturating_sub(hanging);
let first_non_blank_child = node
.children()
.find(|child| child.kind() != SyntaxKind::BLANK_LINE);
if let Some(leading_heading) = first_non_blank_child
&& leading_heading.kind() == SyntaxKind::HEADING
{
self.output.push_str(&" ".repeat(total_indent));
self.output
.push_str(&" ".repeat(list_indent.marker_padding));
self.output.push_str(&marker);
self.output.push_str(&" ".repeat(list_indent.spaces_after));
if let Some(ref cb) = checkbox {
self.output.push_str(cb);
self.output.push(' ');
}
self.output.push_str(&self.format_heading(&leading_heading));
self.output.push('\n');
let has_following_blocks = node
.children()
.any(|child| child != leading_heading && child.kind() != SyntaxKind::BLANK_LINE);
if has_following_blocks {
self.output.push('\n');
}
for child in node.children() {
if child == leading_heading || child.kind() == SyntaxKind::BLANK_LINE {
continue;
}
match child.kind() {
SyntaxKind::PLAIN | SyntaxKind::PARAGRAPH => {
self.format_list_continuation_paragraph(&child, hanging);
}
SyntaxKind::LIST => {
self.format_node_sync(&child, hanging);
}
SyntaxKind::CODE_BLOCK => {
self.format_indented_code_block(&child, hanging);
}
_ => {
self.format_node_sync(&child, hanging);
}
}
}
return;
}
// Build source node for wrapping from Plain/PARAGRAPH content node if present.
let content_node = Self::find_content_node(node);
let content_has_hard_breaks = content_node
.as_ref()
.map(|content| {
content
.descendants_with_tokens()
.any(|el| el.kind() == SyntaxKind::HARD_LINE_BREAK)
})
.unwrap_or(false);
let wrap_source = content_node.as_ref();
// Check if this item contains only an empty nested list (special case formatting)
let has_only_empty_nested_list = node
.children()
.any(|c| c.kind() == SyntaxKind::LIST && Self::is_empty_nested_list(&c))
&& wrap_source.is_none_or(|source| source.text().to_string().trim().is_empty());
let wrap_mode = self.config.wrap.clone().unwrap_or(WrapMode::Reflow);
let in_blockquote = BlockQuote::contains_node(node);
let line_widths = [available_width];
let lines = match wrap_mode {
WrapMode::Preserve => Vec::new(),
WrapMode::Sentence => Vec::new(),
WrapMode::Reflow => wrap_source
.map(|source| {
inline_layout::wrapped_lines_for_node(
&self.config,
source,
&line_widths,
&|n| self.format_inline_node(n),
WrapStrategy::ListReflow { in_blockquote },
)
})
.unwrap_or_default(),
};
let preserve_lines = match wrap_mode {
WrapMode::Preserve => {
let source = content_node
.as_ref()
.map(|content| content.text().to_string())
.unwrap_or_default();
Some(source.lines().map(ToString::to_string).collect::<Vec<_>>())
}
_ => None,
};
let sentence_lines: Option<Vec<String>> = match wrap_mode {
WrapMode::Sentence => Some(
wrap_source
.map(|source| {
inline_layout::wrapped_lines_for_node(
&self.config,
source,
&[],
&|n| self.format_inline_node(n),
WrapStrategy::ListSentence { in_blockquote },
)
})
.unwrap_or_default(),
),
_ => None,
};
let heading_with_remainder = content_node
.as_ref()
.and_then(|content| self.leading_atx_heading_with_remainder(content));
let content_starts_with_blockquote = content_node
.as_ref()
.map(|content| content.text().to_string().trim_start().starts_with('>'))
.unwrap_or(false);
log::trace!(
"ListItem wrapping: {} lines, hanging indent={}",
lines.len(),
hanging
);
if let Some((heading_line, remainder)) = heading_with_remainder {
self.output.push_str(&" ".repeat(total_indent));
self.output
.push_str(&" ".repeat(list_indent.marker_padding));
self.output.push_str(&marker);
self.output.push_str(&" ".repeat(list_indent.spaces_after));
if let Some(ref cb) = checkbox {
self.output.push_str(cb);
self.output.push(' ');
}
self.output.push_str(&heading_line);
self.output.push('\n');
self.output.push('\n');
for line in self.wrap_text_for_indent(&remainder, hanging) {
self.output.push_str(&" ".repeat(hanging));
self.output.push_str(line.trim_start());
self.output.push('\n');
}
} else if let Some(preserve_lines) = &preserve_lines {
for (i, line) in preserve_lines.iter().enumerate() {
if i == 0 {
self.output.push_str(&" ".repeat(total_indent));
self.output
.push_str(&" ".repeat(list_indent.marker_padding));
self.output.push_str(&marker);
self.output.push_str(&" ".repeat(list_indent.spaces_after));
if let Some(ref cb) = checkbox {
self.output.push_str(cb);
self.output.push(' ');
}
} else {
self.output.push_str(&" ".repeat(hanging));
}
self.output.push_str(line.trim_start());
if !has_only_empty_nested_list {
self.output.push('\n');
}
}
} else if let Some(sentence_lines) = &sentence_lines {
for (i, text) in sentence_lines.iter().enumerate() {
log::trace!(" Line {}: sentence line", i);
if i == 0 {
// First line: output indent + marker padding + marker + spaces + checkbox
self.output.push_str(&" ".repeat(total_indent));
self.output
.push_str(&" ".repeat(list_indent.marker_padding));
self.output.push_str(&marker);
self.output.push_str(&" ".repeat(list_indent.spaces_after));
// Output checkbox if present
if let Some(ref cb) = checkbox {
self.output.push_str(cb);
self.output.push(' ');
}
} else {
// Hanging indent includes all leading whitespace
self.output.push_str(&" ".repeat(hanging));
}
if i > 0 {
self.output.push_str(text.trim_start());
} else {
let normalized = text
.replace("<summary>\n\t", "<summary>\n ")
.replace("<summary>\n ", "<summary>\n ");
self.output.push_str(&normalized);
}
if !has_only_empty_nested_list {
self.output.push('\n');
}
}
} else {
for (i, line) in lines.iter().enumerate() {
log::trace!(" Line {}: {} chars", i, line.len());
if i == 0 {
// First line: output indent + marker padding + marker + spaces + checkbox
self.output.push_str(&" ".repeat(total_indent));
self.output
.push_str(&" ".repeat(list_indent.marker_padding));
self.output.push_str(&marker);
self.output.push_str(&" ".repeat(list_indent.spaces_after));
// Output checkbox if present
if let Some(ref cb) = checkbox {
self.output.push_str(cb);
self.output.push(' ');
}
} else {
// Hanging indent includes all leading whitespace
self.output.push_str(&" ".repeat(hanging));
}
let mut rendered_line = if i > 0 {
line.trim_start().to_string()
} else {
line.to_string()
};
rendered_line = rendered_line
.replace("<summary>\n\t", "<summary>\n ")
.replace("<summary>\n ", "<summary>\n ");
if rendered_line.contains('\n') {
for (idx, segment) in rendered_line.split('\n').enumerate() {
let segment = if content_has_hard_breaks {
segment
} else {
segment.trim_end()
};
if idx == 0 {
self.output.push_str(segment);
} else {
let trimmed = segment.trim_start();
if !trimmed.is_empty() {
self.output.push('\n');
self.output.push_str(&" ".repeat(hanging));
self.output.push_str(trimmed);
}
}
}
} else {
self.output.push_str(&rendered_line);
}
// Only output newline if this item doesn't have an inline empty nested list
if !has_only_empty_nested_list {
self.output.push('\n');
}
}
}
// Special case: if no lines were wrapped but we have empty nested list, still output marker
if lines.is_empty() && has_only_empty_nested_list {
self.output.push_str(&" ".repeat(total_indent));
self.output
.push_str(&" ".repeat(list_indent.marker_padding));
self.output.push_str(&marker);
self.output.push(' '); // Space before nested marker
}
// Format nested blocks inside this list item aligned to the content column.
// Skip Plain/PARAGRAPH nodes that were already processed for word wrapping.
for child in node.children() {
match child.kind() {
SyntaxKind::PLAIN | SyntaxKind::PARAGRAPH => {
// These blocks are already handled by word wrapping above if they're
// direct children. Only process Plain/PARAGRAPH if it comes after a BlankLine
// (indicating it's a true continuation paragraph, not the first content).
let has_blank_before = child
.prev_sibling()
.map(|prev| prev.kind() == SyntaxKind::BLANK_LINE)
.unwrap_or(false);
// Also process if we're in an ignore region (content should be preserved exactly)
let in_ignore_region = self.directive_tracker.is_formatting_ignored();
if has_blank_before || in_ignore_region {
let content_indent = list_indent.hanging_indent(total_indent);
// If in ignore region, just call format_node_sync which preserves content
// The indent parameter isn't used when in ignore mode, so we don't add it
if in_ignore_region {
self.format_node_sync(&child, 0);
} else {
self.format_list_continuation_paragraph(&child, content_indent);
}
}
// Otherwise skip - already handled
}
SyntaxKind::LIST => {
// Check if this is an empty nested list (only has one item with no content)
if Self::is_empty_nested_list(&child) {
// Format inline: output nested marker and newline
let nested_marker = Self::extract_list_marker(
&child
.children()
.find(|c| c.kind() == SyntaxKind::LIST_ITEM)
.unwrap(),
)
.unwrap_or_else(|| "-".to_string());
self.output.push_str(&nested_marker);
self.output.push('\n');
} else {
// Normal nested list: indent on next line
self.format_node_sync(&child, list_indent.hanging_indent(total_indent));
}
}
SyntaxKind::CODE_BLOCK => {
// Code blocks in list items need indentation
let content_indent = list_indent.hanging_indent(total_indent);
self.format_indented_code_block(&child, content_indent);
}
SyntaxKind::BLOCK_QUOTE => {
let follows_primary_content = child
.prev_sibling()
.map(|prev| {
matches!(prev.kind(), SyntaxKind::PLAIN | SyntaxKind::PARAGRAPH)
})
.unwrap_or(false);
if content_starts_with_blockquote && follows_primary_content {
if self.output.ends_with('\n') {
self.output.pop();
}
let mut pieces: Vec<String> = Vec::new();
let child_text = child.text().to_string();
for line in child_text.lines() {
let trimmed = line.trim_start();
let content = if let Some(rest) = trimmed.strip_prefix('>') {
rest.trim_start()
} else {
trimmed
};
if !content.is_empty() {
pieces.push(content.to_string());
}
}
if !pieces.is_empty() {
self.output.push(' ');
self.output.push_str(&pieces.join(" "));
}
self.output.push('\n');
} else {
let content_indent = list_indent.hanging_indent(total_indent);
self.format_node_sync(&child, content_indent);
}
}
SyntaxKind::BLANK_LINE => {
// Blank lines within list items
self.output.push('\n');
}
_ => {
// Other block elements - format with proper indentation
let content_indent = list_indent.hanging_indent(total_indent);
self.format_node_sync(&child, content_indent);
}
}
}
}
}