Skip to main content

rumdl_lib/lint_context/
mod.rs

1pub mod types;
2pub use types::*;
3
4mod element_parsers;
5mod flavor_detection;
6mod heading_detection;
7mod line_computation;
8mod link_parser;
9mod list_blocks;
10#[cfg(test)]
11mod tests;
12
13use crate::config::MarkdownFlavor;
14use crate::inline_config::InlineConfig;
15use crate::rules::front_matter_utils::FrontMatterUtils;
16use crate::utils::code_block_utils::CodeBlockUtils;
17use std::collections::HashMap;
18use std::path::PathBuf;
19
20/// Macro for profiling sections - only active in non-WASM builds
21#[cfg(not(target_arch = "wasm32"))]
22macro_rules! profile_section {
23    ($name:expr, $profile:expr, $code:expr) => {{
24        let start = std::time::Instant::now();
25        let result = $code;
26        if $profile {
27            eprintln!("[PROFILE] {}: {:?}", $name, start.elapsed());
28        }
29        result
30    }};
31}
32
33#[cfg(target_arch = "wasm32")]
34macro_rules! profile_section {
35    ($name:expr, $profile:expr, $code:expr) => {{ $code }};
36}
37
38/// Grouped byte ranges for skip context detection
39/// Used to reduce parameter count in internal functions
40pub(super) struct SkipByteRanges<'a> {
41    pub(super) html_comment_ranges: &'a [crate::utils::skip_context::ByteRange],
42    pub(super) autodoc_ranges: &'a [crate::utils::skip_context::ByteRange],
43    pub(super) quarto_div_ranges: &'a [crate::utils::skip_context::ByteRange],
44    pub(super) pymdown_block_ranges: &'a [crate::utils::skip_context::ByteRange],
45}
46
47use std::sync::{Arc, OnceLock};
48
49/// Map from line byte offset to list item data: (is_ordered, marker, marker_column, content_column, number)
50pub(super) type ListItemMap = std::collections::HashMap<usize, (bool, String, usize, usize, Option<usize>)>;
51
52/// Type alias for byte ranges used in JSX expression and MDX comment detection
53pub(super) type ByteRanges = Vec<(usize, usize)>;
54
55pub struct LintContext<'a> {
56    pub content: &'a str,
57    content_lines: Vec<&'a str>, // Pre-split lines from content (avoids repeated allocations)
58    pub line_offsets: Vec<usize>,
59    pub code_blocks: Vec<(usize, usize)>, // Cached code block ranges (not including inline code spans)
60    pub lines: Vec<LineInfo>,             // Pre-computed line information
61    pub links: Vec<ParsedLink<'a>>,       // Pre-parsed links
62    pub images: Vec<ParsedImage<'a>>,     // Pre-parsed images
63    pub broken_links: Vec<BrokenLinkInfo>, // Broken/undefined references
64    pub footnote_refs: Vec<FootnoteRef>,  // Pre-parsed footnote references
65    pub reference_defs: Vec<ReferenceDef>, // Reference definitions
66    reference_defs_map: HashMap<String, usize>, // O(1) lookup by lowercase ID -> index in reference_defs
67    code_spans_cache: OnceLock<Arc<Vec<CodeSpan>>>, // Lazy-loaded inline code spans
68    math_spans_cache: OnceLock<Arc<Vec<MathSpan>>>, // Lazy-loaded math spans ($...$ and $$...$$)
69    pub list_blocks: Vec<ListBlock>,      // Pre-parsed list blocks
70    pub char_frequency: CharFrequency,    // Character frequency analysis
71    html_tags_cache: OnceLock<Arc<Vec<HtmlTag>>>, // Lazy-loaded HTML tags
72    emphasis_spans_cache: OnceLock<Arc<Vec<EmphasisSpan>>>, // Lazy-loaded emphasis spans
73    table_rows_cache: OnceLock<Arc<Vec<TableRow>>>, // Lazy-loaded table rows
74    bare_urls_cache: OnceLock<Arc<Vec<BareUrl>>>, // Lazy-loaded bare URLs
75    has_mixed_list_nesting_cache: OnceLock<bool>, // Cached result for mixed ordered/unordered list nesting detection
76    html_comment_ranges: Vec<crate::utils::skip_context::ByteRange>, // Pre-computed HTML comment ranges
77    pub table_blocks: Vec<crate::utils::table_utils::TableBlock>, // Pre-computed table blocks
78    pub line_index: crate::utils::range_utils::LineIndex<'a>, // Pre-computed line index for byte position calculations
79    jinja_ranges: Vec<(usize, usize)>,    // Pre-computed Jinja template ranges ({{ }}, {% %})
80    pub flavor: MarkdownFlavor,           // Markdown flavor being used
81    pub source_file: Option<PathBuf>,     // Source file path (for rules that need file context)
82    jsx_expression_ranges: Vec<(usize, usize)>, // Pre-computed JSX expression ranges (MDX: {expression})
83    mdx_comment_ranges: Vec<(usize, usize)>, // Pre-computed MDX comment ranges ({/* ... */})
84    citation_ranges: Vec<crate::utils::skip_context::ByteRange>, // Pre-computed Pandoc/Quarto citation ranges (Quarto: @key, [@key])
85    shortcode_ranges: Vec<(usize, usize)>, // Pre-computed Hugo/Quarto shortcode ranges ({{< ... >}} and {{% ... %}})
86    inline_config: InlineConfig,           // Parsed inline configuration comments for rule disabling
87    obsidian_comment_ranges: Vec<(usize, usize)>, // Pre-computed Obsidian comment ranges (%%...%%)
88}
89
90impl<'a> LintContext<'a> {
91    pub fn new(content: &'a str, flavor: MarkdownFlavor, source_file: Option<PathBuf>) -> Self {
92        #[cfg(not(target_arch = "wasm32"))]
93        let profile = std::env::var("RUMDL_PROFILE_QUADRATIC").is_ok();
94        #[cfg(target_arch = "wasm32")]
95        let profile = false;
96
97        let line_offsets = profile_section!("Line offsets", profile, {
98            let mut offsets = vec![0];
99            for (i, c) in content.char_indices() {
100                if c == '\n' {
101                    offsets.push(i + 1);
102                }
103            }
104            offsets
105        });
106
107        // Compute content_lines once for all functions that need it
108        let content_lines: Vec<&str> = content.lines().collect();
109
110        // Detect front matter boundaries once for all functions that need it
111        let front_matter_end = FrontMatterUtils::get_front_matter_end_line(content);
112
113        // Detect code blocks and code spans once and cache them
114        let (code_blocks, code_span_ranges) = profile_section!(
115            "Code blocks",
116            profile,
117            CodeBlockUtils::detect_code_blocks_and_spans(content)
118        );
119
120        // Pre-compute HTML comment ranges ONCE for all operations
121        let html_comment_ranges = profile_section!(
122            "HTML comment ranges",
123            profile,
124            crate::utils::skip_context::compute_html_comment_ranges(content)
125        );
126
127        // Pre-compute autodoc block ranges for MkDocs flavor (avoids O(n^2) scaling)
128        let autodoc_ranges = profile_section!("Autodoc block ranges", profile, {
129            if flavor == MarkdownFlavor::MkDocs {
130                crate::utils::mkdocstrings_refs::detect_autodoc_block_ranges(content)
131            } else {
132                Vec::new()
133            }
134        });
135
136        // Pre-compute Quarto div block ranges for Quarto flavor
137        let quarto_div_ranges = profile_section!("Quarto div ranges", profile, {
138            if flavor == MarkdownFlavor::Quarto {
139                crate::utils::quarto_divs::detect_div_block_ranges(content)
140            } else {
141                Vec::new()
142            }
143        });
144
145        // Pre-compute PyMdown Blocks ranges for MkDocs flavor (/// ... ///)
146        let pymdown_block_ranges = profile_section!("PyMdown block ranges", profile, {
147            if flavor == MarkdownFlavor::MkDocs {
148                crate::utils::pymdown_blocks::detect_block_ranges(content)
149            } else {
150                Vec::new()
151            }
152        });
153
154        // Pre-compute line information AND emphasis spans (without headings/blockquotes yet)
155        // Emphasis spans are captured during the same pulldown-cmark parse as list detection
156        let skip_ranges = SkipByteRanges {
157            html_comment_ranges: &html_comment_ranges,
158            autodoc_ranges: &autodoc_ranges,
159            quarto_div_ranges: &quarto_div_ranges,
160            pymdown_block_ranges: &pymdown_block_ranges,
161        };
162        let (mut lines, emphasis_spans) = profile_section!(
163            "Basic line info",
164            profile,
165            line_computation::compute_basic_line_info(
166                content,
167                &content_lines,
168                &line_offsets,
169                &code_blocks,
170                flavor,
171                &skip_ranges,
172                front_matter_end,
173            )
174        );
175
176        // Detect HTML blocks BEFORE heading detection
177        profile_section!(
178            "HTML blocks",
179            profile,
180            heading_detection::detect_html_blocks(content, &mut lines)
181        );
182
183        // Detect ESM import/export blocks in MDX files BEFORE heading detection
184        profile_section!(
185            "ESM blocks",
186            profile,
187            flavor_detection::detect_esm_blocks(content, &mut lines, flavor)
188        );
189
190        // Detect JSX expressions and MDX comments in MDX files
191        let (jsx_expression_ranges, mdx_comment_ranges) = profile_section!(
192            "JSX/MDX detection",
193            profile,
194            flavor_detection::detect_jsx_and_mdx_comments(content, &mut lines, flavor, &code_blocks)
195        );
196
197        // Detect MkDocs-specific constructs (admonitions, tabs, definition lists)
198        profile_section!(
199            "MkDocs constructs",
200            profile,
201            flavor_detection::detect_mkdocs_line_info(&content_lines, &mut lines, flavor)
202        );
203
204        // Detect kramdown constructs (extension blocks, IALs, ALDs) in kramdown flavor
205        profile_section!(
206            "Kramdown constructs",
207            profile,
208            flavor_detection::detect_kramdown_line_info(content, &mut lines, flavor)
209        );
210
211        // Layer 1: Sanitize content-derived fields inside kramdown extension blocks
212        // so downstream heading detection and collection builders never see them.
213        // This must run BEFORE detect_headings_and_blockquotes to prevent headings
214        // from being populated inside extension blocks.
215        for line in &mut lines {
216            if line.in_kramdown_extension_block {
217                line.list_item = None;
218                line.is_horizontal_rule = false;
219                line.blockquote = None;
220                line.is_kramdown_block_ial = false;
221            }
222        }
223
224        // Detect Obsidian comments (%%...%%) in Obsidian flavor
225        let obsidian_comment_ranges = profile_section!(
226            "Obsidian comments",
227            profile,
228            flavor_detection::detect_obsidian_comments(content, &mut lines, flavor, &code_span_ranges)
229        );
230
231        // Collect link byte ranges early for heading detection (to skip lines inside link syntax)
232        let link_byte_ranges = profile_section!(
233            "Link byte ranges",
234            profile,
235            link_parser::collect_link_byte_ranges(content)
236        );
237
238        // Now detect headings and blockquotes
239        profile_section!(
240            "Headings & blockquotes",
241            profile,
242            heading_detection::detect_headings_and_blockquotes(
243                &content_lines,
244                &mut lines,
245                flavor,
246                &html_comment_ranges,
247                &link_byte_ranges,
248                front_matter_end,
249            )
250        );
251
252        // Clear headings that were detected inside kramdown extension blocks
253        for line in &mut lines {
254            if line.in_kramdown_extension_block {
255                line.heading = None;
256            }
257        }
258
259        // Parse code spans early so we can exclude them from link/image parsing
260        let code_spans = profile_section!(
261            "Code spans",
262            profile,
263            element_parsers::build_code_spans_from_ranges(content, &lines, &code_span_ranges)
264        );
265
266        // Mark lines that are continuations of multi-line code spans
267        // This is needed for parse_list_blocks to correctly handle list items with multi-line code spans
268        for span in &code_spans {
269            if span.end_line > span.line {
270                // Mark lines after the first line as continuations
271                for line_num in (span.line + 1)..=span.end_line {
272                    if let Some(line_info) = lines.get_mut(line_num - 1) {
273                        line_info.in_code_span_continuation = true;
274                    }
275                }
276            }
277        }
278
279        // Parse links, images, references, and list blocks
280        let (links, broken_links, footnote_refs) = profile_section!(
281            "Links",
282            profile,
283            link_parser::parse_links(content, &lines, &code_blocks, &code_spans, flavor, &html_comment_ranges)
284        );
285
286        let images = profile_section!(
287            "Images",
288            profile,
289            link_parser::parse_images(content, &lines, &code_blocks, &code_spans, &html_comment_ranges)
290        );
291
292        let reference_defs = profile_section!(
293            "Reference defs",
294            profile,
295            link_parser::parse_reference_defs(content, &lines)
296        );
297
298        let list_blocks = profile_section!("List blocks", profile, list_blocks::parse_list_blocks(content, &lines));
299
300        // Compute character frequency for fast content analysis
301        let char_frequency = profile_section!(
302            "Char frequency",
303            profile,
304            line_computation::compute_char_frequency(content)
305        );
306
307        // Pre-compute table blocks for rules that need them (MD013, MD055, MD056, MD058, MD060)
308        let table_blocks = profile_section!(
309            "Table blocks",
310            profile,
311            crate::utils::table_utils::TableUtils::find_table_blocks_with_code_info(
312                content,
313                &code_blocks,
314                &code_spans,
315                &html_comment_ranges,
316            )
317        );
318
319        // Layer 2: Filter pre-computed collections to exclude items inside kramdown extension blocks.
320        // Rules that iterate these collections automatically skip kramdown content.
321        let links = links
322            .into_iter()
323            .filter(|link| !lines.get(link.line - 1).is_some_and(|l| l.in_kramdown_extension_block))
324            .collect::<Vec<_>>();
325        let images = images
326            .into_iter()
327            .filter(|img| !lines.get(img.line - 1).is_some_and(|l| l.in_kramdown_extension_block))
328            .collect::<Vec<_>>();
329        let broken_links = broken_links
330            .into_iter()
331            .filter(|bl| {
332                // BrokenLinkInfo has span but no line field; find line from byte offset
333                let line_idx = line_offsets
334                    .partition_point(|&offset| offset <= bl.span.start)
335                    .saturating_sub(1);
336                !lines.get(line_idx).is_some_and(|l| l.in_kramdown_extension_block)
337            })
338            .collect::<Vec<_>>();
339        let footnote_refs = footnote_refs
340            .into_iter()
341            .filter(|fr| !lines.get(fr.line - 1).is_some_and(|l| l.in_kramdown_extension_block))
342            .collect::<Vec<_>>();
343        let reference_defs = reference_defs
344            .into_iter()
345            .filter(|def| !lines.get(def.line - 1).is_some_and(|l| l.in_kramdown_extension_block))
346            .collect::<Vec<_>>();
347        let list_blocks = list_blocks
348            .into_iter()
349            .filter(|block| {
350                !lines
351                    .get(block.start_line - 1)
352                    .is_some_and(|l| l.in_kramdown_extension_block)
353            })
354            .collect::<Vec<_>>();
355        let table_blocks = table_blocks
356            .into_iter()
357            .filter(|block| {
358                // TableBlock.start_line is 0-indexed
359                !lines
360                    .get(block.start_line)
361                    .is_some_and(|l| l.in_kramdown_extension_block)
362            })
363            .collect::<Vec<_>>();
364        let emphasis_spans = emphasis_spans
365            .into_iter()
366            .filter(|span| !lines.get(span.line - 1).is_some_and(|l| l.in_kramdown_extension_block))
367            .collect::<Vec<_>>();
368
369        // Rebuild reference_defs_map after filtering
370        let reference_defs_map: HashMap<String, usize> = reference_defs
371            .iter()
372            .enumerate()
373            .map(|(idx, def)| (def.id.to_lowercase(), idx))
374            .collect();
375
376        // Reuse already-computed line_offsets and code_blocks instead of re-detecting
377        let line_index = profile_section!(
378            "Line index",
379            profile,
380            crate::utils::range_utils::LineIndex::with_line_starts_and_code_blocks(
381                content,
382                line_offsets.clone(),
383                &code_blocks,
384            )
385        );
386
387        // Pre-compute Jinja template ranges once for all rules (eliminates O(n*m) in MD011)
388        let jinja_ranges = profile_section!(
389            "Jinja ranges",
390            profile,
391            crate::utils::jinja_utils::find_jinja_ranges(content)
392        );
393
394        // Pre-compute Pandoc/Quarto citation ranges for Quarto flavor
395        let citation_ranges = profile_section!("Citation ranges", profile, {
396            if flavor == MarkdownFlavor::Quarto {
397                crate::utils::quarto_divs::find_citation_ranges(content)
398            } else {
399                Vec::new()
400            }
401        });
402
403        // Pre-compute Hugo/Quarto shortcode ranges ({{< ... >}} and {{% ... %}})
404        let shortcode_ranges = profile_section!("Shortcode ranges", profile, {
405            use crate::utils::regex_cache::HUGO_SHORTCODE_REGEX;
406            let mut ranges = Vec::new();
407            for mat in HUGO_SHORTCODE_REGEX.find_iter(content).flatten() {
408                ranges.push((mat.start(), mat.end()));
409            }
410            ranges
411        });
412
413        let inline_config = InlineConfig::from_content_with_code_blocks(content, &code_blocks);
414
415        Self {
416            content,
417            content_lines,
418            line_offsets,
419            code_blocks,
420            lines,
421            links,
422            images,
423            broken_links,
424            footnote_refs,
425            reference_defs,
426            reference_defs_map,
427            code_spans_cache: OnceLock::from(Arc::new(code_spans)),
428            math_spans_cache: OnceLock::new(), // Lazy-loaded on first access
429            list_blocks,
430            char_frequency,
431            html_tags_cache: OnceLock::new(),
432            emphasis_spans_cache: OnceLock::from(Arc::new(emphasis_spans)),
433            table_rows_cache: OnceLock::new(),
434            bare_urls_cache: OnceLock::new(),
435            has_mixed_list_nesting_cache: OnceLock::new(),
436            html_comment_ranges,
437            table_blocks,
438            line_index,
439            jinja_ranges,
440            flavor,
441            source_file,
442            jsx_expression_ranges,
443            mdx_comment_ranges,
444            citation_ranges,
445            shortcode_ranges,
446            inline_config,
447            obsidian_comment_ranges,
448        }
449    }
450
451    /// Get parsed inline configuration state.
452    pub fn inline_config(&self) -> &InlineConfig {
453        &self.inline_config
454    }
455
456    /// Get pre-split content lines, avoiding repeated `content.lines().collect()` allocations.
457    ///
458    /// Lines are 0-indexed (line 0 corresponds to line number 1 in the document).
459    pub fn raw_lines(&self) -> &[&'a str] {
460        &self.content_lines
461    }
462
463    /// Check if a rule is disabled at a specific line number (1-indexed)
464    ///
465    /// This method checks both persistent disable comments (<!-- rumdl-disable -->)
466    /// and line-specific comments (<!-- rumdl-disable-line -->, <!-- rumdl-disable-next-line -->).
467    pub fn is_rule_disabled(&self, rule_name: &str, line_number: usize) -> bool {
468        self.inline_config.is_rule_disabled(rule_name, line_number)
469    }
470
471    /// Get code spans - computed lazily on first access
472    pub fn code_spans(&self) -> Arc<Vec<CodeSpan>> {
473        Arc::clone(
474            self.code_spans_cache
475                .get_or_init(|| Arc::new(element_parsers::parse_code_spans(self.content, &self.lines))),
476        )
477    }
478
479    /// Get math spans - computed lazily on first access
480    pub fn math_spans(&self) -> Arc<Vec<MathSpan>> {
481        Arc::clone(
482            self.math_spans_cache
483                .get_or_init(|| Arc::new(element_parsers::parse_math_spans(self.content, &self.lines))),
484        )
485    }
486
487    /// Check if a byte position is within a math span (inline $...$ or display $$...$$)
488    pub fn is_in_math_span(&self, byte_pos: usize) -> bool {
489        let math_spans = self.math_spans();
490        math_spans
491            .iter()
492            .any(|span| byte_pos >= span.byte_offset && byte_pos < span.byte_end)
493    }
494
495    /// Get HTML comment ranges - pre-computed during LintContext construction
496    pub fn html_comment_ranges(&self) -> &[crate::utils::skip_context::ByteRange] {
497        &self.html_comment_ranges
498    }
499
500    /// Get Obsidian comment ranges - pre-computed during LintContext construction
501    /// Returns empty slice for non-Obsidian flavors
502    pub fn obsidian_comment_ranges(&self) -> &[(usize, usize)] {
503        &self.obsidian_comment_ranges
504    }
505
506    /// Check if a byte position is inside an Obsidian comment
507    ///
508    /// Returns false for non-Obsidian flavors.
509    pub fn is_in_obsidian_comment(&self, byte_pos: usize) -> bool {
510        self.obsidian_comment_ranges
511            .iter()
512            .any(|(start, end)| byte_pos >= *start && byte_pos < *end)
513    }
514
515    /// Check if a line/column position is inside an Obsidian comment
516    ///
517    /// Line number is 1-indexed, column is 1-indexed.
518    /// Returns false for non-Obsidian flavors.
519    pub fn is_position_in_obsidian_comment(&self, line_num: usize, col: usize) -> bool {
520        if self.obsidian_comment_ranges.is_empty() {
521            return false;
522        }
523
524        // Convert line/column (1-indexed, char-based) to byte position
525        let byte_pos = self.line_index.line_col_to_byte_range(line_num, col).start;
526        self.is_in_obsidian_comment(byte_pos)
527    }
528
529    /// Get HTML tags - computed lazily on first access
530    pub fn html_tags(&self) -> Arc<Vec<HtmlTag>> {
531        Arc::clone(self.html_tags_cache.get_or_init(|| {
532            let tags = element_parsers::parse_html_tags(self.content, &self.lines, &self.code_blocks, self.flavor);
533            // Filter out HTML tags inside kramdown extension blocks
534            Arc::new(
535                tags.into_iter()
536                    .filter(|tag| {
537                        !self
538                            .lines
539                            .get(tag.line - 1)
540                            .is_some_and(|l| l.in_kramdown_extension_block)
541                    })
542                    .collect(),
543            )
544        }))
545    }
546
547    /// Get emphasis spans - pre-computed during construction
548    pub fn emphasis_spans(&self) -> Arc<Vec<EmphasisSpan>> {
549        Arc::clone(
550            self.emphasis_spans_cache
551                .get()
552                .expect("emphasis_spans_cache initialized during construction"),
553        )
554    }
555
556    /// Get table rows - computed lazily on first access
557    pub fn table_rows(&self) -> Arc<Vec<TableRow>> {
558        Arc::clone(
559            self.table_rows_cache
560                .get_or_init(|| Arc::new(element_parsers::parse_table_rows(self.content, &self.lines))),
561        )
562    }
563
564    /// Get bare URLs - computed lazily on first access
565    pub fn bare_urls(&self) -> Arc<Vec<BareUrl>> {
566        Arc::clone(self.bare_urls_cache.get_or_init(|| {
567            Arc::new(element_parsers::parse_bare_urls(
568                self.content,
569                &self.lines,
570                &self.code_blocks,
571            ))
572        }))
573    }
574
575    /// Check if document has mixed ordered/unordered list nesting.
576    /// Result is cached after first computation (document-level invariant).
577    /// This is used by MD007 for smart style auto-detection.
578    pub fn has_mixed_list_nesting(&self) -> bool {
579        *self
580            .has_mixed_list_nesting_cache
581            .get_or_init(|| self.compute_mixed_list_nesting())
582    }
583
584    /// Internal computation for mixed list nesting (only called once per LintContext).
585    fn compute_mixed_list_nesting(&self) -> bool {
586        // Track parent list items by their marker position and type
587        // Using marker_column instead of indent because it works correctly
588        // for blockquoted content where indent doesn't account for the prefix
589        // Stack stores: (marker_column, is_ordered)
590        let mut stack: Vec<(usize, bool)> = Vec::new();
591        let mut last_was_blank = false;
592
593        for line_info in &self.lines {
594            // Skip non-content lines (code blocks, frontmatter, HTML comments, etc.)
595            if line_info.in_code_block
596                || line_info.in_front_matter
597                || line_info.in_mkdocstrings
598                || line_info.in_html_comment
599                || line_info.in_esm_block
600            {
601                continue;
602            }
603
604            // OPTIMIZATION: Use pre-computed is_blank instead of content().trim()
605            if line_info.is_blank {
606                last_was_blank = true;
607                continue;
608            }
609
610            if let Some(list_item) = &line_info.list_item {
611                // Normalize column 1 to column 0 (consistent with MD007 check function)
612                let current_pos = if list_item.marker_column == 1 {
613                    0
614                } else {
615                    list_item.marker_column
616                };
617
618                // If there was a blank line and this item is at root level, reset stack
619                if last_was_blank && current_pos == 0 {
620                    stack.clear();
621                }
622                last_was_blank = false;
623
624                // Pop items at same or greater position (they're siblings or deeper, not parents)
625                while let Some(&(pos, _)) = stack.last() {
626                    if pos >= current_pos {
627                        stack.pop();
628                    } else {
629                        break;
630                    }
631                }
632
633                // Check if immediate parent has different type - this is mixed nesting
634                if let Some(&(_, parent_is_ordered)) = stack.last()
635                    && parent_is_ordered != list_item.is_ordered
636                {
637                    return true; // Found mixed nesting - early exit
638                }
639
640                stack.push((current_pos, list_item.is_ordered));
641            } else {
642                // Non-list line (but not blank) - could be paragraph or other content
643                last_was_blank = false;
644            }
645        }
646
647        false
648    }
649
650    /// Map a byte offset to (line, column)
651    pub fn offset_to_line_col(&self, offset: usize) -> (usize, usize) {
652        match self.line_offsets.binary_search(&offset) {
653            Ok(line) => (line + 1, 1),
654            Err(line) => {
655                let line_start = self.line_offsets.get(line.wrapping_sub(1)).copied().unwrap_or(0);
656                (line, offset - line_start + 1)
657            }
658        }
659    }
660
661    /// Check if a position is within a code block or code span
662    pub fn is_in_code_block_or_span(&self, pos: usize) -> bool {
663        // Check code blocks first
664        if CodeBlockUtils::is_in_code_block_or_span(&self.code_blocks, pos) {
665            return true;
666        }
667
668        // Check inline code spans (lazy load if needed)
669        self.code_spans()
670            .iter()
671            .any(|span| pos >= span.byte_offset && pos < span.byte_end)
672    }
673
674    /// Get line information by line number (1-indexed)
675    pub fn line_info(&self, line_num: usize) -> Option<&LineInfo> {
676        if line_num > 0 {
677            self.lines.get(line_num - 1)
678        } else {
679            None
680        }
681    }
682
683    /// Get byte offset for a line number (1-indexed)
684    pub fn line_to_byte_offset(&self, line_num: usize) -> Option<usize> {
685        self.line_info(line_num).map(|info| info.byte_offset)
686    }
687
688    /// Get URL for a reference link/image by its ID (O(1) lookup via HashMap)
689    pub fn get_reference_url(&self, ref_id: &str) -> Option<&str> {
690        let normalized_id = ref_id.to_lowercase();
691        self.reference_defs_map
692            .get(&normalized_id)
693            .map(|&idx| self.reference_defs[idx].url.as_str())
694    }
695
696    /// Get a reference definition by its ID (O(1) lookup via HashMap)
697    pub fn get_reference_def(&self, ref_id: &str) -> Option<&ReferenceDef> {
698        let normalized_id = ref_id.to_lowercase();
699        self.reference_defs_map
700            .get(&normalized_id)
701            .map(|&idx| &self.reference_defs[idx])
702    }
703
704    /// Check if a reference definition exists by ID (O(1) lookup via HashMap)
705    pub fn has_reference_def(&self, ref_id: &str) -> bool {
706        let normalized_id = ref_id.to_lowercase();
707        self.reference_defs_map.contains_key(&normalized_id)
708    }
709
710    /// Check if a line is part of a list block
711    pub fn is_in_list_block(&self, line_num: usize) -> bool {
712        self.list_blocks
713            .iter()
714            .any(|block| line_num >= block.start_line && line_num <= block.end_line)
715    }
716
717    /// Get the list block containing a specific line
718    pub fn list_block_for_line(&self, line_num: usize) -> Option<&ListBlock> {
719        self.list_blocks
720            .iter()
721            .find(|block| line_num >= block.start_line && line_num <= block.end_line)
722    }
723
724    // Compatibility methods for DocumentStructure migration
725
726    /// Check if a line is within a code block
727    pub fn is_in_code_block(&self, line_num: usize) -> bool {
728        if line_num == 0 || line_num > self.lines.len() {
729            return false;
730        }
731        self.lines[line_num - 1].in_code_block
732    }
733
734    /// Check if a line is within front matter
735    pub fn is_in_front_matter(&self, line_num: usize) -> bool {
736        if line_num == 0 || line_num > self.lines.len() {
737            return false;
738        }
739        self.lines[line_num - 1].in_front_matter
740    }
741
742    /// Check if a line is within an HTML block
743    pub fn is_in_html_block(&self, line_num: usize) -> bool {
744        if line_num == 0 || line_num > self.lines.len() {
745            return false;
746        }
747        self.lines[line_num - 1].in_html_block
748    }
749
750    /// Check if a line and column is within a code span
751    pub fn is_in_code_span(&self, line_num: usize, col: usize) -> bool {
752        if line_num == 0 || line_num > self.lines.len() {
753            return false;
754        }
755
756        // Use the code spans cache to check
757        // Note: col is 1-indexed from caller, but span.start_col and span.end_col are 0-indexed
758        // Convert col to 0-indexed for comparison
759        let col_0indexed = if col > 0 { col - 1 } else { 0 };
760        let code_spans = self.code_spans();
761        code_spans.iter().any(|span| {
762            // Check if line is within the span's line range
763            if line_num < span.line || line_num > span.end_line {
764                return false;
765            }
766
767            if span.line == span.end_line {
768                // Single-line span: check column bounds
769                col_0indexed >= span.start_col && col_0indexed < span.end_col
770            } else if line_num == span.line {
771                // First line of multi-line span: anything after start_col is in span
772                col_0indexed >= span.start_col
773            } else if line_num == span.end_line {
774                // Last line of multi-line span: anything before end_col is in span
775                col_0indexed < span.end_col
776            } else {
777                // Middle line of multi-line span: entire line is in span
778                true
779            }
780        })
781    }
782
783    /// Check if a byte offset is within a code span
784    #[inline]
785    pub fn is_byte_offset_in_code_span(&self, byte_offset: usize) -> bool {
786        let code_spans = self.code_spans();
787        code_spans
788            .iter()
789            .any(|span| byte_offset >= span.byte_offset && byte_offset < span.byte_end)
790    }
791
792    /// Check if a byte position is within a reference definition
793    #[inline]
794    pub fn is_in_reference_def(&self, byte_pos: usize) -> bool {
795        self.reference_defs
796            .iter()
797            .any(|ref_def| byte_pos >= ref_def.byte_offset && byte_pos < ref_def.byte_end)
798    }
799
800    /// Check if a byte position is within an HTML comment
801    #[inline]
802    pub fn is_in_html_comment(&self, byte_pos: usize) -> bool {
803        self.html_comment_ranges
804            .iter()
805            .any(|range| byte_pos >= range.start && byte_pos < range.end)
806    }
807
808    /// Check if a byte position is within an HTML tag (including multiline tags)
809    /// Uses the pre-parsed html_tags which correctly handles tags spanning multiple lines
810    #[inline]
811    pub fn is_in_html_tag(&self, byte_pos: usize) -> bool {
812        self.html_tags()
813            .iter()
814            .any(|tag| byte_pos >= tag.byte_offset && byte_pos < tag.byte_end)
815    }
816
817    /// Check if a byte position is within a Jinja template ({{ }} or {% %})
818    pub fn is_in_jinja_range(&self, byte_pos: usize) -> bool {
819        self.jinja_ranges
820            .iter()
821            .any(|(start, end)| byte_pos >= *start && byte_pos < *end)
822    }
823
824    /// Check if a byte position is within a JSX expression (MDX: {expression})
825    #[inline]
826    pub fn is_in_jsx_expression(&self, byte_pos: usize) -> bool {
827        self.jsx_expression_ranges
828            .iter()
829            .any(|(start, end)| byte_pos >= *start && byte_pos < *end)
830    }
831
832    /// Check if a byte position is within an MDX comment ({/* ... */})
833    #[inline]
834    pub fn is_in_mdx_comment(&self, byte_pos: usize) -> bool {
835        self.mdx_comment_ranges
836            .iter()
837            .any(|(start, end)| byte_pos >= *start && byte_pos < *end)
838    }
839
840    /// Get all JSX expression byte ranges
841    pub fn jsx_expression_ranges(&self) -> &[(usize, usize)] {
842        &self.jsx_expression_ranges
843    }
844
845    /// Get all MDX comment byte ranges
846    pub fn mdx_comment_ranges(&self) -> &[(usize, usize)] {
847        &self.mdx_comment_ranges
848    }
849
850    /// Check if a byte position is within a Pandoc/Quarto citation (`@key` or `[@key]`)
851    /// Only active in Quarto flavor
852    #[inline]
853    pub fn is_in_citation(&self, byte_pos: usize) -> bool {
854        self.citation_ranges
855            .iter()
856            .any(|range| byte_pos >= range.start && byte_pos < range.end)
857    }
858
859    /// Get all citation byte ranges (Quarto flavor only)
860    pub fn citation_ranges(&self) -> &[crate::utils::skip_context::ByteRange] {
861        &self.citation_ranges
862    }
863
864    /// Check if a byte position is within a Hugo/Quarto shortcode ({{< ... >}} or {{% ... %}})
865    #[inline]
866    pub fn is_in_shortcode(&self, byte_pos: usize) -> bool {
867        self.shortcode_ranges
868            .iter()
869            .any(|(start, end)| byte_pos >= *start && byte_pos < *end)
870    }
871
872    /// Get all shortcode byte ranges
873    pub fn shortcode_ranges(&self) -> &[(usize, usize)] {
874        &self.shortcode_ranges
875    }
876
877    /// Check if a byte position is within a link reference definition title
878    pub fn is_in_link_title(&self, byte_pos: usize) -> bool {
879        self.reference_defs.iter().any(|def| {
880            if let (Some(start), Some(end)) = (def.title_byte_start, def.title_byte_end) {
881                byte_pos >= start && byte_pos < end
882            } else {
883                false
884            }
885        })
886    }
887
888    /// Check if content has any instances of a specific character (fast)
889    pub fn has_char(&self, ch: char) -> bool {
890        match ch {
891            '#' => self.char_frequency.hash_count > 0,
892            '*' => self.char_frequency.asterisk_count > 0,
893            '_' => self.char_frequency.underscore_count > 0,
894            '-' => self.char_frequency.hyphen_count > 0,
895            '+' => self.char_frequency.plus_count > 0,
896            '>' => self.char_frequency.gt_count > 0,
897            '|' => self.char_frequency.pipe_count > 0,
898            '[' => self.char_frequency.bracket_count > 0,
899            '`' => self.char_frequency.backtick_count > 0,
900            '<' => self.char_frequency.lt_count > 0,
901            '!' => self.char_frequency.exclamation_count > 0,
902            '\n' => self.char_frequency.newline_count > 0,
903            _ => self.content.contains(ch), // Fallback for other characters
904        }
905    }
906
907    /// Get count of a specific character (fast)
908    pub fn char_count(&self, ch: char) -> usize {
909        match ch {
910            '#' => self.char_frequency.hash_count,
911            '*' => self.char_frequency.asterisk_count,
912            '_' => self.char_frequency.underscore_count,
913            '-' => self.char_frequency.hyphen_count,
914            '+' => self.char_frequency.plus_count,
915            '>' => self.char_frequency.gt_count,
916            '|' => self.char_frequency.pipe_count,
917            '[' => self.char_frequency.bracket_count,
918            '`' => self.char_frequency.backtick_count,
919            '<' => self.char_frequency.lt_count,
920            '!' => self.char_frequency.exclamation_count,
921            '\n' => self.char_frequency.newline_count,
922            _ => self.content.matches(ch).count(), // Fallback for other characters
923        }
924    }
925
926    /// Check if content likely contains headings (fast)
927    pub fn likely_has_headings(&self) -> bool {
928        self.char_frequency.hash_count > 0 || self.char_frequency.hyphen_count > 2 // Potential setext underlines
929    }
930
931    /// Check if content likely contains lists (fast)
932    pub fn likely_has_lists(&self) -> bool {
933        self.char_frequency.asterisk_count > 0
934            || self.char_frequency.hyphen_count > 0
935            || self.char_frequency.plus_count > 0
936    }
937
938    /// Check if content likely contains emphasis (fast)
939    pub fn likely_has_emphasis(&self) -> bool {
940        self.char_frequency.asterisk_count > 1 || self.char_frequency.underscore_count > 1
941    }
942
943    /// Check if content likely contains tables (fast)
944    pub fn likely_has_tables(&self) -> bool {
945        self.char_frequency.pipe_count > 2
946    }
947
948    /// Check if content likely contains blockquotes (fast)
949    pub fn likely_has_blockquotes(&self) -> bool {
950        self.char_frequency.gt_count > 0
951    }
952
953    /// Check if content likely contains code (fast)
954    pub fn likely_has_code(&self) -> bool {
955        self.char_frequency.backtick_count > 0
956    }
957
958    /// Check if content likely contains links or images (fast)
959    pub fn likely_has_links_or_images(&self) -> bool {
960        self.char_frequency.bracket_count > 0 || self.char_frequency.exclamation_count > 0
961    }
962
963    /// Check if content likely contains HTML (fast)
964    pub fn likely_has_html(&self) -> bool {
965        self.char_frequency.lt_count > 0
966    }
967
968    /// Get the blockquote prefix for inserting a blank line at the given line index.
969    /// Returns the prefix without trailing content (e.g., ">" or ">>").
970    /// This is needed because blank lines inside blockquotes must preserve the blockquote structure.
971    /// Returns an empty string if the line is not inside a blockquote.
972    pub fn blockquote_prefix_for_blank_line(&self, line_idx: usize) -> String {
973        if let Some(line_info) = self.lines.get(line_idx)
974            && let Some(ref bq) = line_info.blockquote
975        {
976            bq.prefix.trim_end().to_string()
977        } else {
978            String::new()
979        }
980    }
981
982    /// Get HTML tags on a specific line
983    pub fn html_tags_on_line(&self, line_num: usize) -> Vec<HtmlTag> {
984        self.html_tags()
985            .iter()
986            .filter(|tag| tag.line == line_num)
987            .cloned()
988            .collect()
989    }
990
991    /// Get emphasis spans on a specific line
992    pub fn emphasis_spans_on_line(&self, line_num: usize) -> Vec<EmphasisSpan> {
993        self.emphasis_spans()
994            .iter()
995            .filter(|span| span.line == line_num)
996            .cloned()
997            .collect()
998    }
999
1000    /// Get table rows on a specific line
1001    pub fn table_rows_on_line(&self, line_num: usize) -> Vec<TableRow> {
1002        self.table_rows()
1003            .iter()
1004            .filter(|row| row.line == line_num)
1005            .cloned()
1006            .collect()
1007    }
1008
1009    /// Get bare URLs on a specific line
1010    pub fn bare_urls_on_line(&self, line_num: usize) -> Vec<BareUrl> {
1011        self.bare_urls()
1012            .iter()
1013            .filter(|url| url.line == line_num)
1014            .cloned()
1015            .collect()
1016    }
1017
1018    /// Find the line index for a given byte offset using binary search.
1019    /// Returns (line_index, line_number, column) where:
1020    /// - line_index is the 0-based index in the lines array
1021    /// - line_number is the 1-based line number
1022    /// - column is the byte offset within that line
1023    #[inline]
1024    fn find_line_for_offset(lines: &[LineInfo], byte_offset: usize) -> (usize, usize, usize) {
1025        // Binary search to find the line containing this byte offset
1026        let idx = match lines.binary_search_by(|line| {
1027            if byte_offset < line.byte_offset {
1028                std::cmp::Ordering::Greater
1029            } else if byte_offset > line.byte_offset + line.byte_len {
1030                std::cmp::Ordering::Less
1031            } else {
1032                std::cmp::Ordering::Equal
1033            }
1034        }) {
1035            Ok(idx) => idx,
1036            Err(idx) => idx.saturating_sub(1),
1037        };
1038
1039        let line = &lines[idx];
1040        let line_num = idx + 1;
1041        let col = byte_offset.saturating_sub(line.byte_offset);
1042
1043        (idx, line_num, col)
1044    }
1045
1046    /// Check if a byte offset is within a code span using binary search
1047    #[inline]
1048    fn is_offset_in_code_span(code_spans: &[CodeSpan], offset: usize) -> bool {
1049        // Since spans are sorted by byte_offset, use partition_point for binary search
1050        let idx = code_spans.partition_point(|span| span.byte_offset <= offset);
1051
1052        // Check the span that starts at or before our offset
1053        if idx > 0 {
1054            let span = &code_spans[idx - 1];
1055            if offset >= span.byte_offset && offset < span.byte_end {
1056                return true;
1057            }
1058        }
1059
1060        false
1061    }
1062
1063    /// Get an iterator over valid headings (skipping invalid ones like `#NoSpace`)
1064    ///
1065    /// Valid headings have proper spacing after the `#` markers (or are level > 1).
1066    /// This is the standard iterator for rules that need to process headings.
1067    ///
1068    /// # Examples
1069    ///
1070    /// ```
1071    /// use rumdl::lint_context::LintContext;
1072    /// use rumdl::config::MarkdownFlavor;
1073    ///
1074    /// let content = "# Valid Heading\n#NoSpace\n## Another Valid";
1075    /// let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
1076    ///
1077    /// for heading in ctx.valid_headings() {
1078    ///     println!("Line {}: {} (level {})", heading.line_num, heading.heading.text, heading.heading.level);
1079    /// }
1080    /// // Only prints valid headings, skips `#NoSpace`
1081    /// ```
1082    #[must_use]
1083    pub fn valid_headings(&self) -> ValidHeadingsIter<'_> {
1084        ValidHeadingsIter::new(&self.lines)
1085    }
1086
1087    /// Check if the document contains any valid CommonMark headings
1088    ///
1089    /// Returns `true` if there is at least one heading with proper space after `#`.
1090    #[must_use]
1091    pub fn has_valid_headings(&self) -> bool {
1092        self.lines
1093            .iter()
1094            .any(|line| line.heading.as_ref().is_some_and(|h| h.is_valid))
1095    }
1096}