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, flavor);
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(
919        content: &str,
920        line_offsets: &[usize],
921        code_blocks: &[(usize, usize)],
922        flavor: MarkdownFlavor,
923    ) -> Vec<LineInfo> {
924        lazy_static! {
925            // Regex for list detection - allow any whitespace including no space (to catch malformed lists)
926            static ref UNORDERED_REGEX: regex::Regex = regex::Regex::new(r"^(\s*)([-*+])([ \t]*)(.*)").unwrap();
927            static ref ORDERED_REGEX: regex::Regex = regex::Regex::new(r"^(\s*)(\d+)([.)])([ \t]*)(.*)").unwrap();
928
929            // Regex for blockquote prefix
930            static ref BLOCKQUOTE_REGEX: regex::Regex = regex::Regex::new(r"^(\s*>\s*)(.*)").unwrap();
931
932            // Regex for heading detection
933            static ref ATX_HEADING_REGEX: regex::Regex = regex::Regex::new(r"^(\s*)(#{1,6})(\s*)(.*)$").unwrap();
934            static ref SETEXT_UNDERLINE_REGEX: regex::Regex = regex::Regex::new(r"^(\s*)(=+|-+)\s*$").unwrap();
935
936            // Regex for blockquote detection
937            static ref BLOCKQUOTE_REGEX_FULL: regex::Regex = regex::Regex::new(r"^(\s*)(>+)(\s*)(.*)$").unwrap();
938        }
939
940        let content_lines: Vec<&str> = content.lines().collect();
941        let mut lines = Vec::with_capacity(content_lines.len());
942
943        // Detect front matter boundaries FIRST, before any other parsing
944        let mut in_front_matter = false;
945        let mut front_matter_end = 0;
946        if content_lines.first().map(|l| l.trim()) == Some("---") {
947            in_front_matter = true;
948            for (idx, line) in content_lines.iter().enumerate().skip(1) {
949                if line.trim() == "---" {
950                    front_matter_end = idx;
951                    break;
952                }
953            }
954        }
955
956        for (i, line) in content_lines.iter().enumerate() {
957            let byte_offset = line_offsets.get(i).copied().unwrap_or(0);
958            let indent = line.len() - line.trim_start().len();
959            // For blank detection, consider blockquote context
960            let is_blank = if let Some(caps) = BLOCKQUOTE_REGEX.captures(line) {
961                // In blockquote context, check if content after prefix is blank
962                let after_prefix = caps.get(2).map_or("", |m| m.as_str());
963                after_prefix.trim().is_empty()
964            } else {
965                line.trim().is_empty()
966            };
967            // Check if this line is inside a code block (not inline code span)
968            // We only want to check for fenced/indented code blocks, not inline code
969            let in_code_block = code_blocks.iter().any(|&(start, end)| {
970                // Only consider ranges that span multiple lines (code blocks)
971                // Inline code spans are typically on a single line
972                let block_content = &content[start..end];
973                let is_multiline = block_content.contains('\n');
974                let is_fenced = block_content.starts_with("```") || block_content.starts_with("~~~");
975                let is_indented = !is_fenced
976                    && block_content
977                        .lines()
978                        .all(|l| l.starts_with("    ") || l.starts_with("\t") || l.trim().is_empty());
979
980                byte_offset >= start && byte_offset < end && (is_multiline || is_fenced || is_indented)
981            });
982
983            // Detect list items (skip if in frontmatter)
984            let list_item = if !(in_code_block || is_blank || in_front_matter && i <= front_matter_end) {
985                // Strip blockquote prefix if present for list detection
986                let (line_for_list_check, blockquote_prefix_len) = if let Some(caps) = BLOCKQUOTE_REGEX.captures(line) {
987                    let prefix = caps.get(1).unwrap().as_str();
988                    let content = caps.get(2).unwrap().as_str();
989                    (content, prefix.len())
990                } else {
991                    (&**line, 0)
992                };
993
994                if let Some(caps) = UNORDERED_REGEX.captures(line_for_list_check) {
995                    let leading_spaces = caps.get(1).map_or("", |m| m.as_str());
996                    let marker = caps.get(2).map_or("", |m| m.as_str());
997                    let spacing = caps.get(3).map_or("", |m| m.as_str());
998                    let _content = caps.get(4).map_or("", |m| m.as_str());
999                    let marker_column = blockquote_prefix_len + leading_spaces.len();
1000                    let content_column = marker_column + marker.len() + spacing.len();
1001
1002                    // According to CommonMark spec, unordered list items MUST have at least one space
1003                    // after the marker (-, *, or +). Without a space, it's not a list item.
1004                    // This also naturally handles cases like:
1005                    // - *emphasis* (not a list)
1006                    // - **bold** (not a list)
1007                    // - --- (horizontal rule, not a list)
1008                    if spacing.is_empty() {
1009                        None
1010                    } else {
1011                        Some(ListItemInfo {
1012                            marker: marker.to_string(),
1013                            is_ordered: false,
1014                            number: None,
1015                            marker_column,
1016                            content_column,
1017                        })
1018                    }
1019                } else if let Some(caps) = ORDERED_REGEX.captures(line_for_list_check) {
1020                    let leading_spaces = caps.get(1).map_or("", |m| m.as_str());
1021                    let number_str = caps.get(2).map_or("", |m| m.as_str());
1022                    let delimiter = caps.get(3).map_or("", |m| m.as_str());
1023                    let spacing = caps.get(4).map_or("", |m| m.as_str());
1024                    let _content = caps.get(5).map_or("", |m| m.as_str());
1025                    let marker = format!("{number_str}{delimiter}");
1026                    let marker_column = blockquote_prefix_len + leading_spaces.len();
1027                    let content_column = marker_column + marker.len() + spacing.len();
1028
1029                    // According to CommonMark spec, ordered list items MUST have at least one space
1030                    // after the marker (period or parenthesis). Without a space, it's not a list item.
1031                    if spacing.is_empty() {
1032                        None
1033                    } else {
1034                        Some(ListItemInfo {
1035                            marker,
1036                            is_ordered: true,
1037                            number: number_str.parse().ok(),
1038                            marker_column,
1039                            content_column,
1040                        })
1041                    }
1042                } else {
1043                    None
1044                }
1045            } else {
1046                None
1047            };
1048
1049            lines.push(LineInfo {
1050                content: line.to_string(),
1051                byte_offset,
1052                indent,
1053                is_blank,
1054                in_code_block,
1055                in_front_matter: in_front_matter && i <= front_matter_end,
1056                list_item,
1057                heading: None,    // Will be populated in second pass for Setext headings
1058                blockquote: None, // Will be populated after line creation
1059            });
1060        }
1061
1062        // Second pass: detect headings (including Setext which needs look-ahead) and blockquotes
1063        for i in 0..content_lines.len() {
1064            if lines[i].in_code_block {
1065                continue;
1066            }
1067
1068            // Skip lines in front matter
1069            if in_front_matter && i <= front_matter_end {
1070                continue;
1071            }
1072
1073            let line = content_lines[i];
1074
1075            // Check for blockquotes (even on blank lines within blockquotes)
1076            if let Some(caps) = BLOCKQUOTE_REGEX_FULL.captures(line) {
1077                let indent_str = caps.get(1).map_or("", |m| m.as_str());
1078                let markers = caps.get(2).map_or("", |m| m.as_str());
1079                let spaces_after = caps.get(3).map_or("", |m| m.as_str());
1080                let content = caps.get(4).map_or("", |m| m.as_str());
1081
1082                let nesting_level = markers.chars().filter(|&c| c == '>').count();
1083                let marker_column = indent_str.len();
1084
1085                // Build the prefix (indentation + markers + space)
1086                let prefix = format!("{indent_str}{markers}{spaces_after}");
1087
1088                // Check for various blockquote issues
1089                let has_no_space = spaces_after.is_empty() && !content.is_empty();
1090                // Consider tabs as multiple spaces, or actual multiple spaces
1091                let has_multiple_spaces = spaces_after.len() > 1 || spaces_after.contains('\t');
1092
1093                // Check if needs MD028 fix (empty blockquote line without proper spacing)
1094                // MD028 flags empty blockquote lines that don't have a single space after the marker
1095                // Lines like "> " or ">> " are already correct and don't need fixing
1096                let needs_md028_fix = content.is_empty() && spaces_after.is_empty();
1097
1098                lines[i].blockquote = Some(BlockquoteInfo {
1099                    nesting_level,
1100                    indent: indent_str.to_string(),
1101                    marker_column,
1102                    prefix,
1103                    content: content.to_string(),
1104                    has_no_space_after_marker: has_no_space,
1105                    has_multiple_spaces_after_marker: has_multiple_spaces,
1106                    needs_md028_fix,
1107                });
1108            }
1109
1110            // Skip heading detection for blank lines
1111            if lines[i].is_blank {
1112                continue;
1113            }
1114
1115            // Check for ATX headings (but skip MkDocs snippet lines)
1116            // In MkDocs flavor, lines like "# -8<- [start:name]" are snippet markers, not headings
1117            let is_snippet_line = if flavor == MarkdownFlavor::MkDocs {
1118                crate::utils::mkdocs_snippets::is_snippet_section_start(line)
1119                    || crate::utils::mkdocs_snippets::is_snippet_section_end(line)
1120            } else {
1121                false
1122            };
1123
1124            if !is_snippet_line && let Some(caps) = ATX_HEADING_REGEX.captures(line) {
1125                let leading_spaces = caps.get(1).map_or("", |m| m.as_str());
1126                let hashes = caps.get(2).map_or("", |m| m.as_str());
1127                let spaces_after = caps.get(3).map_or("", |m| m.as_str());
1128                let rest = caps.get(4).map_or("", |m| m.as_str());
1129
1130                let level = hashes.len() as u8;
1131                let marker_column = leading_spaces.len();
1132
1133                // Check for closing sequence, but handle custom IDs that might come after
1134                let (text, has_closing, closing_seq) = {
1135                    // First check if there's a custom ID at the end
1136                    let (rest_without_id, custom_id_part) = if let Some(id_start) = rest.rfind(" {#") {
1137                        // Check if this looks like a valid custom ID (ends with })
1138                        if rest[id_start..].trim_end().ends_with('}') {
1139                            // Split off the custom ID
1140                            (&rest[..id_start], &rest[id_start..])
1141                        } else {
1142                            (rest, "")
1143                        }
1144                    } else {
1145                        (rest, "")
1146                    };
1147
1148                    // Now look for closing hashes in the part before the custom ID
1149                    let trimmed_rest = rest_without_id.trim_end();
1150                    if let Some(last_hash_pos) = trimmed_rest.rfind('#') {
1151                        // Look for the start of the hash sequence
1152                        let mut start_of_hashes = last_hash_pos;
1153                        while start_of_hashes > 0 && trimmed_rest.chars().nth(start_of_hashes - 1) == Some('#') {
1154                            start_of_hashes -= 1;
1155                        }
1156
1157                        // Check if there's at least one space before the closing hashes
1158                        let has_space_before = start_of_hashes == 0
1159                            || trimmed_rest
1160                                .chars()
1161                                .nth(start_of_hashes - 1)
1162                                .is_some_and(|c| c.is_whitespace());
1163
1164                        // Check if this is a valid closing sequence (all hashes to end of trimmed part)
1165                        let potential_closing = &trimmed_rest[start_of_hashes..];
1166                        let is_all_hashes = potential_closing.chars().all(|c| c == '#');
1167
1168                        if is_all_hashes && has_space_before {
1169                            // This is a closing sequence
1170                            let closing_hashes = potential_closing.to_string();
1171                            // The text is everything before the closing hashes
1172                            // Don't include the custom ID here - it will be extracted later
1173                            let text_part = if !custom_id_part.is_empty() {
1174                                // If we have a custom ID, append it back to get the full rest
1175                                // This allows the extract_header_id function to handle it properly
1176                                format!("{}{}", rest_without_id[..start_of_hashes].trim_end(), custom_id_part)
1177                            } else {
1178                                rest_without_id[..start_of_hashes].trim_end().to_string()
1179                            };
1180                            (text_part, true, closing_hashes)
1181                        } else {
1182                            // Not a valid closing sequence, return the full content
1183                            (rest.to_string(), false, String::new())
1184                        }
1185                    } else {
1186                        // No hashes found, return the full content
1187                        (rest.to_string(), false, String::new())
1188                    }
1189                };
1190
1191                let content_column = marker_column + hashes.len() + spaces_after.len();
1192
1193                // Extract custom header ID if present
1194                let raw_text = text.trim().to_string();
1195                let (clean_text, mut custom_id) = crate::utils::header_id_utils::extract_header_id(&raw_text);
1196
1197                // If no custom ID was found on the header line, check the next line for standalone attr-list
1198                if custom_id.is_none() && i + 1 < content_lines.len() && i + 1 < lines.len() {
1199                    let next_line = content_lines[i + 1];
1200                    if !lines[i + 1].in_code_block
1201                        && crate::utils::header_id_utils::is_standalone_attr_list(next_line)
1202                        && let Some(next_line_id) =
1203                            crate::utils::header_id_utils::extract_standalone_attr_list_id(next_line)
1204                    {
1205                        custom_id = Some(next_line_id);
1206                    }
1207                }
1208
1209                lines[i].heading = Some(HeadingInfo {
1210                    level,
1211                    style: HeadingStyle::ATX,
1212                    marker: hashes.to_string(),
1213                    marker_column,
1214                    content_column,
1215                    text: clean_text,
1216                    custom_id,
1217                    raw_text,
1218                    has_closing_sequence: has_closing,
1219                    closing_sequence: closing_seq,
1220                });
1221            }
1222            // Check for Setext headings (need to look at next line)
1223            else if i + 1 < content_lines.len() {
1224                let next_line = content_lines[i + 1];
1225                if !lines[i + 1].in_code_block && SETEXT_UNDERLINE_REGEX.is_match(next_line) {
1226                    // Skip if next line is front matter delimiter
1227                    if in_front_matter && i < front_matter_end {
1228                        continue;
1229                    }
1230
1231                    let underline = next_line.trim();
1232                    let level = if underline.starts_with('=') { 1 } else { 2 };
1233                    let style = if level == 1 {
1234                        HeadingStyle::Setext1
1235                    } else {
1236                        HeadingStyle::Setext2
1237                    };
1238
1239                    // Extract custom header ID if present
1240                    let raw_text = line.trim().to_string();
1241                    let (clean_text, mut custom_id) = crate::utils::header_id_utils::extract_header_id(&raw_text);
1242
1243                    // If no custom ID was found on the header line, check the line after underline for standalone attr-list
1244                    if custom_id.is_none() && i + 2 < content_lines.len() && i + 2 < lines.len() {
1245                        let attr_line = content_lines[i + 2];
1246                        if !lines[i + 2].in_code_block
1247                            && crate::utils::header_id_utils::is_standalone_attr_list(attr_line)
1248                            && let Some(attr_line_id) =
1249                                crate::utils::header_id_utils::extract_standalone_attr_list_id(attr_line)
1250                        {
1251                            custom_id = Some(attr_line_id);
1252                        }
1253                    }
1254
1255                    lines[i].heading = Some(HeadingInfo {
1256                        level,
1257                        style,
1258                        marker: underline.to_string(),
1259                        marker_column: next_line.len() - next_line.trim_start().len(),
1260                        content_column: lines[i].indent,
1261                        text: clean_text,
1262                        custom_id,
1263                        raw_text,
1264                        has_closing_sequence: false,
1265                        closing_sequence: String::new(),
1266                    });
1267                }
1268            }
1269        }
1270
1271        lines
1272    }
1273
1274    /// Parse all inline code spans in the content
1275    fn parse_code_spans(content: &str, lines: &[LineInfo]) -> Vec<CodeSpan> {
1276        // Pre-size based on content - code spans are fairly common
1277        let mut code_spans = Vec::with_capacity(content.matches('`').count() / 2); // Each code span has 2 backticks
1278
1279        // Quick check - if no backticks, no code spans
1280        if !content.contains('`') {
1281            return code_spans;
1282        }
1283
1284        let mut pos = 0;
1285        let bytes = content.as_bytes();
1286
1287        while pos < bytes.len() {
1288            // Find the next backtick
1289            if let Some(backtick_start) = content[pos..].find('`') {
1290                let start_pos = pos + backtick_start;
1291
1292                // Skip if this backtick is inside a code block
1293                let mut in_code_block = false;
1294                for (line_idx, line_info) in lines.iter().enumerate() {
1295                    if start_pos >= line_info.byte_offset
1296                        && (line_idx + 1 >= lines.len() || start_pos < lines[line_idx + 1].byte_offset)
1297                    {
1298                        in_code_block = line_info.in_code_block;
1299                        break;
1300                    }
1301                }
1302
1303                if in_code_block {
1304                    pos = start_pos + 1;
1305                    continue;
1306                }
1307
1308                // Count consecutive backticks
1309                let mut backtick_count = 0;
1310                let mut i = start_pos;
1311                while i < bytes.len() && bytes[i] == b'`' {
1312                    backtick_count += 1;
1313                    i += 1;
1314                }
1315
1316                // Look for matching closing backticks
1317                let search_start = start_pos + backtick_count;
1318                let closing_pattern = &content[start_pos..start_pos + backtick_count];
1319
1320                if let Some(rel_end) = content[search_start..].find(closing_pattern) {
1321                    // Check that the closing backticks are not followed by more backticks
1322                    let end_pos = search_start + rel_end;
1323                    let check_pos = end_pos + backtick_count;
1324
1325                    // Make sure we have exactly the right number of backticks (not more)
1326                    if check_pos >= bytes.len() || bytes[check_pos] != b'`' {
1327                        // We found a valid code span
1328                        let content_start = start_pos + backtick_count;
1329                        let content_end = end_pos;
1330                        let span_content = content[content_start..content_end].to_string();
1331
1332                        // Find which line this code span starts on
1333                        let mut line_num = 1;
1334                        let mut col_start = start_pos;
1335                        for (idx, line_info) in lines.iter().enumerate() {
1336                            if start_pos >= line_info.byte_offset {
1337                                line_num = idx + 1;
1338                                col_start = start_pos - line_info.byte_offset;
1339                            } else {
1340                                break;
1341                            }
1342                        }
1343
1344                        // Find end column
1345                        let mut col_end = end_pos + backtick_count;
1346                        for line_info in lines.iter() {
1347                            if end_pos + backtick_count > line_info.byte_offset {
1348                                col_end = end_pos + backtick_count - line_info.byte_offset;
1349                            } else {
1350                                break;
1351                            }
1352                        }
1353
1354                        code_spans.push(CodeSpan {
1355                            line: line_num,
1356                            start_col: col_start,
1357                            end_col: col_end,
1358                            byte_offset: start_pos,
1359                            byte_end: end_pos + backtick_count,
1360                            backtick_count,
1361                            content: span_content,
1362                        });
1363
1364                        // Continue searching after this code span
1365                        pos = end_pos + backtick_count;
1366                        continue;
1367                    }
1368                }
1369
1370                // No matching closing backticks found, move past these opening backticks
1371                pos = start_pos + backtick_count;
1372            } else {
1373                // No more backticks found
1374                break;
1375            }
1376        }
1377
1378        code_spans
1379    }
1380
1381    /// Parse all list blocks in the content
1382    fn parse_list_blocks(lines: &[LineInfo]) -> Vec<ListBlock> {
1383        // Pre-size based on lines that could be list items
1384        let mut list_blocks = Vec::with_capacity(lines.len() / 10); // Estimate ~10% of lines might start list blocks
1385        let mut current_block: Option<ListBlock> = None;
1386        let mut last_list_item_line = 0;
1387        let mut current_indent_level = 0;
1388        let mut last_marker_width = 0;
1389
1390        for (line_idx, line_info) in lines.iter().enumerate() {
1391            let line_num = line_idx + 1;
1392
1393            // Enhanced code block handling using Design #3's context analysis
1394            if line_info.in_code_block {
1395                if let Some(ref mut block) = current_block {
1396                    // Calculate minimum indentation for list continuation
1397                    let min_continuation_indent = CodeBlockUtils::calculate_min_continuation_indent(lines, line_idx);
1398
1399                    // Analyze code block context using the three-tier classification
1400                    let context = CodeBlockUtils::analyze_code_block_context(lines, line_idx, min_continuation_indent);
1401
1402                    match context {
1403                        CodeBlockContext::Indented => {
1404                            // Code block is properly indented - continues the list
1405                            block.end_line = line_num;
1406                            continue;
1407                        }
1408                        CodeBlockContext::Standalone => {
1409                            // Code block separates lists - end current block
1410                            let completed_block = current_block.take().unwrap();
1411                            list_blocks.push(completed_block);
1412                            continue;
1413                        }
1414                        CodeBlockContext::Adjacent => {
1415                            // Edge case - use conservative behavior (continue list)
1416                            block.end_line = line_num;
1417                            continue;
1418                        }
1419                    }
1420                } else {
1421                    // No current list block - skip code block lines
1422                    continue;
1423                }
1424            }
1425
1426            // Extract blockquote prefix if any
1427            let blockquote_prefix = if let Some(caps) = BLOCKQUOTE_PREFIX_REGEX.captures(&line_info.content) {
1428                caps.get(0).unwrap().as_str().to_string()
1429            } else {
1430                String::new()
1431            };
1432
1433            // Check if this line is a list item
1434            if let Some(list_item) = &line_info.list_item {
1435                // Calculate nesting level based on indentation
1436                let item_indent = list_item.marker_column;
1437                let nesting = item_indent / 2; // Assume 2-space indentation for nesting
1438
1439                if let Some(ref mut block) = current_block {
1440                    // Check if this continues the current block
1441                    // For nested lists, we need to check if this is a nested item (higher nesting level)
1442                    // or a continuation at the same or lower level
1443                    let is_nested = nesting > block.nesting_level;
1444                    let same_type =
1445                        (block.is_ordered && list_item.is_ordered) || (!block.is_ordered && !list_item.is_ordered);
1446                    let same_context = block.blockquote_prefix == blockquote_prefix;
1447                    let reasonable_distance = line_num <= last_list_item_line + 2; // Allow one blank line
1448
1449                    // For unordered lists, also check marker consistency
1450                    let marker_compatible =
1451                        block.is_ordered || block.marker.is_none() || block.marker.as_ref() == Some(&list_item.marker);
1452
1453                    // Check if there's non-list content between the last item and this one
1454                    let has_non_list_content = {
1455                        let mut found_non_list = false;
1456                        // Use the last item from the current block, not the global last_list_item_line
1457                        let block_last_item_line = block.item_lines.last().copied().unwrap_or(block.end_line);
1458                        for check_line in (block_last_item_line + 1)..line_num {
1459                            let check_idx = check_line - 1;
1460                            if check_idx < lines.len() {
1461                                let check_info = &lines[check_idx];
1462                                // Check for content that breaks the list
1463                                let is_list_breaking_content = if check_info.in_code_block {
1464                                    // Use enhanced code block classification for list separation
1465                                    let last_item_marker_width =
1466                                        if block_last_item_line > 0 && block_last_item_line <= lines.len() {
1467                                            lines[block_last_item_line - 1]
1468                                                .list_item
1469                                                .as_ref()
1470                                                .map(|li| {
1471                                                    if li.is_ordered {
1472                                                        li.marker.len() + 1 // Add 1 for the space after ordered list markers
1473                                                    } else {
1474                                                        li.marker.len()
1475                                                    }
1476                                                })
1477                                                .unwrap_or(3) // fallback to 3 if no list item found
1478                                        } else {
1479                                            3 // fallback
1480                                        };
1481
1482                                    let min_continuation = if block.is_ordered { last_item_marker_width } else { 2 };
1483
1484                                    // Analyze code block context using our enhanced classification
1485                                    let context = CodeBlockUtils::analyze_code_block_context(
1486                                        lines,
1487                                        check_line - 1,
1488                                        min_continuation,
1489                                    );
1490
1491                                    // Standalone code blocks break lists, indented ones continue them
1492                                    matches!(context, CodeBlockContext::Standalone)
1493                                } else if !check_info.is_blank && check_info.list_item.is_none() {
1494                                    // Check for structural separators that should break lists (from issue #42)
1495                                    let line_content = check_info.content.trim();
1496
1497                                    // Any of these structural separators break lists
1498                                    if check_info.heading.is_some()
1499                                        || line_content.starts_with("---")
1500                                        || line_content.starts_with("***")
1501                                        || line_content.starts_with("___")
1502                                        || line_content.contains('|')
1503                                        || line_content.starts_with(">")
1504                                    {
1505                                        true
1506                                    }
1507                                    // Other non-list content - check if properly indented
1508                                    else {
1509                                        let last_item_marker_width =
1510                                            if block_last_item_line > 0 && block_last_item_line <= lines.len() {
1511                                                lines[block_last_item_line - 1]
1512                                                    .list_item
1513                                                    .as_ref()
1514                                                    .map(|li| {
1515                                                        if li.is_ordered {
1516                                                            li.marker.len() + 1 // Add 1 for the space after ordered list markers
1517                                                        } else {
1518                                                            li.marker.len()
1519                                                        }
1520                                                    })
1521                                                    .unwrap_or(3) // fallback to 3 if no list item found
1522                                            } else {
1523                                                3 // fallback
1524                                            };
1525
1526                                        let min_continuation =
1527                                            if block.is_ordered { last_item_marker_width } else { 2 };
1528                                        check_info.indent < min_continuation
1529                                    }
1530                                } else {
1531                                    false
1532                                };
1533
1534                                if is_list_breaking_content {
1535                                    // Not indented enough, so it breaks the list
1536                                    found_non_list = true;
1537                                    break;
1538                                }
1539                            }
1540                        }
1541                        found_non_list
1542                    };
1543
1544                    // A list continues if:
1545                    // 1. It's a nested item (indented more than the parent), OR
1546                    // 2. It's the same type at the same level with reasonable distance
1547                    let continues_list = if is_nested {
1548                        // Nested items always continue the list if they're in the same context
1549                        same_context && reasonable_distance && !has_non_list_content
1550                    } else {
1551                        // Same-level items need to match type and markers
1552                        same_type && same_context && reasonable_distance && marker_compatible && !has_non_list_content
1553                    };
1554
1555                    if continues_list {
1556                        // Extend current block
1557                        block.end_line = line_num;
1558                        block.item_lines.push(line_num);
1559
1560                        // Update max marker width
1561                        block.max_marker_width = block.max_marker_width.max(if list_item.is_ordered {
1562                            list_item.marker.len() + 1
1563                        } else {
1564                            list_item.marker.len()
1565                        });
1566
1567                        // Update marker consistency for unordered lists
1568                        if !block.is_ordered
1569                            && block.marker.is_some()
1570                            && block.marker.as_ref() != Some(&list_item.marker)
1571                        {
1572                            // Mixed markers, clear the marker field
1573                            block.marker = None;
1574                        }
1575                    } else {
1576                        // End current block and start a new one
1577                        list_blocks.push(block.clone());
1578
1579                        *block = ListBlock {
1580                            start_line: line_num,
1581                            end_line: line_num,
1582                            is_ordered: list_item.is_ordered,
1583                            marker: if list_item.is_ordered {
1584                                None
1585                            } else {
1586                                Some(list_item.marker.clone())
1587                            },
1588                            blockquote_prefix: blockquote_prefix.clone(),
1589                            item_lines: vec![line_num],
1590                            nesting_level: nesting,
1591                            max_marker_width: if list_item.is_ordered {
1592                                list_item.marker.len() + 1
1593                            } else {
1594                                list_item.marker.len()
1595                            },
1596                        };
1597                    }
1598                } else {
1599                    // Start a new block
1600                    current_block = Some(ListBlock {
1601                        start_line: line_num,
1602                        end_line: line_num,
1603                        is_ordered: list_item.is_ordered,
1604                        marker: if list_item.is_ordered {
1605                            None
1606                        } else {
1607                            Some(list_item.marker.clone())
1608                        },
1609                        blockquote_prefix,
1610                        item_lines: vec![line_num],
1611                        nesting_level: nesting,
1612                        max_marker_width: list_item.marker.len(),
1613                    });
1614                }
1615
1616                last_list_item_line = line_num;
1617                current_indent_level = item_indent;
1618                last_marker_width = if list_item.is_ordered {
1619                    list_item.marker.len() + 1 // Add 1 for the space after ordered list markers
1620                } else {
1621                    list_item.marker.len()
1622                };
1623            } else if let Some(ref mut block) = current_block {
1624                // Not a list item - check if it continues the current block
1625
1626                // For MD032 compatibility, we use a simple approach:
1627                // - Indented lines continue the list
1628                // - Blank lines followed by indented content continue the list
1629                // - Everything else ends the list
1630
1631                // Calculate minimum indentation for list continuation
1632                // For ordered lists, use the last marker width (e.g., 3 for "1. ", 4 for "10. ")
1633                // For unordered lists like "- ", content starts at column 2, so continuations need at least 2 spaces
1634                let min_continuation_indent = if block.is_ordered {
1635                    current_indent_level + last_marker_width
1636                } else {
1637                    current_indent_level + 2 // Unordered lists need at least 2 spaces (e.g., "- " = 2 chars)
1638                };
1639
1640                if line_info.indent >= min_continuation_indent {
1641                    // Indented line continues the list
1642                    block.end_line = line_num;
1643                } else if line_info.is_blank {
1644                    // Blank line - check if it's internal to the list or ending it
1645                    // We only include blank lines that are followed by more list content
1646                    let mut check_idx = line_idx + 1;
1647                    let mut found_continuation = false;
1648
1649                    // Skip additional blank lines
1650                    while check_idx < lines.len() && lines[check_idx].is_blank {
1651                        check_idx += 1;
1652                    }
1653
1654                    if check_idx < lines.len() {
1655                        let next_line = &lines[check_idx];
1656                        // Check if followed by indented content (list continuation)
1657                        if !next_line.in_code_block && next_line.indent >= min_continuation_indent {
1658                            found_continuation = true;
1659                        }
1660                        // Check if followed by another list item at the same level
1661                        else if !next_line.in_code_block
1662                            && next_line.list_item.is_some()
1663                            && let Some(item) = &next_line.list_item
1664                        {
1665                            let next_blockquote_prefix = BLOCKQUOTE_PREFIX_REGEX
1666                                .find(&next_line.content)
1667                                .map_or(String::new(), |m| m.as_str().to_string());
1668                            if item.marker_column == current_indent_level
1669                                && item.is_ordered == block.is_ordered
1670                                && block.blockquote_prefix.trim() == next_blockquote_prefix.trim()
1671                            {
1672                                // Check if there was meaningful content between the list items (unused now)
1673                                // This variable is kept for potential future use but is currently replaced by has_structural_separators
1674                                let _has_meaningful_content = (line_idx + 1..check_idx).any(|idx| {
1675                                    if let Some(between_line) = lines.get(idx) {
1676                                        let trimmed = between_line.content.trim();
1677                                        // Skip empty lines
1678                                        if trimmed.is_empty() {
1679                                            return false;
1680                                        }
1681                                        // Check for meaningful content
1682                                        let line_indent =
1683                                            between_line.content.len() - between_line.content.trim_start().len();
1684
1685                                        // Structural separators (code fences, headings, etc.) are meaningful and should BREAK lists
1686                                        if trimmed.starts_with("```")
1687                                            || trimmed.starts_with("~~~")
1688                                            || trimmed.starts_with("---")
1689                                            || trimmed.starts_with("***")
1690                                            || trimmed.starts_with("___")
1691                                            || trimmed.starts_with(">")
1692                                            || trimmed.contains('|') // Tables
1693                                            || between_line.heading.is_some()
1694                                        {
1695                                            return true; // These are structural separators - meaningful content that breaks lists
1696                                        }
1697
1698                                        // Only properly indented content continues the list
1699                                        line_indent >= min_continuation_indent
1700                                    } else {
1701                                        false
1702                                    }
1703                                });
1704
1705                                if block.is_ordered {
1706                                    // For ordered lists: don't continue if there are structural separators
1707                                    // Check if there are structural separators between the list items
1708                                    let has_structural_separators = (line_idx + 1..check_idx).any(|idx| {
1709                                        if let Some(between_line) = lines.get(idx) {
1710                                            let trimmed = between_line.content.trim();
1711                                            if trimmed.is_empty() {
1712                                                return false;
1713                                            }
1714                                            // Check for structural separators that break lists
1715                                            trimmed.starts_with("```")
1716                                                || trimmed.starts_with("~~~")
1717                                                || trimmed.starts_with("---")
1718                                                || trimmed.starts_with("***")
1719                                                || trimmed.starts_with("___")
1720                                                || trimmed.starts_with(">")
1721                                                || trimmed.contains('|') // Tables
1722                                                || between_line.heading.is_some()
1723                                        } else {
1724                                            false
1725                                        }
1726                                    });
1727                                    found_continuation = !has_structural_separators;
1728                                } else {
1729                                    // For unordered lists: also check for structural separators
1730                                    let has_structural_separators = (line_idx + 1..check_idx).any(|idx| {
1731                                        if let Some(between_line) = lines.get(idx) {
1732                                            let trimmed = between_line.content.trim();
1733                                            if trimmed.is_empty() {
1734                                                return false;
1735                                            }
1736                                            // Check for structural separators that break lists
1737                                            trimmed.starts_with("```")
1738                                                || trimmed.starts_with("~~~")
1739                                                || trimmed.starts_with("---")
1740                                                || trimmed.starts_with("***")
1741                                                || trimmed.starts_with("___")
1742                                                || trimmed.starts_with(">")
1743                                                || trimmed.contains('|') // Tables
1744                                                || between_line.heading.is_some()
1745                                        } else {
1746                                            false
1747                                        }
1748                                    });
1749                                    found_continuation = !has_structural_separators;
1750                                }
1751                            }
1752                        }
1753                    }
1754
1755                    if found_continuation {
1756                        // Include the blank line in the block
1757                        block.end_line = line_num;
1758                    } else {
1759                        // Blank line ends the list - don't include it
1760                        list_blocks.push(block.clone());
1761                        current_block = None;
1762                    }
1763                } else {
1764                    // Check for lazy continuation - non-indented line immediately after a list item
1765                    // But only if the line has sufficient indentation for the list type
1766                    let min_required_indent = if block.is_ordered {
1767                        current_indent_level + last_marker_width
1768                    } else {
1769                        current_indent_level + 2
1770                    };
1771
1772                    // For lazy continuation to apply, the line must either:
1773                    // 1. Have no indentation (true lazy continuation)
1774                    // 2. Have sufficient indentation for the list type
1775                    // BUT structural separators (headings, code blocks, etc.) should never be lazy continuations
1776                    let line_content = line_info.content.trim();
1777                    let is_structural_separator = line_info.heading.is_some()
1778                        || line_content.starts_with("```")
1779                        || line_content.starts_with("~~~")
1780                        || line_content.starts_with("---")
1781                        || line_content.starts_with("***")
1782                        || line_content.starts_with("___")
1783                        || line_content.starts_with(">")
1784                        || line_content.contains('|'); // Tables
1785
1786                    let is_lazy_continuation = last_list_item_line == line_num - 1
1787                        && !is_structural_separator
1788                        && !line_info.is_blank
1789                        && (line_info.indent == 0 || line_info.indent >= min_required_indent);
1790
1791                    if is_lazy_continuation {
1792                        // Additional check: if the line starts with uppercase and looks like a new sentence,
1793                        // it's probably not a continuation
1794                        let content_to_check = if !blockquote_prefix.is_empty() {
1795                            // Strip blockquote prefix to check the actual content
1796                            line_info
1797                                .content
1798                                .strip_prefix(&blockquote_prefix)
1799                                .unwrap_or(&line_info.content)
1800                                .trim()
1801                        } else {
1802                            line_info.content.trim()
1803                        };
1804
1805                        let starts_with_uppercase = content_to_check.chars().next().is_some_and(|c| c.is_uppercase());
1806
1807                        // If it starts with uppercase and the previous line ended with punctuation,
1808                        // it's likely a new paragraph, not a continuation
1809                        if starts_with_uppercase && last_list_item_line > 0 {
1810                            // This looks like a new paragraph
1811                            list_blocks.push(block.clone());
1812                            current_block = None;
1813                        } else {
1814                            // This is a lazy continuation line
1815                            block.end_line = line_num;
1816                        }
1817                    } else {
1818                        // Non-indented, non-blank line that's not a lazy continuation - end the block
1819                        list_blocks.push(block.clone());
1820                        current_block = None;
1821                    }
1822                }
1823            }
1824        }
1825
1826        // Don't forget the last block
1827        if let Some(block) = current_block {
1828            list_blocks.push(block);
1829        }
1830
1831        // Merge adjacent blocks that should be one
1832        merge_adjacent_list_blocks(&mut list_blocks, lines);
1833
1834        list_blocks
1835    }
1836
1837    /// Compute character frequency for fast content analysis
1838    fn compute_char_frequency(content: &str) -> CharFrequency {
1839        let mut frequency = CharFrequency::default();
1840
1841        for ch in content.chars() {
1842            match ch {
1843                '#' => frequency.hash_count += 1,
1844                '*' => frequency.asterisk_count += 1,
1845                '_' => frequency.underscore_count += 1,
1846                '-' => frequency.hyphen_count += 1,
1847                '+' => frequency.plus_count += 1,
1848                '>' => frequency.gt_count += 1,
1849                '|' => frequency.pipe_count += 1,
1850                '[' => frequency.bracket_count += 1,
1851                '`' => frequency.backtick_count += 1,
1852                '<' => frequency.lt_count += 1,
1853                '!' => frequency.exclamation_count += 1,
1854                '\n' => frequency.newline_count += 1,
1855                _ => {}
1856            }
1857        }
1858
1859        frequency
1860    }
1861
1862    /// Parse HTML tags in the content
1863    fn parse_html_tags(content: &str, lines: &[LineInfo], code_blocks: &[(usize, usize)]) -> Vec<HtmlTag> {
1864        lazy_static! {
1865            static ref HTML_TAG_REGEX: regex::Regex =
1866                regex::Regex::new(r"(?i)<(/?)([a-zA-Z][a-zA-Z0-9]*)\b[^>]*(/?)>").unwrap();
1867        }
1868
1869        let mut html_tags = Vec::with_capacity(content.matches('<').count());
1870
1871        for cap in HTML_TAG_REGEX.captures_iter(content) {
1872            let full_match = cap.get(0).unwrap();
1873            let match_start = full_match.start();
1874            let match_end = full_match.end();
1875
1876            // Skip if in code block
1877            if CodeBlockUtils::is_in_code_block_or_span(code_blocks, match_start) {
1878                continue;
1879            }
1880
1881            let is_closing = !cap.get(1).unwrap().as_str().is_empty();
1882            let tag_name = cap.get(2).unwrap().as_str().to_lowercase();
1883            let is_self_closing = !cap.get(3).unwrap().as_str().is_empty();
1884
1885            // Find which line this tag is on
1886            let mut line_num = 1;
1887            let mut col_start = match_start;
1888            let mut col_end = match_end;
1889            for (idx, line_info) in lines.iter().enumerate() {
1890                if match_start >= line_info.byte_offset {
1891                    line_num = idx + 1;
1892                    col_start = match_start - line_info.byte_offset;
1893                    col_end = match_end - line_info.byte_offset;
1894                } else {
1895                    break;
1896                }
1897            }
1898
1899            html_tags.push(HtmlTag {
1900                line: line_num,
1901                start_col: col_start,
1902                end_col: col_end,
1903                byte_offset: match_start,
1904                byte_end: match_end,
1905                tag_name,
1906                is_closing,
1907                is_self_closing,
1908                raw_content: full_match.as_str().to_string(),
1909            });
1910        }
1911
1912        html_tags
1913    }
1914
1915    /// Parse emphasis spans in the content
1916    fn parse_emphasis_spans(content: &str, lines: &[LineInfo], code_blocks: &[(usize, usize)]) -> Vec<EmphasisSpan> {
1917        lazy_static! {
1918            static ref EMPHASIS_REGEX: regex::Regex =
1919                regex::Regex::new(r"(\*{1,3}|_{1,3})([^*_\s][^*_]*?)(\*{1,3}|_{1,3})").unwrap();
1920        }
1921
1922        let mut emphasis_spans = Vec::with_capacity(content.matches('*').count() + content.matches('_').count() / 4);
1923
1924        for cap in EMPHASIS_REGEX.captures_iter(content) {
1925            let full_match = cap.get(0).unwrap();
1926            let match_start = full_match.start();
1927            let match_end = full_match.end();
1928
1929            // Skip if in code block
1930            if CodeBlockUtils::is_in_code_block_or_span(code_blocks, match_start) {
1931                continue;
1932            }
1933
1934            let opening_markers = cap.get(1).unwrap().as_str();
1935            let content_part = cap.get(2).unwrap().as_str();
1936            let closing_markers = cap.get(3).unwrap().as_str();
1937
1938            // Validate matching markers
1939            if opening_markers.chars().next() != closing_markers.chars().next()
1940                || opening_markers.len() != closing_markers.len()
1941            {
1942                continue;
1943            }
1944
1945            let marker = opening_markers.chars().next().unwrap();
1946            let marker_count = opening_markers.len();
1947
1948            // Find which line this emphasis is on
1949            let mut line_num = 1;
1950            let mut col_start = match_start;
1951            let mut col_end = match_end;
1952            for (idx, line_info) in lines.iter().enumerate() {
1953                if match_start >= line_info.byte_offset {
1954                    line_num = idx + 1;
1955                    col_start = match_start - line_info.byte_offset;
1956                    col_end = match_end - line_info.byte_offset;
1957                } else {
1958                    break;
1959                }
1960            }
1961
1962            emphasis_spans.push(EmphasisSpan {
1963                line: line_num,
1964                start_col: col_start,
1965                end_col: col_end,
1966                byte_offset: match_start,
1967                byte_end: match_end,
1968                marker,
1969                marker_count,
1970                content: content_part.to_string(),
1971            });
1972        }
1973
1974        emphasis_spans
1975    }
1976
1977    /// Parse table rows in the content
1978    fn parse_table_rows(lines: &[LineInfo]) -> Vec<TableRow> {
1979        let mut table_rows = Vec::with_capacity(lines.len() / 20);
1980
1981        for (line_idx, line_info) in lines.iter().enumerate() {
1982            // Skip lines in code blocks or blank lines
1983            if line_info.in_code_block || line_info.is_blank {
1984                continue;
1985            }
1986
1987            let line = &line_info.content;
1988            let line_num = line_idx + 1;
1989
1990            // Check if this line contains pipes (potential table row)
1991            if !line.contains('|') {
1992                continue;
1993            }
1994
1995            // Count columns by splitting on pipes
1996            let parts: Vec<&str> = line.split('|').collect();
1997            let column_count = if parts.len() > 2 { parts.len() - 2 } else { parts.len() };
1998
1999            // Check if this is a separator row
2000            let is_separator = line.chars().all(|c| "|:-+ \t".contains(c));
2001            let mut column_alignments = Vec::new();
2002
2003            if is_separator {
2004                for part in &parts[1..parts.len() - 1] {
2005                    // Skip first and last empty parts
2006                    let trimmed = part.trim();
2007                    let alignment = if trimmed.starts_with(':') && trimmed.ends_with(':') {
2008                        "center".to_string()
2009                    } else if trimmed.ends_with(':') {
2010                        "right".to_string()
2011                    } else if trimmed.starts_with(':') {
2012                        "left".to_string()
2013                    } else {
2014                        "none".to_string()
2015                    };
2016                    column_alignments.push(alignment);
2017                }
2018            }
2019
2020            table_rows.push(TableRow {
2021                line: line_num,
2022                is_separator,
2023                column_count,
2024                column_alignments,
2025            });
2026        }
2027
2028        table_rows
2029    }
2030
2031    /// Parse bare URLs and emails in the content
2032    fn parse_bare_urls(content: &str, lines: &[LineInfo], code_blocks: &[(usize, usize)]) -> Vec<BareUrl> {
2033        let mut bare_urls = Vec::with_capacity(content.matches("http").count() + content.matches('@').count());
2034
2035        // Check for bare URLs (not in angle brackets or markdown links)
2036        for cap in BARE_URL_PATTERN.captures_iter(content) {
2037            let full_match = cap.get(0).unwrap();
2038            let match_start = full_match.start();
2039            let match_end = full_match.end();
2040
2041            // Skip if in code block
2042            if CodeBlockUtils::is_in_code_block_or_span(code_blocks, match_start) {
2043                continue;
2044            }
2045
2046            // Skip if already in angle brackets or markdown links
2047            let preceding_char = if match_start > 0 {
2048                content.chars().nth(match_start - 1)
2049            } else {
2050                None
2051            };
2052            let following_char = content.chars().nth(match_end);
2053
2054            if preceding_char == Some('<') || preceding_char == Some('(') || preceding_char == Some('[') {
2055                continue;
2056            }
2057            if following_char == Some('>') || following_char == Some(')') || following_char == Some(']') {
2058                continue;
2059            }
2060
2061            let url = full_match.as_str();
2062            let url_type = if url.starts_with("https://") {
2063                "https"
2064            } else if url.starts_with("http://") {
2065                "http"
2066            } else if url.starts_with("ftp://") {
2067                "ftp"
2068            } else {
2069                "other"
2070            };
2071
2072            // Find which line this URL is on
2073            let mut line_num = 1;
2074            let mut col_start = match_start;
2075            let mut col_end = match_end;
2076            for (idx, line_info) in lines.iter().enumerate() {
2077                if match_start >= line_info.byte_offset {
2078                    line_num = idx + 1;
2079                    col_start = match_start - line_info.byte_offset;
2080                    col_end = match_end - line_info.byte_offset;
2081                } else {
2082                    break;
2083                }
2084            }
2085
2086            bare_urls.push(BareUrl {
2087                line: line_num,
2088                start_col: col_start,
2089                end_col: col_end,
2090                byte_offset: match_start,
2091                byte_end: match_end,
2092                url: url.to_string(),
2093                url_type: url_type.to_string(),
2094            });
2095        }
2096
2097        // Check for bare email addresses
2098        for cap in BARE_EMAIL_PATTERN.captures_iter(content) {
2099            let full_match = cap.get(0).unwrap();
2100            let match_start = full_match.start();
2101            let match_end = full_match.end();
2102
2103            // Skip if in code block
2104            if CodeBlockUtils::is_in_code_block_or_span(code_blocks, match_start) {
2105                continue;
2106            }
2107
2108            // Skip if already in angle brackets or markdown links
2109            let preceding_char = if match_start > 0 {
2110                content.chars().nth(match_start - 1)
2111            } else {
2112                None
2113            };
2114            let following_char = content.chars().nth(match_end);
2115
2116            if preceding_char == Some('<') || preceding_char == Some('(') || preceding_char == Some('[') {
2117                continue;
2118            }
2119            if following_char == Some('>') || following_char == Some(')') || following_char == Some(']') {
2120                continue;
2121            }
2122
2123            let email = full_match.as_str();
2124
2125            // Find which line this email is on
2126            let mut line_num = 1;
2127            let mut col_start = match_start;
2128            let mut col_end = match_end;
2129            for (idx, line_info) in lines.iter().enumerate() {
2130                if match_start >= line_info.byte_offset {
2131                    line_num = idx + 1;
2132                    col_start = match_start - line_info.byte_offset;
2133                    col_end = match_end - line_info.byte_offset;
2134                } else {
2135                    break;
2136                }
2137            }
2138
2139            bare_urls.push(BareUrl {
2140                line: line_num,
2141                start_col: col_start,
2142                end_col: col_end,
2143                byte_offset: match_start,
2144                byte_end: match_end,
2145                url: email.to_string(),
2146                url_type: "email".to_string(),
2147            });
2148        }
2149
2150        bare_urls
2151    }
2152}
2153
2154/// Merge adjacent list blocks that should be treated as one
2155fn merge_adjacent_list_blocks(list_blocks: &mut Vec<ListBlock>, lines: &[LineInfo]) {
2156    if list_blocks.len() < 2 {
2157        return;
2158    }
2159
2160    let mut merger = ListBlockMerger::new(lines);
2161    *list_blocks = merger.merge(list_blocks);
2162}
2163
2164/// Helper struct to manage the complex logic of merging list blocks
2165struct ListBlockMerger<'a> {
2166    lines: &'a [LineInfo],
2167}
2168
2169impl<'a> ListBlockMerger<'a> {
2170    fn new(lines: &'a [LineInfo]) -> Self {
2171        Self { lines }
2172    }
2173
2174    fn merge(&mut self, list_blocks: &[ListBlock]) -> Vec<ListBlock> {
2175        let mut merged = Vec::with_capacity(list_blocks.len());
2176        let mut current = list_blocks[0].clone();
2177
2178        for next in list_blocks.iter().skip(1) {
2179            if self.should_merge_blocks(&current, next) {
2180                current = self.merge_two_blocks(current, next);
2181            } else {
2182                merged.push(current);
2183                current = next.clone();
2184            }
2185        }
2186
2187        merged.push(current);
2188        merged
2189    }
2190
2191    /// Determine if two adjacent list blocks should be merged
2192    fn should_merge_blocks(&self, current: &ListBlock, next: &ListBlock) -> bool {
2193        // Basic compatibility checks
2194        if !self.blocks_are_compatible(current, next) {
2195            return false;
2196        }
2197
2198        // Check spacing and content between blocks
2199        let spacing = self.analyze_spacing_between(current, next);
2200        match spacing {
2201            BlockSpacing::Consecutive => true,
2202            BlockSpacing::SingleBlank => self.can_merge_with_blank_between(current, next),
2203            BlockSpacing::MultipleBlanks | BlockSpacing::ContentBetween => {
2204                self.can_merge_with_content_between(current, next)
2205            }
2206        }
2207    }
2208
2209    /// Check if blocks have compatible structure for merging
2210    fn blocks_are_compatible(&self, current: &ListBlock, next: &ListBlock) -> bool {
2211        current.is_ordered == next.is_ordered
2212            && current.blockquote_prefix == next.blockquote_prefix
2213            && current.nesting_level == next.nesting_level
2214    }
2215
2216    /// Analyze the spacing between two list blocks
2217    fn analyze_spacing_between(&self, current: &ListBlock, next: &ListBlock) -> BlockSpacing {
2218        let gap = next.start_line - current.end_line;
2219
2220        match gap {
2221            1 => BlockSpacing::Consecutive,
2222            2 => BlockSpacing::SingleBlank,
2223            _ if gap > 2 => {
2224                if self.has_only_blank_lines_between(current, next) {
2225                    BlockSpacing::MultipleBlanks
2226                } else {
2227                    BlockSpacing::ContentBetween
2228                }
2229            }
2230            _ => BlockSpacing::Consecutive, // gap == 0, overlapping (shouldn't happen)
2231        }
2232    }
2233
2234    /// Check if unordered lists can be merged with a single blank line between
2235    fn can_merge_with_blank_between(&self, current: &ListBlock, next: &ListBlock) -> bool {
2236        // Check if there are structural separators between the blocks
2237        // If has_meaningful_content_between returns true, it means there are structural separators
2238        if has_meaningful_content_between(current, next, self.lines) {
2239            return false; // Structural separators prevent merging
2240        }
2241
2242        // Only merge unordered lists with same marker across single blank
2243        !current.is_ordered && current.marker == next.marker
2244    }
2245
2246    /// Check if ordered lists can be merged when there's content between them
2247    fn can_merge_with_content_between(&self, current: &ListBlock, next: &ListBlock) -> bool {
2248        // Do not merge lists if there are structural separators between them
2249        if has_meaningful_content_between(current, next, self.lines) {
2250            return false; // Structural separators prevent merging
2251        }
2252
2253        // Only consider merging ordered lists if there's no structural content between
2254        current.is_ordered && next.is_ordered
2255    }
2256
2257    /// Check if there are only blank lines between blocks
2258    fn has_only_blank_lines_between(&self, current: &ListBlock, next: &ListBlock) -> bool {
2259        for line_num in (current.end_line + 1)..next.start_line {
2260            if let Some(line_info) = self.lines.get(line_num - 1)
2261                && !line_info.content.trim().is_empty()
2262            {
2263                return false;
2264            }
2265        }
2266        true
2267    }
2268
2269    /// Merge two compatible list blocks into one
2270    fn merge_two_blocks(&self, mut current: ListBlock, next: &ListBlock) -> ListBlock {
2271        current.end_line = next.end_line;
2272        current.item_lines.extend_from_slice(&next.item_lines);
2273
2274        // Update max marker width
2275        current.max_marker_width = current.max_marker_width.max(next.max_marker_width);
2276
2277        // Handle marker consistency for unordered lists
2278        if !current.is_ordered && self.markers_differ(&current, next) {
2279            current.marker = None; // Mixed markers
2280        }
2281
2282        current
2283    }
2284
2285    /// Check if two blocks have different markers
2286    fn markers_differ(&self, current: &ListBlock, next: &ListBlock) -> bool {
2287        current.marker.is_some() && next.marker.is_some() && current.marker != next.marker
2288    }
2289}
2290
2291/// Types of spacing between list blocks
2292#[derive(Debug, PartialEq)]
2293enum BlockSpacing {
2294    Consecutive,    // No gap between blocks
2295    SingleBlank,    // One blank line between blocks
2296    MultipleBlanks, // Multiple blank lines but no content
2297    ContentBetween, // Content exists between blocks
2298}
2299
2300/// Check if there's meaningful content (not just blank lines) between two list blocks
2301fn has_meaningful_content_between(current: &ListBlock, next: &ListBlock, lines: &[LineInfo]) -> bool {
2302    // Check lines between current.end_line and next.start_line
2303    for line_num in (current.end_line + 1)..next.start_line {
2304        if let Some(line_info) = lines.get(line_num - 1) {
2305            // Convert to 0-indexed
2306            let trimmed = line_info.content.trim();
2307
2308            // Skip empty lines
2309            if trimmed.is_empty() {
2310                continue;
2311            }
2312
2313            // Check for structural separators that should separate lists (CommonMark compliant)
2314
2315            // Headings separate lists
2316            if line_info.heading.is_some() {
2317                return true; // Has meaningful content - headings separate lists
2318            }
2319
2320            // Horizontal rules separate lists (---, ***, ___)
2321            if is_horizontal_rule(trimmed) {
2322                return true; // Has meaningful content - horizontal rules separate lists
2323            }
2324
2325            // Tables separate lists (lines containing |)
2326            if trimmed.contains('|') && trimmed.len() > 1 {
2327                return true; // Has meaningful content - tables separate lists
2328            }
2329
2330            // Blockquotes separate lists
2331            if trimmed.starts_with('>') {
2332                return true; // Has meaningful content - blockquotes separate lists
2333            }
2334
2335            // Code block fences separate lists (unless properly indented as list content)
2336            if trimmed.starts_with("```") || trimmed.starts_with("~~~") {
2337                let line_indent = line_info.content.len() - line_info.content.trim_start().len();
2338
2339                // Check if this code block is properly indented as list continuation
2340                let min_continuation_indent = if current.is_ordered {
2341                    current.nesting_level + current.max_marker_width + 1 // +1 for space after marker
2342                } else {
2343                    current.nesting_level + 2
2344                };
2345
2346                if line_indent < min_continuation_indent {
2347                    // This is a standalone code block that separates lists
2348                    return true; // Has meaningful content - standalone code blocks separate lists
2349                }
2350            }
2351
2352            // Check if this line has proper indentation for list continuation
2353            let line_indent = line_info.content.len() - line_info.content.trim_start().len();
2354
2355            // Calculate minimum indentation needed to be list continuation
2356            let min_indent = if current.is_ordered {
2357                current.nesting_level + current.max_marker_width
2358            } else {
2359                current.nesting_level + 2
2360            };
2361
2362            // If the line is not indented enough to be list continuation, it's meaningful content
2363            if line_indent < min_indent {
2364                return true; // Has meaningful content - content not indented as list continuation
2365            }
2366
2367            // If we reach here, the line is properly indented as list continuation
2368            // Continue checking other lines
2369        }
2370    }
2371
2372    // Only blank lines or properly indented list continuation content between blocks
2373    false
2374}
2375
2376/// Check if a line is a horizontal rule (---, ***, ___)
2377fn is_horizontal_rule(trimmed: &str) -> bool {
2378    if trimmed.len() < 3 {
2379        return false;
2380    }
2381
2382    // Check for three or more consecutive -, *, or _ characters (with optional spaces)
2383    let chars: Vec<char> = trimmed.chars().collect();
2384    if let Some(&first_char) = chars.first()
2385        && (first_char == '-' || first_char == '*' || first_char == '_')
2386    {
2387        let mut count = 0;
2388        for &ch in &chars {
2389            if ch == first_char {
2390                count += 1;
2391            } else if ch != ' ' && ch != '\t' {
2392                return false; // Non-matching, non-whitespace character
2393            }
2394        }
2395        return count >= 3;
2396    }
2397    false
2398}
2399
2400/// Check if content contains patterns that cause the markdown crate to panic
2401#[cfg(test)]
2402mod tests {
2403    use super::*;
2404
2405    #[test]
2406    fn test_empty_content() {
2407        let ctx = LintContext::new("", MarkdownFlavor::Standard);
2408        assert_eq!(ctx.content, "");
2409        assert_eq!(ctx.line_offsets, vec![0]);
2410        assert_eq!(ctx.offset_to_line_col(0), (1, 1));
2411        assert_eq!(ctx.lines.len(), 0);
2412    }
2413
2414    #[test]
2415    fn test_single_line() {
2416        let ctx = LintContext::new("# Hello", MarkdownFlavor::Standard);
2417        assert_eq!(ctx.content, "# Hello");
2418        assert_eq!(ctx.line_offsets, vec![0]);
2419        assert_eq!(ctx.offset_to_line_col(0), (1, 1));
2420        assert_eq!(ctx.offset_to_line_col(3), (1, 4));
2421    }
2422
2423    #[test]
2424    fn test_multi_line() {
2425        let content = "# Title\n\nSecond line\nThird line";
2426        let ctx = LintContext::new(content, MarkdownFlavor::Standard);
2427        assert_eq!(ctx.line_offsets, vec![0, 8, 9, 21]);
2428        // Test offset to line/col
2429        assert_eq!(ctx.offset_to_line_col(0), (1, 1)); // start
2430        assert_eq!(ctx.offset_to_line_col(8), (2, 1)); // start of blank line
2431        assert_eq!(ctx.offset_to_line_col(9), (3, 1)); // start of 'Second line'
2432        assert_eq!(ctx.offset_to_line_col(15), (3, 7)); // middle of 'Second line'
2433        assert_eq!(ctx.offset_to_line_col(21), (4, 1)); // start of 'Third line'
2434    }
2435
2436    #[test]
2437    fn test_line_info() {
2438        let content = "# Title\n    indented\n\ncode:\n```rust\nfn main() {}\n```";
2439        let ctx = LintContext::new(content, MarkdownFlavor::Standard);
2440
2441        // Test line info
2442        assert_eq!(ctx.lines.len(), 7);
2443
2444        // Line 1: "# Title"
2445        let line1 = &ctx.lines[0];
2446        assert_eq!(line1.content, "# Title");
2447        assert_eq!(line1.byte_offset, 0);
2448        assert_eq!(line1.indent, 0);
2449        assert!(!line1.is_blank);
2450        assert!(!line1.in_code_block);
2451        assert!(line1.list_item.is_none());
2452
2453        // Line 2: "    indented"
2454        let line2 = &ctx.lines[1];
2455        assert_eq!(line2.content, "    indented");
2456        assert_eq!(line2.byte_offset, 8);
2457        assert_eq!(line2.indent, 4);
2458        assert!(!line2.is_blank);
2459
2460        // Line 3: "" (blank)
2461        let line3 = &ctx.lines[2];
2462        assert_eq!(line3.content, "");
2463        assert!(line3.is_blank);
2464
2465        // Test helper methods
2466        assert_eq!(ctx.line_to_byte_offset(1), Some(0));
2467        assert_eq!(ctx.line_to_byte_offset(2), Some(8));
2468        assert_eq!(ctx.line_info(1).map(|l| l.indent), Some(0));
2469        assert_eq!(ctx.line_info(2).map(|l| l.indent), Some(4));
2470    }
2471
2472    #[test]
2473    fn test_list_item_detection() {
2474        let content = "- Unordered item\n  * Nested item\n1. Ordered item\n   2) Nested ordered\n\nNot a list";
2475        let ctx = LintContext::new(content, MarkdownFlavor::Standard);
2476
2477        // Line 1: "- Unordered item"
2478        let line1 = &ctx.lines[0];
2479        assert!(line1.list_item.is_some());
2480        let list1 = line1.list_item.as_ref().unwrap();
2481        assert_eq!(list1.marker, "-");
2482        assert!(!list1.is_ordered);
2483        assert_eq!(list1.marker_column, 0);
2484        assert_eq!(list1.content_column, 2);
2485
2486        // Line 2: "  * Nested item"
2487        let line2 = &ctx.lines[1];
2488        assert!(line2.list_item.is_some());
2489        let list2 = line2.list_item.as_ref().unwrap();
2490        assert_eq!(list2.marker, "*");
2491        assert_eq!(list2.marker_column, 2);
2492
2493        // Line 3: "1. Ordered item"
2494        let line3 = &ctx.lines[2];
2495        assert!(line3.list_item.is_some());
2496        let list3 = line3.list_item.as_ref().unwrap();
2497        assert_eq!(list3.marker, "1.");
2498        assert!(list3.is_ordered);
2499        assert_eq!(list3.number, Some(1));
2500
2501        // Line 6: "Not a list"
2502        let line6 = &ctx.lines[5];
2503        assert!(line6.list_item.is_none());
2504    }
2505
2506    #[test]
2507    fn test_offset_to_line_col_edge_cases() {
2508        let content = "a\nb\nc";
2509        let ctx = LintContext::new(content, MarkdownFlavor::Standard);
2510        // line_offsets: [0, 2, 4]
2511        assert_eq!(ctx.offset_to_line_col(0), (1, 1)); // 'a'
2512        assert_eq!(ctx.offset_to_line_col(1), (1, 2)); // after 'a'
2513        assert_eq!(ctx.offset_to_line_col(2), (2, 1)); // 'b'
2514        assert_eq!(ctx.offset_to_line_col(3), (2, 2)); // after 'b'
2515        assert_eq!(ctx.offset_to_line_col(4), (3, 1)); // 'c'
2516        assert_eq!(ctx.offset_to_line_col(5), (3, 2)); // after 'c'
2517    }
2518}