1use crate::options::ParserOptions;
2use crate::syntax::{SyntaxKind, SyntaxNode};
3use rowan::GreenNodeBuilder;
4
5use super::block_dispatcher::{
6 BlockContext, BlockDetectionResult, BlockEffect, BlockParserRegistry, BlockQuotePrepared,
7 PreparedBlockMatch,
8};
9use super::blocks::blockquotes;
10use super::blocks::code_blocks;
11use super::blocks::definition_lists;
12use super::blocks::fenced_divs;
13use super::blocks::headings::{emit_atx_heading, try_parse_atx_heading};
14use super::blocks::line_blocks;
15use super::blocks::lists;
16use super::blocks::paragraphs;
17use super::blocks::raw_blocks::{extract_environment_name, is_inline_math_environment};
18use super::utils::container_stack;
19use super::utils::helpers::{split_lines_inclusive, strip_newline};
20use super::utils::inline_emission;
21use super::utils::marker_utils;
22use super::utils::text_buffer;
23
24use super::blocks::blockquotes::strip_n_blockquote_markers;
25use super::utils::continuation::ContinuationPolicy;
26use container_stack::{Container, ContainerStack, byte_index_at_column, leading_indent};
27use definition_lists::{emit_definition_marker, emit_term};
28use line_blocks::{parse_line_block, try_parse_line_block_start};
29use lists::{
30 ListItemEmissionInput, ListMarker, is_content_nested_bullet_marker, start_nested_list,
31 try_parse_list_marker,
32};
33use marker_utils::{count_blockquote_markers, parse_blockquote_marker_info};
34use text_buffer::TextBuffer;
35
36const GITHUB_ALERT_MARKERS: [&str; 5] = [
37 "[!TIP]",
38 "[!WARNING]",
39 "[!IMPORTANT]",
40 "[!CAUTION]",
41 "[!NOTE]",
42];
43
44pub struct Parser<'a> {
45 lines: Vec<&'a str>,
46 pos: usize,
47 builder: GreenNodeBuilder<'static>,
48 containers: ContainerStack,
49 config: &'a ParserOptions,
50 block_registry: BlockParserRegistry,
51 after_metadata_block: bool,
55}
56
57impl<'a> Parser<'a> {
58 pub fn new(input: &'a str, config: &'a ParserOptions) -> Self {
59 let lines = split_lines_inclusive(input);
61 Self {
62 lines,
63 pos: 0,
64 builder: GreenNodeBuilder::new(),
65 containers: ContainerStack::new(),
66 config,
67 block_registry: BlockParserRegistry::new(),
68 after_metadata_block: false,
69 }
70 }
71
72 pub fn parse(mut self) -> SyntaxNode {
73 self.parse_document_stack();
74
75 SyntaxNode::new_root(self.builder.finish())
76 }
77
78 fn close_containers_to(&mut self, keep: usize) {
81 while self.containers.depth() > keep {
83 match self.containers.stack.last() {
84 Some(Container::ListItem { buffer, .. }) if !buffer.is_empty() => {
86 let buffer_clone = buffer.clone();
88
89 log::debug!(
90 "Closing ListItem with buffer (is_empty={}, segment_count={})",
91 buffer_clone.is_empty(),
92 buffer_clone.segment_count()
93 );
94
95 let parent_list_is_loose = self
99 .containers
100 .stack
101 .iter()
102 .rev()
103 .find_map(|c| match c {
104 Container::List {
105 has_blank_between_items,
106 ..
107 } => Some(*has_blank_between_items),
108 _ => None,
109 })
110 .unwrap_or(false);
111
112 let use_paragraph =
113 parent_list_is_loose || buffer_clone.has_blank_lines_between_content();
114
115 log::debug!(
116 "Emitting ListItem buffer: use_paragraph={} (parent_list_is_loose={}, item_has_blanks={})",
117 use_paragraph,
118 parent_list_is_loose,
119 buffer_clone.has_blank_lines_between_content()
120 );
121
122 self.containers.stack.pop();
124 buffer_clone.emit_as_block(&mut self.builder, use_paragraph, self.config);
126 self.builder.finish_node(); }
128 Some(Container::ListItem { .. }) => {
130 log::debug!("Closing empty ListItem (no buffer content)");
131 self.containers.stack.pop();
133 self.builder.finish_node();
134 }
135 Some(Container::Paragraph { buffer, .. }) if !buffer.is_empty() => {
137 let buffer_clone = buffer.clone();
139 self.containers.stack.pop();
141 buffer_clone.emit_with_inlines(&mut self.builder, self.config);
143 self.builder.finish_node();
144 }
145 Some(Container::Paragraph { .. }) => {
147 self.containers.stack.pop();
149 self.builder.finish_node();
150 }
151 Some(Container::Definition {
153 plain_open: true,
154 plain_buffer,
155 ..
156 }) if !plain_buffer.is_empty() => {
157 let text = plain_buffer.get_accumulated_text();
158 let line_without_newline = text
159 .strip_suffix("\r\n")
160 .or_else(|| text.strip_suffix('\n'));
161 if let Some(line) = line_without_newline
162 && !line.contains('\n')
163 && !line.contains('\r')
164 && let Some(level) = try_parse_atx_heading(line)
165 {
166 emit_atx_heading(&mut self.builder, &text, level, self.config);
167 } else {
168 self.builder.start_node(SyntaxKind::PLAIN.into());
170 inline_emission::emit_inlines(&mut self.builder, &text, self.config);
171 self.builder.finish_node();
172 }
173
174 if let Some(Container::Definition {
176 plain_open,
177 plain_buffer,
178 ..
179 }) = self.containers.stack.last_mut()
180 {
181 plain_buffer.clear();
182 *plain_open = false;
183 }
184
185 self.containers.stack.pop();
187 self.builder.finish_node();
188 }
189 Some(Container::Definition {
191 plain_open: true, ..
192 }) => {
193 if let Some(Container::Definition {
195 plain_open,
196 plain_buffer,
197 ..
198 }) = self.containers.stack.last_mut()
199 {
200 plain_buffer.clear();
201 *plain_open = false;
202 }
203
204 self.containers.stack.pop();
206 self.builder.finish_node();
207 }
208 _ => {
210 self.containers.stack.pop();
211 self.builder.finish_node();
212 }
213 }
214 }
215 }
216
217 fn emit_buffered_plain_if_needed(&mut self) {
220 if let Some(Container::Definition {
222 plain_open: true,
223 plain_buffer,
224 ..
225 }) = self.containers.stack.last()
226 && !plain_buffer.is_empty()
227 {
228 let text = plain_buffer.get_accumulated_text();
229 let line_without_newline = text
230 .strip_suffix("\r\n")
231 .or_else(|| text.strip_suffix('\n'));
232 if let Some(line) = line_without_newline
233 && !line.contains('\n')
234 && !line.contains('\r')
235 && let Some(level) = try_parse_atx_heading(line)
236 {
237 emit_atx_heading(&mut self.builder, &text, level, self.config);
238 } else {
239 self.builder.start_node(SyntaxKind::PLAIN.into());
241 inline_emission::emit_inlines(&mut self.builder, &text, self.config);
242 self.builder.finish_node();
243 }
244 }
245
246 if let Some(Container::Definition {
248 plain_open,
249 plain_buffer,
250 ..
251 }) = self.containers.stack.last_mut()
252 && *plain_open
253 {
254 plain_buffer.clear();
255 *plain_open = false;
256 }
257 }
258
259 fn close_blockquotes_to_depth(&mut self, target_depth: usize) {
264 let mut current = self.current_blockquote_depth();
265 while current > target_depth {
266 while !matches!(self.containers.last(), Some(Container::BlockQuote { .. })) {
267 if self.containers.depth() == 0 {
268 break;
269 }
270 self.close_containers_to(self.containers.depth() - 1);
271 }
272 if matches!(self.containers.last(), Some(Container::BlockQuote { .. })) {
273 self.close_containers_to(self.containers.depth() - 1);
274 current -= 1;
275 } else {
276 break;
277 }
278 }
279 }
280
281 fn active_alert_blockquote_depth(&self) -> Option<usize> {
282 self.containers.stack.iter().rev().find_map(|c| match c {
283 Container::Alert { blockquote_depth } => Some(*blockquote_depth),
284 _ => None,
285 })
286 }
287
288 fn in_active_alert(&self) -> bool {
289 self.active_alert_blockquote_depth().is_some()
290 }
291
292 fn alert_marker_from_content(content: &str) -> Option<&'static str> {
293 let (without_newline, _) = strip_newline(content);
294 let trimmed = without_newline.trim();
295 GITHUB_ALERT_MARKERS
296 .into_iter()
297 .find(|marker| *marker == trimmed)
298 }
299
300 fn emit_list_item_buffer_if_needed(&mut self) {
303 if let Some(Container::ListItem { buffer, .. }) = self.containers.stack.last_mut()
304 && !buffer.is_empty()
305 {
306 let buffer_clone = buffer.clone();
307 buffer.clear();
308 let use_paragraph = buffer_clone.has_blank_lines_between_content();
309 buffer_clone.emit_as_block(&mut self.builder, use_paragraph, self.config);
310 }
311 }
312
313 fn is_paragraph_open(&self) -> bool {
315 matches!(self.containers.last(), Some(Container::Paragraph { .. }))
316 }
317
318 fn close_paragraph_if_open(&mut self) {
320 if self.is_paragraph_open() {
321 self.close_containers_to(self.containers.depth() - 1);
322 }
323 }
324
325 fn prepare_for_block_element(&mut self) {
328 self.emit_list_item_buffer_if_needed();
329 self.close_paragraph_if_open();
330 }
331
332 fn handle_footnote_open_effect(
333 &mut self,
334 block_match: &super::block_dispatcher::PreparedBlockMatch,
335 content: &str,
336 ) {
337 let content_start = block_match
338 .payload
339 .as_ref()
340 .and_then(|p| p.downcast_ref::<super::block_dispatcher::FootnoteDefinitionPrepared>())
341 .map(|p| p.content_start)
342 .unwrap_or(0);
343
344 while matches!(
345 self.containers.last(),
346 Some(Container::FootnoteDefinition { .. })
347 ) {
348 self.close_containers_to(self.containers.depth() - 1);
349 }
350
351 let content_col = 4;
352 self.containers
353 .push(Container::FootnoteDefinition { content_col });
354
355 if content_start > 0 {
356 let first_line_content = &content[content_start..];
357 if !first_line_content.trim().is_empty() {
358 paragraphs::start_paragraph_if_needed(&mut self.containers, &mut self.builder);
359 paragraphs::append_paragraph_line(
360 &mut self.containers,
361 &mut self.builder,
362 first_line_content,
363 self.config,
364 );
365 } else {
366 let (_, newline_str) = strip_newline(content);
367 if !newline_str.is_empty() {
368 self.builder.token(SyntaxKind::NEWLINE.into(), newline_str);
369 }
370 }
371 }
372 }
373
374 fn handle_list_open_effect(
375 &mut self,
376 block_match: &super::block_dispatcher::PreparedBlockMatch,
377 content: &str,
378 indent_to_emit: Option<&str>,
379 ) {
380 use super::block_dispatcher::ListPrepared;
381
382 let prepared = block_match
383 .payload
384 .as_ref()
385 .and_then(|p| p.downcast_ref::<ListPrepared>());
386 let Some(prepared) = prepared else {
387 return;
388 };
389
390 if prepared.indent_cols >= 4 && !lists::in_list(&self.containers) {
391 paragraphs::start_paragraph_if_needed(&mut self.containers, &mut self.builder);
392 paragraphs::append_paragraph_line(
393 &mut self.containers,
394 &mut self.builder,
395 content,
396 self.config,
397 );
398 return;
399 }
400
401 if self.is_paragraph_open() {
402 if !block_match.detection.eq(&BlockDetectionResult::Yes) {
403 paragraphs::append_paragraph_line(
404 &mut self.containers,
405 &mut self.builder,
406 content,
407 self.config,
408 );
409 return;
410 }
411 self.close_containers_to(self.containers.depth() - 1);
412 }
413
414 if matches!(
415 self.containers.last(),
416 Some(Container::Definition {
417 plain_open: true,
418 ..
419 })
420 ) {
421 self.emit_buffered_plain_if_needed();
422 }
423
424 let matched_level = lists::find_matching_list_level(
425 &self.containers,
426 &prepared.marker,
427 prepared.indent_cols,
428 );
429 let list_item = ListItemEmissionInput {
430 content,
431 marker_len: prepared.marker_len,
432 spaces_after_cols: prepared.spaces_after_cols,
433 spaces_after_bytes: prepared.spaces_after,
434 indent_cols: prepared.indent_cols,
435 indent_bytes: prepared.indent_bytes,
436 };
437 let current_content_col = paragraphs::current_content_col(&self.containers);
438 let deep_ordered_matched_level = matched_level
439 .and_then(|level| self.containers.stack.get(level).map(|c| (level, c)))
440 .and_then(|(level, container)| match container {
441 Container::List {
442 marker: list_marker,
443 base_indent_cols,
444 ..
445 } if matches!(
446 (&prepared.marker, list_marker),
447 (ListMarker::Ordered(_), ListMarker::Ordered(_))
448 ) && prepared.indent_cols >= 4
449 && *base_indent_cols >= 4
450 && prepared.indent_cols.abs_diff(*base_indent_cols) <= 3 =>
451 {
452 Some(level)
453 }
454 _ => None,
455 });
456
457 if deep_ordered_matched_level.is_none()
458 && current_content_col > 0
459 && prepared.indent_cols >= current_content_col
460 {
461 if let Some(level) = matched_level
462 && let Some(Container::List {
463 base_indent_cols, ..
464 }) = self.containers.stack.get(level)
465 && prepared.indent_cols == *base_indent_cols
466 {
467 let num_parent_lists = self.containers.stack[..level]
468 .iter()
469 .filter(|c| matches!(c, Container::List { .. }))
470 .count();
471
472 if num_parent_lists > 0 {
473 self.close_containers_to(level + 1);
474
475 if matches!(self.containers.last(), Some(Container::Paragraph { .. })) {
476 self.close_containers_to(self.containers.depth() - 1);
477 }
478 if matches!(self.containers.last(), Some(Container::ListItem { .. })) {
479 self.close_containers_to(self.containers.depth() - 1);
480 }
481
482 if let Some(indent_str) = indent_to_emit {
483 self.builder
484 .token(SyntaxKind::WHITESPACE.into(), indent_str);
485 }
486
487 if let Some(nested_marker) = prepared.nested_marker {
488 lists::add_list_item_with_nested_empty_list(
489 &mut self.containers,
490 &mut self.builder,
491 &list_item,
492 nested_marker,
493 );
494 } else {
495 lists::add_list_item(&mut self.containers, &mut self.builder, &list_item);
496 }
497 return;
498 }
499 }
500
501 self.emit_list_item_buffer_if_needed();
502
503 start_nested_list(
504 &mut self.containers,
505 &mut self.builder,
506 &prepared.marker,
507 &list_item,
508 indent_to_emit,
509 );
510 return;
511 }
512
513 if let Some(level) = matched_level {
514 self.close_containers_to(level + 1);
515
516 if matches!(self.containers.last(), Some(Container::Paragraph { .. })) {
517 self.close_containers_to(self.containers.depth() - 1);
518 }
519 if matches!(self.containers.last(), Some(Container::ListItem { .. })) {
520 self.close_containers_to(self.containers.depth() - 1);
521 }
522
523 if let Some(indent_str) = indent_to_emit {
524 self.builder
525 .token(SyntaxKind::WHITESPACE.into(), indent_str);
526 }
527
528 if let Some(nested_marker) = prepared.nested_marker {
529 lists::add_list_item_with_nested_empty_list(
530 &mut self.containers,
531 &mut self.builder,
532 &list_item,
533 nested_marker,
534 );
535 } else {
536 lists::add_list_item(&mut self.containers, &mut self.builder, &list_item);
537 }
538 return;
539 }
540
541 if matches!(self.containers.last(), Some(Container::Paragraph { .. })) {
542 self.close_containers_to(self.containers.depth() - 1);
543 }
544 while matches!(self.containers.last(), Some(Container::ListItem { .. })) {
545 self.close_containers_to(self.containers.depth() - 1);
546 }
547 while matches!(self.containers.last(), Some(Container::List { .. })) {
548 self.close_containers_to(self.containers.depth() - 1);
549 }
550
551 self.builder.start_node(SyntaxKind::LIST.into());
552 if let Some(indent_str) = indent_to_emit {
553 self.builder
554 .token(SyntaxKind::WHITESPACE.into(), indent_str);
555 }
556 self.containers.push(Container::List {
557 marker: prepared.marker.clone(),
558 base_indent_cols: prepared.indent_cols,
559 has_blank_between_items: false,
560 });
561
562 if let Some(nested_marker) = prepared.nested_marker {
563 lists::add_list_item_with_nested_empty_list(
564 &mut self.containers,
565 &mut self.builder,
566 &list_item,
567 nested_marker,
568 );
569 } else {
570 lists::add_list_item(&mut self.containers, &mut self.builder, &list_item);
571 }
572 }
573
574 fn handle_definition_list_effect(
575 &mut self,
576 block_match: &super::block_dispatcher::PreparedBlockMatch,
577 content: &str,
578 indent_to_emit: Option<&str>,
579 ) {
580 use super::block_dispatcher::DefinitionPrepared;
581
582 let prepared = block_match
583 .payload
584 .as_ref()
585 .and_then(|p| p.downcast_ref::<DefinitionPrepared>());
586 let Some(prepared) = prepared else {
587 return;
588 };
589
590 match prepared {
591 DefinitionPrepared::Definition {
592 marker_char,
593 indent,
594 spaces_after,
595 spaces_after_cols,
596 has_content,
597 } => {
598 self.emit_buffered_plain_if_needed();
599
600 while matches!(self.containers.last(), Some(Container::ListItem { .. })) {
601 self.close_containers_to(self.containers.depth() - 1);
602 }
603 while matches!(self.containers.last(), Some(Container::List { .. })) {
604 self.close_containers_to(self.containers.depth() - 1);
605 }
606
607 if matches!(self.containers.last(), Some(Container::Definition { .. })) {
608 self.close_containers_to(self.containers.depth() - 1);
609 }
610
611 if matches!(self.containers.last(), Some(Container::Paragraph { .. })) {
612 self.close_containers_to(self.containers.depth() - 1);
613 }
614
615 if definition_lists::in_definition_list(&self.containers)
619 && !matches!(
620 self.containers.last(),
621 Some(Container::DefinitionItem { .. })
622 )
623 {
624 self.builder.start_node(SyntaxKind::DEFINITION_ITEM.into());
625 self.containers.push(Container::DefinitionItem {});
626 }
627
628 if !definition_lists::in_definition_list(&self.containers) {
629 self.builder.start_node(SyntaxKind::DEFINITION_LIST.into());
630 self.containers.push(Container::DefinitionList {});
631 }
632
633 if !matches!(
634 self.containers.last(),
635 Some(Container::DefinitionItem { .. })
636 ) {
637 self.builder.start_node(SyntaxKind::DEFINITION_ITEM.into());
638 self.containers.push(Container::DefinitionItem {});
639 }
640
641 self.builder.start_node(SyntaxKind::DEFINITION.into());
642
643 if let Some(indent_str) = indent_to_emit {
644 self.builder
645 .token(SyntaxKind::WHITESPACE.into(), indent_str);
646 }
647
648 emit_definition_marker(&mut self.builder, *marker_char, *indent);
649 let indent_bytes = byte_index_at_column(content, *indent);
650 if *spaces_after > 0 {
651 let space_start = indent_bytes + 1;
652 let space_end = space_start + *spaces_after;
653 if space_end <= content.len() {
654 self.builder.token(
655 SyntaxKind::WHITESPACE.into(),
656 &content[space_start..space_end],
657 );
658 }
659 }
660
661 if !*has_content {
662 let current_line = self.lines[self.pos];
663 let (_, newline_str) = strip_newline(current_line);
664 if !newline_str.is_empty() {
665 self.builder.token(SyntaxKind::NEWLINE.into(), newline_str);
666 }
667 }
668
669 let content_col = *indent + 1 + *spaces_after_cols;
670 let content_start_bytes = indent_bytes + 1 + *spaces_after;
671 let after_marker_and_spaces = content.get(content_start_bytes..).unwrap_or("");
672 let mut plain_buffer = TextBuffer::new();
673 let mut definition_pushed = false;
674
675 if *has_content {
676 let current_line = self.lines[self.pos];
677 let (trimmed_line, _) = strip_newline(current_line);
678
679 let content_start = content_start_bytes.min(trimmed_line.len());
680 let content_slice = &trimmed_line[content_start..];
681 let content_line = ¤t_line[content_start_bytes.min(current_line.len())..];
682
683 let (blockquote_depth, inner_blockquote_content) =
684 count_blockquote_markers(content_line);
685
686 let should_start_list_from_first_line = self
687 .lines
688 .get(self.pos + 1)
689 .map(|next_line| {
690 let (next_without_newline, _) = strip_newline(next_line);
691 if next_without_newline.trim().is_empty() {
692 return false;
693 }
694
695 let (next_indent_cols, _) = leading_indent(next_without_newline);
696 next_indent_cols >= content_col
697 })
698 .unwrap_or(false);
699
700 if blockquote_depth > 0 {
701 self.containers.push(Container::Definition {
702 content_col,
703 plain_open: false,
704 plain_buffer: TextBuffer::new(),
705 });
706 definition_pushed = true;
707
708 let marker_info = parse_blockquote_marker_info(content_line);
709 for level in 0..blockquote_depth {
710 self.builder.start_node(SyntaxKind::BLOCK_QUOTE.into());
711 if let Some(info) = marker_info.get(level) {
712 blockquotes::emit_one_blockquote_marker(
713 &mut self.builder,
714 info.leading_spaces,
715 info.has_trailing_space,
716 );
717 }
718 self.containers.push(Container::BlockQuote {});
719 }
720
721 if !inner_blockquote_content.trim().is_empty() {
722 paragraphs::start_paragraph_if_needed(
723 &mut self.containers,
724 &mut self.builder,
725 );
726 paragraphs::append_paragraph_line(
727 &mut self.containers,
728 &mut self.builder,
729 inner_blockquote_content,
730 self.config,
731 );
732 }
733 } else if let Some(marker_match) =
734 try_parse_list_marker(content_slice, self.config)
735 && should_start_list_from_first_line
736 {
737 self.containers.push(Container::Definition {
738 content_col,
739 plain_open: false,
740 plain_buffer: TextBuffer::new(),
741 });
742 definition_pushed = true;
743
744 let (indent_cols, indent_bytes) = leading_indent(content_line);
745 self.builder.start_node(SyntaxKind::LIST.into());
746 self.containers.push(Container::List {
747 marker: marker_match.marker.clone(),
748 base_indent_cols: indent_cols,
749 has_blank_between_items: false,
750 });
751
752 let list_item = ListItemEmissionInput {
753 content: content_line,
754 marker_len: marker_match.marker_len,
755 spaces_after_cols: marker_match.spaces_after_cols,
756 spaces_after_bytes: marker_match.spaces_after_bytes,
757 indent_cols,
758 indent_bytes,
759 };
760
761 if let Some(nested_marker) = is_content_nested_bullet_marker(
762 content_line,
763 marker_match.marker_len,
764 marker_match.spaces_after_bytes,
765 ) {
766 lists::add_list_item_with_nested_empty_list(
767 &mut self.containers,
768 &mut self.builder,
769 &list_item,
770 nested_marker,
771 );
772 } else {
773 lists::add_list_item(
774 &mut self.containers,
775 &mut self.builder,
776 &list_item,
777 );
778 }
779 } else if let Some(fence) = code_blocks::try_parse_fence_open(content_slice) {
780 self.containers.push(Container::Definition {
781 content_col,
782 plain_open: false,
783 plain_buffer: TextBuffer::new(),
784 });
785 definition_pushed = true;
786
787 let bq_depth = self.current_blockquote_depth();
788 if let Some(indent_str) = indent_to_emit {
789 self.builder
790 .token(SyntaxKind::WHITESPACE.into(), indent_str);
791 }
792 let fence_line = current_line[content_start..].to_string();
793 let new_pos = if self.config.extensions.tex_math_gfm
794 && code_blocks::is_gfm_math_fence(&fence)
795 {
796 code_blocks::parse_fenced_math_block(
797 &mut self.builder,
798 &self.lines,
799 self.pos,
800 fence,
801 bq_depth,
802 content_col,
803 Some(&fence_line),
804 )
805 } else {
806 code_blocks::parse_fenced_code_block(
807 &mut self.builder,
808 &self.lines,
809 self.pos,
810 fence,
811 bq_depth,
812 content_col,
813 Some(&fence_line),
814 )
815 };
816 self.pos = new_pos - 1;
817 } else {
818 let (_, newline_str) = strip_newline(current_line);
819 let (content_without_newline, _) = strip_newline(after_marker_and_spaces);
820 if content_without_newline.is_empty() {
821 plain_buffer.push_line(newline_str);
822 } else {
823 let line_with_newline = if !newline_str.is_empty() {
824 format!("{}{}", content_without_newline, newline_str)
825 } else {
826 content_without_newline.to_string()
827 };
828 plain_buffer.push_line(line_with_newline);
829 }
830 }
831 }
832
833 if !definition_pushed {
834 self.containers.push(Container::Definition {
835 content_col,
836 plain_open: *has_content,
837 plain_buffer,
838 });
839 }
840 }
841 DefinitionPrepared::Term { blank_count } => {
842 self.emit_buffered_plain_if_needed();
843
844 if matches!(self.containers.last(), Some(Container::Paragraph { .. })) {
845 self.close_containers_to(self.containers.depth() - 1);
846 }
847
848 if !definition_lists::in_definition_list(&self.containers) {
849 self.builder.start_node(SyntaxKind::DEFINITION_LIST.into());
850 self.containers.push(Container::DefinitionList {});
851 }
852
853 while matches!(
854 self.containers.last(),
855 Some(Container::Definition { .. }) | Some(Container::DefinitionItem { .. })
856 ) {
857 self.close_containers_to(self.containers.depth() - 1);
858 }
859
860 self.builder.start_node(SyntaxKind::DEFINITION_ITEM.into());
861 self.containers.push(Container::DefinitionItem {});
862
863 emit_term(&mut self.builder, content, self.config);
864
865 for i in 0..*blank_count {
866 let blank_pos = self.pos + 1 + i;
867 if blank_pos < self.lines.len() {
868 let blank_line = self.lines[blank_pos];
869 self.builder.start_node(SyntaxKind::BLANK_LINE.into());
870 self.builder
871 .token(SyntaxKind::BLANK_LINE.into(), blank_line);
872 self.builder.finish_node();
873 }
874 }
875 self.pos += *blank_count;
876 }
877 }
878 }
879
880 fn blockquote_marker_info(
882 &self,
883 payload: Option<&BlockQuotePrepared>,
884 line: &str,
885 ) -> Vec<marker_utils::BlockQuoteMarkerInfo> {
886 payload
887 .map(|payload| payload.marker_info.clone())
888 .unwrap_or_else(|| parse_blockquote_marker_info(line))
889 }
890
891 fn emit_blockquote_markers(
892 &mut self,
893 marker_info: &[marker_utils::BlockQuoteMarkerInfo],
894 depth: usize,
895 ) {
896 for i in 0..depth {
897 if let Some(info) = marker_info.get(i) {
898 blockquotes::emit_one_blockquote_marker(
899 &mut self.builder,
900 info.leading_spaces,
901 info.has_trailing_space,
902 );
903 }
904 }
905 }
906
907 fn current_blockquote_depth(&self) -> usize {
908 blockquotes::current_blockquote_depth(&self.containers)
909 }
910
911 fn emit_or_buffer_blockquote_marker(
916 &mut self,
917 leading_spaces: usize,
918 has_trailing_space: bool,
919 ) {
920 if matches!(self.containers.last(), Some(Container::Paragraph { .. })) {
922 paragraphs::append_paragraph_marker(
924 &mut self.containers,
925 leading_spaces,
926 has_trailing_space,
927 );
928 } else {
929 blockquotes::emit_one_blockquote_marker(
931 &mut self.builder,
932 leading_spaces,
933 has_trailing_space,
934 );
935 }
936 }
937
938 fn parse_document_stack(&mut self) {
939 self.builder.start_node(SyntaxKind::DOCUMENT.into());
940
941 log::debug!("Starting document parse");
942
943 while self.pos < self.lines.len() {
946 let line = self.lines[self.pos];
947
948 log::debug!("Parsing line {}: {}", self.pos + 1, line);
949
950 if self.parse_line(line) {
951 continue;
952 }
953 self.pos += 1;
954 }
955
956 self.close_containers_to(0);
957 self.builder.finish_node(); }
959
960 fn parse_line(&mut self, line: &str) -> bool {
962 let (bq_depth, inner_content) = count_blockquote_markers(line);
964 let current_bq_depth = self.current_blockquote_depth();
965
966 let has_blank_before = self.pos == 0 || self.lines[self.pos - 1].trim().is_empty();
967 let mut blockquote_match: Option<PreparedBlockMatch> = None;
968 let dispatcher_ctx = if current_bq_depth == 0 {
969 Some(BlockContext {
970 content: line,
971 has_blank_before,
972 has_blank_before_strict: has_blank_before,
973 at_document_start: self.pos == 0,
974 in_fenced_div: self.in_fenced_div(),
975 blockquote_depth: current_bq_depth,
976 config: self.config,
977 content_indent: 0,
978 indent_to_emit: None,
979 list_indent_info: None,
980 in_list: lists::in_list(&self.containers),
981 next_line: if self.pos + 1 < self.lines.len() {
982 Some(self.lines[self.pos + 1])
983 } else {
984 None
985 },
986 })
987 } else {
988 None
989 };
990
991 let blockquote_payload = if let Some(dispatcher_ctx) = dispatcher_ctx.as_ref() {
992 self.block_registry
993 .detect_prepared(dispatcher_ctx, &self.lines, self.pos)
994 .and_then(|prepared| {
995 if matches!(prepared.effect, BlockEffect::OpenBlockQuote) {
996 blockquote_match = Some(prepared);
997 blockquote_match.as_ref().and_then(|prepared| {
998 prepared
999 .payload
1000 .as_ref()
1001 .and_then(|payload| payload.downcast_ref::<BlockQuotePrepared>())
1002 .cloned()
1003 })
1004 } else {
1005 None
1006 }
1007 })
1008 } else {
1009 None
1010 };
1011
1012 log::debug!(
1013 "parse_line [{}]: bq_depth={}, current_bq={}, depth={}, line={:?}",
1014 self.pos,
1015 bq_depth,
1016 current_bq_depth,
1017 self.containers.depth(),
1018 line.trim_end()
1019 );
1020
1021 let is_blank = line.trim_end_matches('\n').trim().is_empty()
1026 || (bq_depth > 0 && inner_content.trim_end_matches('\n').trim().is_empty());
1027
1028 if is_blank {
1029 if self.is_paragraph_open()
1030 && paragraphs::has_open_inline_math_environment(&self.containers)
1031 {
1032 paragraphs::append_paragraph_line(
1033 &mut self.containers,
1034 &mut self.builder,
1035 line,
1036 self.config,
1037 );
1038 self.pos += 1;
1039 return true;
1040 }
1041
1042 self.close_paragraph_if_open();
1044
1045 self.emit_buffered_plain_if_needed();
1049
1050 if bq_depth > current_bq_depth {
1056 for _ in current_bq_depth..bq_depth {
1058 self.builder.start_node(SyntaxKind::BLOCK_QUOTE.into());
1059 self.containers.push(Container::BlockQuote {});
1060 }
1061 } else if bq_depth < current_bq_depth {
1062 self.close_blockquotes_to_depth(bq_depth);
1064 }
1065
1066 let mut peek = self.pos + 1;
1068 while peek < self.lines.len() && self.lines[peek].trim().is_empty() {
1069 peek += 1;
1070 }
1071
1072 let levels_to_keep = if peek < self.lines.len() {
1074 ContinuationPolicy::new(self.config, &self.block_registry).compute_levels_to_keep(
1075 self.current_blockquote_depth(),
1076 &self.containers,
1077 &self.lines,
1078 peek,
1079 self.lines[peek],
1080 )
1081 } else {
1082 0
1083 };
1084 log::trace!(
1085 "Blank line: depth={}, levels_to_keep={}, next='{}'",
1086 self.containers.depth(),
1087 levels_to_keep,
1088 if peek < self.lines.len() {
1089 self.lines[peek]
1090 } else {
1091 "<EOF>"
1092 }
1093 );
1094
1095 while self.containers.depth() > levels_to_keep {
1099 match self.containers.last() {
1100 Some(Container::ListItem { .. }) => {
1101 log::debug!(
1103 "Closing ListItem at blank line (levels_to_keep={} < depth={})",
1104 levels_to_keep,
1105 self.containers.depth()
1106 );
1107 self.close_containers_to(self.containers.depth() - 1);
1108 }
1109 Some(Container::List { .. })
1110 | Some(Container::FootnoteDefinition { .. })
1111 | Some(Container::Alert { .. })
1112 | Some(Container::Paragraph { .. })
1113 | Some(Container::Definition { .. })
1114 | Some(Container::DefinitionItem { .. })
1115 | Some(Container::DefinitionList { .. }) => {
1116 log::debug!(
1117 "Closing {:?} at blank line (depth {} > levels_to_keep {})",
1118 self.containers.last(),
1119 self.containers.depth(),
1120 levels_to_keep
1121 );
1122
1123 self.close_containers_to(self.containers.depth() - 1);
1124 }
1125 _ => break,
1126 }
1127 }
1128
1129 if matches!(self.containers.last(), Some(Container::ListItem { .. })) {
1133 self.emit_list_item_buffer_if_needed();
1134 }
1135
1136 if bq_depth > 0 {
1138 let marker_info = self.blockquote_marker_info(blockquote_payload.as_ref(), line);
1139 self.emit_blockquote_markers(&marker_info, bq_depth);
1140 }
1141
1142 self.builder.start_node(SyntaxKind::BLANK_LINE.into());
1143 self.builder
1144 .token(SyntaxKind::BLANK_LINE.into(), inner_content);
1145 self.builder.finish_node();
1146
1147 self.pos += 1;
1148 return true;
1149 }
1150
1151 if bq_depth > current_bq_depth {
1153 if self.config.extensions.blank_before_blockquote
1156 && current_bq_depth == 0
1157 && !blockquote_payload
1158 .as_ref()
1159 .map(|payload| payload.can_start)
1160 .unwrap_or_else(|| blockquotes::can_start_blockquote(self.pos, &self.lines))
1161 {
1162 paragraphs::start_paragraph_if_needed(&mut self.containers, &mut self.builder);
1164 paragraphs::append_paragraph_line(
1165 &mut self.containers,
1166 &mut self.builder,
1167 line,
1168 self.config,
1169 );
1170 self.pos += 1;
1171 return true;
1172 }
1173
1174 let can_nest = if current_bq_depth > 0 {
1177 if self.config.extensions.blank_before_blockquote {
1178 matches!(self.containers.last(), Some(Container::BlockQuote { .. }))
1180 || (self.pos > 0 && {
1181 let prev_line = self.lines[self.pos - 1];
1182 let (prev_bq_depth, prev_inner) = count_blockquote_markers(prev_line);
1183 prev_bq_depth >= current_bq_depth && prev_inner.trim().is_empty()
1184 })
1185 } else {
1186 true
1187 }
1188 } else {
1189 blockquote_payload
1190 .as_ref()
1191 .map(|payload| payload.can_nest)
1192 .unwrap_or(true)
1193 };
1194
1195 if !can_nest {
1196 let content_at_current_depth =
1199 blockquotes::strip_n_blockquote_markers(line, current_bq_depth);
1200
1201 let marker_info = self.blockquote_marker_info(blockquote_payload.as_ref(), line);
1203 for i in 0..current_bq_depth {
1204 if let Some(info) = marker_info.get(i) {
1205 self.emit_or_buffer_blockquote_marker(
1206 info.leading_spaces,
1207 info.has_trailing_space,
1208 );
1209 }
1210 }
1211
1212 if matches!(self.containers.last(), Some(Container::Paragraph { .. })) {
1213 paragraphs::append_paragraph_line(
1215 &mut self.containers,
1216 &mut self.builder,
1217 content_at_current_depth,
1218 self.config,
1219 );
1220 self.pos += 1;
1221 return true;
1222 } else {
1223 paragraphs::start_paragraph_if_needed(&mut self.containers, &mut self.builder);
1225 paragraphs::append_paragraph_line(
1226 &mut self.containers,
1227 &mut self.builder,
1228 content_at_current_depth,
1229 self.config,
1230 );
1231 self.pos += 1;
1232 return true;
1233 }
1234 }
1235
1236 if matches!(self.containers.last(), Some(Container::Paragraph { .. })) {
1238 self.close_containers_to(self.containers.depth() - 1);
1239 }
1240
1241 let marker_info = self.blockquote_marker_info(blockquote_payload.as_ref(), line);
1243
1244 if let (Some(dispatcher_ctx), Some(prepared)) =
1245 (dispatcher_ctx.as_ref(), blockquote_match.as_ref())
1246 {
1247 let _ = self.block_registry.parse_prepared(
1248 prepared,
1249 dispatcher_ctx,
1250 &mut self.builder,
1251 &self.lines,
1252 self.pos,
1253 );
1254 for _ in 0..bq_depth {
1255 self.containers.push(Container::BlockQuote {});
1256 }
1257 } else {
1258 for level in 0..current_bq_depth {
1260 if let Some(info) = marker_info.get(level) {
1261 self.emit_or_buffer_blockquote_marker(
1262 info.leading_spaces,
1263 info.has_trailing_space,
1264 );
1265 }
1266 }
1267
1268 for level in current_bq_depth..bq_depth {
1270 self.builder.start_node(SyntaxKind::BLOCK_QUOTE.into());
1271
1272 if let Some(info) = marker_info.get(level) {
1274 blockquotes::emit_one_blockquote_marker(
1275 &mut self.builder,
1276 info.leading_spaces,
1277 info.has_trailing_space,
1278 );
1279 }
1280
1281 self.containers.push(Container::BlockQuote {});
1282 }
1283 }
1284
1285 return self.parse_inner_content(inner_content, Some(inner_content));
1288 } else if bq_depth < current_bq_depth {
1289 if bq_depth == 0 {
1292 if matches!(self.containers.last(), Some(Container::Paragraph { .. })) {
1294 paragraphs::append_paragraph_line(
1295 &mut self.containers,
1296 &mut self.builder,
1297 line,
1298 self.config,
1299 );
1300 self.pos += 1;
1301 return true;
1302 }
1303
1304 if lists::in_blockquote_list(&self.containers)
1307 && let Some(marker_match) = try_parse_list_marker(line, self.config)
1308 {
1309 let (indent_cols, indent_bytes) = leading_indent(line);
1310 if let Some(level) = lists::find_matching_list_level(
1311 &self.containers,
1312 &marker_match.marker,
1313 indent_cols,
1314 ) {
1315 self.close_containers_to(level + 1);
1318
1319 if matches!(self.containers.last(), Some(Container::Paragraph { .. })) {
1321 self.close_containers_to(self.containers.depth() - 1);
1322 }
1323 if matches!(self.containers.last(), Some(Container::ListItem { .. })) {
1324 self.close_containers_to(self.containers.depth() - 1);
1325 }
1326
1327 if let Some(nested_marker) = is_content_nested_bullet_marker(
1329 line,
1330 marker_match.marker_len,
1331 marker_match.spaces_after_bytes,
1332 ) {
1333 let list_item = ListItemEmissionInput {
1334 content: line,
1335 marker_len: marker_match.marker_len,
1336 spaces_after_cols: marker_match.spaces_after_cols,
1337 spaces_after_bytes: marker_match.spaces_after_bytes,
1338 indent_cols,
1339 indent_bytes,
1340 };
1341 lists::add_list_item_with_nested_empty_list(
1342 &mut self.containers,
1343 &mut self.builder,
1344 &list_item,
1345 nested_marker,
1346 );
1347 } else {
1348 let list_item = ListItemEmissionInput {
1349 content: line,
1350 marker_len: marker_match.marker_len,
1351 spaces_after_cols: marker_match.spaces_after_cols,
1352 spaces_after_bytes: marker_match.spaces_after_bytes,
1353 indent_cols,
1354 indent_bytes,
1355 };
1356 lists::add_list_item(
1357 &mut self.containers,
1358 &mut self.builder,
1359 &list_item,
1360 );
1361 }
1362 self.pos += 1;
1363 return true;
1364 }
1365 }
1366 }
1367
1368 if matches!(self.containers.last(), Some(Container::Paragraph { .. })) {
1370 self.close_containers_to(self.containers.depth() - 1);
1371 }
1372
1373 self.close_blockquotes_to_depth(bq_depth);
1375
1376 if bq_depth > 0 {
1378 let marker_info = parse_blockquote_marker_info(line);
1380 for i in 0..bq_depth {
1381 if let Some(info) = marker_info.get(i) {
1382 self.emit_or_buffer_blockquote_marker(
1383 info.leading_spaces,
1384 info.has_trailing_space,
1385 );
1386 }
1387 }
1388 return self.parse_inner_content(inner_content, Some(inner_content));
1390 } else {
1391 return self.parse_inner_content(line, None);
1393 }
1394 } else if bq_depth > 0 {
1395 let mut list_item_continuation = false;
1397
1398 if matches!(
1401 self.containers.last(),
1402 Some(Container::ListItem { content_col: _, .. })
1403 ) {
1404 let (indent_cols, _) = leading_indent(inner_content);
1405 let content_indent = self.content_container_indent_to_strip();
1406 let effective_indent = indent_cols.saturating_sub(content_indent);
1407 let content_col = match self.containers.last() {
1408 Some(Container::ListItem { content_col, .. }) => *content_col,
1409 _ => 0,
1410 };
1411
1412 let is_new_item_at_outer_level =
1414 if try_parse_list_marker(inner_content, self.config).is_some() {
1415 effective_indent < content_col
1416 } else {
1417 false
1418 };
1419
1420 if is_new_item_at_outer_level || effective_indent < content_col {
1424 log::debug!(
1425 "Closing ListItem: is_new_item={}, effective_indent={} < content_col={}",
1426 is_new_item_at_outer_level,
1427 effective_indent,
1428 content_col
1429 );
1430 self.close_containers_to(self.containers.depth() - 1);
1431 } else {
1432 log::debug!(
1433 "Keeping ListItem: effective_indent={} >= content_col={}",
1434 effective_indent,
1435 content_col
1436 );
1437 list_item_continuation = true;
1438 }
1439 }
1440
1441 if list_item_continuation && code_blocks::try_parse_fence_open(inner_content).is_some()
1445 {
1446 list_item_continuation = false;
1447 }
1448
1449 if !list_item_continuation {
1450 let marker_info = parse_blockquote_marker_info(line);
1451 for i in 0..bq_depth {
1452 if let Some(info) = marker_info.get(i) {
1453 self.emit_or_buffer_blockquote_marker(
1454 info.leading_spaces,
1455 info.has_trailing_space,
1456 );
1457 }
1458 }
1459 }
1460 let line_to_append = if list_item_continuation {
1463 Some(line)
1464 } else {
1465 Some(inner_content)
1466 };
1467 return self.parse_inner_content(inner_content, line_to_append);
1468 }
1469
1470 if current_bq_depth > 0 {
1473 if matches!(self.containers.last(), Some(Container::Paragraph { .. })) {
1475 paragraphs::append_paragraph_line(
1476 &mut self.containers,
1477 &mut self.builder,
1478 line,
1479 self.config,
1480 );
1481 self.pos += 1;
1482 return true;
1483 }
1484
1485 if lists::in_blockquote_list(&self.containers)
1487 && let Some(marker_match) = try_parse_list_marker(line, self.config)
1488 {
1489 let (indent_cols, indent_bytes) = leading_indent(line);
1490 if let Some(level) = lists::find_matching_list_level(
1491 &self.containers,
1492 &marker_match.marker,
1493 indent_cols,
1494 ) {
1495 self.close_containers_to(level + 1);
1497
1498 if matches!(self.containers.last(), Some(Container::Paragraph { .. })) {
1500 self.close_containers_to(self.containers.depth() - 1);
1501 }
1502 if matches!(self.containers.last(), Some(Container::ListItem { .. })) {
1503 self.close_containers_to(self.containers.depth() - 1);
1504 }
1505
1506 if let Some(nested_marker) = is_content_nested_bullet_marker(
1508 line,
1509 marker_match.marker_len,
1510 marker_match.spaces_after_bytes,
1511 ) {
1512 let list_item = ListItemEmissionInput {
1513 content: line,
1514 marker_len: marker_match.marker_len,
1515 spaces_after_cols: marker_match.spaces_after_cols,
1516 spaces_after_bytes: marker_match.spaces_after_bytes,
1517 indent_cols,
1518 indent_bytes,
1519 };
1520 lists::add_list_item_with_nested_empty_list(
1521 &mut self.containers,
1522 &mut self.builder,
1523 &list_item,
1524 nested_marker,
1525 );
1526 } else {
1527 let list_item = ListItemEmissionInput {
1528 content: line,
1529 marker_len: marker_match.marker_len,
1530 spaces_after_cols: marker_match.spaces_after_cols,
1531 spaces_after_bytes: marker_match.spaces_after_bytes,
1532 indent_cols,
1533 indent_bytes,
1534 };
1535 lists::add_list_item(&mut self.containers, &mut self.builder, &list_item);
1536 }
1537 self.pos += 1;
1538 return true;
1539 }
1540 }
1541 }
1542
1543 self.parse_inner_content(line, None)
1545 }
1546
1547 fn content_container_indent_to_strip(&self) -> usize {
1549 self.containers
1550 .stack
1551 .iter()
1552 .filter_map(|c| match c {
1553 Container::FootnoteDefinition { content_col, .. } => Some(*content_col),
1554 Container::Definition { content_col, .. } => Some(*content_col),
1555 _ => None,
1556 })
1557 .sum()
1558 }
1559
1560 fn parse_inner_content(&mut self, content: &str, line_to_append: Option<&str>) -> bool {
1566 log::debug!(
1567 "parse_inner_content [{}]: depth={}, last={:?}, content={:?}",
1568 self.pos,
1569 self.containers.depth(),
1570 self.containers.last(),
1571 content.trim_end()
1572 );
1573 let content_indent = self.content_container_indent_to_strip();
1576 let (stripped_content, indent_to_emit) = if content_indent > 0 {
1577 let (indent_cols, _) = leading_indent(content);
1578 if indent_cols >= content_indent {
1579 let idx = byte_index_at_column(content, content_indent);
1580 (&content[idx..], Some(&content[..idx]))
1581 } else {
1582 let trimmed_start = content.trim_start();
1584 let ws_len = content.len() - trimmed_start.len();
1585 if ws_len > 0 {
1586 (trimmed_start, Some(&content[..ws_len]))
1587 } else {
1588 (content, None)
1589 }
1590 }
1591 } else {
1592 (content, None)
1593 };
1594
1595 if self.config.extensions.alerts
1596 && self.current_blockquote_depth() > 0
1597 && !self.in_active_alert()
1598 && !self.is_paragraph_open()
1599 && let Some(marker) = Self::alert_marker_from_content(stripped_content)
1600 {
1601 let (_, newline_str) = strip_newline(stripped_content);
1602 self.builder.start_node(SyntaxKind::ALERT.into());
1603 self.builder.token(SyntaxKind::ALERT_MARKER.into(), marker);
1604 if !newline_str.is_empty() {
1605 self.builder.token(SyntaxKind::NEWLINE.into(), newline_str);
1606 }
1607 self.containers.push(Container::Alert {
1608 blockquote_depth: self.current_blockquote_depth(),
1609 });
1610 self.pos += 1;
1611 return true;
1612 }
1613
1614 if matches!(self.containers.last(), Some(Container::Definition { .. })) {
1618 let is_definition_marker =
1619 definition_lists::try_parse_definition_marker(stripped_content).is_some()
1620 && !stripped_content.starts_with(':');
1621 if content_indent == 0 && is_definition_marker {
1622 } else {
1624 let policy = ContinuationPolicy::new(self.config, &self.block_registry);
1625
1626 if policy.definition_plain_can_continue(
1627 stripped_content,
1628 content,
1629 content_indent,
1630 &BlockContext {
1631 content: stripped_content,
1632 has_blank_before: self.pos == 0
1633 || self.lines[self.pos - 1].trim().is_empty(),
1634 has_blank_before_strict: self.pos == 0
1635 || self.lines[self.pos - 1].trim().is_empty(),
1636 at_document_start: self.pos == 0 && self.current_blockquote_depth() == 0,
1637 in_fenced_div: self.in_fenced_div(),
1638 blockquote_depth: self.current_blockquote_depth(),
1639 config: self.config,
1640 content_indent,
1641 indent_to_emit: None,
1642 list_indent_info: None,
1643 in_list: lists::in_list(&self.containers),
1644 next_line: if self.pos + 1 < self.lines.len() {
1645 Some(self.lines[self.pos + 1])
1646 } else {
1647 None
1648 },
1649 },
1650 &self.lines,
1651 self.pos,
1652 ) {
1653 let content_line = stripped_content;
1654 let (text_without_newline, newline_str) = strip_newline(content_line);
1655 let indent_prefix = if !text_without_newline.trim().is_empty() {
1656 indent_to_emit.unwrap_or("")
1657 } else {
1658 ""
1659 };
1660 let content_line = format!("{}{}", indent_prefix, text_without_newline);
1661
1662 if let Some(Container::Definition {
1663 plain_open,
1664 plain_buffer,
1665 ..
1666 }) = self.containers.stack.last_mut()
1667 {
1668 let line_with_newline = if !newline_str.is_empty() {
1669 format!("{}{}", content_line, newline_str)
1670 } else {
1671 content_line
1672 };
1673 plain_buffer.push_line(line_with_newline);
1674 *plain_open = true;
1675 }
1676
1677 self.pos += 1;
1678 return true;
1679 }
1680 }
1681 }
1682
1683 if content_indent > 0 {
1686 let (bq_depth, inner_content) = count_blockquote_markers(stripped_content);
1687 let current_bq_depth = self.current_blockquote_depth();
1688
1689 if bq_depth > 0 {
1690 self.emit_buffered_plain_if_needed();
1693 self.emit_list_item_buffer_if_needed();
1694
1695 self.close_paragraph_if_open();
1698
1699 if bq_depth > current_bq_depth {
1700 let marker_info = parse_blockquote_marker_info(stripped_content);
1701
1702 for level in current_bq_depth..bq_depth {
1704 self.builder.start_node(SyntaxKind::BLOCK_QUOTE.into());
1705
1706 if level == current_bq_depth
1707 && let Some(indent_str) = indent_to_emit
1708 {
1709 self.builder
1710 .token(SyntaxKind::WHITESPACE.into(), indent_str);
1711 }
1712
1713 if let Some(info) = marker_info.get(level) {
1714 blockquotes::emit_one_blockquote_marker(
1715 &mut self.builder,
1716 info.leading_spaces,
1717 info.has_trailing_space,
1718 );
1719 }
1720
1721 self.containers.push(Container::BlockQuote {});
1722 }
1723 } else if bq_depth < current_bq_depth {
1724 self.close_blockquotes_to_depth(bq_depth);
1725 } else {
1726 let marker_info = parse_blockquote_marker_info(stripped_content);
1728 self.emit_blockquote_markers(&marker_info, bq_depth);
1729 }
1730
1731 return self.parse_inner_content(inner_content, Some(inner_content));
1732 }
1733 }
1734
1735 let content = stripped_content;
1737
1738 if self.is_paragraph_open()
1739 && paragraphs::has_open_inline_math_environment(&self.containers)
1740 {
1741 paragraphs::append_paragraph_line(
1742 &mut self.containers,
1743 &mut self.builder,
1744 line_to_append.unwrap_or(self.lines[self.pos]),
1745 self.config,
1746 );
1747 self.pos += 1;
1748 return true;
1749 }
1750
1751 use super::blocks::lists;
1755 use super::blocks::paragraphs;
1756 let list_indent_info = if lists::in_list(&self.containers) {
1757 let content_col = paragraphs::current_content_col(&self.containers);
1758 if content_col > 0 {
1759 Some(super::block_dispatcher::ListIndentInfo { content_col })
1760 } else {
1761 None
1762 }
1763 } else {
1764 None
1765 };
1766
1767 let next_line = if self.pos + 1 < self.lines.len() {
1768 Some(count_blockquote_markers(self.lines[self.pos + 1]).1)
1771 } else {
1772 None
1773 };
1774
1775 let current_bq_depth = self.current_blockquote_depth();
1776 if let Some(alert_bq_depth) = self.active_alert_blockquote_depth()
1777 && current_bq_depth < alert_bq_depth
1778 {
1779 while matches!(self.containers.last(), Some(Container::Alert { .. })) {
1780 self.close_containers_to(self.containers.depth() - 1);
1781 }
1782 }
1783
1784 let dispatcher_ctx = BlockContext {
1785 content,
1786 has_blank_before: false, has_blank_before_strict: false, at_document_start: false, in_fenced_div: self.in_fenced_div(),
1790 blockquote_depth: current_bq_depth,
1791 config: self.config,
1792 content_indent,
1793 indent_to_emit,
1794 list_indent_info,
1795 in_list: lists::in_list(&self.containers),
1796 next_line,
1797 };
1798
1799 let mut dispatcher_ctx = dispatcher_ctx;
1802
1803 let dispatcher_match =
1806 self.block_registry
1807 .detect_prepared(&dispatcher_ctx, &self.lines, self.pos);
1808
1809 let after_metadata_block = std::mem::replace(&mut self.after_metadata_block, false);
1815 let has_blank_before = if self.pos == 0 || after_metadata_block {
1816 true
1817 } else {
1818 let prev_line = self.lines[self.pos - 1];
1819 let (prev_bq_depth, prev_inner) = count_blockquote_markers(prev_line);
1820 let (prev_inner_no_nl, _) = strip_newline(prev_inner);
1821 let prev_is_fenced_div_open = self.config.extensions.fenced_divs
1822 && fenced_divs::try_parse_div_fence_open(
1823 strip_n_blockquote_markers(prev_inner_no_nl, prev_bq_depth).trim_start(),
1824 )
1825 .is_some();
1826
1827 prev_line.trim().is_empty()
1828 || prev_is_fenced_div_open
1829 || matches!(self.containers.last(), Some(Container::BlockQuote { .. }))
1830 };
1831
1832 let at_document_start = self.pos == 0 && current_bq_depth == 0;
1835
1836 let prev_line_blank = if self.pos > 0 {
1837 let prev_line = self.lines[self.pos - 1];
1838 let (prev_bq_depth, prev_inner) = count_blockquote_markers(prev_line);
1839 prev_line.trim().is_empty() || (prev_bq_depth > 0 && prev_inner.trim().is_empty())
1840 } else {
1841 false
1842 };
1843 let has_blank_before_strict = at_document_start || prev_line_blank;
1844
1845 dispatcher_ctx.has_blank_before = has_blank_before;
1846 dispatcher_ctx.has_blank_before_strict = has_blank_before_strict;
1847 dispatcher_ctx.at_document_start = at_document_start;
1848
1849 let dispatcher_match =
1850 if dispatcher_ctx.has_blank_before || dispatcher_ctx.at_document_start {
1851 self.block_registry
1853 .detect_prepared(&dispatcher_ctx, &self.lines, self.pos)
1854 } else {
1855 dispatcher_match
1856 };
1857
1858 if has_blank_before {
1859 if let Some(env_name) = extract_environment_name(content)
1860 && is_inline_math_environment(&env_name)
1861 {
1862 if !self.is_paragraph_open() {
1863 paragraphs::start_paragraph_if_needed(&mut self.containers, &mut self.builder);
1864 }
1865 paragraphs::append_paragraph_line(
1866 &mut self.containers,
1867 &mut self.builder,
1868 line_to_append.unwrap_or(self.lines[self.pos]),
1869 self.config,
1870 );
1871 self.pos += 1;
1872 return true;
1873 }
1874
1875 if let Some(block_match) = dispatcher_match.as_ref() {
1876 let detection = block_match.detection;
1877
1878 match detection {
1879 BlockDetectionResult::YesCanInterrupt => {
1880 self.emit_list_item_buffer_if_needed();
1881 if self.is_paragraph_open() {
1882 self.close_containers_to(self.containers.depth() - 1);
1883 }
1884 }
1885 BlockDetectionResult::Yes => {
1886 self.prepare_for_block_element();
1887 }
1888 BlockDetectionResult::No => unreachable!(),
1889 }
1890
1891 if matches!(block_match.effect, BlockEffect::CloseFencedDiv) {
1892 self.close_containers_to_fenced_div();
1893 }
1894
1895 let lines_consumed = self.block_registry.parse_prepared(
1896 block_match,
1897 &dispatcher_ctx,
1898 &mut self.builder,
1899 &self.lines,
1900 self.pos,
1901 );
1902
1903 if matches!(
1904 self.block_registry.parser_name(block_match),
1905 "yaml_metadata" | "pandoc_title_block" | "mmd_title_block"
1906 ) {
1907 self.after_metadata_block = true;
1908 }
1909
1910 match block_match.effect {
1911 BlockEffect::None => {}
1912 BlockEffect::OpenFencedDiv => {
1913 self.containers.push(Container::FencedDiv {});
1914 }
1915 BlockEffect::CloseFencedDiv => {
1916 self.close_fenced_div();
1917 }
1918 BlockEffect::OpenFootnoteDefinition => {
1919 self.handle_footnote_open_effect(block_match, content);
1920 }
1921 BlockEffect::OpenList => {
1922 self.handle_list_open_effect(block_match, content, indent_to_emit);
1923 }
1924 BlockEffect::OpenDefinitionList => {
1925 self.handle_definition_list_effect(block_match, content, indent_to_emit);
1926 }
1927 BlockEffect::OpenBlockQuote => {
1928 }
1930 }
1931
1932 if lines_consumed == 0 {
1933 log::warn!(
1934 "block parser made no progress at line {} (parser={})",
1935 self.pos + 1,
1936 self.block_registry.parser_name(block_match)
1937 );
1938 return false;
1939 }
1940
1941 self.pos += lines_consumed;
1942 return true;
1943 }
1944 } else if let Some(block_match) = dispatcher_match.as_ref() {
1945 let parser_name = self.block_registry.parser_name(block_match);
1948 match block_match.detection {
1949 BlockDetectionResult::YesCanInterrupt => {
1950 if matches!(block_match.effect, BlockEffect::OpenFencedDiv)
1951 && self.is_paragraph_open()
1952 {
1953 if !self.is_paragraph_open() {
1955 paragraphs::start_paragraph_if_needed(
1956 &mut self.containers,
1957 &mut self.builder,
1958 );
1959 }
1960 paragraphs::append_paragraph_line(
1961 &mut self.containers,
1962 &mut self.builder,
1963 line_to_append.unwrap_or(self.lines[self.pos]),
1964 self.config,
1965 );
1966 self.pos += 1;
1967 return true;
1968 }
1969
1970 if matches!(block_match.effect, BlockEffect::OpenList)
1971 && self.is_paragraph_open()
1972 && !lists::in_list(&self.containers)
1973 && self.content_container_indent_to_strip() == 0
1974 {
1975 paragraphs::append_paragraph_line(
1977 &mut self.containers,
1978 &mut self.builder,
1979 line_to_append.unwrap_or(self.lines[self.pos]),
1980 self.config,
1981 );
1982 self.pos += 1;
1983 return true;
1984 }
1985
1986 self.emit_list_item_buffer_if_needed();
1987 if self.is_paragraph_open() {
1988 self.close_containers_to(self.containers.depth() - 1);
1989 }
1990 }
1991 BlockDetectionResult::Yes => {
1992 if parser_name == "fenced_div_open" && self.is_paragraph_open() {
1995 if !self.is_paragraph_open() {
1996 paragraphs::start_paragraph_if_needed(
1997 &mut self.containers,
1998 &mut self.builder,
1999 );
2000 }
2001 paragraphs::append_paragraph_line(
2002 &mut self.containers,
2003 &mut self.builder,
2004 line_to_append.unwrap_or(self.lines[self.pos]),
2005 self.config,
2006 );
2007 self.pos += 1;
2008 return true;
2009 }
2010 }
2011 BlockDetectionResult::No => unreachable!(),
2012 }
2013
2014 if !matches!(block_match.detection, BlockDetectionResult::No) {
2015 if matches!(block_match.effect, BlockEffect::CloseFencedDiv) {
2016 self.close_containers_to_fenced_div();
2017 }
2018
2019 let lines_consumed = self.block_registry.parse_prepared(
2020 block_match,
2021 &dispatcher_ctx,
2022 &mut self.builder,
2023 &self.lines,
2024 self.pos,
2025 );
2026
2027 match block_match.effect {
2028 BlockEffect::None => {}
2029 BlockEffect::OpenFencedDiv => {
2030 self.containers.push(Container::FencedDiv {});
2031 }
2032 BlockEffect::CloseFencedDiv => {
2033 self.close_fenced_div();
2034 }
2035 BlockEffect::OpenFootnoteDefinition => {
2036 self.handle_footnote_open_effect(block_match, content);
2037 }
2038 BlockEffect::OpenList => {
2039 self.handle_list_open_effect(block_match, content, indent_to_emit);
2040 }
2041 BlockEffect::OpenDefinitionList => {
2042 self.handle_definition_list_effect(block_match, content, indent_to_emit);
2043 }
2044 BlockEffect::OpenBlockQuote => {
2045 }
2047 }
2048
2049 if lines_consumed == 0 {
2050 log::warn!(
2051 "block parser made no progress at line {} (parser={})",
2052 self.pos + 1,
2053 self.block_registry.parser_name(block_match)
2054 );
2055 return false;
2056 }
2057
2058 self.pos += lines_consumed;
2059 return true;
2060 }
2061 }
2062
2063 if self.config.extensions.line_blocks
2065 && (has_blank_before || self.pos == 0)
2066 && try_parse_line_block_start(content).is_some()
2067 && try_parse_line_block_start(self.lines[self.pos]).is_some()
2071 {
2072 log::debug!("Parsed line block at line {}", self.pos);
2073 self.close_paragraph_if_open();
2075
2076 let new_pos = parse_line_block(&self.lines, self.pos, &mut self.builder, self.config);
2077 if new_pos > self.pos {
2078 self.pos = new_pos;
2079 return true;
2080 }
2081 }
2082
2083 if matches!(self.containers.last(), Some(Container::ListItem { .. })) {
2086 log::debug!(
2087 "Inside ListItem - buffering content: {:?}",
2088 line_to_append.unwrap_or(self.lines[self.pos]).trim_end()
2089 );
2090 let line = line_to_append.unwrap_or(self.lines[self.pos]);
2092
2093 if let Some(Container::ListItem { buffer, .. }) = self.containers.stack.last_mut() {
2095 buffer.push_text(line);
2096 }
2097
2098 self.pos += 1;
2099 return true;
2100 }
2101
2102 log::debug!(
2103 "Not in ListItem - creating paragraph for: {:?}",
2104 line_to_append.unwrap_or(self.lines[self.pos]).trim_end()
2105 );
2106 paragraphs::start_paragraph_if_needed(&mut self.containers, &mut self.builder);
2108 let line = line_to_append.unwrap_or(self.lines[self.pos]);
2111 paragraphs::append_paragraph_line(
2112 &mut self.containers,
2113 &mut self.builder,
2114 line,
2115 self.config,
2116 );
2117 self.pos += 1;
2118 true
2119 }
2120
2121 fn fenced_div_container_index(&self) -> Option<usize> {
2122 self.containers
2123 .stack
2124 .iter()
2125 .rposition(|c| matches!(c, Container::FencedDiv { .. }))
2126 }
2127
2128 fn close_containers_to_fenced_div(&mut self) {
2129 if let Some(index) = self.fenced_div_container_index() {
2130 self.close_containers_to(index + 1);
2131 }
2132 }
2133
2134 fn close_fenced_div(&mut self) {
2135 if let Some(index) = self.fenced_div_container_index() {
2136 self.close_containers_to(index);
2137 }
2138 }
2139
2140 fn in_fenced_div(&self) -> bool {
2141 self.containers
2142 .stack
2143 .iter()
2144 .any(|c| matches!(c, Container::FencedDiv { .. }))
2145 }
2146}