Skip to main content

rumdl_lib/
workspace_index.rs

1//! Workspace-wide index for cross-file analysis
2//!
3//! This module provides infrastructure for rules that need to validate
4//! references across multiple files, such as MD051 which validates that
5//! cross-file link fragments point to valid headings.
6//!
7//! The index is built in parallel and designed for minimal memory overhead.
8//!
9//! ## Cache Format
10//!
11//! The workspace index can be persisted to disk for faster startup on
12//! repeated runs. The cache format includes a version header to detect
13//! incompatible format changes:
14//!
15//! ```text
16//! [4 bytes: magic "RWSI" - Rumdl Workspace Index]
17//! [4 bytes: format version (u32 little-endian)]
18//! [N bytes: postcard-serialized WorkspaceIndex]
19//! ```
20
21use regex::Regex;
22use serde::{Deserialize, Serialize};
23use std::collections::{HashMap, HashSet};
24use std::path::{Path, PathBuf};
25use std::sync::LazyLock;
26
27use crate::lint_context::LintContext;
28use crate::utils::element_cache::ElementCache;
29
30// =============================================================================
31// URL Decoding Helper
32// =============================================================================
33
34/// Convert a hex digit character to its numeric value (0-15)
35fn hex_digit_to_value(c: u8) -> Option<u8> {
36    match c {
37        b'0'..=b'9' => Some(c - b'0'),
38        b'a'..=b'f' => Some(c - b'a' + 10),
39        b'A'..=b'F' => Some(c - b'A' + 10),
40        _ => None,
41    }
42}
43
44/// URL-decode a string, handling percent-encoded characters.
45/// Returns the decoded string, or the original if decoding fails.
46/// Used for matching URL-encoded CJK fragments against raw anchors.
47fn url_decode(s: &str) -> String {
48    // Fast path: no percent signs means no encoding
49    if !s.contains('%') {
50        return s.to_string();
51    }
52
53    let bytes = s.as_bytes();
54    let mut result = Vec::with_capacity(bytes.len());
55    let mut i = 0;
56
57    while i < bytes.len() {
58        if bytes[i] == b'%' && i + 2 < bytes.len() {
59            // Try to parse the two hex digits following %
60            let hex1 = bytes[i + 1];
61            let hex2 = bytes[i + 2];
62            if let (Some(d1), Some(d2)) = (hex_digit_to_value(hex1), hex_digit_to_value(hex2)) {
63                result.push(d1 * 16 + d2);
64                i += 3;
65                continue;
66            }
67        }
68        result.push(bytes[i]);
69        i += 1;
70    }
71
72    // Convert to UTF-8, falling back to original if invalid
73    String::from_utf8(result).unwrap_or_else(|_| s.to_string())
74}
75
76// =============================================================================
77// Shared cross-file link extraction utilities
78//
79// These regexes and helpers are the canonical implementation for extracting
80// cross-file links. Both MD057 and LSP use this shared code path for correct
81// position tracking.
82// =============================================================================
83
84/// Regex to match the start of a link
85static LINK_START_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"!?\[[^\]]*\]").unwrap());
86
87/// Regex to extract the URL from an angle-bracketed markdown link
88/// Format: `](<URL>)` or `](<URL> "title")`
89static URL_EXTRACT_ANGLE_BRACKET_REGEX: LazyLock<Regex> =
90    LazyLock::new(|| Regex::new(r#"\]\(\s*<([^>]+)>(#[^\)\s]*)?\s*(?:"[^"]*")?\s*\)"#).unwrap());
91
92/// Regex to extract the URL from a normal markdown link (without angle brackets)
93/// Format: `](URL)` or `](URL "title")`
94static URL_EXTRACT_REGEX: LazyLock<Regex> =
95    LazyLock::new(|| Regex::new(r#"]\(\s*([^>)\s#]+)(#[^)\s]*)?\s*(?:"[^"]*")?\s*\)"#).unwrap());
96
97/// Regex to detect URLs with explicit schemes
98static PROTOCOL_DOMAIN_REGEX: LazyLock<Regex> =
99    LazyLock::new(|| Regex::new(r"^([a-zA-Z][a-zA-Z0-9+.-]*://|[a-zA-Z][a-zA-Z0-9+.-]*:|www\.)").unwrap());
100
101/// Supported markdown file extensions
102const MARKDOWN_EXTENSIONS: &[&str] = &[
103    ".md",
104    ".markdown",
105    ".mdx",
106    ".mkd",
107    ".mkdn",
108    ".mdown",
109    ".mdwn",
110    ".qmd",
111    ".rmd",
112];
113
114/// Check if a path has a markdown extension (case-insensitive)
115#[inline]
116fn is_markdown_file(path: &str) -> bool {
117    let path_lower = path.to_lowercase();
118    MARKDOWN_EXTENSIONS.iter().any(|ext| path_lower.ends_with(ext))
119}
120
121/// Strip query parameters and fragments from a URL path
122/// Returns the path portion before `?` or `#`
123fn strip_query_and_fragment(url: &str) -> &str {
124    let query_pos = url.find('?');
125    let fragment_pos = url.find('#');
126
127    match (query_pos, fragment_pos) {
128        (Some(q), Some(f)) => &url[..q.min(f)],
129        (Some(q), None) => &url[..q],
130        (None, Some(f)) => &url[..f],
131        (None, None) => url,
132    }
133}
134
135/// Extract cross-file links from content using correct regex-based position tracking.
136///
137/// This is the canonical implementation used by both MD057 and LSP to ensure
138/// consistent and correct column positions for diagnostic reporting.
139///
140/// Returns a vector of `CrossFileLinkIndex` entries, one for each markdown file
141/// link found in the content.
142pub fn extract_cross_file_links(ctx: &LintContext) -> Vec<CrossFileLinkIndex> {
143    let content = ctx.content;
144
145    // Early returns for performance
146    if content.is_empty() || !content.contains("](") {
147        return Vec::new();
148    }
149
150    let mut links = Vec::new();
151    let lines: Vec<&str> = content.lines().collect();
152    let element_cache = ElementCache::new(content);
153    let line_index = &ctx.line_index;
154
155    // Track which lines we've already processed to avoid duplicates
156    // (ctx.links may have multiple entries for the same line)
157    let mut processed_lines = HashSet::new();
158
159    for link in &ctx.links {
160        let line_idx = link.line - 1;
161        if line_idx >= lines.len() {
162            continue;
163        }
164
165        // Skip if we've already processed this line
166        if !processed_lines.insert(line_idx) {
167            continue;
168        }
169
170        let line = lines[line_idx];
171        if !line.contains("](") {
172            continue;
173        }
174
175        // Find all links in this line
176        for link_match in LINK_START_REGEX.find_iter(line) {
177            let start_pos = link_match.start();
178            let end_pos = link_match.end();
179
180            // Calculate absolute position for code span detection
181            let line_start_byte = line_index.get_line_start_byte(line_idx + 1).unwrap_or(0);
182            let absolute_start_pos = line_start_byte + start_pos;
183
184            // Skip if in code span
185            if element_cache.is_in_code_span(absolute_start_pos) {
186                continue;
187            }
188
189            // Extract the URL (group 1) and fragment (group 2)
190            // Try angle-bracket regex first (handles URLs with parens)
191            let caps_result = URL_EXTRACT_ANGLE_BRACKET_REGEX
192                .captures_at(line, end_pos - 1)
193                .or_else(|| URL_EXTRACT_REGEX.captures_at(line, end_pos - 1));
194
195            if let Some(caps) = caps_result
196                && let Some(url_group) = caps.get(1)
197            {
198                let file_path = url_group.as_str().trim();
199
200                // Skip empty, external, template variables, absolute URL paths,
201                // framework aliases, fragment-only URLs, or rustdoc intra-doc links
202                if file_path.is_empty()
203                    || PROTOCOL_DOMAIN_REGEX.is_match(file_path)
204                    || file_path.starts_with("www.")
205                    || file_path.starts_with('#')
206                    || file_path.starts_with("{{")
207                    || file_path.starts_with("{%")
208                    || file_path.starts_with('/')
209                    || file_path.starts_with('~')
210                    || file_path.starts_with('@')
211                    || (file_path.starts_with('`') && file_path.ends_with('`'))
212                {
213                    continue;
214                }
215
216                // Strip query parameters before indexing
217                let file_path = strip_query_and_fragment(file_path);
218
219                // Get fragment from capture group 2 (includes # prefix)
220                let fragment = caps.get(2).map(|m| m.as_str().trim_start_matches('#')).unwrap_or("");
221
222                // Only index markdown file links for cross-file validation
223                if is_markdown_file(file_path) {
224                    links.push(CrossFileLinkIndex {
225                        target_path: file_path.to_string(),
226                        fragment: fragment.to_string(),
227                        line: link.line,
228                        column: url_group.start() + 1,
229                    });
230                }
231            }
232        }
233    }
234
235    links
236}
237
238/// Magic bytes identifying a workspace index cache file
239#[cfg(feature = "native")]
240const CACHE_MAGIC: &[u8; 4] = b"RWSI";
241
242/// Cache format version - increment when WorkspaceIndex serialization changes
243#[cfg(feature = "native")]
244const CACHE_FORMAT_VERSION: u32 = 5;
245
246/// Cache file name within the version directory
247#[cfg(feature = "native")]
248const CACHE_FILE_NAME: &str = "workspace_index.bin";
249
250/// Workspace-wide index for cross-file analysis
251///
252/// Contains pre-extracted information from all markdown files in the workspace,
253/// enabling rules to validate cross-file references efficiently.
254#[derive(Debug, Default, Clone, Serialize, Deserialize)]
255pub struct WorkspaceIndex {
256    /// Map from file path to its extracted data
257    files: HashMap<PathBuf, FileIndex>,
258    /// Reverse dependency graph: target file → files that link to it
259    /// Used to efficiently re-lint dependent files when a target changes
260    reverse_deps: HashMap<PathBuf, HashSet<PathBuf>>,
261    /// Version counter for cache invalidation (incremented on any change)
262    version: u64,
263}
264
265/// Index data extracted from a single file
266#[derive(Debug, Clone, Default, Serialize, Deserialize)]
267pub struct FileIndex {
268    /// Headings in this file with their anchors
269    pub headings: Vec<HeadingIndex>,
270    /// Reference links in this file (for cross-file analysis)
271    pub reference_links: Vec<ReferenceLinkIndex>,
272    /// Cross-file links in this file (for MD051 cross-file validation)
273    pub cross_file_links: Vec<CrossFileLinkIndex>,
274    /// Defined reference IDs (e.g., from `[ref]: url` definitions)
275    /// Used to filter out reference links that have explicit definitions
276    pub defined_references: HashSet<String>,
277    /// Content hash for change detection
278    pub content_hash: String,
279    /// O(1) anchor lookup: lowercased anchor → heading index
280    /// Includes both auto-generated and custom anchors
281    anchor_to_heading: HashMap<String, usize>,
282    /// HTML anchors defined via `<a id="...">` or `<element id="...">` tags
283    /// Stored lowercase for case-insensitive matching
284    html_anchors: HashSet<String>,
285    /// Attribute anchors defined via { #id } syntax (kramdown/MkDocs attr_list)
286    /// Can appear on any element, not just headings
287    /// Stored lowercase for case-insensitive matching
288    attribute_anchors: HashSet<String>,
289    /// Rules disabled for the entire file (from inline comments)
290    /// Used by cross-file rules to respect inline disable directives
291    pub file_disabled_rules: HashSet<String>,
292    /// Persistent disable/enable state transitions, sorted by line number.
293    /// Each entry: (line, disabled_rules, enabled_rules). Use binary search to query.
294    pub persistent_transitions: Vec<(usize, HashSet<String>, HashSet<String>)>,
295    /// Rules disabled at specific lines via disable-line / disable-next-line
296    pub line_disabled_rules: HashMap<usize, HashSet<String>>,
297}
298
299/// Information about a heading for cross-file lookup
300#[derive(Debug, Clone, Serialize, Deserialize)]
301pub struct HeadingIndex {
302    /// The heading text (e.g., "Installation Guide")
303    pub text: String,
304    /// Auto-generated anchor (e.g., "installation-guide")
305    pub auto_anchor: String,
306    /// Custom anchor if present (e.g., "install")
307    pub custom_anchor: Option<String>,
308    /// Line number (1-indexed)
309    pub line: usize,
310}
311
312/// Information about a reference link for cross-file analysis
313#[derive(Debug, Clone, Serialize, Deserialize)]
314pub struct ReferenceLinkIndex {
315    /// The reference ID (the part in `[text][ref]`)
316    pub reference_id: String,
317    /// Line number (1-indexed)
318    pub line: usize,
319    /// Column number (1-indexed)
320    pub column: usize,
321}
322
323/// Information about a cross-file link for validation
324#[derive(Debug, Clone, Serialize, Deserialize)]
325pub struct CrossFileLinkIndex {
326    /// The target file path (relative, as it appears in the link)
327    pub target_path: String,
328    /// The fragment/anchor being linked to (without #)
329    pub fragment: String,
330    /// Line number (1-indexed)
331    pub line: usize,
332    /// Column number (1-indexed)
333    pub column: usize,
334}
335
336/// Information about a vulnerable anchor (heading without custom ID)
337#[derive(Debug, Clone, Serialize, Deserialize)]
338pub struct VulnerableAnchor {
339    /// File path where the heading is located
340    pub file: PathBuf,
341    /// Line number of the heading
342    pub line: usize,
343    /// The heading text
344    pub text: String,
345}
346
347impl WorkspaceIndex {
348    /// Create a new empty workspace index
349    pub fn new() -> Self {
350        Self::default()
351    }
352
353    /// Get the current version (for cache invalidation)
354    pub fn version(&self) -> u64 {
355        self.version
356    }
357
358    /// Get the number of indexed files
359    pub fn file_count(&self) -> usize {
360        self.files.len()
361    }
362
363    /// Check if a file is in the index
364    pub fn contains_file(&self, path: &Path) -> bool {
365        self.files.contains_key(path)
366    }
367
368    /// Get the index data for a specific file
369    pub fn get_file(&self, path: &Path) -> Option<&FileIndex> {
370        self.files.get(path)
371    }
372
373    /// Insert or update a file's index data
374    pub fn insert_file(&mut self, path: PathBuf, index: FileIndex) {
375        self.files.insert(path, index);
376        self.version = self.version.wrapping_add(1);
377    }
378
379    /// Remove a file from the index
380    pub fn remove_file(&mut self, path: &Path) -> Option<FileIndex> {
381        // Clean up reverse deps for this file
382        self.clear_reverse_deps_for(path);
383
384        let result = self.files.remove(path);
385        if result.is_some() {
386            self.version = self.version.wrapping_add(1);
387        }
388        result
389    }
390
391    /// Build a map of all "vulnerable" anchors across the workspace
392    ///
393    /// A vulnerable anchor is an auto-generated anchor for a heading that
394    /// does NOT have a custom anchor defined. These are problematic for
395    /// translated content because the anchor changes when the heading is translated.
396    ///
397    /// Returns: Map from lowercase anchor → Vec of VulnerableAnchor info
398    /// Multiple files can have headings with the same auto-generated anchor,
399    /// so we collect all occurrences.
400    pub fn get_vulnerable_anchors(&self) -> HashMap<String, Vec<VulnerableAnchor>> {
401        let mut vulnerable: HashMap<String, Vec<VulnerableAnchor>> = HashMap::new();
402
403        for (file_path, file_index) in &self.files {
404            for heading in &file_index.headings {
405                // Only include headings WITHOUT custom anchors
406                if heading.custom_anchor.is_none() && !heading.auto_anchor.is_empty() {
407                    let anchor_key = heading.auto_anchor.to_lowercase();
408                    vulnerable.entry(anchor_key).or_default().push(VulnerableAnchor {
409                        file: file_path.clone(),
410                        line: heading.line,
411                        text: heading.text.clone(),
412                    });
413                }
414            }
415        }
416
417        vulnerable
418    }
419
420    /// Get all headings across the workspace (for debugging/testing)
421    pub fn all_headings(&self) -> impl Iterator<Item = (&Path, &HeadingIndex)> {
422        self.files
423            .iter()
424            .flat_map(|(path, index)| index.headings.iter().map(move |h| (path.as_path(), h)))
425    }
426
427    /// Iterate over all files in the index
428    pub fn files(&self) -> impl Iterator<Item = (&Path, &FileIndex)> {
429        self.files.iter().map(|(p, i)| (p.as_path(), i))
430    }
431
432    /// Clear the entire index
433    pub fn clear(&mut self) {
434        self.files.clear();
435        self.reverse_deps.clear();
436        self.version = self.version.wrapping_add(1);
437    }
438
439    /// Update a file's index and maintain reverse dependencies
440    ///
441    /// This method:
442    /// 1. Removes this file as a source (dependent) from all reverse deps
443    /// 2. Inserts the new file index
444    /// 3. Builds new reverse deps from cross_file_links
445    pub fn update_file(&mut self, path: &Path, index: FileIndex) {
446        // Remove this file as a source (dependent) from all target entries
447        // Note: We don't remove it as a target - other files may still link to it
448        self.clear_reverse_deps_as_source(path);
449
450        // Build new reverse deps from cross_file_links
451        for link in &index.cross_file_links {
452            let target = self.resolve_target_path(path, &link.target_path);
453            self.reverse_deps.entry(target).or_default().insert(path.to_path_buf());
454        }
455
456        self.files.insert(path.to_path_buf(), index);
457        self.version = self.version.wrapping_add(1);
458    }
459
460    /// Get files that depend on (link to) the given file
461    ///
462    /// Returns a list of file paths that contain links targeting this file.
463    /// Used to re-lint dependent files when a target file changes.
464    pub fn get_dependents(&self, path: &Path) -> Vec<PathBuf> {
465        self.reverse_deps
466            .get(path)
467            .map(|set| set.iter().cloned().collect())
468            .unwrap_or_default()
469    }
470
471    /// Check if a file needs re-indexing based on its content hash
472    ///
473    /// Returns `true` if the file is not in the index or has a different hash.
474    pub fn is_file_stale(&self, path: &Path, current_hash: &str) -> bool {
475        self.files
476            .get(path)
477            .map(|f| f.content_hash != current_hash)
478            .unwrap_or(true)
479    }
480
481    /// Retain only files that exist in the given set, removing deleted files
482    ///
483    /// This prunes stale entries from the cache for files that no longer exist.
484    /// Returns the number of files removed.
485    pub fn retain_only(&mut self, current_files: &std::collections::HashSet<PathBuf>) -> usize {
486        let before_count = self.files.len();
487
488        // Collect files to remove
489        let to_remove: Vec<PathBuf> = self
490            .files
491            .keys()
492            .filter(|path| !current_files.contains(*path))
493            .cloned()
494            .collect();
495
496        // Remove each file properly (clears reverse deps)
497        for path in &to_remove {
498            self.remove_file(path);
499        }
500
501        before_count - self.files.len()
502    }
503
504    /// Save the workspace index to a cache file
505    ///
506    /// Uses postcard for efficient binary serialization with:
507    /// - Magic header for file type validation
508    /// - Format version for compatibility detection
509    /// - Atomic writes (temp file + rename) to prevent corruption
510    #[cfg(feature = "native")]
511    pub fn save_to_cache(&self, cache_dir: &Path) -> std::io::Result<()> {
512        use std::fs;
513        use std::io::Write;
514
515        // Ensure cache directory exists
516        fs::create_dir_all(cache_dir)?;
517
518        // Serialize the index data using postcard
519        let encoded = postcard::to_allocvec(self)
520            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()))?;
521
522        // Build versioned cache file: [magic][version][data]
523        let mut cache_data = Vec::with_capacity(8 + encoded.len());
524        cache_data.extend_from_slice(CACHE_MAGIC);
525        cache_data.extend_from_slice(&CACHE_FORMAT_VERSION.to_le_bytes());
526        cache_data.extend_from_slice(&encoded);
527
528        // Write atomically: write to temp file then rename
529        let final_path = cache_dir.join(CACHE_FILE_NAME);
530        let temp_path = cache_dir.join(format!("{}.tmp.{}", CACHE_FILE_NAME, std::process::id()));
531
532        // Write to temp file
533        {
534            let mut file = fs::File::create(&temp_path)?;
535            file.write_all(&cache_data)?;
536            file.sync_all()?;
537        }
538
539        // Atomic rename
540        fs::rename(&temp_path, &final_path)?;
541
542        log::debug!(
543            "Saved workspace index to cache: {} files, {} bytes (format v{})",
544            self.files.len(),
545            cache_data.len(),
546            CACHE_FORMAT_VERSION
547        );
548
549        Ok(())
550    }
551
552    /// Load the workspace index from a cache file
553    ///
554    /// Returns `None` if:
555    /// - Cache file doesn't exist
556    /// - Magic header doesn't match
557    /// - Format version is incompatible
558    /// - Data is corrupted
559    #[cfg(feature = "native")]
560    pub fn load_from_cache(cache_dir: &Path) -> Option<Self> {
561        use std::fs;
562
563        let path = cache_dir.join(CACHE_FILE_NAME);
564        let data = fs::read(&path).ok()?;
565
566        // Validate header: need at least 8 bytes for magic + version
567        if data.len() < 8 {
568            log::warn!("Workspace index cache too small, discarding");
569            let _ = fs::remove_file(&path);
570            return None;
571        }
572
573        // Check magic header
574        if &data[0..4] != CACHE_MAGIC {
575            log::warn!("Workspace index cache has invalid magic header, discarding");
576            let _ = fs::remove_file(&path);
577            return None;
578        }
579
580        // Check format version
581        let version = u32::from_le_bytes([data[4], data[5], data[6], data[7]]);
582        if version != CACHE_FORMAT_VERSION {
583            log::info!(
584                "Workspace index cache format version mismatch (got {version}, expected {CACHE_FORMAT_VERSION}), rebuilding"
585            );
586            let _ = fs::remove_file(&path);
587            return None;
588        }
589
590        // Deserialize the index data using postcard
591        match postcard::from_bytes::<Self>(&data[8..]) {
592            Ok(index) => {
593                log::debug!(
594                    "Loaded workspace index from cache: {} files (format v{})",
595                    index.files.len(),
596                    version
597                );
598                Some(index)
599            }
600            Err(e) => {
601                log::warn!("Failed to deserialize workspace index cache: {e}");
602                let _ = fs::remove_file(&path);
603                None
604            }
605        }
606    }
607
608    /// Remove a file as a source from all reverse dependency entries
609    ///
610    /// This removes the file from being listed as a dependent in all target entries.
611    /// Used when updating a file (we need to remove old outgoing links before adding new ones).
612    fn clear_reverse_deps_as_source(&mut self, path: &Path) {
613        for deps in self.reverse_deps.values_mut() {
614            deps.remove(path);
615        }
616        // Clean up empty entries
617        self.reverse_deps.retain(|_, deps| !deps.is_empty());
618    }
619
620    /// Remove a file completely from reverse dependency tracking
621    ///
622    /// Removes the file as both a source (dependent) and as a target.
623    /// Used when deleting a file from the index.
624    fn clear_reverse_deps_for(&mut self, path: &Path) {
625        // Remove as source (dependent)
626        self.clear_reverse_deps_as_source(path);
627
628        // Also remove as target
629        self.reverse_deps.remove(path);
630    }
631
632    /// Resolve a relative path from a source file to an absolute target path
633    fn resolve_target_path(&self, source_file: &Path, relative_target: &str) -> PathBuf {
634        // Get the directory containing the source file
635        let source_dir = source_file.parent().unwrap_or(Path::new(""));
636
637        // Join with the relative target and normalize
638        let target = source_dir.join(relative_target);
639
640        // Normalize the path (handle .., ., etc.)
641        Self::normalize_path(&target)
642    }
643
644    /// Normalize a path by resolving . and .. components
645    fn normalize_path(path: &Path) -> PathBuf {
646        let mut components = Vec::new();
647
648        for component in path.components() {
649            match component {
650                std::path::Component::ParentDir => {
651                    // Go up one level if possible
652                    if !components.is_empty() {
653                        components.pop();
654                    }
655                }
656                std::path::Component::CurDir => {
657                    // Skip current directory markers
658                }
659                _ => {
660                    components.push(component);
661                }
662            }
663        }
664
665        components.iter().collect()
666    }
667}
668
669impl FileIndex {
670    /// Create a new empty file index
671    pub fn new() -> Self {
672        Self::default()
673    }
674
675    /// Create a file index with the given content hash
676    pub fn with_hash(content_hash: String) -> Self {
677        Self {
678            content_hash,
679            ..Default::default()
680        }
681    }
682
683    /// Add a heading to the index
684    ///
685    /// Also updates the anchor lookup map for O(1) anchor queries
686    pub fn add_heading(&mut self, heading: HeadingIndex) {
687        let index = self.headings.len();
688
689        // Add auto-generated anchor to lookup map (lowercased for case-insensitive matching)
690        self.anchor_to_heading.insert(heading.auto_anchor.to_lowercase(), index);
691
692        // Add custom anchor if present
693        if let Some(ref custom) = heading.custom_anchor {
694            self.anchor_to_heading.insert(custom.to_lowercase(), index);
695        }
696
697        self.headings.push(heading);
698    }
699
700    /// Add an alternative anchor that resolves to an existing heading.
701    /// Used for platform-specific anchor conventions (e.g., Python-Markdown `_N` dedup).
702    pub fn add_anchor_alias(&mut self, anchor: String, heading_index: usize) {
703        if heading_index < self.headings.len() {
704            self.anchor_to_heading.insert(anchor.to_lowercase(), heading_index);
705        }
706    }
707
708    /// Check if an anchor exists in this file (O(1) lookup)
709    ///
710    /// Returns true if the anchor matches any of:
711    /// - Auto-generated heading anchors
712    /// - Custom heading anchors (from {#id} syntax on headings)
713    /// - HTML anchors (from `<a id="...">` or `<element id="...">`)
714    /// - Attribute anchors (from { #id } syntax on non-heading elements)
715    ///
716    /// Matching is case-insensitive. URL-encoded anchors (e.g., CJK characters
717    /// like `%E6%97%A5%E6%9C%AC%E8%AA%9E` for `日本語`) are decoded before matching.
718    pub fn has_anchor(&self, anchor: &str) -> bool {
719        let lower = anchor.to_lowercase();
720
721        // Fast path: try exact match first
722        if self.anchor_to_heading.contains_key(&lower)
723            || self.html_anchors.contains(&lower)
724            || self.attribute_anchors.contains(&lower)
725        {
726            return true;
727        }
728
729        // Slow path: if anchor contains percent-encoding, try decoded version
730        if anchor.contains('%') {
731            let decoded = url_decode(anchor).to_lowercase();
732            if decoded != lower {
733                return self.anchor_to_heading.contains_key(&decoded)
734                    || self.html_anchors.contains(&decoded)
735                    || self.attribute_anchors.contains(&decoded);
736            }
737        }
738
739        false
740    }
741
742    /// Add an HTML anchor (from `<a id="...">` or `<element id="...">` tags)
743    pub fn add_html_anchor(&mut self, anchor: String) {
744        if !anchor.is_empty() {
745            self.html_anchors.insert(anchor.to_lowercase());
746        }
747    }
748
749    /// Add an attribute anchor (from { #id } syntax on non-heading elements)
750    pub fn add_attribute_anchor(&mut self, anchor: String) {
751        if !anchor.is_empty() {
752            self.attribute_anchors.insert(anchor.to_lowercase());
753        }
754    }
755
756    /// Get the heading index for an anchor (O(1) lookup)
757    ///
758    /// Returns the index into `self.headings` if found.
759    pub fn get_heading_by_anchor(&self, anchor: &str) -> Option<&HeadingIndex> {
760        self.anchor_to_heading
761            .get(&anchor.to_lowercase())
762            .and_then(|&idx| self.headings.get(idx))
763    }
764
765    /// Add a reference link to the index
766    pub fn add_reference_link(&mut self, link: ReferenceLinkIndex) {
767        self.reference_links.push(link);
768    }
769
770    /// Check if a rule is disabled at a specific line
771    ///
772    /// Used by cross-file rules to respect inline disable directives.
773    /// Checks both file-wide disables and line-specific disables.
774    pub fn is_rule_disabled_at_line(&self, rule_name: &str, line: usize) -> bool {
775        // Check file-wide disables (highest priority)
776        if self.file_disabled_rules.contains("*") || self.file_disabled_rules.contains(rule_name) {
777            return true;
778        }
779
780        // Check line-specific disables (disable-line / disable-next-line)
781        if let Some(rules) = self.line_disabled_rules.get(&line)
782            && (rules.contains("*") || rules.contains(rule_name))
783        {
784            return true;
785        }
786
787        // Check persistent disable/enable transitions via binary search
788        if !self.persistent_transitions.is_empty() {
789            let idx = match self.persistent_transitions.binary_search_by_key(&line, |t| t.0) {
790                Ok(i) => Some(i),
791                Err(i) => {
792                    if i > 0 {
793                        Some(i - 1)
794                    } else {
795                        None
796                    }
797                }
798            };
799            if let Some(i) = idx {
800                let (_, ref disabled, ref enabled) = self.persistent_transitions[i];
801                if disabled.contains("*") {
802                    return !enabled.contains(rule_name);
803                }
804                return disabled.contains(rule_name);
805            }
806        }
807
808        false
809    }
810
811    /// Add a cross-file link to the index (deduplicates by target_path, fragment, line)
812    pub fn add_cross_file_link(&mut self, link: CrossFileLinkIndex) {
813        // Deduplicate: multiple rules may contribute the same link with different columns
814        // (e.g., MD051 uses link start, MD057 uses URL start)
815        let is_duplicate = self.cross_file_links.iter().any(|existing| {
816            existing.target_path == link.target_path && existing.fragment == link.fragment && existing.line == link.line
817        });
818        if !is_duplicate {
819            self.cross_file_links.push(link);
820        }
821    }
822
823    /// Add a defined reference ID (e.g., from `[ref]: url`)
824    pub fn add_defined_reference(&mut self, ref_id: String) {
825        self.defined_references.insert(ref_id);
826    }
827
828    /// Check if a reference ID has an explicit definition
829    pub fn has_defined_reference(&self, ref_id: &str) -> bool {
830        self.defined_references.contains(ref_id)
831    }
832
833    /// Check if the content hash matches
834    pub fn hash_matches(&self, hash: &str) -> bool {
835        self.content_hash == hash
836    }
837
838    /// Get the number of headings
839    pub fn heading_count(&self) -> usize {
840        self.headings.len()
841    }
842
843    /// Get the number of reference links
844    pub fn reference_link_count(&self) -> usize {
845        self.reference_links.len()
846    }
847}
848
849#[cfg(test)]
850mod tests {
851    use super::*;
852
853    #[test]
854    fn test_workspace_index_basic() {
855        let mut index = WorkspaceIndex::new();
856        assert_eq!(index.file_count(), 0);
857        assert_eq!(index.version(), 0);
858
859        let mut file_index = FileIndex::with_hash("abc123".to_string());
860        file_index.add_heading(HeadingIndex {
861            text: "Installation".to_string(),
862            auto_anchor: "installation".to_string(),
863            custom_anchor: None,
864            line: 1,
865        });
866
867        index.insert_file(PathBuf::from("docs/install.md"), file_index);
868        assert_eq!(index.file_count(), 1);
869        assert_eq!(index.version(), 1);
870
871        assert!(index.contains_file(Path::new("docs/install.md")));
872        assert!(!index.contains_file(Path::new("docs/other.md")));
873    }
874
875    #[test]
876    fn test_vulnerable_anchors() {
877        let mut index = WorkspaceIndex::new();
878
879        // File 1: heading without custom anchor (vulnerable)
880        let mut file1 = FileIndex::new();
881        file1.add_heading(HeadingIndex {
882            text: "Getting Started".to_string(),
883            auto_anchor: "getting-started".to_string(),
884            custom_anchor: None,
885            line: 1,
886        });
887        index.insert_file(PathBuf::from("docs/guide.md"), file1);
888
889        // File 2: heading with custom anchor (not vulnerable)
890        let mut file2 = FileIndex::new();
891        file2.add_heading(HeadingIndex {
892            text: "Installation".to_string(),
893            auto_anchor: "installation".to_string(),
894            custom_anchor: Some("install".to_string()),
895            line: 1,
896        });
897        index.insert_file(PathBuf::from("docs/install.md"), file2);
898
899        let vulnerable = index.get_vulnerable_anchors();
900        assert_eq!(vulnerable.len(), 1);
901        assert!(vulnerable.contains_key("getting-started"));
902        assert!(!vulnerable.contains_key("installation"));
903
904        let anchors = vulnerable.get("getting-started").unwrap();
905        assert_eq!(anchors.len(), 1);
906        assert_eq!(anchors[0].file, PathBuf::from("docs/guide.md"));
907        assert_eq!(anchors[0].text, "Getting Started");
908    }
909
910    #[test]
911    fn test_vulnerable_anchors_multiple_files_same_anchor() {
912        // Multiple files can have headings with the same auto-generated anchor
913        // get_vulnerable_anchors() should collect all of them
914        let mut index = WorkspaceIndex::new();
915
916        // File 1: has "Installation" heading (vulnerable)
917        let mut file1 = FileIndex::new();
918        file1.add_heading(HeadingIndex {
919            text: "Installation".to_string(),
920            auto_anchor: "installation".to_string(),
921            custom_anchor: None,
922            line: 1,
923        });
924        index.insert_file(PathBuf::from("docs/en/guide.md"), file1);
925
926        // File 2: also has "Installation" heading with same anchor (vulnerable)
927        let mut file2 = FileIndex::new();
928        file2.add_heading(HeadingIndex {
929            text: "Installation".to_string(),
930            auto_anchor: "installation".to_string(),
931            custom_anchor: None,
932            line: 5,
933        });
934        index.insert_file(PathBuf::from("docs/fr/guide.md"), file2);
935
936        // File 3: has "Installation" but WITH custom anchor (not vulnerable)
937        let mut file3 = FileIndex::new();
938        file3.add_heading(HeadingIndex {
939            text: "Installation".to_string(),
940            auto_anchor: "installation".to_string(),
941            custom_anchor: Some("install".to_string()),
942            line: 10,
943        });
944        index.insert_file(PathBuf::from("docs/de/guide.md"), file3);
945
946        let vulnerable = index.get_vulnerable_anchors();
947        assert_eq!(vulnerable.len(), 1); // One unique anchor
948        assert!(vulnerable.contains_key("installation"));
949
950        let anchors = vulnerable.get("installation").unwrap();
951        // Should have 2 entries (en and fr), NOT 3 (de has custom anchor)
952        assert_eq!(anchors.len(), 2, "Should collect both vulnerable anchors");
953
954        // Verify both files are represented
955        let files: std::collections::HashSet<_> = anchors.iter().map(|a| &a.file).collect();
956        assert!(files.contains(&PathBuf::from("docs/en/guide.md")));
957        assert!(files.contains(&PathBuf::from("docs/fr/guide.md")));
958    }
959
960    #[test]
961    fn test_file_index_hash() {
962        let index = FileIndex::with_hash("hash123".to_string());
963        assert!(index.hash_matches("hash123"));
964        assert!(!index.hash_matches("other"));
965    }
966
967    #[test]
968    fn test_version_increment() {
969        let mut index = WorkspaceIndex::new();
970        assert_eq!(index.version(), 0);
971
972        index.insert_file(PathBuf::from("a.md"), FileIndex::new());
973        assert_eq!(index.version(), 1);
974
975        index.insert_file(PathBuf::from("b.md"), FileIndex::new());
976        assert_eq!(index.version(), 2);
977
978        index.remove_file(Path::new("a.md"));
979        assert_eq!(index.version(), 3);
980
981        // Removing non-existent file doesn't increment
982        index.remove_file(Path::new("nonexistent.md"));
983        assert_eq!(index.version(), 3);
984    }
985
986    #[test]
987    fn test_reverse_deps_basic() {
988        let mut index = WorkspaceIndex::new();
989
990        // File A links to file B
991        let mut file_a = FileIndex::new();
992        file_a.add_cross_file_link(CrossFileLinkIndex {
993            target_path: "b.md".to_string(),
994            fragment: "section".to_string(),
995            line: 10,
996            column: 5,
997        });
998        index.update_file(Path::new("docs/a.md"), file_a);
999
1000        // Check that B has A as a dependent
1001        let dependents = index.get_dependents(Path::new("docs/b.md"));
1002        assert_eq!(dependents.len(), 1);
1003        assert_eq!(dependents[0], PathBuf::from("docs/a.md"));
1004
1005        // A has no dependents
1006        let a_dependents = index.get_dependents(Path::new("docs/a.md"));
1007        assert!(a_dependents.is_empty());
1008    }
1009
1010    #[test]
1011    fn test_reverse_deps_multiple() {
1012        let mut index = WorkspaceIndex::new();
1013
1014        // Files A and C both link to B
1015        let mut file_a = FileIndex::new();
1016        file_a.add_cross_file_link(CrossFileLinkIndex {
1017            target_path: "../b.md".to_string(),
1018            fragment: "".to_string(),
1019            line: 1,
1020            column: 1,
1021        });
1022        index.update_file(Path::new("docs/sub/a.md"), file_a);
1023
1024        let mut file_c = FileIndex::new();
1025        file_c.add_cross_file_link(CrossFileLinkIndex {
1026            target_path: "b.md".to_string(),
1027            fragment: "".to_string(),
1028            line: 1,
1029            column: 1,
1030        });
1031        index.update_file(Path::new("docs/c.md"), file_c);
1032
1033        // B should have both A and C as dependents
1034        let dependents = index.get_dependents(Path::new("docs/b.md"));
1035        assert_eq!(dependents.len(), 2);
1036        assert!(dependents.contains(&PathBuf::from("docs/sub/a.md")));
1037        assert!(dependents.contains(&PathBuf::from("docs/c.md")));
1038    }
1039
1040    #[test]
1041    fn test_reverse_deps_update_clears_old() {
1042        let mut index = WorkspaceIndex::new();
1043
1044        // File A initially links to B
1045        let mut file_a = FileIndex::new();
1046        file_a.add_cross_file_link(CrossFileLinkIndex {
1047            target_path: "b.md".to_string(),
1048            fragment: "".to_string(),
1049            line: 1,
1050            column: 1,
1051        });
1052        index.update_file(Path::new("docs/a.md"), file_a);
1053
1054        // Verify B has A as dependent
1055        assert_eq!(index.get_dependents(Path::new("docs/b.md")).len(), 1);
1056
1057        // Update A to link to C instead of B
1058        let mut file_a_updated = FileIndex::new();
1059        file_a_updated.add_cross_file_link(CrossFileLinkIndex {
1060            target_path: "c.md".to_string(),
1061            fragment: "".to_string(),
1062            line: 1,
1063            column: 1,
1064        });
1065        index.update_file(Path::new("docs/a.md"), file_a_updated);
1066
1067        // B should no longer have A as dependent
1068        assert!(index.get_dependents(Path::new("docs/b.md")).is_empty());
1069
1070        // C should now have A as dependent
1071        let c_deps = index.get_dependents(Path::new("docs/c.md"));
1072        assert_eq!(c_deps.len(), 1);
1073        assert_eq!(c_deps[0], PathBuf::from("docs/a.md"));
1074    }
1075
1076    #[test]
1077    fn test_reverse_deps_remove_file() {
1078        let mut index = WorkspaceIndex::new();
1079
1080        // File A links to B
1081        let mut file_a = FileIndex::new();
1082        file_a.add_cross_file_link(CrossFileLinkIndex {
1083            target_path: "b.md".to_string(),
1084            fragment: "".to_string(),
1085            line: 1,
1086            column: 1,
1087        });
1088        index.update_file(Path::new("docs/a.md"), file_a);
1089
1090        // Verify B has A as dependent
1091        assert_eq!(index.get_dependents(Path::new("docs/b.md")).len(), 1);
1092
1093        // Remove file A
1094        index.remove_file(Path::new("docs/a.md"));
1095
1096        // B should no longer have any dependents
1097        assert!(index.get_dependents(Path::new("docs/b.md")).is_empty());
1098    }
1099
1100    #[test]
1101    fn test_normalize_path() {
1102        // Test .. handling
1103        let path = Path::new("docs/sub/../other.md");
1104        let normalized = WorkspaceIndex::normalize_path(path);
1105        assert_eq!(normalized, PathBuf::from("docs/other.md"));
1106
1107        // Test . handling
1108        let path2 = Path::new("docs/./other.md");
1109        let normalized2 = WorkspaceIndex::normalize_path(path2);
1110        assert_eq!(normalized2, PathBuf::from("docs/other.md"));
1111
1112        // Test multiple ..
1113        let path3 = Path::new("a/b/c/../../d.md");
1114        let normalized3 = WorkspaceIndex::normalize_path(path3);
1115        assert_eq!(normalized3, PathBuf::from("a/d.md"));
1116    }
1117
1118    #[test]
1119    fn test_clear_clears_reverse_deps() {
1120        let mut index = WorkspaceIndex::new();
1121
1122        // File A links to B
1123        let mut file_a = FileIndex::new();
1124        file_a.add_cross_file_link(CrossFileLinkIndex {
1125            target_path: "b.md".to_string(),
1126            fragment: "".to_string(),
1127            line: 1,
1128            column: 1,
1129        });
1130        index.update_file(Path::new("docs/a.md"), file_a);
1131
1132        // Verify B has A as dependent
1133        assert_eq!(index.get_dependents(Path::new("docs/b.md")).len(), 1);
1134
1135        // Clear the index
1136        index.clear();
1137
1138        // Both files and reverse deps should be cleared
1139        assert_eq!(index.file_count(), 0);
1140        assert!(index.get_dependents(Path::new("docs/b.md")).is_empty());
1141    }
1142
1143    #[test]
1144    fn test_is_file_stale() {
1145        let mut index = WorkspaceIndex::new();
1146
1147        // Non-existent file is always stale
1148        assert!(index.is_file_stale(Path::new("nonexistent.md"), "hash123"));
1149
1150        // Add a file with known hash
1151        let file_index = FileIndex::with_hash("hash123".to_string());
1152        index.insert_file(PathBuf::from("docs/test.md"), file_index);
1153
1154        // Same hash means not stale
1155        assert!(!index.is_file_stale(Path::new("docs/test.md"), "hash123"));
1156
1157        // Different hash means stale
1158        assert!(index.is_file_stale(Path::new("docs/test.md"), "different_hash"));
1159    }
1160
1161    #[cfg(feature = "native")]
1162    #[test]
1163    fn test_cache_roundtrip() {
1164        use std::fs;
1165
1166        // Create a temp directory
1167        let temp_dir = std::env::temp_dir().join("rumdl_test_cache_roundtrip");
1168        let _ = fs::remove_dir_all(&temp_dir);
1169        fs::create_dir_all(&temp_dir).unwrap();
1170
1171        // Create an index with some data
1172        let mut index = WorkspaceIndex::new();
1173
1174        let mut file1 = FileIndex::with_hash("abc123".to_string());
1175        file1.add_heading(HeadingIndex {
1176            text: "Test Heading".to_string(),
1177            auto_anchor: "test-heading".to_string(),
1178            custom_anchor: Some("test".to_string()),
1179            line: 1,
1180        });
1181        file1.add_cross_file_link(CrossFileLinkIndex {
1182            target_path: "./other.md".to_string(),
1183            fragment: "section".to_string(),
1184            line: 5,
1185            column: 3,
1186        });
1187        index.update_file(Path::new("docs/file1.md"), file1);
1188
1189        let mut file2 = FileIndex::with_hash("def456".to_string());
1190        file2.add_heading(HeadingIndex {
1191            text: "Another Heading".to_string(),
1192            auto_anchor: "another-heading".to_string(),
1193            custom_anchor: None,
1194            line: 1,
1195        });
1196        index.update_file(Path::new("docs/other.md"), file2);
1197
1198        // Save to cache
1199        index.save_to_cache(&temp_dir).expect("Failed to save cache");
1200
1201        // Verify cache file exists
1202        assert!(temp_dir.join("workspace_index.bin").exists());
1203
1204        // Load from cache
1205        let loaded = WorkspaceIndex::load_from_cache(&temp_dir).expect("Failed to load cache");
1206
1207        // Verify data matches
1208        assert_eq!(loaded.file_count(), 2);
1209        assert!(loaded.contains_file(Path::new("docs/file1.md")));
1210        assert!(loaded.contains_file(Path::new("docs/other.md")));
1211
1212        // Check file1 details
1213        let file1_loaded = loaded.get_file(Path::new("docs/file1.md")).unwrap();
1214        assert_eq!(file1_loaded.content_hash, "abc123");
1215        assert_eq!(file1_loaded.headings.len(), 1);
1216        assert_eq!(file1_loaded.headings[0].text, "Test Heading");
1217        assert_eq!(file1_loaded.headings[0].custom_anchor, Some("test".to_string()));
1218        assert_eq!(file1_loaded.cross_file_links.len(), 1);
1219        assert_eq!(file1_loaded.cross_file_links[0].target_path, "./other.md");
1220
1221        // Check reverse deps were serialized correctly
1222        let dependents = loaded.get_dependents(Path::new("docs/other.md"));
1223        assert_eq!(dependents.len(), 1);
1224        assert_eq!(dependents[0], PathBuf::from("docs/file1.md"));
1225
1226        // Clean up
1227        let _ = fs::remove_dir_all(&temp_dir);
1228    }
1229
1230    #[cfg(feature = "native")]
1231    #[test]
1232    fn test_cache_missing_file() {
1233        let temp_dir = std::env::temp_dir().join("rumdl_test_cache_missing");
1234        let _ = std::fs::remove_dir_all(&temp_dir);
1235
1236        // Should return None for non-existent cache
1237        let result = WorkspaceIndex::load_from_cache(&temp_dir);
1238        assert!(result.is_none());
1239    }
1240
1241    #[cfg(feature = "native")]
1242    #[test]
1243    fn test_cache_corrupted_file() {
1244        use std::fs;
1245
1246        let temp_dir = std::env::temp_dir().join("rumdl_test_cache_corrupted");
1247        let _ = fs::remove_dir_all(&temp_dir);
1248        fs::create_dir_all(&temp_dir).unwrap();
1249
1250        // Write corrupted data (too small for header)
1251        fs::write(temp_dir.join("workspace_index.bin"), b"bad").unwrap();
1252
1253        // Should return None for corrupted cache (and remove the file)
1254        let result = WorkspaceIndex::load_from_cache(&temp_dir);
1255        assert!(result.is_none());
1256
1257        // Corrupted file should be removed
1258        assert!(!temp_dir.join("workspace_index.bin").exists());
1259
1260        // Clean up
1261        let _ = fs::remove_dir_all(&temp_dir);
1262    }
1263
1264    #[cfg(feature = "native")]
1265    #[test]
1266    fn test_cache_invalid_magic() {
1267        use std::fs;
1268
1269        let temp_dir = std::env::temp_dir().join("rumdl_test_cache_invalid_magic");
1270        let _ = fs::remove_dir_all(&temp_dir);
1271        fs::create_dir_all(&temp_dir).unwrap();
1272
1273        // Write data with wrong magic header
1274        let mut data = Vec::new();
1275        data.extend_from_slice(b"XXXX"); // Wrong magic
1276        data.extend_from_slice(&1u32.to_le_bytes()); // Version 1
1277        data.extend_from_slice(&[0; 100]); // Some garbage data
1278        fs::write(temp_dir.join("workspace_index.bin"), &data).unwrap();
1279
1280        // Should return None for invalid magic
1281        let result = WorkspaceIndex::load_from_cache(&temp_dir);
1282        assert!(result.is_none());
1283
1284        // File should be removed
1285        assert!(!temp_dir.join("workspace_index.bin").exists());
1286
1287        // Clean up
1288        let _ = fs::remove_dir_all(&temp_dir);
1289    }
1290
1291    #[cfg(feature = "native")]
1292    #[test]
1293    fn test_cache_version_mismatch() {
1294        use std::fs;
1295
1296        let temp_dir = std::env::temp_dir().join("rumdl_test_cache_version_mismatch");
1297        let _ = fs::remove_dir_all(&temp_dir);
1298        fs::create_dir_all(&temp_dir).unwrap();
1299
1300        // Write data with correct magic but wrong version
1301        let mut data = Vec::new();
1302        data.extend_from_slice(b"RWSI"); // Correct magic
1303        data.extend_from_slice(&999u32.to_le_bytes()); // Future version
1304        data.extend_from_slice(&[0; 100]); // Some garbage data
1305        fs::write(temp_dir.join("workspace_index.bin"), &data).unwrap();
1306
1307        // Should return None for version mismatch
1308        let result = WorkspaceIndex::load_from_cache(&temp_dir);
1309        assert!(result.is_none());
1310
1311        // File should be removed to trigger rebuild
1312        assert!(!temp_dir.join("workspace_index.bin").exists());
1313
1314        // Clean up
1315        let _ = fs::remove_dir_all(&temp_dir);
1316    }
1317
1318    #[cfg(feature = "native")]
1319    #[test]
1320    fn test_cache_atomic_write() {
1321        use std::fs;
1322
1323        // Test that atomic writes work (no temp files left behind)
1324        let temp_dir = std::env::temp_dir().join("rumdl_test_cache_atomic");
1325        let _ = fs::remove_dir_all(&temp_dir);
1326        fs::create_dir_all(&temp_dir).unwrap();
1327
1328        let index = WorkspaceIndex::new();
1329        index.save_to_cache(&temp_dir).expect("Failed to save");
1330
1331        // Only the final cache file should exist, no temp files
1332        let entries: Vec<_> = fs::read_dir(&temp_dir).unwrap().collect();
1333        assert_eq!(entries.len(), 1);
1334        assert!(temp_dir.join("workspace_index.bin").exists());
1335
1336        // Clean up
1337        let _ = fs::remove_dir_all(&temp_dir);
1338    }
1339
1340    #[test]
1341    fn test_has_anchor_auto_generated() {
1342        let mut file_index = FileIndex::new();
1343        file_index.add_heading(HeadingIndex {
1344            text: "Installation Guide".to_string(),
1345            auto_anchor: "installation-guide".to_string(),
1346            custom_anchor: None,
1347            line: 1,
1348        });
1349
1350        // Should find by auto-generated anchor
1351        assert!(file_index.has_anchor("installation-guide"));
1352
1353        // Case-insensitive matching
1354        assert!(file_index.has_anchor("Installation-Guide"));
1355        assert!(file_index.has_anchor("INSTALLATION-GUIDE"));
1356
1357        // Should not find non-existent anchor
1358        assert!(!file_index.has_anchor("nonexistent"));
1359    }
1360
1361    #[test]
1362    fn test_has_anchor_custom() {
1363        let mut file_index = FileIndex::new();
1364        file_index.add_heading(HeadingIndex {
1365            text: "Installation Guide".to_string(),
1366            auto_anchor: "installation-guide".to_string(),
1367            custom_anchor: Some("install".to_string()),
1368            line: 1,
1369        });
1370
1371        // Should find by auto-generated anchor
1372        assert!(file_index.has_anchor("installation-guide"));
1373
1374        // Should also find by custom anchor
1375        assert!(file_index.has_anchor("install"));
1376        assert!(file_index.has_anchor("Install")); // case-insensitive
1377
1378        // Should not find non-existent anchor
1379        assert!(!file_index.has_anchor("nonexistent"));
1380    }
1381
1382    #[test]
1383    fn test_get_heading_by_anchor() {
1384        let mut file_index = FileIndex::new();
1385        file_index.add_heading(HeadingIndex {
1386            text: "Installation Guide".to_string(),
1387            auto_anchor: "installation-guide".to_string(),
1388            custom_anchor: Some("install".to_string()),
1389            line: 10,
1390        });
1391        file_index.add_heading(HeadingIndex {
1392            text: "Configuration".to_string(),
1393            auto_anchor: "configuration".to_string(),
1394            custom_anchor: None,
1395            line: 20,
1396        });
1397
1398        // Get by auto anchor
1399        let heading = file_index.get_heading_by_anchor("installation-guide");
1400        assert!(heading.is_some());
1401        assert_eq!(heading.unwrap().text, "Installation Guide");
1402        assert_eq!(heading.unwrap().line, 10);
1403
1404        // Get by custom anchor
1405        let heading = file_index.get_heading_by_anchor("install");
1406        assert!(heading.is_some());
1407        assert_eq!(heading.unwrap().text, "Installation Guide");
1408
1409        // Get second heading
1410        let heading = file_index.get_heading_by_anchor("configuration");
1411        assert!(heading.is_some());
1412        assert_eq!(heading.unwrap().text, "Configuration");
1413        assert_eq!(heading.unwrap().line, 20);
1414
1415        // Non-existent
1416        assert!(file_index.get_heading_by_anchor("nonexistent").is_none());
1417    }
1418
1419    #[test]
1420    fn test_anchor_lookup_many_headings() {
1421        // Test that O(1) lookup works with many headings
1422        let mut file_index = FileIndex::new();
1423
1424        // Add 100 headings
1425        for i in 0..100 {
1426            file_index.add_heading(HeadingIndex {
1427                text: format!("Heading {i}"),
1428                auto_anchor: format!("heading-{i}"),
1429                custom_anchor: Some(format!("h{i}")),
1430                line: i + 1,
1431            });
1432        }
1433
1434        // Verify all can be found
1435        for i in 0..100 {
1436            assert!(file_index.has_anchor(&format!("heading-{i}")));
1437            assert!(file_index.has_anchor(&format!("h{i}")));
1438
1439            let heading = file_index.get_heading_by_anchor(&format!("heading-{i}"));
1440            assert!(heading.is_some());
1441            assert_eq!(heading.unwrap().line, i + 1);
1442        }
1443    }
1444
1445    // =============================================================================
1446    // Tests for extract_cross_file_links utility
1447    // =============================================================================
1448
1449    #[test]
1450    fn test_extract_cross_file_links_basic() {
1451        use crate::config::MarkdownFlavor;
1452
1453        let content = "# Test\n\nSee [link](./other.md) for info.\n";
1454        let ctx = LintContext::new(content, MarkdownFlavor::default(), None);
1455        let links = extract_cross_file_links(&ctx);
1456
1457        assert_eq!(links.len(), 1);
1458        assert_eq!(links[0].target_path, "./other.md");
1459        assert_eq!(links[0].fragment, "");
1460        assert_eq!(links[0].line, 3);
1461        // "See [link](" = 11 chars, so column 12 is where "./other.md" starts
1462        assert_eq!(links[0].column, 12);
1463    }
1464
1465    #[test]
1466    fn test_extract_cross_file_links_with_fragment() {
1467        use crate::config::MarkdownFlavor;
1468
1469        let content = "Check [guide](./guide.md#install) here.\n";
1470        let ctx = LintContext::new(content, MarkdownFlavor::default(), None);
1471        let links = extract_cross_file_links(&ctx);
1472
1473        assert_eq!(links.len(), 1);
1474        assert_eq!(links[0].target_path, "./guide.md");
1475        assert_eq!(links[0].fragment, "install");
1476        assert_eq!(links[0].line, 1);
1477        // "Check [guide](" = 14 chars, so column 15 is where "./guide.md" starts
1478        assert_eq!(links[0].column, 15);
1479    }
1480
1481    #[test]
1482    fn test_extract_cross_file_links_multiple_on_same_line() {
1483        use crate::config::MarkdownFlavor;
1484
1485        let content = "See [a](a.md) and [b](b.md) here.\n";
1486        let ctx = LintContext::new(content, MarkdownFlavor::default(), None);
1487        let links = extract_cross_file_links(&ctx);
1488
1489        assert_eq!(links.len(), 2);
1490
1491        assert_eq!(links[0].target_path, "a.md");
1492        assert_eq!(links[0].line, 1);
1493        // "See [a](" = 8 chars, so column 9
1494        assert_eq!(links[0].column, 9);
1495
1496        assert_eq!(links[1].target_path, "b.md");
1497        assert_eq!(links[1].line, 1);
1498        // "See [a](a.md) and [b](" = 22 chars, so column 23
1499        assert_eq!(links[1].column, 23);
1500    }
1501
1502    #[test]
1503    fn test_extract_cross_file_links_angle_brackets() {
1504        use crate::config::MarkdownFlavor;
1505
1506        let content = "See [link](<path/with (parens).md>) here.\n";
1507        let ctx = LintContext::new(content, MarkdownFlavor::default(), None);
1508        let links = extract_cross_file_links(&ctx);
1509
1510        assert_eq!(links.len(), 1);
1511        assert_eq!(links[0].target_path, "path/with (parens).md");
1512        assert_eq!(links[0].line, 1);
1513        // "See [link](<" = 12 chars, so column 13
1514        assert_eq!(links[0].column, 13);
1515    }
1516
1517    #[test]
1518    fn test_extract_cross_file_links_skips_external() {
1519        use crate::config::MarkdownFlavor;
1520
1521        let content = r#"
1522[external](https://example.com)
1523[mailto](mailto:test@example.com)
1524[local](./local.md)
1525[fragment](#section)
1526[absolute](/docs/page.md)
1527"#;
1528        let ctx = LintContext::new(content, MarkdownFlavor::default(), None);
1529        let links = extract_cross_file_links(&ctx);
1530
1531        // Only the local markdown link should be extracted
1532        assert_eq!(links.len(), 1);
1533        assert_eq!(links[0].target_path, "./local.md");
1534    }
1535
1536    #[test]
1537    fn test_extract_cross_file_links_skips_non_markdown() {
1538        use crate::config::MarkdownFlavor;
1539
1540        let content = r#"
1541[image](./photo.png)
1542[doc](./readme.md)
1543[pdf](./document.pdf)
1544"#;
1545        let ctx = LintContext::new(content, MarkdownFlavor::default(), None);
1546        let links = extract_cross_file_links(&ctx);
1547
1548        // Only markdown files are indexed for cross-file validation
1549        assert_eq!(links.len(), 1);
1550        assert_eq!(links[0].target_path, "./readme.md");
1551    }
1552
1553    #[test]
1554    fn test_extract_cross_file_links_skips_code_spans() {
1555        use crate::config::MarkdownFlavor;
1556
1557        let content = "Normal [link](./file.md) and `[code](./ignored.md)` here.\n";
1558        let ctx = LintContext::new(content, MarkdownFlavor::default(), None);
1559        let links = extract_cross_file_links(&ctx);
1560
1561        // Only the link outside code span should be extracted
1562        assert_eq!(links.len(), 1);
1563        assert_eq!(links[0].target_path, "./file.md");
1564    }
1565
1566    #[test]
1567    fn test_extract_cross_file_links_with_query_params() {
1568        use crate::config::MarkdownFlavor;
1569
1570        let content = "See [doc](./file.md?raw=true) here.\n";
1571        let ctx = LintContext::new(content, MarkdownFlavor::default(), None);
1572        let links = extract_cross_file_links(&ctx);
1573
1574        assert_eq!(links.len(), 1);
1575        // Query params should be stripped
1576        assert_eq!(links[0].target_path, "./file.md");
1577    }
1578
1579    #[test]
1580    fn test_extract_cross_file_links_empty_content() {
1581        use crate::config::MarkdownFlavor;
1582
1583        let content = "";
1584        let ctx = LintContext::new(content, MarkdownFlavor::default(), None);
1585        let links = extract_cross_file_links(&ctx);
1586
1587        assert!(links.is_empty());
1588    }
1589
1590    #[test]
1591    fn test_extract_cross_file_links_no_links() {
1592        use crate::config::MarkdownFlavor;
1593
1594        let content = "# Just a heading\n\nSome text without links.\n";
1595        let ctx = LintContext::new(content, MarkdownFlavor::default(), None);
1596        let links = extract_cross_file_links(&ctx);
1597
1598        assert!(links.is_empty());
1599    }
1600
1601    #[test]
1602    fn test_extract_cross_file_links_position_accuracy_issue_234() {
1603        // This test verifies the fix for GitHub issue #234
1604        // The LSP was reporting incorrect column positions for MD057 diagnostics
1605        use crate::config::MarkdownFlavor;
1606
1607        let content = r#"# Test Document
1608
1609Here is a [broken link](nonexistent-file.md) that should trigger MD057.
1610
1611And another [link](also-missing.md) on this line.
1612"#;
1613        let ctx = LintContext::new(content, MarkdownFlavor::default(), None);
1614        let links = extract_cross_file_links(&ctx);
1615
1616        assert_eq!(links.len(), 2);
1617
1618        // First link: "Here is a [broken link](" = 24 chars, column 25
1619        assert_eq!(links[0].target_path, "nonexistent-file.md");
1620        assert_eq!(links[0].line, 3);
1621        assert_eq!(links[0].column, 25);
1622
1623        // Second link: "And another [link](" = 19 chars, column 20
1624        assert_eq!(links[1].target_path, "also-missing.md");
1625        assert_eq!(links[1].line, 5);
1626        assert_eq!(links[1].column, 20);
1627    }
1628}