rumdl_lib/
lint_context.rs

1use crate::config::MarkdownFlavor;
2use crate::utils::code_block_utils::{CodeBlockContext, CodeBlockUtils};
3use lazy_static::lazy_static;
4use regex::Regex;
5
6lazy_static! {
7    // Comprehensive link pattern that captures both inline and reference links
8    // Use (?s) flag to make . match newlines
9    static ref LINK_PATTERN: Regex = Regex::new(
10        r"(?sx)
11        \[((?:[^\[\]\\]|\\.|\[[^\]]*\])*)\]          # Link text in group 1 (handles nested brackets)
12        (?:
13            \(([^)]*)\)       # Inline URL in group 2 (can be empty)
14            |
15            \[([^\]]*)\]      # Reference ID in group 3
16        )"
17    ).unwrap();
18
19    // Image pattern (similar to links but with ! prefix)
20    // Use (?s) flag to make . match newlines
21    static ref IMAGE_PATTERN: Regex = Regex::new(
22        r"(?sx)
23        !\[((?:[^\[\]\\]|\\.|\[[^\]]*\])*)\]         # Alt text in group 1 (handles nested brackets)
24        (?:
25            \(([^)]*)\)       # Inline URL in group 2 (can be empty)
26            |
27            \[([^\]]*)\]      # Reference ID in group 3
28        )"
29    ).unwrap();
30
31    // Reference definition pattern
32    static ref REF_DEF_PATTERN: Regex = Regex::new(
33        r#"(?m)^[ ]{0,3}\[([^\]]+)\]:\s*([^\s]+)(?:\s+(?:"([^"]*)"|'([^']*)'))?$"#
34    ).unwrap();
35
36    // Code span pattern - matches backticks and captures content
37    // This handles multi-backtick code spans correctly
38    static ref CODE_SPAN_PATTERN: Regex = Regex::new(
39        r"`+"
40    ).unwrap();
41
42    // Pattern for bare URLs
43    static ref BARE_URL_PATTERN: Regex = Regex::new(
44        r#"(https?|ftp)://[^\s<>\[\]()\\'"`]+(?:\.[^\s<>\[\]()\\'"`]+)*(?::\d+)?(?:/[^\s<>\[\]()\\'"`]*)?(?:\?[^\s<>\[\]()\\'"`]*)?(?:#[^\s<>\[\]()\\'"`]*)?"#
45    ).unwrap();
46
47    // Pattern for email addresses
48    static ref BARE_EMAIL_PATTERN: Regex = Regex::new(
49        r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"
50    ).unwrap();
51
52    // Pattern for angle bracket links (to exclude from bare URL detection)
53    static ref ANGLE_BRACKET_PATTERN: Regex = Regex::new(
54        r"<((?:https?|ftp)://[^>]+|[^@\s]+@[^@\s]+\.[^@\s>]+)>"
55    ).unwrap();
56
57    // Pattern for blockquote prefix in parse_list_blocks
58    static ref BLOCKQUOTE_PREFIX_REGEX: Regex = Regex::new(r"^(\s*>+\s*)").unwrap();
59}
60
61/// Pre-computed information about a line
62#[derive(Debug, Clone)]
63pub struct LineInfo {
64    /// The actual line content (without newline)
65    pub content: String,
66    /// Byte offset where this line starts in the document
67    pub byte_offset: usize,
68    /// Number of leading spaces/tabs
69    pub indent: usize,
70    /// Whether the line is blank (empty or only whitespace)
71    pub is_blank: bool,
72    /// Whether this line is inside a code block
73    pub in_code_block: bool,
74    /// Whether this line is inside front matter
75    pub in_front_matter: bool,
76    /// List item information if this line starts a list item
77    pub list_item: Option<ListItemInfo>,
78    /// Heading information if this line is a heading
79    pub heading: Option<HeadingInfo>,
80    /// Blockquote information if this line is a blockquote
81    pub blockquote: Option<BlockquoteInfo>,
82}
83
84/// Information about a list item
85#[derive(Debug, Clone)]
86pub struct ListItemInfo {
87    /// The marker used (*, -, +, or number with . or ))
88    pub marker: String,
89    /// Whether it's ordered (true) or unordered (false)
90    pub is_ordered: bool,
91    /// The number for ordered lists
92    pub number: Option<usize>,
93    /// Column where the marker starts (0-based)
94    pub marker_column: usize,
95    /// Column where content after marker starts
96    pub content_column: usize,
97}
98
99/// Heading style type
100#[derive(Debug, Clone, PartialEq)]
101pub enum HeadingStyle {
102    /// ATX style heading (# Heading)
103    ATX,
104    /// Setext style heading with = underline
105    Setext1,
106    /// Setext style heading with - underline
107    Setext2,
108}
109
110/// Parsed link information
111#[derive(Debug, Clone)]
112pub struct ParsedLink {
113    /// Line number (1-indexed)
114    pub line: usize,
115    /// Start column (0-indexed) in the line
116    pub start_col: usize,
117    /// End column (0-indexed) in the line
118    pub end_col: usize,
119    /// Byte offset in document
120    pub byte_offset: usize,
121    /// End byte offset in document
122    pub byte_end: usize,
123    /// Link text
124    pub text: String,
125    /// Link URL or reference
126    pub url: String,
127    /// Whether this is a reference link [text][ref] vs inline [text](url)
128    pub is_reference: bool,
129    /// Reference ID for reference links
130    pub reference_id: Option<String>,
131}
132
133/// Parsed image information
134#[derive(Debug, Clone)]
135pub struct ParsedImage {
136    /// Line number (1-indexed)
137    pub line: usize,
138    /// Start column (0-indexed) in the line
139    pub start_col: usize,
140    /// End column (0-indexed) in the line
141    pub end_col: usize,
142    /// Byte offset in document
143    pub byte_offset: usize,
144    /// End byte offset in document
145    pub byte_end: usize,
146    /// Alt text
147    pub alt_text: String,
148    /// Image URL or reference
149    pub url: String,
150    /// Whether this is a reference image ![alt][ref] vs inline ![alt](url)
151    pub is_reference: bool,
152    /// Reference ID for reference images
153    pub reference_id: Option<String>,
154}
155
156/// Reference definition [ref]: url "title"
157#[derive(Debug, Clone)]
158pub struct ReferenceDef {
159    /// Line number (1-indexed)
160    pub line: usize,
161    /// Reference ID (normalized to lowercase)
162    pub id: String,
163    /// URL
164    pub url: String,
165    /// Optional title
166    pub title: Option<String>,
167}
168
169/// Parsed code span information
170#[derive(Debug, Clone)]
171pub struct CodeSpan {
172    /// Line number (1-indexed)
173    pub line: usize,
174    /// Start column (0-indexed) in the line
175    pub start_col: usize,
176    /// End column (0-indexed) in the line
177    pub end_col: usize,
178    /// Byte offset in document
179    pub byte_offset: usize,
180    /// End byte offset in document
181    pub byte_end: usize,
182    /// Number of backticks used (1, 2, 3, etc.)
183    pub backtick_count: usize,
184    /// Content inside the code span (without backticks)
185    pub content: String,
186}
187
188/// Information about a heading
189#[derive(Debug, Clone)]
190pub struct HeadingInfo {
191    /// Heading level (1-6 for ATX, 1-2 for Setext)
192    pub level: u8,
193    /// Style of heading
194    pub style: HeadingStyle,
195    /// The heading marker (# characters or underline)
196    pub marker: String,
197    /// Column where the marker starts (0-based)
198    pub marker_column: usize,
199    /// Column where heading text starts
200    pub content_column: usize,
201    /// The heading text (without markers and without custom ID syntax)
202    pub text: String,
203    /// Custom header ID if present (e.g., from {#custom-id} syntax)
204    pub custom_id: Option<String>,
205    /// Original heading text including custom ID syntax
206    pub raw_text: String,
207    /// Whether it has a closing sequence (for ATX)
208    pub has_closing_sequence: bool,
209    /// The closing sequence if present
210    pub closing_sequence: String,
211}
212
213/// Information about a blockquote line
214#[derive(Debug, Clone)]
215pub struct BlockquoteInfo {
216    /// Nesting level (1 for >, 2 for >>, etc.)
217    pub nesting_level: usize,
218    /// The indentation before the blockquote marker
219    pub indent: String,
220    /// Column where the first > starts (0-based)
221    pub marker_column: usize,
222    /// The blockquote prefix (e.g., "> ", ">> ", etc.)
223    pub prefix: String,
224    /// Content after the blockquote marker(s)
225    pub content: String,
226    /// Whether the line has no space after the marker
227    pub has_no_space_after_marker: bool,
228    /// Whether the line has multiple spaces after the marker
229    pub has_multiple_spaces_after_marker: bool,
230    /// Whether this is an empty blockquote line needing MD028 fix
231    pub needs_md028_fix: bool,
232}
233
234/// Information about a list block
235#[derive(Debug, Clone)]
236pub struct ListBlock {
237    /// Line number where the list starts (1-indexed)
238    pub start_line: usize,
239    /// Line number where the list ends (1-indexed)
240    pub end_line: usize,
241    /// Whether it's ordered or unordered
242    pub is_ordered: bool,
243    /// The consistent marker for unordered lists (if any)
244    pub marker: Option<String>,
245    /// Blockquote prefix for this list (empty if not in blockquote)
246    pub blockquote_prefix: String,
247    /// Lines that are list items within this block
248    pub item_lines: Vec<usize>,
249    /// Nesting level (0 for top-level lists)
250    pub nesting_level: usize,
251    /// Maximum marker width seen in this block (e.g., 3 for "1. ", 4 for "10. ")
252    pub max_marker_width: usize,
253}
254
255use std::sync::{Arc, Mutex};
256
257/// Character frequency data for fast content analysis
258#[derive(Debug, Clone, Default)]
259pub struct CharFrequency {
260    /// Count of # characters (headings)
261    pub hash_count: usize,
262    /// Count of * characters (emphasis, lists, horizontal rules)
263    pub asterisk_count: usize,
264    /// Count of _ characters (emphasis, horizontal rules)
265    pub underscore_count: usize,
266    /// Count of - characters (lists, horizontal rules, setext headings)
267    pub hyphen_count: usize,
268    /// Count of + characters (lists)
269    pub plus_count: usize,
270    /// Count of > characters (blockquotes)
271    pub gt_count: usize,
272    /// Count of | characters (tables)
273    pub pipe_count: usize,
274    /// Count of [ characters (links, images)
275    pub bracket_count: usize,
276    /// Count of ` characters (code spans, code blocks)
277    pub backtick_count: usize,
278    /// Count of < characters (HTML tags, autolinks)
279    pub lt_count: usize,
280    /// Count of ! characters (images)
281    pub exclamation_count: usize,
282    /// Count of newline characters
283    pub newline_count: usize,
284}
285
286/// Pre-parsed HTML tag information
287#[derive(Debug, Clone)]
288pub struct HtmlTag {
289    /// Line number (1-indexed)
290    pub line: usize,
291    /// Start column (0-indexed) in the line
292    pub start_col: usize,
293    /// End column (0-indexed) in the line
294    pub end_col: usize,
295    /// Byte offset in document
296    pub byte_offset: usize,
297    /// End byte offset in document
298    pub byte_end: usize,
299    /// Tag name (e.g., "div", "img", "br")
300    pub tag_name: String,
301    /// Whether it's a closing tag (</tag>)
302    pub is_closing: bool,
303    /// Whether it's self-closing (<tag />)
304    pub is_self_closing: bool,
305    /// Raw tag content
306    pub raw_content: String,
307}
308
309/// Pre-parsed emphasis span information
310#[derive(Debug, Clone)]
311pub struct EmphasisSpan {
312    /// Line number (1-indexed)
313    pub line: usize,
314    /// Start column (0-indexed) in the line
315    pub start_col: usize,
316    /// End column (0-indexed) in the line
317    pub end_col: usize,
318    /// Byte offset in document
319    pub byte_offset: usize,
320    /// End byte offset in document
321    pub byte_end: usize,
322    /// Type of emphasis ('*' or '_')
323    pub marker: char,
324    /// Number of markers (1 for italic, 2 for bold, 3+ for bold+italic)
325    pub marker_count: usize,
326    /// Content inside the emphasis
327    pub content: String,
328}
329
330/// Pre-parsed table row information
331#[derive(Debug, Clone)]
332pub struct TableRow {
333    /// Line number (1-indexed)
334    pub line: usize,
335    /// Whether this is a separator row (contains only |, -, :, and spaces)
336    pub is_separator: bool,
337    /// Number of columns (pipe-separated cells)
338    pub column_count: usize,
339    /// Alignment info from separator row
340    pub column_alignments: Vec<String>, // "left", "center", "right", "none"
341}
342
343/// Pre-parsed bare URL information (not in links)
344#[derive(Debug, Clone)]
345pub struct BareUrl {
346    /// Line number (1-indexed)
347    pub line: usize,
348    /// Start column (0-indexed) in the line
349    pub start_col: usize,
350    /// End column (0-indexed) in the line
351    pub end_col: usize,
352    /// Byte offset in document
353    pub byte_offset: usize,
354    /// End byte offset in document
355    pub byte_end: usize,
356    /// The URL string
357    pub url: String,
358    /// Type of URL ("http", "https", "ftp", "email")
359    pub url_type: String,
360}
361
362pub struct LintContext<'a> {
363    pub content: &'a str,
364    pub line_offsets: Vec<usize>,
365    pub code_blocks: Vec<(usize, usize)>, // Cached code block ranges (not including inline code spans)
366    pub lines: Vec<LineInfo>,             // Pre-computed line information
367    pub links: Vec<ParsedLink>,           // Pre-parsed links
368    pub images: Vec<ParsedImage>,         // Pre-parsed images
369    pub reference_defs: Vec<ReferenceDef>, // Reference definitions
370    code_spans_cache: Mutex<Option<Arc<Vec<CodeSpan>>>>, // Lazy-loaded inline code spans
371    pub list_blocks: Vec<ListBlock>,      // Pre-parsed list blocks
372    pub char_frequency: CharFrequency,    // Character frequency analysis
373    html_tags_cache: Mutex<Option<Arc<Vec<HtmlTag>>>>, // Lazy-loaded HTML tags
374    emphasis_spans_cache: Mutex<Option<Arc<Vec<EmphasisSpan>>>>, // Lazy-loaded emphasis spans
375    table_rows_cache: Mutex<Option<Arc<Vec<TableRow>>>>, // Lazy-loaded table rows
376    bare_urls_cache: Mutex<Option<Arc<Vec<BareUrl>>>>, // Lazy-loaded bare URLs
377    pub flavor: MarkdownFlavor,           // Markdown flavor being used
378}
379
380impl<'a> LintContext<'a> {
381    pub fn new(content: &'a str, flavor: MarkdownFlavor) -> Self {
382        let mut line_offsets = vec![0];
383        for (i, c) in content.char_indices() {
384            if c == '\n' {
385                line_offsets.push(i + 1);
386            }
387        }
388
389        // Detect code blocks once and cache them
390        let code_blocks = CodeBlockUtils::detect_code_blocks(content);
391
392        // Pre-compute line information
393        let lines = Self::compute_line_info(content, &line_offsets, &code_blocks);
394
395        // Parse links, images, references, and list blocks
396        // Skip code spans - they'll be computed lazily
397        let links = Self::parse_links(content, &lines, &code_blocks, flavor);
398        let images = Self::parse_images(content, &lines, &code_blocks);
399        let reference_defs = Self::parse_reference_defs(content, &lines);
400        let list_blocks = Self::parse_list_blocks(&lines);
401
402        // Compute character frequency for fast content analysis
403        let char_frequency = Self::compute_char_frequency(content);
404
405        Self {
406            content,
407            line_offsets,
408            code_blocks,
409            lines,
410            links,
411            images,
412            reference_defs,
413            code_spans_cache: Mutex::new(None),
414            list_blocks,
415            char_frequency,
416            html_tags_cache: Mutex::new(None),
417            emphasis_spans_cache: Mutex::new(None),
418            table_rows_cache: Mutex::new(None),
419            bare_urls_cache: Mutex::new(None),
420            flavor,
421        }
422    }
423
424    /// Get code spans - computed lazily on first access
425    pub fn code_spans(&self) -> Arc<Vec<CodeSpan>> {
426        let mut cache = self.code_spans_cache.lock().unwrap();
427
428        // Check if we need to compute code spans
429        if cache.is_none() {
430            let code_spans = Self::parse_code_spans(self.content, &self.lines);
431            *cache = Some(Arc::new(code_spans));
432        }
433
434        // Return a reference to the cached code spans
435        cache.as_ref().unwrap().clone()
436    }
437
438    /// Get HTML tags - computed lazily on first access
439    pub fn html_tags(&self) -> Arc<Vec<HtmlTag>> {
440        let mut cache = self.html_tags_cache.lock().unwrap();
441
442        if cache.is_none() {
443            let html_tags = Self::parse_html_tags(self.content, &self.lines, &self.code_blocks);
444            *cache = Some(Arc::new(html_tags));
445        }
446
447        cache.as_ref().unwrap().clone()
448    }
449
450    /// Get emphasis spans - computed lazily on first access
451    pub fn emphasis_spans(&self) -> Arc<Vec<EmphasisSpan>> {
452        let mut cache = self.emphasis_spans_cache.lock().unwrap();
453
454        if cache.is_none() {
455            let emphasis_spans = Self::parse_emphasis_spans(self.content, &self.lines, &self.code_blocks);
456            *cache = Some(Arc::new(emphasis_spans));
457        }
458
459        cache.as_ref().unwrap().clone()
460    }
461
462    /// Get table rows - computed lazily on first access
463    pub fn table_rows(&self) -> Arc<Vec<TableRow>> {
464        let mut cache = self.table_rows_cache.lock().unwrap();
465
466        if cache.is_none() {
467            let table_rows = Self::parse_table_rows(&self.lines);
468            *cache = Some(Arc::new(table_rows));
469        }
470
471        cache.as_ref().unwrap().clone()
472    }
473
474    /// Get bare URLs - computed lazily on first access
475    pub fn bare_urls(&self) -> Arc<Vec<BareUrl>> {
476        let mut cache = self.bare_urls_cache.lock().unwrap();
477
478        if cache.is_none() {
479            let bare_urls = Self::parse_bare_urls(self.content, &self.lines, &self.code_blocks);
480            *cache = Some(Arc::new(bare_urls));
481        }
482
483        cache.as_ref().unwrap().clone()
484    }
485
486    /// Map a byte offset to (line, column)
487    pub fn offset_to_line_col(&self, offset: usize) -> (usize, usize) {
488        match self.line_offsets.binary_search(&offset) {
489            Ok(line) => (line + 1, 1),
490            Err(line) => {
491                let line_start = self.line_offsets.get(line.wrapping_sub(1)).copied().unwrap_or(0);
492                (line, offset - line_start + 1)
493            }
494        }
495    }
496
497    /// Check if a position is within a code block or code span
498    pub fn is_in_code_block_or_span(&self, pos: usize) -> bool {
499        // Check code blocks first
500        if CodeBlockUtils::is_in_code_block_or_span(&self.code_blocks, pos) {
501            return true;
502        }
503
504        // Check inline code spans (lazy load if needed)
505        self.code_spans()
506            .iter()
507            .any(|span| pos >= span.byte_offset && pos < span.byte_end)
508    }
509
510    /// Get line information by line number (1-indexed)
511    pub fn line_info(&self, line_num: usize) -> Option<&LineInfo> {
512        if line_num > 0 {
513            self.lines.get(line_num - 1)
514        } else {
515            None
516        }
517    }
518
519    /// Get byte offset for a line number (1-indexed)
520    pub fn line_to_byte_offset(&self, line_num: usize) -> Option<usize> {
521        self.line_info(line_num).map(|info| info.byte_offset)
522    }
523
524    /// Get URL for a reference link/image by its ID
525    pub fn get_reference_url(&self, ref_id: &str) -> Option<&str> {
526        let normalized_id = ref_id.to_lowercase();
527        self.reference_defs
528            .iter()
529            .find(|def| def.id == normalized_id)
530            .map(|def| def.url.as_str())
531    }
532
533    /// Get links on a specific line
534    pub fn links_on_line(&self, line_num: usize) -> Vec<&ParsedLink> {
535        self.links.iter().filter(|link| link.line == line_num).collect()
536    }
537
538    /// Get images on a specific line
539    pub fn images_on_line(&self, line_num: usize) -> Vec<&ParsedImage> {
540        self.images.iter().filter(|img| img.line == line_num).collect()
541    }
542
543    /// Check if a line is part of a list block
544    pub fn is_in_list_block(&self, line_num: usize) -> bool {
545        self.list_blocks
546            .iter()
547            .any(|block| line_num >= block.start_line && line_num <= block.end_line)
548    }
549
550    /// Get the list block containing a specific line
551    pub fn list_block_for_line(&self, line_num: usize) -> Option<&ListBlock> {
552        self.list_blocks
553            .iter()
554            .find(|block| line_num >= block.start_line && line_num <= block.end_line)
555    }
556
557    /// Check if content has any instances of a specific character (fast)
558    pub fn has_char(&self, ch: char) -> bool {
559        match ch {
560            '#' => self.char_frequency.hash_count > 0,
561            '*' => self.char_frequency.asterisk_count > 0,
562            '_' => self.char_frequency.underscore_count > 0,
563            '-' => self.char_frequency.hyphen_count > 0,
564            '+' => self.char_frequency.plus_count > 0,
565            '>' => self.char_frequency.gt_count > 0,
566            '|' => self.char_frequency.pipe_count > 0,
567            '[' => self.char_frequency.bracket_count > 0,
568            '`' => self.char_frequency.backtick_count > 0,
569            '<' => self.char_frequency.lt_count > 0,
570            '!' => self.char_frequency.exclamation_count > 0,
571            '\n' => self.char_frequency.newline_count > 0,
572            _ => self.content.contains(ch), // Fallback for other characters
573        }
574    }
575
576    /// Get count of a specific character (fast)
577    pub fn char_count(&self, ch: char) -> usize {
578        match ch {
579            '#' => self.char_frequency.hash_count,
580            '*' => self.char_frequency.asterisk_count,
581            '_' => self.char_frequency.underscore_count,
582            '-' => self.char_frequency.hyphen_count,
583            '+' => self.char_frequency.plus_count,
584            '>' => self.char_frequency.gt_count,
585            '|' => self.char_frequency.pipe_count,
586            '[' => self.char_frequency.bracket_count,
587            '`' => self.char_frequency.backtick_count,
588            '<' => self.char_frequency.lt_count,
589            '!' => self.char_frequency.exclamation_count,
590            '\n' => self.char_frequency.newline_count,
591            _ => self.content.matches(ch).count(), // Fallback for other characters
592        }
593    }
594
595    /// Check if content likely contains headings (fast)
596    pub fn likely_has_headings(&self) -> bool {
597        self.char_frequency.hash_count > 0 || self.char_frequency.hyphen_count > 2 // Potential setext underlines
598    }
599
600    /// Check if content likely contains lists (fast)
601    pub fn likely_has_lists(&self) -> bool {
602        self.char_frequency.asterisk_count > 0
603            || self.char_frequency.hyphen_count > 0
604            || self.char_frequency.plus_count > 0
605    }
606
607    /// Check if content likely contains emphasis (fast)
608    pub fn likely_has_emphasis(&self) -> bool {
609        self.char_frequency.asterisk_count > 1 || self.char_frequency.underscore_count > 1
610    }
611
612    /// Check if content likely contains tables (fast)
613    pub fn likely_has_tables(&self) -> bool {
614        self.char_frequency.pipe_count > 2
615    }
616
617    /// Check if content likely contains blockquotes (fast)
618    pub fn likely_has_blockquotes(&self) -> bool {
619        self.char_frequency.gt_count > 0
620    }
621
622    /// Check if content likely contains code (fast)
623    pub fn likely_has_code(&self) -> bool {
624        self.char_frequency.backtick_count > 0
625    }
626
627    /// Check if content likely contains links or images (fast)
628    pub fn likely_has_links_or_images(&self) -> bool {
629        self.char_frequency.bracket_count > 0 || self.char_frequency.exclamation_count > 0
630    }
631
632    /// Check if content likely contains HTML (fast)
633    pub fn likely_has_html(&self) -> bool {
634        self.char_frequency.lt_count > 0
635    }
636
637    /// Get HTML tags on a specific line
638    pub fn html_tags_on_line(&self, line_num: usize) -> Vec<HtmlTag> {
639        self.html_tags()
640            .iter()
641            .filter(|tag| tag.line == line_num)
642            .cloned()
643            .collect()
644    }
645
646    /// Get emphasis spans on a specific line
647    pub fn emphasis_spans_on_line(&self, line_num: usize) -> Vec<EmphasisSpan> {
648        self.emphasis_spans()
649            .iter()
650            .filter(|span| span.line == line_num)
651            .cloned()
652            .collect()
653    }
654
655    /// Get table rows on a specific line
656    pub fn table_rows_on_line(&self, line_num: usize) -> Vec<TableRow> {
657        self.table_rows()
658            .iter()
659            .filter(|row| row.line == line_num)
660            .cloned()
661            .collect()
662    }
663
664    /// Get bare URLs on a specific line
665    pub fn bare_urls_on_line(&self, line_num: usize) -> Vec<BareUrl> {
666        self.bare_urls()
667            .iter()
668            .filter(|url| url.line == line_num)
669            .cloned()
670            .collect()
671    }
672
673    /// Parse all links in the content
674    fn parse_links(
675        content: &str,
676        lines: &[LineInfo],
677        code_blocks: &[(usize, usize)],
678        flavor: MarkdownFlavor,
679    ) -> Vec<ParsedLink> {
680        use crate::utils::skip_context::is_mkdocs_snippet_line;
681
682        // Pre-size based on a heuristic: most markdown files have relatively few links
683        let mut links = Vec::with_capacity(content.len() / 500); // ~1 link per 500 chars
684
685        // Parse links across the entire content, not line by line
686        for cap in LINK_PATTERN.captures_iter(content) {
687            let full_match = cap.get(0).unwrap();
688            let match_start = full_match.start();
689            let match_end = full_match.end();
690
691            // Skip if the opening bracket is escaped (preceded by \)
692            if match_start > 0 && content.as_bytes().get(match_start - 1) == Some(&b'\\') {
693                continue;
694            }
695
696            // Skip if this is actually an image (preceded by !)
697            if match_start > 0 && content.as_bytes().get(match_start - 1) == Some(&b'!') {
698                continue;
699            }
700
701            // Skip if in code block or span
702            if CodeBlockUtils::is_in_code_block_or_span(code_blocks, match_start) {
703                continue;
704            }
705
706            // Skip if this link is on a MkDocs snippet line
707            // Find which line this link is on
708            let line_idx = lines
709                .iter()
710                .position(|line| {
711                    match_start >= line.byte_offset && (match_start < line.byte_offset + line.content.len() + 1)
712                })
713                .unwrap_or(0);
714
715            if is_mkdocs_snippet_line(&lines[line_idx].content, flavor) {
716                continue;
717            }
718
719            // Find which line this link starts on
720            let mut line_num = 1;
721            let mut col_start = match_start;
722            for (idx, line_info) in lines.iter().enumerate() {
723                if match_start >= line_info.byte_offset {
724                    line_num = idx + 1;
725                    col_start = match_start - line_info.byte_offset;
726                } else {
727                    break;
728                }
729            }
730
731            // Find which line this link ends on (and calculate column on that line)
732            let mut end_line_num = 1;
733            let mut col_end = match_end;
734            for (idx, line_info) in lines.iter().enumerate() {
735                if match_end > line_info.byte_offset {
736                    end_line_num = idx + 1;
737                    col_end = match_end - line_info.byte_offset;
738                } else {
739                    break;
740                }
741            }
742
743            // For single-line links, use the same approach as before
744            if line_num == end_line_num {
745                // col_end is already correct
746            } else {
747                // For multi-line links, col_end represents the column on the ending line
748                // which is what we want
749            }
750
751            let text = cap.get(1).map_or("", |m| m.as_str()).to_string();
752
753            if let Some(inline_url) = cap.get(2) {
754                // Inline link
755                links.push(ParsedLink {
756                    line: line_num,
757                    start_col: col_start,
758                    end_col: col_end,
759                    byte_offset: match_start,
760                    byte_end: match_end,
761                    text,
762                    url: inline_url.as_str().to_string(),
763                    is_reference: false,
764                    reference_id: None,
765                });
766            } else if let Some(ref_id) = cap.get(3) {
767                // Reference link
768                let ref_id_str = ref_id.as_str();
769                let normalized_ref = if ref_id_str.is_empty() {
770                    text.to_lowercase() // Implicit reference
771                } else {
772                    ref_id_str.to_lowercase()
773                };
774
775                links.push(ParsedLink {
776                    line: line_num,
777                    start_col: col_start,
778                    end_col: col_end,
779                    byte_offset: match_start,
780                    byte_end: match_end,
781                    text,
782                    url: String::new(), // Will be resolved with reference_defs
783                    is_reference: true,
784                    reference_id: Some(normalized_ref),
785                });
786            }
787        }
788
789        links
790    }
791
792    /// Parse all images in the content
793    fn parse_images(content: &str, lines: &[LineInfo], code_blocks: &[(usize, usize)]) -> Vec<ParsedImage> {
794        // Pre-size based on a heuristic: images are less common than links
795        let mut images = Vec::with_capacity(content.len() / 1000); // ~1 image per 1000 chars
796
797        // Parse images across the entire content, not line by line
798        for cap in IMAGE_PATTERN.captures_iter(content) {
799            let full_match = cap.get(0).unwrap();
800            let match_start = full_match.start();
801            let match_end = full_match.end();
802
803            // Skip if the ! is escaped (preceded by \)
804            if match_start > 0 && content.as_bytes().get(match_start - 1) == Some(&b'\\') {
805                continue;
806            }
807
808            // Skip if in code block or span
809            if CodeBlockUtils::is_in_code_block_or_span(code_blocks, match_start) {
810                continue;
811            }
812
813            // Find which line this image starts on
814            let mut line_num = 1;
815            let mut col_start = match_start;
816            for (idx, line_info) in lines.iter().enumerate() {
817                if match_start >= line_info.byte_offset {
818                    line_num = idx + 1;
819                    col_start = match_start - line_info.byte_offset;
820                } else {
821                    break;
822                }
823            }
824
825            // Find which line this image ends on (and calculate column on that line)
826            let mut end_line_num = 1;
827            let mut col_end = match_end;
828            for (idx, line_info) in lines.iter().enumerate() {
829                if match_end > line_info.byte_offset {
830                    end_line_num = idx + 1;
831                    col_end = match_end - line_info.byte_offset;
832                } else {
833                    break;
834                }
835            }
836
837            // For single-line images, use the same approach as before
838            if line_num == end_line_num {
839                // col_end is already correct
840            } else {
841                // For multi-line images, col_end represents the column on the ending line
842                // which is what we want
843            }
844
845            let alt_text = cap.get(1).map_or("", |m| m.as_str()).to_string();
846
847            if let Some(inline_url) = cap.get(2) {
848                // Inline image
849                images.push(ParsedImage {
850                    line: line_num,
851                    start_col: col_start,
852                    end_col: col_end,
853                    byte_offset: match_start,
854                    byte_end: match_end,
855                    alt_text,
856                    url: inline_url.as_str().to_string(),
857                    is_reference: false,
858                    reference_id: None,
859                });
860            } else if let Some(ref_id) = cap.get(3) {
861                // Reference image
862                let ref_id_str = ref_id.as_str();
863                let normalized_ref = if ref_id_str.is_empty() {
864                    alt_text.to_lowercase() // Implicit reference
865                } else {
866                    ref_id_str.to_lowercase()
867                };
868
869                images.push(ParsedImage {
870                    line: line_num,
871                    start_col: col_start,
872                    end_col: col_end,
873                    byte_offset: match_start,
874                    byte_end: match_end,
875                    alt_text,
876                    url: String::new(), // Will be resolved with reference_defs
877                    is_reference: true,
878                    reference_id: Some(normalized_ref),
879                });
880            }
881        }
882
883        images
884    }
885
886    /// Parse reference definitions
887    fn parse_reference_defs(_content: &str, lines: &[LineInfo]) -> Vec<ReferenceDef> {
888        // Pre-size based on lines count as reference definitions are line-based
889        let mut refs = Vec::with_capacity(lines.len() / 20); // ~1 ref per 20 lines
890
891        for (line_idx, line_info) in lines.iter().enumerate() {
892            // Skip lines in code blocks
893            if line_info.in_code_block {
894                continue;
895            }
896
897            let line = &line_info.content;
898            let line_num = line_idx + 1;
899
900            if let Some(cap) = REF_DEF_PATTERN.captures(line) {
901                let id = cap.get(1).unwrap().as_str().to_lowercase();
902                let url = cap.get(2).unwrap().as_str().to_string();
903                let title = cap.get(3).or_else(|| cap.get(4)).map(|m| m.as_str().to_string());
904
905                refs.push(ReferenceDef {
906                    line: line_num,
907                    id,
908                    url,
909                    title,
910                });
911            }
912        }
913
914        refs
915    }
916
917    /// Pre-compute line information
918    fn compute_line_info(content: &str, line_offsets: &[usize], code_blocks: &[(usize, usize)]) -> Vec<LineInfo> {
919        lazy_static! {
920            // Regex for list detection - allow any whitespace including no space (to catch malformed lists)
921            static ref UNORDERED_REGEX: regex::Regex = regex::Regex::new(r"^(\s*)([-*+])([ \t]*)(.*)").unwrap();
922            static ref ORDERED_REGEX: regex::Regex = regex::Regex::new(r"^(\s*)(\d+)([.)])([ \t]*)(.*)").unwrap();
923
924            // Regex for blockquote prefix
925            static ref BLOCKQUOTE_REGEX: regex::Regex = regex::Regex::new(r"^(\s*>\s*)(.*)").unwrap();
926
927            // Regex for heading detection
928            static ref ATX_HEADING_REGEX: regex::Regex = regex::Regex::new(r"^(\s*)(#{1,6})(\s*)(.*)$").unwrap();
929            static ref SETEXT_UNDERLINE_REGEX: regex::Regex = regex::Regex::new(r"^(\s*)(=+|-+)\s*$").unwrap();
930
931            // Regex for blockquote detection
932            static ref BLOCKQUOTE_REGEX_FULL: regex::Regex = regex::Regex::new(r"^(\s*)(>+)(\s*)(.*)$").unwrap();
933        }
934
935        let content_lines: Vec<&str> = content.lines().collect();
936        let mut lines = Vec::with_capacity(content_lines.len());
937
938        // Detect front matter boundaries FIRST, before any other parsing
939        let mut in_front_matter = false;
940        let mut front_matter_end = 0;
941        if content_lines.first().map(|l| l.trim()) == Some("---") {
942            in_front_matter = true;
943            for (idx, line) in content_lines.iter().enumerate().skip(1) {
944                if line.trim() == "---" {
945                    front_matter_end = idx;
946                    break;
947                }
948            }
949        }
950
951        for (i, line) in content_lines.iter().enumerate() {
952            let byte_offset = line_offsets.get(i).copied().unwrap_or(0);
953            let indent = line.len() - line.trim_start().len();
954            // For blank detection, consider blockquote context
955            let is_blank = if let Some(caps) = BLOCKQUOTE_REGEX.captures(line) {
956                // In blockquote context, check if content after prefix is blank
957                let after_prefix = caps.get(2).map_or("", |m| m.as_str());
958                after_prefix.trim().is_empty()
959            } else {
960                line.trim().is_empty()
961            };
962            // Check if this line is inside a code block (not inline code span)
963            // We only want to check for fenced/indented code blocks, not inline code
964            let in_code_block = code_blocks.iter().any(|&(start, end)| {
965                // Only consider ranges that span multiple lines (code blocks)
966                // Inline code spans are typically on a single line
967                let block_content = &content[start..end];
968                let is_multiline = block_content.contains('\n');
969                let is_fenced = block_content.starts_with("```") || block_content.starts_with("~~~");
970                let is_indented = !is_fenced
971                    && block_content
972                        .lines()
973                        .all(|l| l.starts_with("    ") || l.starts_with("\t") || l.trim().is_empty());
974
975                byte_offset >= start && byte_offset < end && (is_multiline || is_fenced || is_indented)
976            });
977
978            // Detect list items (skip if in frontmatter)
979            let list_item = if !(in_code_block || is_blank || in_front_matter && i <= front_matter_end) {
980                // Strip blockquote prefix if present for list detection
981                let (line_for_list_check, blockquote_prefix_len) = if let Some(caps) = BLOCKQUOTE_REGEX.captures(line) {
982                    let prefix = caps.get(1).unwrap().as_str();
983                    let content = caps.get(2).unwrap().as_str();
984                    (content, prefix.len())
985                } else {
986                    (&**line, 0)
987                };
988
989                if let Some(caps) = UNORDERED_REGEX.captures(line_for_list_check) {
990                    let leading_spaces = caps.get(1).map_or("", |m| m.as_str());
991                    let marker = caps.get(2).map_or("", |m| m.as_str());
992                    let spacing = caps.get(3).map_or("", |m| m.as_str());
993                    let _content = caps.get(4).map_or("", |m| m.as_str());
994                    let marker_column = blockquote_prefix_len + leading_spaces.len();
995                    let content_column = marker_column + marker.len() + spacing.len();
996
997                    // According to CommonMark spec, unordered list items MUST have at least one space
998                    // after the marker (-, *, or +). Without a space, it's not a list item.
999                    // This also naturally handles cases like:
1000                    // - *emphasis* (not a list)
1001                    // - **bold** (not a list)
1002                    // - --- (horizontal rule, not a list)
1003                    if spacing.is_empty() {
1004                        None
1005                    } else {
1006                        Some(ListItemInfo {
1007                            marker: marker.to_string(),
1008                            is_ordered: false,
1009                            number: None,
1010                            marker_column,
1011                            content_column,
1012                        })
1013                    }
1014                } else if let Some(caps) = ORDERED_REGEX.captures(line_for_list_check) {
1015                    let leading_spaces = caps.get(1).map_or("", |m| m.as_str());
1016                    let number_str = caps.get(2).map_or("", |m| m.as_str());
1017                    let delimiter = caps.get(3).map_or("", |m| m.as_str());
1018                    let spacing = caps.get(4).map_or("", |m| m.as_str());
1019                    let _content = caps.get(5).map_or("", |m| m.as_str());
1020                    let marker = format!("{number_str}{delimiter}");
1021                    let marker_column = blockquote_prefix_len + leading_spaces.len();
1022                    let content_column = marker_column + marker.len() + spacing.len();
1023
1024                    // According to CommonMark spec, ordered list items MUST have at least one space
1025                    // after the marker (period or parenthesis). Without a space, it's not a list item.
1026                    if spacing.is_empty() {
1027                        None
1028                    } else {
1029                        Some(ListItemInfo {
1030                            marker,
1031                            is_ordered: true,
1032                            number: number_str.parse().ok(),
1033                            marker_column,
1034                            content_column,
1035                        })
1036                    }
1037                } else {
1038                    None
1039                }
1040            } else {
1041                None
1042            };
1043
1044            lines.push(LineInfo {
1045                content: line.to_string(),
1046                byte_offset,
1047                indent,
1048                is_blank,
1049                in_code_block,
1050                in_front_matter: in_front_matter && i <= front_matter_end,
1051                list_item,
1052                heading: None,    // Will be populated in second pass for Setext headings
1053                blockquote: None, // Will be populated after line creation
1054            });
1055        }
1056
1057        // Second pass: detect headings (including Setext which needs look-ahead) and blockquotes
1058        for i in 0..content_lines.len() {
1059            if lines[i].in_code_block {
1060                continue;
1061            }
1062
1063            // Skip lines in front matter
1064            if in_front_matter && i <= front_matter_end {
1065                continue;
1066            }
1067
1068            let line = content_lines[i];
1069
1070            // Check for blockquotes (even on blank lines within blockquotes)
1071            if let Some(caps) = BLOCKQUOTE_REGEX_FULL.captures(line) {
1072                let indent_str = caps.get(1).map_or("", |m| m.as_str());
1073                let markers = caps.get(2).map_or("", |m| m.as_str());
1074                let spaces_after = caps.get(3).map_or("", |m| m.as_str());
1075                let content = caps.get(4).map_or("", |m| m.as_str());
1076
1077                let nesting_level = markers.chars().filter(|&c| c == '>').count();
1078                let marker_column = indent_str.len();
1079
1080                // Build the prefix (indentation + markers + space)
1081                let prefix = format!("{indent_str}{markers}{spaces_after}");
1082
1083                // Check for various blockquote issues
1084                let has_no_space = spaces_after.is_empty() && !content.is_empty();
1085                // Consider tabs as multiple spaces, or actual multiple spaces
1086                let has_multiple_spaces = spaces_after.len() > 1 || spaces_after.contains('\t');
1087
1088                // Check if needs MD028 fix (empty blockquote without proper spacing)
1089                let needs_md028_fix = content.trim().is_empty() && spaces_after.is_empty();
1090
1091                lines[i].blockquote = Some(BlockquoteInfo {
1092                    nesting_level,
1093                    indent: indent_str.to_string(),
1094                    marker_column,
1095                    prefix,
1096                    content: content.to_string(),
1097                    has_no_space_after_marker: has_no_space,
1098                    has_multiple_spaces_after_marker: has_multiple_spaces,
1099                    needs_md028_fix,
1100                });
1101            }
1102
1103            // Skip heading detection for blank lines
1104            if lines[i].is_blank {
1105                continue;
1106            }
1107
1108            // Check for ATX headings
1109            if let Some(caps) = ATX_HEADING_REGEX.captures(line) {
1110                let leading_spaces = caps.get(1).map_or("", |m| m.as_str());
1111                let hashes = caps.get(2).map_or("", |m| m.as_str());
1112                let spaces_after = caps.get(3).map_or("", |m| m.as_str());
1113                let rest = caps.get(4).map_or("", |m| m.as_str());
1114
1115                let level = hashes.len() as u8;
1116                let marker_column = leading_spaces.len();
1117
1118                // Check for closing sequence, but handle custom IDs that might come after
1119                let (text, has_closing, closing_seq) = {
1120                    // First check if there's a custom ID at the end
1121                    let (rest_without_id, custom_id_part) = if let Some(id_start) = rest.rfind(" {#") {
1122                        // Check if this looks like a valid custom ID (ends with })
1123                        if rest[id_start..].trim_end().ends_with('}') {
1124                            // Split off the custom ID
1125                            (&rest[..id_start], &rest[id_start..])
1126                        } else {
1127                            (rest, "")
1128                        }
1129                    } else {
1130                        (rest, "")
1131                    };
1132
1133                    // Now look for closing hashes in the part before the custom ID
1134                    let trimmed_rest = rest_without_id.trim_end();
1135                    if let Some(last_hash_pos) = trimmed_rest.rfind('#') {
1136                        // Look for the start of the hash sequence
1137                        let mut start_of_hashes = last_hash_pos;
1138                        while start_of_hashes > 0 && trimmed_rest.chars().nth(start_of_hashes - 1) == Some('#') {
1139                            start_of_hashes -= 1;
1140                        }
1141
1142                        // Check if there's at least one space before the closing hashes
1143                        let has_space_before = start_of_hashes == 0
1144                            || trimmed_rest
1145                                .chars()
1146                                .nth(start_of_hashes - 1)
1147                                .is_some_and(|c| c.is_whitespace());
1148
1149                        // Check if this is a valid closing sequence (all hashes to end of trimmed part)
1150                        let potential_closing = &trimmed_rest[start_of_hashes..];
1151                        let is_all_hashes = potential_closing.chars().all(|c| c == '#');
1152
1153                        if is_all_hashes && has_space_before {
1154                            // This is a closing sequence
1155                            let closing_hashes = potential_closing.to_string();
1156                            // The text is everything before the closing hashes
1157                            // Don't include the custom ID here - it will be extracted later
1158                            let text_part = if !custom_id_part.is_empty() {
1159                                // If we have a custom ID, append it back to get the full rest
1160                                // This allows the extract_header_id function to handle it properly
1161                                format!("{}{}", rest_without_id[..start_of_hashes].trim_end(), custom_id_part)
1162                            } else {
1163                                rest_without_id[..start_of_hashes].trim_end().to_string()
1164                            };
1165                            (text_part, true, closing_hashes)
1166                        } else {
1167                            // Not a valid closing sequence, return the full content
1168                            (rest.to_string(), false, String::new())
1169                        }
1170                    } else {
1171                        // No hashes found, return the full content
1172                        (rest.to_string(), false, String::new())
1173                    }
1174                };
1175
1176                let content_column = marker_column + hashes.len() + spaces_after.len();
1177
1178                // Extract custom header ID if present
1179                let raw_text = text.trim().to_string();
1180                let (clean_text, mut custom_id) = crate::utils::header_id_utils::extract_header_id(&raw_text);
1181
1182                // If no custom ID was found on the header line, check the next line for standalone attr-list
1183                if custom_id.is_none() && i + 1 < content_lines.len() && i + 1 < lines.len() {
1184                    let next_line = content_lines[i + 1];
1185                    if !lines[i + 1].in_code_block
1186                        && crate::utils::header_id_utils::is_standalone_attr_list(next_line)
1187                        && let Some(next_line_id) =
1188                            crate::utils::header_id_utils::extract_standalone_attr_list_id(next_line)
1189                    {
1190                        custom_id = Some(next_line_id);
1191                    }
1192                }
1193
1194                lines[i].heading = Some(HeadingInfo {
1195                    level,
1196                    style: HeadingStyle::ATX,
1197                    marker: hashes.to_string(),
1198                    marker_column,
1199                    content_column,
1200                    text: clean_text,
1201                    custom_id,
1202                    raw_text,
1203                    has_closing_sequence: has_closing,
1204                    closing_sequence: closing_seq,
1205                });
1206            }
1207            // Check for Setext headings (need to look at next line)
1208            else if i + 1 < content_lines.len() {
1209                let next_line = content_lines[i + 1];
1210                if !lines[i + 1].in_code_block && SETEXT_UNDERLINE_REGEX.is_match(next_line) {
1211                    // Skip if next line is front matter delimiter
1212                    if in_front_matter && i < front_matter_end {
1213                        continue;
1214                    }
1215
1216                    let underline = next_line.trim();
1217                    let level = if underline.starts_with('=') { 1 } else { 2 };
1218                    let style = if level == 1 {
1219                        HeadingStyle::Setext1
1220                    } else {
1221                        HeadingStyle::Setext2
1222                    };
1223
1224                    // Extract custom header ID if present
1225                    let raw_text = line.trim().to_string();
1226                    let (clean_text, mut custom_id) = crate::utils::header_id_utils::extract_header_id(&raw_text);
1227
1228                    // If no custom ID was found on the header line, check the line after underline for standalone attr-list
1229                    if custom_id.is_none() && i + 2 < content_lines.len() && i + 2 < lines.len() {
1230                        let attr_line = content_lines[i + 2];
1231                        if !lines[i + 2].in_code_block
1232                            && crate::utils::header_id_utils::is_standalone_attr_list(attr_line)
1233                            && let Some(attr_line_id) =
1234                                crate::utils::header_id_utils::extract_standalone_attr_list_id(attr_line)
1235                        {
1236                            custom_id = Some(attr_line_id);
1237                        }
1238                    }
1239
1240                    lines[i].heading = Some(HeadingInfo {
1241                        level,
1242                        style,
1243                        marker: underline.to_string(),
1244                        marker_column: next_line.len() - next_line.trim_start().len(),
1245                        content_column: lines[i].indent,
1246                        text: clean_text,
1247                        custom_id,
1248                        raw_text,
1249                        has_closing_sequence: false,
1250                        closing_sequence: String::new(),
1251                    });
1252                }
1253            }
1254        }
1255
1256        lines
1257    }
1258
1259    /// Parse all inline code spans in the content
1260    fn parse_code_spans(content: &str, lines: &[LineInfo]) -> Vec<CodeSpan> {
1261        // Pre-size based on content - code spans are fairly common
1262        let mut code_spans = Vec::with_capacity(content.matches('`').count() / 2); // Each code span has 2 backticks
1263
1264        // Quick check - if no backticks, no code spans
1265        if !content.contains('`') {
1266            return code_spans;
1267        }
1268
1269        let mut pos = 0;
1270        let bytes = content.as_bytes();
1271
1272        while pos < bytes.len() {
1273            // Find the next backtick
1274            if let Some(backtick_start) = content[pos..].find('`') {
1275                let start_pos = pos + backtick_start;
1276
1277                // Skip if this backtick is inside a code block
1278                let mut in_code_block = false;
1279                for (line_idx, line_info) in lines.iter().enumerate() {
1280                    if start_pos >= line_info.byte_offset
1281                        && (line_idx + 1 >= lines.len() || start_pos < lines[line_idx + 1].byte_offset)
1282                    {
1283                        in_code_block = line_info.in_code_block;
1284                        break;
1285                    }
1286                }
1287
1288                if in_code_block {
1289                    pos = start_pos + 1;
1290                    continue;
1291                }
1292
1293                // Count consecutive backticks
1294                let mut backtick_count = 0;
1295                let mut i = start_pos;
1296                while i < bytes.len() && bytes[i] == b'`' {
1297                    backtick_count += 1;
1298                    i += 1;
1299                }
1300
1301                // Look for matching closing backticks
1302                let search_start = start_pos + backtick_count;
1303                let closing_pattern = &content[start_pos..start_pos + backtick_count];
1304
1305                if let Some(rel_end) = content[search_start..].find(closing_pattern) {
1306                    // Check that the closing backticks are not followed by more backticks
1307                    let end_pos = search_start + rel_end;
1308                    let check_pos = end_pos + backtick_count;
1309
1310                    // Make sure we have exactly the right number of backticks (not more)
1311                    if check_pos >= bytes.len() || bytes[check_pos] != b'`' {
1312                        // We found a valid code span
1313                        let content_start = start_pos + backtick_count;
1314                        let content_end = end_pos;
1315                        let span_content = content[content_start..content_end].to_string();
1316
1317                        // Find which line this code span starts on
1318                        let mut line_num = 1;
1319                        let mut col_start = start_pos;
1320                        for (idx, line_info) in lines.iter().enumerate() {
1321                            if start_pos >= line_info.byte_offset {
1322                                line_num = idx + 1;
1323                                col_start = start_pos - line_info.byte_offset;
1324                            } else {
1325                                break;
1326                            }
1327                        }
1328
1329                        // Find end column
1330                        let mut col_end = end_pos + backtick_count;
1331                        for line_info in lines.iter() {
1332                            if end_pos + backtick_count > line_info.byte_offset {
1333                                col_end = end_pos + backtick_count - line_info.byte_offset;
1334                            } else {
1335                                break;
1336                            }
1337                        }
1338
1339                        code_spans.push(CodeSpan {
1340                            line: line_num,
1341                            start_col: col_start,
1342                            end_col: col_end,
1343                            byte_offset: start_pos,
1344                            byte_end: end_pos + backtick_count,
1345                            backtick_count,
1346                            content: span_content,
1347                        });
1348
1349                        // Continue searching after this code span
1350                        pos = end_pos + backtick_count;
1351                        continue;
1352                    }
1353                }
1354
1355                // No matching closing backticks found, move past these opening backticks
1356                pos = start_pos + backtick_count;
1357            } else {
1358                // No more backticks found
1359                break;
1360            }
1361        }
1362
1363        code_spans
1364    }
1365
1366    /// Parse all list blocks in the content
1367    fn parse_list_blocks(lines: &[LineInfo]) -> Vec<ListBlock> {
1368        // Pre-size based on lines that could be list items
1369        let mut list_blocks = Vec::with_capacity(lines.len() / 10); // Estimate ~10% of lines might start list blocks
1370        let mut current_block: Option<ListBlock> = None;
1371        let mut last_list_item_line = 0;
1372        let mut current_indent_level = 0;
1373        let mut last_marker_width = 0;
1374
1375        for (line_idx, line_info) in lines.iter().enumerate() {
1376            let line_num = line_idx + 1;
1377
1378            // Enhanced code block handling using Design #3's context analysis
1379            if line_info.in_code_block {
1380                if let Some(ref mut block) = current_block {
1381                    // Calculate minimum indentation for list continuation
1382                    let min_continuation_indent = CodeBlockUtils::calculate_min_continuation_indent(lines, line_idx);
1383
1384                    // Analyze code block context using the three-tier classification
1385                    let context = CodeBlockUtils::analyze_code_block_context(lines, line_idx, min_continuation_indent);
1386
1387                    match context {
1388                        CodeBlockContext::Indented => {
1389                            // Code block is properly indented - continues the list
1390                            block.end_line = line_num;
1391                            continue;
1392                        }
1393                        CodeBlockContext::Standalone => {
1394                            // Code block separates lists - end current block
1395                            let completed_block = current_block.take().unwrap();
1396                            list_blocks.push(completed_block);
1397                            continue;
1398                        }
1399                        CodeBlockContext::Adjacent => {
1400                            // Edge case - use conservative behavior (continue list)
1401                            block.end_line = line_num;
1402                            continue;
1403                        }
1404                    }
1405                } else {
1406                    // No current list block - skip code block lines
1407                    continue;
1408                }
1409            }
1410
1411            // Extract blockquote prefix if any
1412            let blockquote_prefix = if let Some(caps) = BLOCKQUOTE_PREFIX_REGEX.captures(&line_info.content) {
1413                caps.get(0).unwrap().as_str().to_string()
1414            } else {
1415                String::new()
1416            };
1417
1418            // Check if this line is a list item
1419            if let Some(list_item) = &line_info.list_item {
1420                // Calculate nesting level based on indentation
1421                let item_indent = list_item.marker_column;
1422                let nesting = item_indent / 2; // Assume 2-space indentation for nesting
1423
1424                if let Some(ref mut block) = current_block {
1425                    // Check if this continues the current block
1426                    // For nested lists, we need to check if this is a nested item (higher nesting level)
1427                    // or a continuation at the same or lower level
1428                    let is_nested = nesting > block.nesting_level;
1429                    let same_type =
1430                        (block.is_ordered && list_item.is_ordered) || (!block.is_ordered && !list_item.is_ordered);
1431                    let same_context = block.blockquote_prefix == blockquote_prefix;
1432                    let reasonable_distance = line_num <= last_list_item_line + 2; // Allow one blank line
1433
1434                    // For unordered lists, also check marker consistency
1435                    let marker_compatible =
1436                        block.is_ordered || block.marker.is_none() || block.marker.as_ref() == Some(&list_item.marker);
1437
1438                    // Check if there's non-list content between the last item and this one
1439                    let has_non_list_content = {
1440                        let mut found_non_list = false;
1441                        // Use the last item from the current block, not the global last_list_item_line
1442                        let block_last_item_line = block.item_lines.last().copied().unwrap_or(block.end_line);
1443                        for check_line in (block_last_item_line + 1)..line_num {
1444                            let check_idx = check_line - 1;
1445                            if check_idx < lines.len() {
1446                                let check_info = &lines[check_idx];
1447                                // Check for content that breaks the list
1448                                let is_list_breaking_content = if check_info.in_code_block {
1449                                    // Use enhanced code block classification for list separation
1450                                    let last_item_marker_width =
1451                                        if block_last_item_line > 0 && block_last_item_line <= lines.len() {
1452                                            lines[block_last_item_line - 1]
1453                                                .list_item
1454                                                .as_ref()
1455                                                .map(|li| {
1456                                                    if li.is_ordered {
1457                                                        li.marker.len() + 1 // Add 1 for the space after ordered list markers
1458                                                    } else {
1459                                                        li.marker.len()
1460                                                    }
1461                                                })
1462                                                .unwrap_or(3) // fallback to 3 if no list item found
1463                                        } else {
1464                                            3 // fallback
1465                                        };
1466
1467                                    let min_continuation = if block.is_ordered { last_item_marker_width } else { 2 };
1468
1469                                    // Analyze code block context using our enhanced classification
1470                                    let context = CodeBlockUtils::analyze_code_block_context(
1471                                        lines,
1472                                        check_line - 1,
1473                                        min_continuation,
1474                                    );
1475
1476                                    // Standalone code blocks break lists, indented ones continue them
1477                                    matches!(context, CodeBlockContext::Standalone)
1478                                } else if !check_info.is_blank && check_info.list_item.is_none() {
1479                                    // Check for structural separators that should break lists (from issue #42)
1480                                    let line_content = check_info.content.trim();
1481
1482                                    // Any of these structural separators break lists
1483                                    if check_info.heading.is_some()
1484                                        || line_content.starts_with("---")
1485                                        || line_content.starts_with("***")
1486                                        || line_content.starts_with("___")
1487                                        || line_content.contains('|')
1488                                        || line_content.starts_with(">")
1489                                    {
1490                                        true
1491                                    }
1492                                    // Other non-list content - check if properly indented
1493                                    else {
1494                                        let last_item_marker_width =
1495                                            if block_last_item_line > 0 && block_last_item_line <= lines.len() {
1496                                                lines[block_last_item_line - 1]
1497                                                    .list_item
1498                                                    .as_ref()
1499                                                    .map(|li| {
1500                                                        if li.is_ordered {
1501                                                            li.marker.len() + 1 // Add 1 for the space after ordered list markers
1502                                                        } else {
1503                                                            li.marker.len()
1504                                                        }
1505                                                    })
1506                                                    .unwrap_or(3) // fallback to 3 if no list item found
1507                                            } else {
1508                                                3 // fallback
1509                                            };
1510
1511                                        let min_continuation =
1512                                            if block.is_ordered { last_item_marker_width } else { 2 };
1513                                        check_info.indent < min_continuation
1514                                    }
1515                                } else {
1516                                    false
1517                                };
1518
1519                                if is_list_breaking_content {
1520                                    // Not indented enough, so it breaks the list
1521                                    found_non_list = true;
1522                                    break;
1523                                }
1524                            }
1525                        }
1526                        found_non_list
1527                    };
1528
1529                    // A list continues if:
1530                    // 1. It's a nested item (indented more than the parent), OR
1531                    // 2. It's the same type at the same level with reasonable distance
1532                    let continues_list = if is_nested {
1533                        // Nested items always continue the list if they're in the same context
1534                        same_context && reasonable_distance && !has_non_list_content
1535                    } else {
1536                        // Same-level items need to match type and markers
1537                        same_type && same_context && reasonable_distance && marker_compatible && !has_non_list_content
1538                    };
1539
1540                    if continues_list {
1541                        // Extend current block
1542                        block.end_line = line_num;
1543                        block.item_lines.push(line_num);
1544
1545                        // Update max marker width
1546                        block.max_marker_width = block.max_marker_width.max(if list_item.is_ordered {
1547                            list_item.marker.len() + 1
1548                        } else {
1549                            list_item.marker.len()
1550                        });
1551
1552                        // Update marker consistency for unordered lists
1553                        if !block.is_ordered
1554                            && block.marker.is_some()
1555                            && block.marker.as_ref() != Some(&list_item.marker)
1556                        {
1557                            // Mixed markers, clear the marker field
1558                            block.marker = None;
1559                        }
1560                    } else {
1561                        // End current block and start a new one
1562                        list_blocks.push(block.clone());
1563
1564                        *block = ListBlock {
1565                            start_line: line_num,
1566                            end_line: line_num,
1567                            is_ordered: list_item.is_ordered,
1568                            marker: if list_item.is_ordered {
1569                                None
1570                            } else {
1571                                Some(list_item.marker.clone())
1572                            },
1573                            blockquote_prefix: blockquote_prefix.clone(),
1574                            item_lines: vec![line_num],
1575                            nesting_level: nesting,
1576                            max_marker_width: if list_item.is_ordered {
1577                                list_item.marker.len() + 1
1578                            } else {
1579                                list_item.marker.len()
1580                            },
1581                        };
1582                    }
1583                } else {
1584                    // Start a new block
1585                    current_block = Some(ListBlock {
1586                        start_line: line_num,
1587                        end_line: line_num,
1588                        is_ordered: list_item.is_ordered,
1589                        marker: if list_item.is_ordered {
1590                            None
1591                        } else {
1592                            Some(list_item.marker.clone())
1593                        },
1594                        blockquote_prefix,
1595                        item_lines: vec![line_num],
1596                        nesting_level: nesting,
1597                        max_marker_width: list_item.marker.len(),
1598                    });
1599                }
1600
1601                last_list_item_line = line_num;
1602                current_indent_level = item_indent;
1603                last_marker_width = if list_item.is_ordered {
1604                    list_item.marker.len() + 1 // Add 1 for the space after ordered list markers
1605                } else {
1606                    list_item.marker.len()
1607                };
1608            } else if let Some(ref mut block) = current_block {
1609                // Not a list item - check if it continues the current block
1610
1611                // For MD032 compatibility, we use a simple approach:
1612                // - Indented lines continue the list
1613                // - Blank lines followed by indented content continue the list
1614                // - Everything else ends the list
1615
1616                // Calculate minimum indentation for list continuation
1617                // For ordered lists, use the last marker width (e.g., 3 for "1. ", 4 for "10. ")
1618                // For unordered lists like "- ", content starts at column 2, so continuations need at least 2 spaces
1619                let min_continuation_indent = if block.is_ordered {
1620                    current_indent_level + last_marker_width
1621                } else {
1622                    current_indent_level + 2 // Unordered lists need at least 2 spaces (e.g., "- " = 2 chars)
1623                };
1624
1625                if line_info.indent >= min_continuation_indent {
1626                    // Indented line continues the list
1627                    block.end_line = line_num;
1628                } else if line_info.is_blank {
1629                    // Blank line - check if it's internal to the list or ending it
1630                    // We only include blank lines that are followed by more list content
1631                    let mut check_idx = line_idx + 1;
1632                    let mut found_continuation = false;
1633
1634                    // Skip additional blank lines
1635                    while check_idx < lines.len() && lines[check_idx].is_blank {
1636                        check_idx += 1;
1637                    }
1638
1639                    if check_idx < lines.len() {
1640                        let next_line = &lines[check_idx];
1641                        // Check if followed by indented content (list continuation)
1642                        if !next_line.in_code_block && next_line.indent >= min_continuation_indent {
1643                            found_continuation = true;
1644                        }
1645                        // Check if followed by another list item at the same level
1646                        else if !next_line.in_code_block
1647                            && next_line.list_item.is_some()
1648                            && let Some(item) = &next_line.list_item
1649                        {
1650                            let next_blockquote_prefix = BLOCKQUOTE_PREFIX_REGEX
1651                                .find(&next_line.content)
1652                                .map_or(String::new(), |m| m.as_str().to_string());
1653                            if item.marker_column == current_indent_level
1654                                && item.is_ordered == block.is_ordered
1655                                && block.blockquote_prefix.trim() == next_blockquote_prefix.trim()
1656                            {
1657                                // Check if there was meaningful content between the list items (unused now)
1658                                // This variable is kept for potential future use but is currently replaced by has_structural_separators
1659                                let _has_meaningful_content = (line_idx + 1..check_idx).any(|idx| {
1660                                    if let Some(between_line) = lines.get(idx) {
1661                                        let trimmed = between_line.content.trim();
1662                                        // Skip empty lines
1663                                        if trimmed.is_empty() {
1664                                            return false;
1665                                        }
1666                                        // Check for meaningful content
1667                                        let line_indent =
1668                                            between_line.content.len() - between_line.content.trim_start().len();
1669
1670                                        // Structural separators (code fences, headings, etc.) are meaningful and should BREAK lists
1671                                        if trimmed.starts_with("```")
1672                                            || trimmed.starts_with("~~~")
1673                                            || trimmed.starts_with("---")
1674                                            || trimmed.starts_with("***")
1675                                            || trimmed.starts_with("___")
1676                                            || trimmed.starts_with(">")
1677                                            || trimmed.contains('|') // Tables
1678                                            || between_line.heading.is_some()
1679                                        {
1680                                            return true; // These are structural separators - meaningful content that breaks lists
1681                                        }
1682
1683                                        // Only properly indented content continues the list
1684                                        line_indent >= min_continuation_indent
1685                                    } else {
1686                                        false
1687                                    }
1688                                });
1689
1690                                if block.is_ordered {
1691                                    // For ordered lists: don't continue if there are structural separators
1692                                    // Check if there are structural separators between the list items
1693                                    let has_structural_separators = (line_idx + 1..check_idx).any(|idx| {
1694                                        if let Some(between_line) = lines.get(idx) {
1695                                            let trimmed = between_line.content.trim();
1696                                            if trimmed.is_empty() {
1697                                                return false;
1698                                            }
1699                                            // Check for structural separators that break lists
1700                                            trimmed.starts_with("```")
1701                                                || trimmed.starts_with("~~~")
1702                                                || trimmed.starts_with("---")
1703                                                || trimmed.starts_with("***")
1704                                                || trimmed.starts_with("___")
1705                                                || trimmed.starts_with(">")
1706                                                || trimmed.contains('|') // Tables
1707                                                || between_line.heading.is_some()
1708                                        } else {
1709                                            false
1710                                        }
1711                                    });
1712                                    found_continuation = !has_structural_separators;
1713                                } else {
1714                                    // For unordered lists: also check for structural separators
1715                                    let has_structural_separators = (line_idx + 1..check_idx).any(|idx| {
1716                                        if let Some(between_line) = lines.get(idx) {
1717                                            let trimmed = between_line.content.trim();
1718                                            if trimmed.is_empty() {
1719                                                return false;
1720                                            }
1721                                            // Check for structural separators that break lists
1722                                            trimmed.starts_with("```")
1723                                                || trimmed.starts_with("~~~")
1724                                                || trimmed.starts_with("---")
1725                                                || trimmed.starts_with("***")
1726                                                || trimmed.starts_with("___")
1727                                                || trimmed.starts_with(">")
1728                                                || trimmed.contains('|') // Tables
1729                                                || between_line.heading.is_some()
1730                                        } else {
1731                                            false
1732                                        }
1733                                    });
1734                                    found_continuation = !has_structural_separators;
1735                                }
1736                            }
1737                        }
1738                    }
1739
1740                    if found_continuation {
1741                        // Include the blank line in the block
1742                        block.end_line = line_num;
1743                    } else {
1744                        // Blank line ends the list - don't include it
1745                        list_blocks.push(block.clone());
1746                        current_block = None;
1747                    }
1748                } else {
1749                    // Check for lazy continuation - non-indented line immediately after a list item
1750                    // But only if the line has sufficient indentation for the list type
1751                    let min_required_indent = if block.is_ordered {
1752                        current_indent_level + last_marker_width
1753                    } else {
1754                        current_indent_level + 2
1755                    };
1756
1757                    // For lazy continuation to apply, the line must either:
1758                    // 1. Have no indentation (true lazy continuation)
1759                    // 2. Have sufficient indentation for the list type
1760                    // BUT structural separators (headings, code blocks, etc.) should never be lazy continuations
1761                    let line_content = line_info.content.trim();
1762                    let is_structural_separator = line_info.heading.is_some()
1763                        || line_content.starts_with("```")
1764                        || line_content.starts_with("~~~")
1765                        || line_content.starts_with("---")
1766                        || line_content.starts_with("***")
1767                        || line_content.starts_with("___")
1768                        || line_content.starts_with(">")
1769                        || line_content.contains('|'); // Tables
1770
1771                    let is_lazy_continuation = last_list_item_line == line_num - 1
1772                        && !is_structural_separator
1773                        && !line_info.is_blank
1774                        && (line_info.indent == 0 || line_info.indent >= min_required_indent);
1775
1776                    if is_lazy_continuation {
1777                        // Additional check: if the line starts with uppercase and looks like a new sentence,
1778                        // it's probably not a continuation
1779                        let content_to_check = if !blockquote_prefix.is_empty() {
1780                            // Strip blockquote prefix to check the actual content
1781                            line_info
1782                                .content
1783                                .strip_prefix(&blockquote_prefix)
1784                                .unwrap_or(&line_info.content)
1785                                .trim()
1786                        } else {
1787                            line_info.content.trim()
1788                        };
1789
1790                        let starts_with_uppercase = content_to_check.chars().next().is_some_and(|c| c.is_uppercase());
1791
1792                        // If it starts with uppercase and the previous line ended with punctuation,
1793                        // it's likely a new paragraph, not a continuation
1794                        if starts_with_uppercase && last_list_item_line > 0 {
1795                            // This looks like a new paragraph
1796                            list_blocks.push(block.clone());
1797                            current_block = None;
1798                        } else {
1799                            // This is a lazy continuation line
1800                            block.end_line = line_num;
1801                        }
1802                    } else {
1803                        // Non-indented, non-blank line that's not a lazy continuation - end the block
1804                        list_blocks.push(block.clone());
1805                        current_block = None;
1806                    }
1807                }
1808            }
1809        }
1810
1811        // Don't forget the last block
1812        if let Some(block) = current_block {
1813            list_blocks.push(block);
1814        }
1815
1816        // Merge adjacent blocks that should be one
1817        merge_adjacent_list_blocks(&mut list_blocks, lines);
1818
1819        list_blocks
1820    }
1821
1822    /// Compute character frequency for fast content analysis
1823    fn compute_char_frequency(content: &str) -> CharFrequency {
1824        let mut frequency = CharFrequency::default();
1825
1826        for ch in content.chars() {
1827            match ch {
1828                '#' => frequency.hash_count += 1,
1829                '*' => frequency.asterisk_count += 1,
1830                '_' => frequency.underscore_count += 1,
1831                '-' => frequency.hyphen_count += 1,
1832                '+' => frequency.plus_count += 1,
1833                '>' => frequency.gt_count += 1,
1834                '|' => frequency.pipe_count += 1,
1835                '[' => frequency.bracket_count += 1,
1836                '`' => frequency.backtick_count += 1,
1837                '<' => frequency.lt_count += 1,
1838                '!' => frequency.exclamation_count += 1,
1839                '\n' => frequency.newline_count += 1,
1840                _ => {}
1841            }
1842        }
1843
1844        frequency
1845    }
1846
1847    /// Parse HTML tags in the content
1848    fn parse_html_tags(content: &str, lines: &[LineInfo], code_blocks: &[(usize, usize)]) -> Vec<HtmlTag> {
1849        lazy_static! {
1850            static ref HTML_TAG_REGEX: regex::Regex =
1851                regex::Regex::new(r"(?i)<(/?)([a-zA-Z][a-zA-Z0-9]*)\b[^>]*(/?)>").unwrap();
1852        }
1853
1854        let mut html_tags = Vec::with_capacity(content.matches('<').count());
1855
1856        for cap in HTML_TAG_REGEX.captures_iter(content) {
1857            let full_match = cap.get(0).unwrap();
1858            let match_start = full_match.start();
1859            let match_end = full_match.end();
1860
1861            // Skip if in code block
1862            if CodeBlockUtils::is_in_code_block_or_span(code_blocks, match_start) {
1863                continue;
1864            }
1865
1866            let is_closing = !cap.get(1).unwrap().as_str().is_empty();
1867            let tag_name = cap.get(2).unwrap().as_str().to_lowercase();
1868            let is_self_closing = !cap.get(3).unwrap().as_str().is_empty();
1869
1870            // Find which line this tag is on
1871            let mut line_num = 1;
1872            let mut col_start = match_start;
1873            let mut col_end = match_end;
1874            for (idx, line_info) in lines.iter().enumerate() {
1875                if match_start >= line_info.byte_offset {
1876                    line_num = idx + 1;
1877                    col_start = match_start - line_info.byte_offset;
1878                    col_end = match_end - line_info.byte_offset;
1879                } else {
1880                    break;
1881                }
1882            }
1883
1884            html_tags.push(HtmlTag {
1885                line: line_num,
1886                start_col: col_start,
1887                end_col: col_end,
1888                byte_offset: match_start,
1889                byte_end: match_end,
1890                tag_name,
1891                is_closing,
1892                is_self_closing,
1893                raw_content: full_match.as_str().to_string(),
1894            });
1895        }
1896
1897        html_tags
1898    }
1899
1900    /// Parse emphasis spans in the content
1901    fn parse_emphasis_spans(content: &str, lines: &[LineInfo], code_blocks: &[(usize, usize)]) -> Vec<EmphasisSpan> {
1902        lazy_static! {
1903            static ref EMPHASIS_REGEX: regex::Regex =
1904                regex::Regex::new(r"(\*{1,3}|_{1,3})([^*_\s][^*_]*?)(\*{1,3}|_{1,3})").unwrap();
1905        }
1906
1907        let mut emphasis_spans = Vec::with_capacity(content.matches('*').count() + content.matches('_').count() / 4);
1908
1909        for cap in EMPHASIS_REGEX.captures_iter(content) {
1910            let full_match = cap.get(0).unwrap();
1911            let match_start = full_match.start();
1912            let match_end = full_match.end();
1913
1914            // Skip if in code block
1915            if CodeBlockUtils::is_in_code_block_or_span(code_blocks, match_start) {
1916                continue;
1917            }
1918
1919            let opening_markers = cap.get(1).unwrap().as_str();
1920            let content_part = cap.get(2).unwrap().as_str();
1921            let closing_markers = cap.get(3).unwrap().as_str();
1922
1923            // Validate matching markers
1924            if opening_markers.chars().next() != closing_markers.chars().next()
1925                || opening_markers.len() != closing_markers.len()
1926            {
1927                continue;
1928            }
1929
1930            let marker = opening_markers.chars().next().unwrap();
1931            let marker_count = opening_markers.len();
1932
1933            // Find which line this emphasis is on
1934            let mut line_num = 1;
1935            let mut col_start = match_start;
1936            let mut col_end = match_end;
1937            for (idx, line_info) in lines.iter().enumerate() {
1938                if match_start >= line_info.byte_offset {
1939                    line_num = idx + 1;
1940                    col_start = match_start - line_info.byte_offset;
1941                    col_end = match_end - line_info.byte_offset;
1942                } else {
1943                    break;
1944                }
1945            }
1946
1947            emphasis_spans.push(EmphasisSpan {
1948                line: line_num,
1949                start_col: col_start,
1950                end_col: col_end,
1951                byte_offset: match_start,
1952                byte_end: match_end,
1953                marker,
1954                marker_count,
1955                content: content_part.to_string(),
1956            });
1957        }
1958
1959        emphasis_spans
1960    }
1961
1962    /// Parse table rows in the content
1963    fn parse_table_rows(lines: &[LineInfo]) -> Vec<TableRow> {
1964        let mut table_rows = Vec::with_capacity(lines.len() / 20);
1965
1966        for (line_idx, line_info) in lines.iter().enumerate() {
1967            // Skip lines in code blocks or blank lines
1968            if line_info.in_code_block || line_info.is_blank {
1969                continue;
1970            }
1971
1972            let line = &line_info.content;
1973            let line_num = line_idx + 1;
1974
1975            // Check if this line contains pipes (potential table row)
1976            if !line.contains('|') {
1977                continue;
1978            }
1979
1980            // Count columns by splitting on pipes
1981            let parts: Vec<&str> = line.split('|').collect();
1982            let column_count = if parts.len() > 2 { parts.len() - 2 } else { parts.len() };
1983
1984            // Check if this is a separator row
1985            let is_separator = line.chars().all(|c| "|:-+ \t".contains(c));
1986            let mut column_alignments = Vec::new();
1987
1988            if is_separator {
1989                for part in &parts[1..parts.len() - 1] {
1990                    // Skip first and last empty parts
1991                    let trimmed = part.trim();
1992                    let alignment = if trimmed.starts_with(':') && trimmed.ends_with(':') {
1993                        "center".to_string()
1994                    } else if trimmed.ends_with(':') {
1995                        "right".to_string()
1996                    } else if trimmed.starts_with(':') {
1997                        "left".to_string()
1998                    } else {
1999                        "none".to_string()
2000                    };
2001                    column_alignments.push(alignment);
2002                }
2003            }
2004
2005            table_rows.push(TableRow {
2006                line: line_num,
2007                is_separator,
2008                column_count,
2009                column_alignments,
2010            });
2011        }
2012
2013        table_rows
2014    }
2015
2016    /// Parse bare URLs and emails in the content
2017    fn parse_bare_urls(content: &str, lines: &[LineInfo], code_blocks: &[(usize, usize)]) -> Vec<BareUrl> {
2018        let mut bare_urls = Vec::with_capacity(content.matches("http").count() + content.matches('@').count());
2019
2020        // Check for bare URLs (not in angle brackets or markdown links)
2021        for cap in BARE_URL_PATTERN.captures_iter(content) {
2022            let full_match = cap.get(0).unwrap();
2023            let match_start = full_match.start();
2024            let match_end = full_match.end();
2025
2026            // Skip if in code block
2027            if CodeBlockUtils::is_in_code_block_or_span(code_blocks, match_start) {
2028                continue;
2029            }
2030
2031            // Skip if already in angle brackets or markdown links
2032            let preceding_char = if match_start > 0 {
2033                content.chars().nth(match_start - 1)
2034            } else {
2035                None
2036            };
2037            let following_char = content.chars().nth(match_end);
2038
2039            if preceding_char == Some('<') || preceding_char == Some('(') || preceding_char == Some('[') {
2040                continue;
2041            }
2042            if following_char == Some('>') || following_char == Some(')') || following_char == Some(']') {
2043                continue;
2044            }
2045
2046            let url = full_match.as_str();
2047            let url_type = if url.starts_with("https://") {
2048                "https"
2049            } else if url.starts_with("http://") {
2050                "http"
2051            } else if url.starts_with("ftp://") {
2052                "ftp"
2053            } else {
2054                "other"
2055            };
2056
2057            // Find which line this URL is on
2058            let mut line_num = 1;
2059            let mut col_start = match_start;
2060            let mut col_end = match_end;
2061            for (idx, line_info) in lines.iter().enumerate() {
2062                if match_start >= line_info.byte_offset {
2063                    line_num = idx + 1;
2064                    col_start = match_start - line_info.byte_offset;
2065                    col_end = match_end - line_info.byte_offset;
2066                } else {
2067                    break;
2068                }
2069            }
2070
2071            bare_urls.push(BareUrl {
2072                line: line_num,
2073                start_col: col_start,
2074                end_col: col_end,
2075                byte_offset: match_start,
2076                byte_end: match_end,
2077                url: url.to_string(),
2078                url_type: url_type.to_string(),
2079            });
2080        }
2081
2082        // Check for bare email addresses
2083        for cap in BARE_EMAIL_PATTERN.captures_iter(content) {
2084            let full_match = cap.get(0).unwrap();
2085            let match_start = full_match.start();
2086            let match_end = full_match.end();
2087
2088            // Skip if in code block
2089            if CodeBlockUtils::is_in_code_block_or_span(code_blocks, match_start) {
2090                continue;
2091            }
2092
2093            // Skip if already in angle brackets or markdown links
2094            let preceding_char = if match_start > 0 {
2095                content.chars().nth(match_start - 1)
2096            } else {
2097                None
2098            };
2099            let following_char = content.chars().nth(match_end);
2100
2101            if preceding_char == Some('<') || preceding_char == Some('(') || preceding_char == Some('[') {
2102                continue;
2103            }
2104            if following_char == Some('>') || following_char == Some(')') || following_char == Some(']') {
2105                continue;
2106            }
2107
2108            let email = full_match.as_str();
2109
2110            // Find which line this email is on
2111            let mut line_num = 1;
2112            let mut col_start = match_start;
2113            let mut col_end = match_end;
2114            for (idx, line_info) in lines.iter().enumerate() {
2115                if match_start >= line_info.byte_offset {
2116                    line_num = idx + 1;
2117                    col_start = match_start - line_info.byte_offset;
2118                    col_end = match_end - line_info.byte_offset;
2119                } else {
2120                    break;
2121                }
2122            }
2123
2124            bare_urls.push(BareUrl {
2125                line: line_num,
2126                start_col: col_start,
2127                end_col: col_end,
2128                byte_offset: match_start,
2129                byte_end: match_end,
2130                url: email.to_string(),
2131                url_type: "email".to_string(),
2132            });
2133        }
2134
2135        bare_urls
2136    }
2137}
2138
2139/// Merge adjacent list blocks that should be treated as one
2140fn merge_adjacent_list_blocks(list_blocks: &mut Vec<ListBlock>, lines: &[LineInfo]) {
2141    if list_blocks.len() < 2 {
2142        return;
2143    }
2144
2145    let mut merger = ListBlockMerger::new(lines);
2146    *list_blocks = merger.merge(list_blocks);
2147}
2148
2149/// Helper struct to manage the complex logic of merging list blocks
2150struct ListBlockMerger<'a> {
2151    lines: &'a [LineInfo],
2152}
2153
2154impl<'a> ListBlockMerger<'a> {
2155    fn new(lines: &'a [LineInfo]) -> Self {
2156        Self { lines }
2157    }
2158
2159    fn merge(&mut self, list_blocks: &[ListBlock]) -> Vec<ListBlock> {
2160        let mut merged = Vec::with_capacity(list_blocks.len());
2161        let mut current = list_blocks[0].clone();
2162
2163        for next in list_blocks.iter().skip(1) {
2164            if self.should_merge_blocks(&current, next) {
2165                current = self.merge_two_blocks(current, next);
2166            } else {
2167                merged.push(current);
2168                current = next.clone();
2169            }
2170        }
2171
2172        merged.push(current);
2173        merged
2174    }
2175
2176    /// Determine if two adjacent list blocks should be merged
2177    fn should_merge_blocks(&self, current: &ListBlock, next: &ListBlock) -> bool {
2178        // Basic compatibility checks
2179        if !self.blocks_are_compatible(current, next) {
2180            return false;
2181        }
2182
2183        // Check spacing and content between blocks
2184        let spacing = self.analyze_spacing_between(current, next);
2185        match spacing {
2186            BlockSpacing::Consecutive => true,
2187            BlockSpacing::SingleBlank => self.can_merge_with_blank_between(current, next),
2188            BlockSpacing::MultipleBlanks | BlockSpacing::ContentBetween => {
2189                self.can_merge_with_content_between(current, next)
2190            }
2191        }
2192    }
2193
2194    /// Check if blocks have compatible structure for merging
2195    fn blocks_are_compatible(&self, current: &ListBlock, next: &ListBlock) -> bool {
2196        current.is_ordered == next.is_ordered
2197            && current.blockquote_prefix == next.blockquote_prefix
2198            && current.nesting_level == next.nesting_level
2199    }
2200
2201    /// Analyze the spacing between two list blocks
2202    fn analyze_spacing_between(&self, current: &ListBlock, next: &ListBlock) -> BlockSpacing {
2203        let gap = next.start_line - current.end_line;
2204
2205        match gap {
2206            1 => BlockSpacing::Consecutive,
2207            2 => BlockSpacing::SingleBlank,
2208            _ if gap > 2 => {
2209                if self.has_only_blank_lines_between(current, next) {
2210                    BlockSpacing::MultipleBlanks
2211                } else {
2212                    BlockSpacing::ContentBetween
2213                }
2214            }
2215            _ => BlockSpacing::Consecutive, // gap == 0, overlapping (shouldn't happen)
2216        }
2217    }
2218
2219    /// Check if unordered lists can be merged with a single blank line between
2220    fn can_merge_with_blank_between(&self, current: &ListBlock, next: &ListBlock) -> bool {
2221        // Check if there are structural separators between the blocks
2222        // If has_meaningful_content_between returns true, it means there are structural separators
2223        if has_meaningful_content_between(current, next, self.lines) {
2224            return false; // Structural separators prevent merging
2225        }
2226
2227        // Only merge unordered lists with same marker across single blank
2228        !current.is_ordered && current.marker == next.marker
2229    }
2230
2231    /// Check if ordered lists can be merged when there's content between them
2232    fn can_merge_with_content_between(&self, current: &ListBlock, next: &ListBlock) -> bool {
2233        // Do not merge lists if there are structural separators between them
2234        if has_meaningful_content_between(current, next, self.lines) {
2235            return false; // Structural separators prevent merging
2236        }
2237
2238        // Only consider merging ordered lists if there's no structural content between
2239        current.is_ordered && next.is_ordered
2240    }
2241
2242    /// Check if there are only blank lines between blocks
2243    fn has_only_blank_lines_between(&self, current: &ListBlock, next: &ListBlock) -> bool {
2244        for line_num in (current.end_line + 1)..next.start_line {
2245            if let Some(line_info) = self.lines.get(line_num - 1)
2246                && !line_info.content.trim().is_empty()
2247            {
2248                return false;
2249            }
2250        }
2251        true
2252    }
2253
2254    /// Merge two compatible list blocks into one
2255    fn merge_two_blocks(&self, mut current: ListBlock, next: &ListBlock) -> ListBlock {
2256        current.end_line = next.end_line;
2257        current.item_lines.extend_from_slice(&next.item_lines);
2258
2259        // Update max marker width
2260        current.max_marker_width = current.max_marker_width.max(next.max_marker_width);
2261
2262        // Handle marker consistency for unordered lists
2263        if !current.is_ordered && self.markers_differ(&current, next) {
2264            current.marker = None; // Mixed markers
2265        }
2266
2267        current
2268    }
2269
2270    /// Check if two blocks have different markers
2271    fn markers_differ(&self, current: &ListBlock, next: &ListBlock) -> bool {
2272        current.marker.is_some() && next.marker.is_some() && current.marker != next.marker
2273    }
2274}
2275
2276/// Types of spacing between list blocks
2277#[derive(Debug, PartialEq)]
2278enum BlockSpacing {
2279    Consecutive,    // No gap between blocks
2280    SingleBlank,    // One blank line between blocks
2281    MultipleBlanks, // Multiple blank lines but no content
2282    ContentBetween, // Content exists between blocks
2283}
2284
2285/// Check if there's meaningful content (not just blank lines) between two list blocks
2286fn has_meaningful_content_between(current: &ListBlock, next: &ListBlock, lines: &[LineInfo]) -> bool {
2287    // Check lines between current.end_line and next.start_line
2288    for line_num in (current.end_line + 1)..next.start_line {
2289        if let Some(line_info) = lines.get(line_num - 1) {
2290            // Convert to 0-indexed
2291            let trimmed = line_info.content.trim();
2292
2293            // Skip empty lines
2294            if trimmed.is_empty() {
2295                continue;
2296            }
2297
2298            // Check for structural separators that should separate lists (CommonMark compliant)
2299
2300            // Headings separate lists
2301            if line_info.heading.is_some() {
2302                return true; // Has meaningful content - headings separate lists
2303            }
2304
2305            // Horizontal rules separate lists (---, ***, ___)
2306            if is_horizontal_rule(trimmed) {
2307                return true; // Has meaningful content - horizontal rules separate lists
2308            }
2309
2310            // Tables separate lists (lines containing |)
2311            if trimmed.contains('|') && trimmed.len() > 1 {
2312                return true; // Has meaningful content - tables separate lists
2313            }
2314
2315            // Blockquotes separate lists
2316            if trimmed.starts_with('>') {
2317                return true; // Has meaningful content - blockquotes separate lists
2318            }
2319
2320            // Code block fences separate lists (unless properly indented as list content)
2321            if trimmed.starts_with("```") || trimmed.starts_with("~~~") {
2322                let line_indent = line_info.content.len() - line_info.content.trim_start().len();
2323
2324                // Check if this code block is properly indented as list continuation
2325                let min_continuation_indent = if current.is_ordered {
2326                    current.nesting_level + current.max_marker_width + 1 // +1 for space after marker
2327                } else {
2328                    current.nesting_level + 2
2329                };
2330
2331                if line_indent < min_continuation_indent {
2332                    // This is a standalone code block that separates lists
2333                    return true; // Has meaningful content - standalone code blocks separate lists
2334                }
2335            }
2336
2337            // Check if this line has proper indentation for list continuation
2338            let line_indent = line_info.content.len() - line_info.content.trim_start().len();
2339
2340            // Calculate minimum indentation needed to be list continuation
2341            let min_indent = if current.is_ordered {
2342                current.nesting_level + current.max_marker_width
2343            } else {
2344                current.nesting_level + 2
2345            };
2346
2347            // If the line is not indented enough to be list continuation, it's meaningful content
2348            if line_indent < min_indent {
2349                return true; // Has meaningful content - content not indented as list continuation
2350            }
2351
2352            // If we reach here, the line is properly indented as list continuation
2353            // Continue checking other lines
2354        }
2355    }
2356
2357    // Only blank lines or properly indented list continuation content between blocks
2358    false
2359}
2360
2361/// Check if a line is a horizontal rule (---, ***, ___)
2362fn is_horizontal_rule(trimmed: &str) -> bool {
2363    if trimmed.len() < 3 {
2364        return false;
2365    }
2366
2367    // Check for three or more consecutive -, *, or _ characters (with optional spaces)
2368    let chars: Vec<char> = trimmed.chars().collect();
2369    if let Some(&first_char) = chars.first()
2370        && (first_char == '-' || first_char == '*' || first_char == '_')
2371    {
2372        let mut count = 0;
2373        for &ch in &chars {
2374            if ch == first_char {
2375                count += 1;
2376            } else if ch != ' ' && ch != '\t' {
2377                return false; // Non-matching, non-whitespace character
2378            }
2379        }
2380        return count >= 3;
2381    }
2382    false
2383}
2384
2385/// Check if content contains patterns that cause the markdown crate to panic
2386#[cfg(test)]
2387mod tests {
2388    use super::*;
2389
2390    #[test]
2391    fn test_empty_content() {
2392        let ctx = LintContext::new("", MarkdownFlavor::Standard);
2393        assert_eq!(ctx.content, "");
2394        assert_eq!(ctx.line_offsets, vec![0]);
2395        assert_eq!(ctx.offset_to_line_col(0), (1, 1));
2396        assert_eq!(ctx.lines.len(), 0);
2397    }
2398
2399    #[test]
2400    fn test_single_line() {
2401        let ctx = LintContext::new("# Hello", MarkdownFlavor::Standard);
2402        assert_eq!(ctx.content, "# Hello");
2403        assert_eq!(ctx.line_offsets, vec![0]);
2404        assert_eq!(ctx.offset_to_line_col(0), (1, 1));
2405        assert_eq!(ctx.offset_to_line_col(3), (1, 4));
2406    }
2407
2408    #[test]
2409    fn test_multi_line() {
2410        let content = "# Title\n\nSecond line\nThird line";
2411        let ctx = LintContext::new(content, MarkdownFlavor::Standard);
2412        assert_eq!(ctx.line_offsets, vec![0, 8, 9, 21]);
2413        // Test offset to line/col
2414        assert_eq!(ctx.offset_to_line_col(0), (1, 1)); // start
2415        assert_eq!(ctx.offset_to_line_col(8), (2, 1)); // start of blank line
2416        assert_eq!(ctx.offset_to_line_col(9), (3, 1)); // start of 'Second line'
2417        assert_eq!(ctx.offset_to_line_col(15), (3, 7)); // middle of 'Second line'
2418        assert_eq!(ctx.offset_to_line_col(21), (4, 1)); // start of 'Third line'
2419    }
2420
2421    #[test]
2422    fn test_line_info() {
2423        let content = "# Title\n    indented\n\ncode:\n```rust\nfn main() {}\n```";
2424        let ctx = LintContext::new(content, MarkdownFlavor::Standard);
2425
2426        // Test line info
2427        assert_eq!(ctx.lines.len(), 7);
2428
2429        // Line 1: "# Title"
2430        let line1 = &ctx.lines[0];
2431        assert_eq!(line1.content, "# Title");
2432        assert_eq!(line1.byte_offset, 0);
2433        assert_eq!(line1.indent, 0);
2434        assert!(!line1.is_blank);
2435        assert!(!line1.in_code_block);
2436        assert!(line1.list_item.is_none());
2437
2438        // Line 2: "    indented"
2439        let line2 = &ctx.lines[1];
2440        assert_eq!(line2.content, "    indented");
2441        assert_eq!(line2.byte_offset, 8);
2442        assert_eq!(line2.indent, 4);
2443        assert!(!line2.is_blank);
2444
2445        // Line 3: "" (blank)
2446        let line3 = &ctx.lines[2];
2447        assert_eq!(line3.content, "");
2448        assert!(line3.is_blank);
2449
2450        // Test helper methods
2451        assert_eq!(ctx.line_to_byte_offset(1), Some(0));
2452        assert_eq!(ctx.line_to_byte_offset(2), Some(8));
2453        assert_eq!(ctx.line_info(1).map(|l| l.indent), Some(0));
2454        assert_eq!(ctx.line_info(2).map(|l| l.indent), Some(4));
2455    }
2456
2457    #[test]
2458    fn test_list_item_detection() {
2459        let content = "- Unordered item\n  * Nested item\n1. Ordered item\n   2) Nested ordered\n\nNot a list";
2460        let ctx = LintContext::new(content, MarkdownFlavor::Standard);
2461
2462        // Line 1: "- Unordered item"
2463        let line1 = &ctx.lines[0];
2464        assert!(line1.list_item.is_some());
2465        let list1 = line1.list_item.as_ref().unwrap();
2466        assert_eq!(list1.marker, "-");
2467        assert!(!list1.is_ordered);
2468        assert_eq!(list1.marker_column, 0);
2469        assert_eq!(list1.content_column, 2);
2470
2471        // Line 2: "  * Nested item"
2472        let line2 = &ctx.lines[1];
2473        assert!(line2.list_item.is_some());
2474        let list2 = line2.list_item.as_ref().unwrap();
2475        assert_eq!(list2.marker, "*");
2476        assert_eq!(list2.marker_column, 2);
2477
2478        // Line 3: "1. Ordered item"
2479        let line3 = &ctx.lines[2];
2480        assert!(line3.list_item.is_some());
2481        let list3 = line3.list_item.as_ref().unwrap();
2482        assert_eq!(list3.marker, "1.");
2483        assert!(list3.is_ordered);
2484        assert_eq!(list3.number, Some(1));
2485
2486        // Line 6: "Not a list"
2487        let line6 = &ctx.lines[5];
2488        assert!(line6.list_item.is_none());
2489    }
2490
2491    #[test]
2492    fn test_offset_to_line_col_edge_cases() {
2493        let content = "a\nb\nc";
2494        let ctx = LintContext::new(content, MarkdownFlavor::Standard);
2495        // line_offsets: [0, 2, 4]
2496        assert_eq!(ctx.offset_to_line_col(0), (1, 1)); // 'a'
2497        assert_eq!(ctx.offset_to_line_col(1), (1, 2)); // after 'a'
2498        assert_eq!(ctx.offset_to_line_col(2), (2, 1)); // 'b'
2499        assert_eq!(ctx.offset_to_line_col(3), (2, 2)); // after 'b'
2500        assert_eq!(ctx.offset_to_line_col(4), (3, 1)); // 'c'
2501        assert_eq!(ctx.offset_to_line_col(5), (3, 2)); // after 'c'
2502    }
2503}