use super::document_parser::{LineEvent, PendingLineEvents}; use super::scratch::LineFlags; use crate::config::MarkdownFlavor; use crate::utils::blockquote::parse_blockquote_prefix; use crate::utils::calculate_indentation_width_default; use crate::utils::code_block_utils::CodeBlockUtils; use pulldown_cmark::{Options, Parser}; use super::types::*; use super::{ListItemMap, SkipByteRanges}; /// The indices of the lines a byte range touches. pub(super) fn spanned_lines(lines: &[LineInfo], start: usize, end: usize) -> std::ops::Range { let first = lines .partition_point(|line| line.byte_offset <= start) .saturating_sub(1); let touched = lines[first..].iter().take_while(|line| line.byte_offset < end).count(); first..first + touched } /// Pre-compute basic line information (without headings/blockquotes) /// Also returns emphasis spans from the shared analysis or a separate dialect reading. pub(super) fn compute_basic_line_info( content: &str, content_lines: &[&str], line_offsets: &[usize], code_blocks: &[(usize, usize)], flavor: MarkdownFlavor, skip_ranges: &SkipByteRanges<'_>, pending_lines: PendingLineEvents, ) -> (Vec, Vec) { let front_matter_end = skip_ranges.front_matter_end; let mut lines = Vec::with_capacity(content_lines.len()); let extension_line_map = if flavor.supports_kramdown_syntax() { Some(compute_kramdown_extension_line_map(content_lines)) } else { None }; // Pre-compute which lines are in code blocks. // // Kramdown extension blocks are non-markdown regions. Fences inside them may cause // pulldown-cmark to report a code block that leaks past {:/...} when the fence is // unclosed inside the extension block. Filter out code blocks that start inside // extension blocks so post-block lines are not incorrectly marked as code. let filtered_code_blocks; let code_blocks_for_map = if let Some(extension_line_map) = &extension_line_map { filtered_code_blocks = filter_code_blocks_starting_in_extension_blocks(content, line_offsets, code_blocks, extension_line_map); filtered_code_blocks.as_slice() } else { code_blocks }; let code_block_map = compute_code_block_line_map(content, line_offsets, code_blocks_for_map); // Pre-compute which lines are in math blocks ($$ ... $$) let math_block_map = if content.contains("$$") { compute_math_block_line_map(content_lines, &code_block_map) } else { LineFlags::new(0) }; // Run pulldown on a masked view of kramdown extension-block lines so parser state // from non-Markdown regions (like unclosed fences) can't leak into real Markdown. // The masked content preserves byte length and newline positions for offset stability. let pulldown_content; let pulldown_input = match extension_line_map.as_deref() { // Only build the masked copy when there is actually an extension block // to mask; otherwise the mask would clone the whole document for nothing. Some(map) if map.iter().any(|&in_ext| in_ext) => { pulldown_content = mask_kramdown_extension_lines(content, line_offsets, map); pulldown_content.as_str() } _ => content, }; // Consume shared list/emphasis facts when the input has the same reading. let (list_item_map, emphasis_spans) = pending_lines.finish( pulldown_input, line_offsets, front_matter_end, code_blocks_for_map, flavor, std::ptr::eq(pulldown_input.as_ptr(), content.as_ptr()), ); for (i, line) in content_lines.iter().enumerate() { let byte_offset = line_offsets.get(i).copied().unwrap_or(0); // Indentation is spaces and tabs only; any other whitespace is content. let indent = line.len() - line.trim_start_matches([' ', '\t']).len(); // Compute visual indent with proper CommonMark tab expansion let visual_indent = calculate_indentation_width_default(line); // Parse blockquote prefix once and reuse it (avoid redundant parsing) let blockquote_parse = parse_blockquote_prefix(line); // For blank detection, consider blockquote context. CommonMark's blank line holds // only spaces and tabs; U+00A0 and the other Unicode spaces are paragraph text. let is_blank_text = |text: &str| text.trim_matches([' ', '\t', '\r']).is_empty(); let is_blank = if let Some(parsed) = blockquote_parse { is_blank_text(parsed.content) } else { is_blank_text(line) }; // Use pre-computed map for O(1) lookup instead of O(m) iteration let in_code_block = code_block_map.get(i).copied().unwrap_or(false); // Detect list items (skip if in frontmatter, in mkdocstrings block, or in HTML comment) let in_mkdocstrings = crate::utils::mkdocstrings_refs::is_within_autodoc_block_ranges(skip_ranges.autodoc_ranges, byte_offset); // Use the line's content bounds (indent and trailing whitespace trimmed) so an // indented HTML comment, whose `