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::range_utils::byte_to_char_count;
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
98pub(crate) static 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/// Check if a link-target path has a markdown extension (case-insensitive)
102#[inline]
103fn is_markdown_file(path: &str) -> bool {
104    crate::discovery::has_markdown_extension(std::path::Path::new(path))
105}
106
107/// Strip query parameters and fragments from a URL path
108/// Returns the path portion before `?` or `#`
109fn strip_query_and_fragment(url: &str) -> &str {
110    let query_pos = url.find('?');
111    let fragment_pos = url.find('#');
112
113    match (query_pos, fragment_pos) {
114        (Some(q), Some(f)) => &url[..q.min(f)],
115        (Some(q), None) => &url[..q],
116        (None, Some(f)) => &url[..f],
117        (None, None) => url,
118    }
119}
120
121/// The file a directory-relative link names, resolved against the directory
122/// holding the document that wrote it.
123///
124/// The index keeps a destination as the document spelled it, because that is the
125/// text an edit to the link has to be measured against, and a spelling can carry
126/// a query string. A query is not part of a file name - no file is ever called
127/// `b.md?raw=true` - so it is stripped here. Every consumer asking which file a
128/// link points at goes through this, so the index's own keys and the answers
129/// navigation gives cannot disagree.
130pub fn link_target_file(source_dir: &Path, target_path: &str) -> PathBuf {
131    normalize_relative_path(&source_dir.join(strip_query_and_fragment(target_path)))
132}
133
134/// Markdown file links extracted from a document, split by how they resolve.
135///
136/// Linting rules only understand `relative` links (resolved against the source
137/// file's directory). `root_relative` links (leading `/`) are an LSP concept
138/// resolved against the configured content roots, so they are kept separate to
139/// avoid changing linting behavior.
140#[derive(Debug, Default)]
141pub struct ExtractedCrossFileLinks {
142    /// Links resolved relative to the source file's directory.
143    pub relative: Vec<CrossFileLinkIndex>,
144    /// Root-relative links. `target_path` has the leading `/` stripped so it can
145    /// be joined directly to a content root. Parent-traversal and
146    /// protocol-relative (`//host`) links are excluded.
147    pub root_relative: Vec<CrossFileLinkIndex>,
148}
149
150/// Extract cross-file links from content using correct regex-based position tracking.
151///
152/// This is the canonical implementation used by both MD057 and LSP to ensure
153/// consistent and correct column positions for diagnostic reporting.
154///
155/// Returns one `CrossFileLinkIndex` per markdown file link, split into directory
156/// relative links and root-relative links (see `ExtractedCrossFileLinks`).
157pub fn extract_cross_file_links(ctx: &LintContext) -> ExtractedCrossFileLinks {
158    let content = ctx.content;
159
160    // Early returns for performance
161    if content.is_empty() || !content.contains("](") {
162        return ExtractedCrossFileLinks::default();
163    }
164
165    let mut links = ExtractedCrossFileLinks::default();
166    let lines: Vec<&str> = content.lines().collect();
167
168    // Track which lines we've already processed to avoid duplicates
169    // (ctx.links() may have multiple entries for the same line)
170    let mut processed_lines = HashSet::new();
171
172    for link in ctx.links() {
173        let line_idx = link.line - 1;
174        if line_idx >= lines.len() {
175            continue;
176        }
177
178        // Skip if we've already processed this line
179        if !processed_lines.insert(line_idx) {
180            continue;
181        }
182
183        let line = lines[line_idx];
184        if !line.contains("](") {
185            continue;
186        }
187
188        // Find all links in this line
189        for link_match in LINK_START_REGEX.find_iter(line) {
190            let start_pos = link_match.start();
191            let end_pos = link_match.end();
192
193            // Calculate absolute position for code span detection
194            let line_start_byte = ctx.line_start_byte(line_idx + 1).unwrap_or(0);
195            let absolute_start_pos = line_start_byte + start_pos;
196
197            // Skip if in code span
198            if ctx.is_in_code_span_byte(absolute_start_pos) {
199                continue;
200            }
201
202            // Extract the URL (group 1) and fragment (group 2)
203            // Try angle-bracket regex first (handles URLs with parens)
204            let caps_result = URL_EXTRACT_ANGLE_BRACKET_REGEX
205                .captures_at(line, end_pos - 1)
206                .or_else(|| URL_EXTRACT_REGEX.captures_at(line, end_pos - 1));
207
208            if let Some(caps) = caps_result
209                && let Some(url_group) = caps.get(1)
210            {
211                let file_path = url_group.as_str().trim();
212
213                // Root-relative links (leading `/`) resolve against content roots
214                // in the LSP, not the source directory, so they are captured in a
215                // separate bucket. Protocol-relative (`//host`) and parent-traversal
216                // links are excluded so they cannot escape a content root.
217                if let Some(rel) = file_path.strip_prefix('/') {
218                    if !rel.starts_with('/')
219                        && !Path::new(rel)
220                            .components()
221                            .any(|c| matches!(c, std::path::Component::ParentDir))
222                    {
223                        let stripped = strip_query_and_fragment(rel);
224                        if is_markdown_file(stripped) {
225                            let fragment = caps.get(2).map_or("", |m| m.as_str().trim_start_matches('#'));
226                            links.root_relative.push(CrossFileLinkIndex {
227                                target_path: stripped.to_string(),
228                                fragment: fragment.to_string(),
229                                line: link.line,
230                                column: byte_to_char_count(line, url_group.start()),
231                                origin: LinkOrigin::Body,
232                            });
233                        }
234                    }
235                    continue;
236                }
237
238                // Skip empty, external, template variables, framework aliases,
239                // fragment-only URLs, or rustdoc intra-doc links
240                if file_path.is_empty()
241                    || PROTOCOL_DOMAIN_REGEX.is_match(file_path)
242                    || file_path.starts_with("www.")
243                    || file_path.starts_with('#')
244                    || file_path.starts_with("{{")
245                    || file_path.starts_with("{%")
246                    || file_path.starts_with('~')
247                    || file_path.starts_with('@')
248                    || (file_path.starts_with('`') && file_path.ends_with('`'))
249                {
250                    continue;
251                }
252
253                // Strip query parameters before indexing
254                let file_path = strip_query_and_fragment(file_path);
255
256                // Get fragment from capture group 2 (includes # prefix)
257                let fragment = caps.get(2).map_or("", |m| m.as_str().trim_start_matches('#'));
258
259                // Only index markdown file links for cross-file validation
260                if is_markdown_file(file_path) {
261                    links.relative.push(CrossFileLinkIndex {
262                        target_path: file_path.to_string(),
263                        fragment: fragment.to_string(),
264                        line: link.line,
265                        column: byte_to_char_count(line, url_group.start()),
266                        origin: LinkOrigin::Body,
267                    });
268                }
269            }
270        }
271    }
272
273    links
274}
275
276/// Magic bytes identifying a workspace index cache file
277#[cfg(feature = "postcard")]
278const CACHE_MAGIC: &[u8; 4] = b"RWSI";
279
280/// Cache format version - increment when WorkspaceIndex serialization changes
281/// or when the meaning of persisted fields changes such that older caches are
282/// no longer correct. Version 9 adds `CrossFileLinkIndex::origin`; postcard is
283/// not self-describing, so a version 8 cache would decode the following field's
284/// bytes as the new one and yield nonsense.
285///
286/// Version 10 changes what `cross_file_links` holds rather than how it is laid
287/// out: one link is now one entry however each rule spells the destination. The
288/// bytes still decode, so nothing here would notice, and a cached index is
289/// reused whole when a file's content is unchanged - a version 9 cache would
290/// keep reporting the duplicate this version exists to stop.
291///
292/// Version 11 is the same shape of change: a file's entry no longer depends on
293/// its own `per-file-ignores`, so an entry written before it can be missing the
294/// headings an ignored rule would have recorded. Content is what decides reuse,
295/// and the content did not change, so without this the fixed build would keep
296/// serving the false positive from the cache the old one left behind.
297///
298/// Version 12 adds the path inputs MD057 needs to validate a cached lint result.
299/// An older entry cannot distinguish a document with no paths from one whose
300/// path data was never recorded.
301#[cfg(feature = "postcard")]
302const CACHE_FORMAT_VERSION: u32 = 12;
303
304/// Cache file name within the version directory
305#[cfg(feature = "postcard")]
306const CACHE_FILE_NAME: &str = "workspace_index.bin";
307
308/// Monotonic counter making cache temp-file names unique per write, mirroring
309/// `cache.rs`. Combined with the process id (where available) it keeps
310/// concurrent writers from colliding on the temp path before the atomic rename.
311#[cfg(feature = "postcard")]
312static CACHE_TEMP_COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
313
314/// Workspace-wide index for cross-file analysis
315///
316/// Contains pre-extracted information from all markdown files in the workspace,
317/// enabling rules to validate cross-file references efficiently.
318#[derive(Debug, Default, Clone, Serialize, Deserialize)]
319pub struct WorkspaceIndex {
320    /// Map from file path to its extracted data
321    files: HashMap<PathBuf, FileIndex>,
322    /// Reverse dependency graph: target file → files that link to it
323    /// Used to efficiently re-lint dependent files when a target changes
324    reverse_deps: HashMap<PathBuf, HashSet<PathBuf>>,
325    /// Version counter for cache invalidation (incremented on any change)
326    version: u64,
327}
328
329/// Index data extracted from a single file
330#[derive(Debug, Clone, Default, Serialize, Deserialize)]
331pub struct FileIndex {
332    /// Headings in this file with their anchors
333    pub headings: Vec<HeadingIndex>,
334    /// Reference links in this file (for cross-file analysis)
335    pub reference_links: Vec<ReferenceLinkIndex>,
336    /// Cross-file links in this file (for MD051 cross-file validation)
337    pub cross_file_links: Vec<CrossFileLinkIndex>,
338    /// Root-relative links (leading `/`) in this file. Resolved against the
339    /// configured content roots by the LSP for go-to-definition, hover, and
340    /// find-references. `target_path` has the leading `/` stripped. Linting does
341    /// not use these, so they never affect diagnostics.
342    #[serde(default)]
343    pub root_relative_links: Vec<CrossFileLinkIndex>,
344    /// Filesystem destinations MD057 may validate for this file.
345    ///
346    /// These inputs stay unresolved so the rule can apply the current search
347    /// paths, absolute-link policy, and per-file flavor during cache lookup.
348    #[serde(default)]
349    pub md057_link_targets: Vec<Md057LinkTarget>,
350    /// Defined reference IDs (e.g., from `[ref]: url` definitions)
351    /// Used to filter out reference links that have explicit definitions
352    pub defined_references: HashSet<String>,
353    /// Content hash for change detection
354    pub content_hash: String,
355    /// O(1) anchor lookup: lowercased anchor → heading index
356    /// Includes both auto-generated and custom anchors
357    anchor_to_heading: HashMap<String, usize>,
358    /// O(1) anchor lookup with original case preserved → heading index.
359    /// Used for `ignore_case = false` (markdownlint strict parity). Skipped at
360    /// query time when the lowercase map is sufficient.
361    #[serde(default)]
362    anchor_to_heading_exact: HashMap<String, usize>,
363    /// HTML anchors defined via `<a id="...">` or `<element id="...">` tags.
364    /// Stored lowercase for case-insensitive matching.
365    html_anchors: HashSet<String>,
366    /// HTML anchors with original case preserved.
367    /// Used for `ignore_case = false` (markdownlint strict parity).
368    #[serde(default)]
369    html_anchors_exact: HashSet<String>,
370    /// Attribute anchors defined via { #id } syntax (kramdown/MkDocs attr_list).
371    /// Can appear on any element, not just headings.
372    /// Stored lowercase for case-insensitive matching.
373    attribute_anchors: HashSet<String>,
374    /// Attribute anchors with original case preserved.
375    /// Used for `ignore_case = false` (markdownlint strict parity).
376    #[serde(default)]
377    attribute_anchors_exact: HashSet<String>,
378    /// Rules disabled for the entire file (from inline comments)
379    /// Used by cross-file rules to respect inline disable directives
380    pub file_disabled_rules: HashSet<String>,
381    /// Persistent disable/enable state transitions, sorted by line number.
382    /// Each entry: (line, disabled_rules, enabled_rules). Use binary search to query.
383    pub persistent_transitions: Vec<(usize, HashSet<String>, HashSet<String>)>,
384    /// Rules disabled at specific lines via disable-line / disable-next-line
385    pub line_disabled_rules: HashMap<usize, HashSet<String>>,
386}
387
388/// Information about a heading for cross-file lookup
389#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
390pub struct HeadingIndex {
391    /// The heading text (e.g., "Installation Guide")
392    pub text: String,
393    /// Auto-generated anchor (e.g., "installation-guide")
394    pub auto_anchor: String,
395    /// Custom anchor if present (e.g., "install")
396    pub custom_anchor: Option<String>,
397    /// Line number (1-indexed)
398    pub line: usize,
399    /// Whether this is a Setext-style heading (underlined with = or -)
400    #[serde(default)]
401    pub is_setext: bool,
402}
403
404/// Information about a reference link for cross-file analysis
405#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
406pub struct ReferenceLinkIndex {
407    /// The reference ID (the part in `[text][ref]`)
408    pub reference_id: String,
409    /// Line number (1-indexed)
410    pub line: usize,
411    /// Column number (1-indexed)
412    pub column: usize,
413}
414
415/// Where a cross-file link was written.
416///
417/// The index holds one entry per file while configuration resolves per file, so
418/// it records where a link came from rather than whether some configuration
419/// wanted it, and the rule reading it applies its own settings at check time.
420/// That is the only arrangement that can be right when two files resolving
421/// different configurations reference the same target.
422#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
423pub enum LinkOrigin {
424    /// The document body.
425    Body,
426    /// A frontmatter value that reads as a path, such as `link: other.md#a`.
427    /// `field` is the lowercased top-level key owning the value, or `None` where
428    /// no owner is determinable. The two are kept distinct from `Body` so a
429    /// value with no determinable owner is still recognizable as frontmatter.
430    ///
431    /// A Markdown link that happens to be written inside frontmatter
432    /// (`link: [a](other.md#a)`) is `Body`: it is real Markdown link syntax, so
433    /// the extraction that finds body links finds it and rename can rewrite it.
434    FrontMatter { field: Option<String> },
435}
436
437/// Information about a cross-file link for validation
438#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
439pub struct CrossFileLinkIndex {
440    /// The target file path (relative, as it appears in the link)
441    pub target_path: String,
442    /// The fragment/anchor being linked to (without #)
443    pub fragment: String,
444    /// Line number (1-indexed)
445    pub line: usize,
446    /// Column number (1-indexed)
447    pub column: usize,
448    /// Where in the document the link was written.
449    pub origin: LinkOrigin,
450}
451
452/// A path input whose filesystem state can affect MD057's per-file verdict.
453#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
454pub struct Md057LinkTarget {
455    /// The destination as MD057 passes it to its resolver.
456    pub target: String,
457    /// Whether the destination came from Markdown syntax or frontmatter.
458    pub origin: LinkOrigin,
459}
460
461/// Every path a cross-file link target can name, in the order to try them.
462///
463/// The target is resolved against the directory holding the link and normalized.
464/// A target carrying no extension is then tried against each extension discovery
465/// treats as Markdown, so a GitHub-style `[x](page#section)` finds `page.md`. A
466/// query string is not part of a file name, so `other.md?raw=true` names
467/// `other.md`.
468///
469/// This is the single answer to "which file does this link mean", shared by the
470/// workspace index lookup and by any caller that has to read the target itself.
471pub fn link_target_candidates(source_file: &Path, target_path: &str) -> Vec<PathBuf> {
472    let target_path = strip_query_and_fragment(target_path);
473
474    let joined = match source_file.parent() {
475        Some(parent) => parent.join(target_path),
476        None => PathBuf::from(target_path),
477    };
478    let base = normalize_relative_path(&joined);
479
480    if base.extension().is_some() {
481        return vec![base];
482    }
483
484    // The exact path first: an extension-less file can be indexed under the name
485    // as written.
486    let mut candidates = Vec::with_capacity(crate::discovery::MARKDOWN_EXTENSIONS.len() + 1);
487    for ext in crate::discovery::MARKDOWN_EXTENSIONS {
488        candidates.push(base.with_extension(ext));
489    }
490    candidates.insert(0, base);
491    candidates
492}
493
494/// Resolve `.` and `..` components without touching the filesystem.
495///
496/// This is how [`link_target_candidates`] spells the paths it returns, so a caller
497/// that has to recognize one of those candidates spells its own path the same way.
498///
499/// A `..` cancels the name before it, and nothing else. A relative path with no
500/// name left to cancel keeps saying `..`, because dropping it would rename the
501/// file: `../notes.md` read from `a.md` would become `notes.md`, a different file
502/// that may well exist. Above a root there is nothing to name, so `/..` is `/`,
503/// which is what the filesystem itself answers.
504pub fn normalize_relative_path(path: &Path) -> PathBuf {
505    let mut components: Vec<std::path::Component<'_>> = Vec::new();
506    for component in path.components() {
507        match component {
508            std::path::Component::CurDir => {}
509            std::path::Component::ParentDir => match components.last() {
510                Some(std::path::Component::Normal(_)) => {
511                    components.pop();
512                }
513                Some(std::path::Component::RootDir) => {}
514                _ => components.push(component),
515            },
516            c => components.push(c),
517        }
518    }
519    components.iter().collect()
520}
521
522impl CrossFileLinkIndex {
523    /// Whether the editor navigates by this link.
524    ///
525    /// Frontmatter values are excluded. They are indexed so MD051 can validate
526    /// them when asked to, but they are not Markdown links: rename computes its
527    /// edits by re-parsing the line as one, so it cannot rewrite a frontmatter
528    /// value, and listing it as a reference would offer a location that a
529    /// subsequent rename silently leaves stale.
530    pub fn is_navigable(&self) -> bool {
531        matches!(self.origin, LinkOrigin::Body)
532    }
533}
534
535/// Information about a vulnerable anchor (heading without custom ID)
536#[derive(Debug, Clone, Serialize, Deserialize)]
537pub struct VulnerableAnchor {
538    /// File path where the heading is located
539    pub file: PathBuf,
540    /// Line number of the heading
541    pub line: usize,
542    /// The heading text
543    pub text: String,
544}
545
546impl WorkspaceIndex {
547    /// Create a new empty workspace index
548    pub fn new() -> Self {
549        Self::default()
550    }
551
552    /// Get the current version (for cache invalidation)
553    pub fn version(&self) -> u64 {
554        self.version
555    }
556
557    /// Get the number of indexed files
558    pub fn file_count(&self) -> usize {
559        self.files.len()
560    }
561
562    /// Check if a file is in the index
563    pub fn contains_file(&self, path: &Path) -> bool {
564        self.files.contains_key(path)
565    }
566
567    /// Get the index data for a specific file
568    pub fn get_file(&self, path: &Path) -> Option<&FileIndex> {
569        self.files.get(path)
570    }
571
572    /// Insert or update a file's index data
573    pub fn insert_file(&mut self, path: PathBuf, index: FileIndex) {
574        self.files.insert(path, index);
575        self.version = self.version.wrapping_add(1);
576    }
577
578    /// Remove a file from the index
579    pub fn remove_file(&mut self, path: &Path) -> Option<FileIndex> {
580        // Clean up reverse deps for this file
581        self.clear_reverse_deps_for(path);
582
583        let result = self.files.remove(path);
584        if result.is_some() {
585            self.version = self.version.wrapping_add(1);
586        }
587        result
588    }
589
590    /// Build a map of all "vulnerable" anchors across the workspace
591    ///
592    /// A vulnerable anchor is an auto-generated anchor for a heading that
593    /// does NOT have a custom anchor defined. These are problematic for
594    /// translated content because the anchor changes when the heading is translated.
595    ///
596    /// Returns: Map from lowercase anchor → Vec of VulnerableAnchor info
597    /// Multiple files can have headings with the same auto-generated anchor,
598    /// so we collect all occurrences.
599    pub fn get_vulnerable_anchors(&self) -> HashMap<String, Vec<VulnerableAnchor>> {
600        let mut vulnerable: HashMap<String, Vec<VulnerableAnchor>> = HashMap::new();
601
602        for (file_path, file_index) in &self.files {
603            for heading in &file_index.headings {
604                // Only include headings WITHOUT custom anchors
605                if heading.custom_anchor.is_none() && !heading.auto_anchor.is_empty() {
606                    let anchor_key = heading.auto_anchor.to_lowercase();
607                    vulnerable.entry(anchor_key).or_default().push(VulnerableAnchor {
608                        file: file_path.clone(),
609                        line: heading.line,
610                        text: heading.text.clone(),
611                    });
612                }
613            }
614        }
615
616        vulnerable
617    }
618
619    /// Get all headings across the workspace (for debugging/testing)
620    pub fn all_headings(&self) -> impl Iterator<Item = (&Path, &HeadingIndex)> {
621        self.files
622            .iter()
623            .flat_map(|(path, index)| index.headings.iter().map(move |h| (path.as_path(), h)))
624    }
625
626    /// Iterate over all files in the index
627    pub fn files(&self) -> impl Iterator<Item = (&Path, &FileIndex)> {
628        self.files.iter().map(|(p, i)| (p.as_path(), i))
629    }
630
631    /// All files in the index, ordered by path.
632    ///
633    /// `files()` iterates the backing `HashMap` in an unspecified order, so
634    /// consumers that emit output (cross-file diagnostics) must use this to
635    /// keep results stable across runs on identical input.
636    pub fn files_sorted(&self) -> Vec<(&Path, &FileIndex)> {
637        let mut entries: Vec<(&Path, &FileIndex)> = self.files.iter().map(|(p, i)| (p.as_path(), i)).collect();
638        entries.sort_by_key(|(a, _)| *a);
639        entries
640    }
641
642    /// Clear the entire index
643    pub fn clear(&mut self) {
644        self.files.clear();
645        self.reverse_deps.clear();
646        self.version = self.version.wrapping_add(1);
647    }
648
649    /// Update a file's index and maintain reverse dependencies
650    ///
651    /// This method:
652    /// 1. Removes this file as a source (dependent) from all reverse deps
653    /// 2. Inserts the new file index
654    /// 3. Builds new reverse deps from cross_file_links
655    pub fn update_file(&mut self, path: &Path, index: FileIndex) {
656        // Remove this file as a source (dependent) from all target entries
657        // Note: We don't remove it as a target - other files may still link to it
658        self.clear_reverse_deps_as_source(path);
659
660        // Build new reverse deps from cross_file_links
661        for link in &index.cross_file_links {
662            let target = self.resolve_target_path(path, &link.target_path);
663            self.reverse_deps.entry(target).or_default().insert(path.to_path_buf());
664        }
665
666        self.files.insert(path.to_path_buf(), index);
667        self.version = self.version.wrapping_add(1);
668    }
669
670    /// Get files that depend on (link to) the given file
671    ///
672    /// Returns a list of file paths that contain links targeting this file.
673    /// Used to re-lint dependent files when a target file changes.
674    pub fn get_dependents(&self, path: &Path) -> Vec<PathBuf> {
675        self.reverse_deps
676            .get(path)
677            .map(|set| set.iter().cloned().collect())
678            .unwrap_or_default()
679    }
680
681    /// Check if a file needs re-indexing based on its content hash
682    ///
683    /// Returns `true` if the file is not in the index or has a different hash.
684    pub fn is_file_stale(&self, path: &Path, current_hash: &str) -> bool {
685        self.files.get(path).is_none_or(|f| f.content_hash != current_hash)
686    }
687
688    /// Retain only files that exist in the given set, removing deleted files
689    ///
690    /// This prunes stale entries from the cache for files that no longer exist.
691    /// Returns the number of files removed.
692    pub fn retain_only(&mut self, current_files: &std::collections::HashSet<PathBuf>) -> usize {
693        let before_count = self.files.len();
694
695        // Collect files to remove
696        let to_remove: Vec<PathBuf> = self
697            .files
698            .keys()
699            .filter(|path| !current_files.contains(*path))
700            .cloned()
701            .collect();
702
703        // Remove each file properly (clears reverse deps)
704        for path in &to_remove {
705            self.remove_file(path);
706        }
707
708        before_count - self.files.len()
709    }
710
711    /// Save the workspace index to a cache file
712    ///
713    /// Uses postcard for efficient binary serialization with:
714    /// - Magic header for file type validation
715    /// - Format version for compatibility detection
716    /// - Atomic writes (temp file + rename) to prevent corruption
717    #[cfg(feature = "postcard")]
718    pub fn save_to_cache(&self, cache_dir: &Path) -> std::io::Result<()> {
719        use std::fs;
720        use std::io::Write;
721
722        // Ensure cache directory exists
723        fs::create_dir_all(cache_dir)?;
724
725        // Serialize the index data using postcard
726        let encoded = postcard::to_allocvec(self)
727            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()))?;
728
729        // Build versioned cache file: [magic][version][data]
730        let mut cache_data = Vec::with_capacity(8 + encoded.len());
731        cache_data.extend_from_slice(CACHE_MAGIC);
732        cache_data.extend_from_slice(&CACHE_FORMAT_VERSION.to_le_bytes());
733        cache_data.extend_from_slice(&encoded);
734
735        // Write atomically: write to temp file then rename. A per-write unique
736        // suffix keeps concurrent writers from clobbering each other's temp file
737        // before the rename. WASI has no process id, so the counter alone carries
738        // uniqueness there (matching `cache.rs`).
739        let final_path = cache_dir.join(CACHE_FILE_NAME);
740        let counter = CACHE_TEMP_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
741        #[cfg(not(target_arch = "wasm32"))]
742        let temp_path = cache_dir.join(format!("{CACHE_FILE_NAME}.tmp.{}.{counter}", std::process::id()));
743        #[cfg(target_arch = "wasm32")]
744        let temp_path = cache_dir.join(format!("{CACHE_FILE_NAME}.tmp.{counter}"));
745
746        // Write to temp file
747        {
748            let mut file = fs::File::create(&temp_path)?;
749            file.write_all(&cache_data)?;
750            file.sync_all()?;
751        }
752
753        // Atomic rename
754        fs::rename(&temp_path, &final_path)?;
755
756        log::debug!(
757            "Saved workspace index to cache: {} files, {} bytes (format v{})",
758            self.files.len(),
759            cache_data.len(),
760            CACHE_FORMAT_VERSION
761        );
762
763        Ok(())
764    }
765
766    /// Load the workspace index from a cache file
767    ///
768    /// Returns `None` if:
769    /// - Cache file doesn't exist
770    /// - Magic header doesn't match
771    /// - Format version is incompatible
772    /// - Data is corrupted
773    #[cfg(feature = "postcard")]
774    pub fn load_from_cache(cache_dir: &Path) -> Option<Self> {
775        use std::fs;
776
777        let path = cache_dir.join(CACHE_FILE_NAME);
778        let data = fs::read(&path).ok()?;
779
780        // Validate header: need at least 8 bytes for magic + version
781        if data.len() < 8 {
782            log::warn!("Workspace index cache too small, discarding");
783            let _ = fs::remove_file(&path);
784            return None;
785        }
786
787        // Check magic header
788        if &data[0..4] != CACHE_MAGIC {
789            log::warn!("Workspace index cache has invalid magic header, discarding");
790            let _ = fs::remove_file(&path);
791            return None;
792        }
793
794        // Check format version
795        let version = u32::from_le_bytes([data[4], data[5], data[6], data[7]]);
796        if version != CACHE_FORMAT_VERSION {
797            log::info!(
798                "Workspace index cache format version mismatch (got {version}, expected {CACHE_FORMAT_VERSION}), rebuilding"
799            );
800            let _ = fs::remove_file(&path);
801            return None;
802        }
803
804        // Deserialize the index data using postcard
805        match postcard::from_bytes::<Self>(&data[8..]) {
806            Ok(index) => {
807                log::debug!(
808                    "Loaded workspace index from cache: {} files (format v{})",
809                    index.files.len(),
810                    version
811                );
812                Some(index)
813            }
814            Err(e) => {
815                log::warn!("Failed to deserialize workspace index cache: {e}");
816                let _ = fs::remove_file(&path);
817                None
818            }
819        }
820    }
821
822    /// Remove a file as a source from all reverse dependency entries
823    ///
824    /// This removes the file from being listed as a dependent in all target entries.
825    /// Used when updating a file (we need to remove old outgoing links before adding new ones).
826    fn clear_reverse_deps_as_source(&mut self, path: &Path) {
827        // Remove `path` as a dependent only from the targets it actually links
828        // to, discovered from its current index, instead of scanning every
829        // entry in reverse_deps. Both callers (update_file, clear_reverse_deps_for)
830        // run this before the file's entry is replaced/removed, so self.files[path]
831        // still holds the links that produced these reverse-dep entries; resolving
832        // them the same way reverses exactly the prior insertions.
833        let targets: Vec<PathBuf> = match self.files.get(path) {
834            Some(index) => index
835                .cross_file_links
836                .iter()
837                .map(|link| self.resolve_target_path(path, &link.target_path))
838                .collect(),
839            None => return,
840        };
841        for target in targets {
842            if let Some(deps) = self.reverse_deps.get_mut(&target) {
843                deps.remove(path);
844                if deps.is_empty() {
845                    self.reverse_deps.remove(&target);
846                }
847            }
848        }
849    }
850
851    /// Remove a file completely from reverse dependency tracking
852    ///
853    /// Removes the file as both a source (dependent) and as a target.
854    /// Used when deleting a file from the index.
855    fn clear_reverse_deps_for(&mut self, path: &Path) {
856        // Remove as source (dependent)
857        self.clear_reverse_deps_as_source(path);
858
859        // Also remove as target
860        self.reverse_deps.remove(path);
861    }
862
863    /// Resolve a relative path from a source file to an absolute target path
864    ///
865    /// This keys the reverse dependency graph, which is looked up by the path of
866    /// a file that changed, so it has to answer with a file name - the same
867    /// question [`link_target_file`] answers for every other consumer.
868    fn resolve_target_path(&self, source_file: &Path, relative_target: &str) -> PathBuf {
869        let source_dir = source_file.parent().unwrap_or(Path::new(""));
870        link_target_file(source_dir, relative_target)
871    }
872}
873
874impl FileIndex {
875    /// Create a new empty file index
876    pub fn new() -> Self {
877        Self::default()
878    }
879
880    /// Create a file index with the given content hash
881    pub fn with_hash(content_hash: String) -> Self {
882        Self {
883            content_hash,
884            ..Default::default()
885        }
886    }
887
888    /// Whether the data extracted from the file differs, ignoring the hash of
889    /// the text it came from.
890    ///
891    /// This answers whether re-indexing a file can have changed a cross-file
892    /// result, its own or that of a file linking to it. Every cross-file check
893    /// reads the entry rather than the document, so two entries that agree on
894    /// everything compared here produce the same answers however much the text
895    /// between them changed. The hash is excluded precisely because it is a
896    /// fingerprint of that text: it moves on every keystroke while the links
897    /// and anchors a check reads stay put.
898    ///
899    /// Entries carry the positions their headings and links were written at, so
900    /// an edit that adds or removes a line does differ even when it changes no
901    /// anchor. That is the conservative direction: it costs a re-lint of the
902    /// open files linking here, and never withholds one.
903    pub fn extracted_data_differs(&self, other: &Self) -> bool {
904        // Destructured so that adding a field to the index is a compile error
905        // here rather than a silent omission, which would make a real change
906        // look like no change and leave a stale diagnostic on screen.
907        let Self {
908            headings,
909            reference_links,
910            cross_file_links,
911            root_relative_links,
912            md057_link_targets,
913            defined_references,
914            content_hash: _,
915            anchor_to_heading,
916            anchor_to_heading_exact,
917            html_anchors,
918            html_anchors_exact,
919            attribute_anchors,
920            attribute_anchors_exact,
921            file_disabled_rules,
922            persistent_transitions,
923            line_disabled_rules,
924        } = self;
925
926        headings != &other.headings
927            || reference_links != &other.reference_links
928            || cross_file_links != &other.cross_file_links
929            || root_relative_links != &other.root_relative_links
930            || md057_link_targets != &other.md057_link_targets
931            || defined_references != &other.defined_references
932            || anchor_to_heading != &other.anchor_to_heading
933            || anchor_to_heading_exact != &other.anchor_to_heading_exact
934            || html_anchors != &other.html_anchors
935            || html_anchors_exact != &other.html_anchors_exact
936            || attribute_anchors != &other.attribute_anchors
937            || attribute_anchors_exact != &other.attribute_anchors_exact
938            || file_disabled_rules != &other.file_disabled_rules
939            || persistent_transitions != &other.persistent_transitions
940            || line_disabled_rules != &other.line_disabled_rules
941    }
942
943    /// Add a heading to the index
944    ///
945    /// Also updates the anchor lookup maps for O(1) anchor queries. Both
946    /// lowercased (for `ignore_case = true`) and case-preserving (for
947    /// `ignore_case = false`) maps are populated.
948    pub fn add_heading(&mut self, heading: HeadingIndex) {
949        let index = self.headings.len();
950
951        // Auto-generated anchor — slugs are already lowercase, but we still
952        // populate both maps so query-time dispatch is uniform.
953        self.anchor_to_heading.insert(heading.auto_anchor.to_lowercase(), index);
954        self.anchor_to_heading_exact.insert(heading.auto_anchor.clone(), index);
955
956        // Custom anchor preserves original case as written by the author.
957        if let Some(ref custom) = heading.custom_anchor {
958            self.anchor_to_heading.insert(custom.to_lowercase(), index);
959            self.anchor_to_heading_exact.insert(custom.clone(), index);
960        }
961
962        self.headings.push(heading);
963    }
964
965    /// Add an alternative anchor that resolves to an existing heading.
966    /// Used for platform-specific anchor conventions (e.g., Python-Markdown `_N` dedup).
967    pub fn add_anchor_alias(&mut self, anchor: &str, heading_index: usize) {
968        if heading_index < self.headings.len() {
969            self.anchor_to_heading.insert(anchor.to_lowercase(), heading_index);
970            self.anchor_to_heading_exact.insert(anchor.to_string(), heading_index);
971        }
972    }
973
974    /// Check if an anchor exists in this file (O(1) lookup)
975    ///
976    /// Returns true if the anchor matches any of:
977    /// - Auto-generated heading anchors
978    /// - Custom heading anchors (from {#id} syntax on headings)
979    /// - HTML anchors (from `<a id="...">` or `<element id="...">`)
980    /// - Attribute anchors (from { #id } syntax on non-heading elements)
981    ///
982    /// Matching is case-insensitive. URL-encoded anchors (e.g., CJK characters
983    /// like `%E6%97%A5%E6%9C%AC%E8%AA%9E` for `日本語`) are decoded before matching.
984    pub fn has_anchor(&self, anchor: &str) -> bool {
985        self.has_anchor_with_case(anchor, true)
986    }
987
988    /// Check if an anchor exists in this file, with explicit case sensitivity.
989    ///
990    /// When `ignore_case` is `true`, behaves identically to [`has_anchor`] —
991    /// inputs are lowercased and matched against the lowercase storage.
992    /// When `false`, the input is compared as-is against parallel
993    /// case-preserving storage, matching markdownlint's strict behavior for
994    /// generated heading slugs, custom heading IDs, HTML anchors, and
995    /// attribute anchors.
996    pub fn has_anchor_with_case(&self, anchor: &str, ignore_case: bool) -> bool {
997        if self.lookup_anchor(anchor, ignore_case) {
998            return true;
999        }
1000
1001        // Slow path: if anchor contains percent-encoding, try decoded version
1002        if anchor.contains('%') {
1003            let decoded = url_decode(anchor);
1004            if decoded != anchor {
1005                return self.lookup_anchor(&decoded, ignore_case);
1006            }
1007        }
1008
1009        false
1010    }
1011
1012    /// Direct anchor lookup, dispatching to the lowercase or exact-case
1013    /// storage based on `ignore_case`.
1014    fn lookup_anchor(&self, anchor: &str, ignore_case: bool) -> bool {
1015        if ignore_case {
1016            let lower = anchor.to_lowercase();
1017            self.anchor_to_heading.contains_key(&lower)
1018                || self.html_anchors.contains(&lower)
1019                || self.attribute_anchors.contains(&lower)
1020        } else {
1021            self.anchor_to_heading_exact.contains_key(anchor)
1022                || self.html_anchors_exact.contains(anchor)
1023                || self.attribute_anchors_exact.contains(anchor)
1024        }
1025    }
1026
1027    /// Add an HTML anchor (from `<a id="...">` or `<element id="...">` tags).
1028    /// Populates both lowercase (case-insensitive) and case-preserving sets.
1029    pub fn add_html_anchor(&mut self, anchor: &str) {
1030        if !anchor.is_empty() {
1031            self.html_anchors.insert(anchor.to_lowercase());
1032            self.html_anchors_exact.insert(anchor.to_string());
1033        }
1034    }
1035
1036    /// Add an attribute anchor (from { #id } syntax on non-heading elements).
1037    /// Populates both lowercase (case-insensitive) and case-preserving sets.
1038    pub fn add_attribute_anchor(&mut self, anchor: &str) {
1039        if !anchor.is_empty() {
1040            self.attribute_anchors.insert(anchor.to_lowercase());
1041            self.attribute_anchors_exact.insert(anchor.to_string());
1042        }
1043    }
1044
1045    /// Get the heading index for an anchor (O(1) lookup)
1046    ///
1047    /// Returns the index into `self.headings` if found.
1048    pub fn get_heading_by_anchor(&self, anchor: &str) -> Option<&HeadingIndex> {
1049        self.anchor_to_heading
1050            .get(&anchor.to_lowercase())
1051            .and_then(|&idx| self.headings.get(idx))
1052    }
1053
1054    /// Add a reference link to the index
1055    pub fn add_reference_link(&mut self, link: ReferenceLinkIndex) {
1056        self.reference_links.push(link);
1057    }
1058
1059    /// Check if a rule is disabled at a specific line
1060    ///
1061    /// Used by cross-file rules to respect inline disable directives.
1062    /// Checks both file-wide disables and line-specific disables.
1063    pub fn is_rule_disabled_at_line(&self, rule_name: &str, line: usize) -> bool {
1064        // Check file-wide disables (highest priority)
1065        if self.file_disabled_rules.contains("*") || self.file_disabled_rules.contains(rule_name) {
1066            return true;
1067        }
1068
1069        // Check line-specific disables (disable-line / disable-next-line)
1070        if let Some(rules) = self.line_disabled_rules.get(&line)
1071            && (rules.contains("*") || rules.contains(rule_name))
1072        {
1073            return true;
1074        }
1075
1076        // Check persistent disable/enable transitions via binary search
1077        if !self.persistent_transitions.is_empty() {
1078            let idx = match self.persistent_transitions.binary_search_by_key(&line, |t| t.0) {
1079                Ok(i) => Some(i),
1080                Err(i) => {
1081                    if i > 0 {
1082                        Some(i - 1)
1083                    } else {
1084                        None
1085                    }
1086                }
1087            };
1088            if let Some(i) = idx {
1089                let (_, ref disabled, ref enabled) = self.persistent_transitions[i];
1090                if disabled.contains("*") {
1091                    return !enabled.contains(rule_name);
1092                }
1093                return disabled.contains(rule_name);
1094            }
1095        }
1096
1097        false
1098    }
1099
1100    /// Add a cross-file link to the index, keyed on the file it names, the
1101    /// fragment it asks for, and the line it sits on.
1102    ///
1103    /// Several rules contribute the same link and spell it differently: MD051
1104    /// records the destination as written and starts at the link, MD057 records
1105    /// the file that destination names and starts at the URL. Neither the string
1106    /// nor the column can identify a link, so the file it names does - comparing
1107    /// the raw strings let `page.md?raw=true` in as a second entry alongside
1108    /// `page.md`, and MD051, which reports every entry, then reported the same
1109    /// broken fragment twice.
1110    ///
1111    /// One key per line is what the index has always recorded, so two links on
1112    /// one line asking the same file for the same fragment are one entry
1113    /// however each of them spells the destination.
1114    pub fn add_cross_file_link(&mut self, link: CrossFileLinkIndex) {
1115        let existing = self.cross_file_links.iter_mut().find(|existing| {
1116            existing.fragment == link.fragment
1117                && existing.line == link.line
1118                && strip_query_and_fragment(&existing.target_path) == strip_query_and_fragment(&link.target_path)
1119        });
1120        match existing {
1121            // A message quotes the destination back, so the spelling that kept the
1122            // query string is the one to keep, whichever rule recorded it first.
1123            Some(existing) => {
1124                if !existing.target_path.contains('?') && link.target_path.contains('?') {
1125                    *existing = link;
1126                }
1127            }
1128            None => self.cross_file_links.push(link),
1129        }
1130    }
1131
1132    /// Add a root-relative link to the index (deduplicates by target_path, fragment, line)
1133    pub fn add_root_relative_link(&mut self, link: CrossFileLinkIndex) {
1134        let is_duplicate = self.root_relative_links.iter().any(|existing| {
1135            existing.target_path == link.target_path && existing.fragment == link.fragment && existing.line == link.line
1136        });
1137        if !is_duplicate {
1138            self.root_relative_links.push(link);
1139        }
1140    }
1141
1142    /// Add one filesystem destination used by MD057.
1143    pub fn add_md057_link_target(&mut self, target: Md057LinkTarget) {
1144        self.md057_link_targets.push(target);
1145    }
1146
1147    /// Add a defined reference ID (e.g., from `[ref]: url`)
1148    pub fn add_defined_reference(&mut self, ref_id: String) {
1149        self.defined_references.insert(ref_id);
1150    }
1151
1152    /// Check if a reference ID has an explicit definition
1153    pub fn has_defined_reference(&self, ref_id: &str) -> bool {
1154        self.defined_references.contains(ref_id)
1155    }
1156
1157    /// Check if the content hash matches
1158    pub fn hash_matches(&self, hash: &str) -> bool {
1159        self.content_hash == hash
1160    }
1161
1162    /// Get the number of headings
1163    pub fn heading_count(&self) -> usize {
1164        self.headings.len()
1165    }
1166
1167    /// Get the number of reference links
1168    pub fn reference_link_count(&self) -> usize {
1169        self.reference_links.len()
1170    }
1171}
1172
1173#[cfg(test)]
1174mod tests {
1175    use super::*;
1176
1177    #[test]
1178    fn test_workspace_index_basic() {
1179        let mut index = WorkspaceIndex::new();
1180        assert_eq!(index.file_count(), 0);
1181        assert_eq!(index.version(), 0);
1182
1183        let mut file_index = FileIndex::with_hash("abc123".to_string());
1184        file_index.add_heading(HeadingIndex {
1185            text: "Installation".to_string(),
1186            auto_anchor: "installation".to_string(),
1187            custom_anchor: None,
1188            line: 1,
1189            is_setext: false,
1190        });
1191
1192        index.insert_file(PathBuf::from("docs/install.md"), file_index);
1193        assert_eq!(index.file_count(), 1);
1194        assert_eq!(index.version(), 1);
1195
1196        assert!(index.contains_file(Path::new("docs/install.md")));
1197        assert!(!index.contains_file(Path::new("docs/other.md")));
1198    }
1199
1200    #[test]
1201    fn test_vulnerable_anchors() {
1202        let mut index = WorkspaceIndex::new();
1203
1204        // File 1: heading without custom anchor (vulnerable)
1205        let mut file1 = FileIndex::new();
1206        file1.add_heading(HeadingIndex {
1207            text: "Getting Started".to_string(),
1208            auto_anchor: "getting-started".to_string(),
1209            custom_anchor: None,
1210            line: 1,
1211            is_setext: false,
1212        });
1213        index.insert_file(PathBuf::from("docs/guide.md"), file1);
1214
1215        // File 2: heading with custom anchor (not vulnerable)
1216        let mut file2 = FileIndex::new();
1217        file2.add_heading(HeadingIndex {
1218            text: "Installation".to_string(),
1219            auto_anchor: "installation".to_string(),
1220            custom_anchor: Some("install".to_string()),
1221            line: 1,
1222            is_setext: false,
1223        });
1224        index.insert_file(PathBuf::from("docs/install.md"), file2);
1225
1226        let vulnerable = index.get_vulnerable_anchors();
1227        assert_eq!(vulnerable.len(), 1);
1228        assert!(vulnerable.contains_key("getting-started"));
1229        assert!(!vulnerable.contains_key("installation"));
1230
1231        let anchors = vulnerable.get("getting-started").unwrap();
1232        assert_eq!(anchors.len(), 1);
1233        assert_eq!(anchors[0].file, PathBuf::from("docs/guide.md"));
1234        assert_eq!(anchors[0].text, "Getting Started");
1235    }
1236
1237    #[test]
1238    fn test_vulnerable_anchors_multiple_files_same_anchor() {
1239        // Multiple files can have headings with the same auto-generated anchor
1240        // get_vulnerable_anchors() should collect all of them
1241        let mut index = WorkspaceIndex::new();
1242
1243        // File 1: has "Installation" heading (vulnerable)
1244        let mut file1 = FileIndex::new();
1245        file1.add_heading(HeadingIndex {
1246            text: "Installation".to_string(),
1247            auto_anchor: "installation".to_string(),
1248            custom_anchor: None,
1249            line: 1,
1250            is_setext: false,
1251        });
1252        index.insert_file(PathBuf::from("docs/en/guide.md"), file1);
1253
1254        // File 2: also has "Installation" heading with same anchor (vulnerable)
1255        let mut file2 = FileIndex::new();
1256        file2.add_heading(HeadingIndex {
1257            text: "Installation".to_string(),
1258            auto_anchor: "installation".to_string(),
1259            custom_anchor: None,
1260            line: 5,
1261            is_setext: false,
1262        });
1263        index.insert_file(PathBuf::from("docs/fr/guide.md"), file2);
1264
1265        // File 3: has "Installation" but WITH custom anchor (not vulnerable)
1266        let mut file3 = FileIndex::new();
1267        file3.add_heading(HeadingIndex {
1268            text: "Installation".to_string(),
1269            auto_anchor: "installation".to_string(),
1270            custom_anchor: Some("install".to_string()),
1271            line: 10,
1272            is_setext: false,
1273        });
1274        index.insert_file(PathBuf::from("docs/de/guide.md"), file3);
1275
1276        let vulnerable = index.get_vulnerable_anchors();
1277        assert_eq!(vulnerable.len(), 1); // One unique anchor
1278        assert!(vulnerable.contains_key("installation"));
1279
1280        let anchors = vulnerable.get("installation").unwrap();
1281        // Should have 2 entries (en and fr), NOT 3 (de has custom anchor)
1282        assert_eq!(anchors.len(), 2, "Should collect both vulnerable anchors");
1283
1284        // Verify both files are represented
1285        let files: std::collections::HashSet<_> = anchors.iter().map(|a| &a.file).collect();
1286        assert!(files.contains(&PathBuf::from("docs/en/guide.md")));
1287        assert!(files.contains(&PathBuf::from("docs/fr/guide.md")));
1288    }
1289
1290    #[test]
1291    fn test_file_index_hash() {
1292        let index = FileIndex::with_hash("hash123".to_string());
1293        assert!(index.hash_matches("hash123"));
1294        assert!(!index.hash_matches("other"));
1295    }
1296
1297    #[test]
1298    fn test_version_increment() {
1299        let mut index = WorkspaceIndex::new();
1300        assert_eq!(index.version(), 0);
1301
1302        index.insert_file(PathBuf::from("a.md"), FileIndex::new());
1303        assert_eq!(index.version(), 1);
1304
1305        index.insert_file(PathBuf::from("b.md"), FileIndex::new());
1306        assert_eq!(index.version(), 2);
1307
1308        index.remove_file(Path::new("a.md"));
1309        assert_eq!(index.version(), 3);
1310
1311        // Removing non-existent file doesn't increment
1312        index.remove_file(Path::new("nonexistent.md"));
1313        assert_eq!(index.version(), 3);
1314    }
1315
1316    #[test]
1317    fn test_files_sorted_is_path_ordered() {
1318        let mut index = WorkspaceIndex::new();
1319        // Insert in a deliberately non-sorted order.
1320        for name in ["docs/zebra.md", "docs/apple.md", "docs/mango.md"] {
1321            index.update_file(Path::new(name), FileIndex::new());
1322        }
1323
1324        let paths: Vec<&Path> = index.files_sorted().into_iter().map(|(p, _)| p).collect();
1325        assert_eq!(
1326            paths,
1327            vec![
1328                Path::new("docs/apple.md"),
1329                Path::new("docs/mango.md"),
1330                Path::new("docs/zebra.md"),
1331            ],
1332            "files_sorted() must return entries ordered by path"
1333        );
1334    }
1335
1336    /// Two rules record the same link, one keeping the query string and one
1337    /// keeping only the file it names. That is one link, and the spelling kept
1338    /// is the destination as written whichever rule got there first - a message
1339    /// quotes it back, so the answer must not depend on rule order.
1340    #[test]
1341    fn test_add_cross_file_link_keeps_the_destination_as_written() {
1342        let as_written = CrossFileLinkIndex {
1343            target_path: "other.md?raw=true".to_string(),
1344            fragment: "missing".to_string(),
1345            line: 3,
1346            column: 1,
1347            origin: LinkOrigin::Body,
1348        };
1349        let file_named = CrossFileLinkIndex {
1350            target_path: "other.md".to_string(),
1351            fragment: "missing".to_string(),
1352            line: 3,
1353            column: 9,
1354            origin: LinkOrigin::Body,
1355        };
1356
1357        for (first, second) in [
1358            (as_written.clone(), file_named.clone()),
1359            (file_named.clone(), as_written.clone()),
1360        ] {
1361            let mut index = FileIndex::new();
1362            index.add_cross_file_link(first);
1363            index.add_cross_file_link(second);
1364
1365            assert_eq!(
1366                index.cross_file_links.len(),
1367                1,
1368                "one link is one entry, got: {:?}",
1369                index.cross_file_links
1370            );
1371            assert_eq!(index.cross_file_links[0].target_path, "other.md?raw=true");
1372        }
1373    }
1374
1375    /// Links to two different files are two entries, so the deduplication above
1376    /// cannot swallow a second target.
1377    #[test]
1378    fn test_add_cross_file_link_keeps_distinct_targets() {
1379        let mut index = FileIndex::new();
1380        for target in ["one.md", "two.md"] {
1381            index.add_cross_file_link(CrossFileLinkIndex {
1382                target_path: target.to_string(),
1383                fragment: "missing".to_string(),
1384                line: 3,
1385                column: 1,
1386                origin: LinkOrigin::Body,
1387            });
1388        }
1389        assert_eq!(index.cross_file_links.len(), 2);
1390    }
1391
1392    /// Two links on one line asking the same file for the same fragment are one
1393    /// entry, which is what the index has always recorded for two identically
1394    /// spelled destinations. Differing query strings do not make them two links,
1395    /// because a query string is not part of a file name.
1396    ///
1397    /// A different fragment, or the same link on another line, stays its own
1398    /// entry - so this is a boundary, not a blanket collapse to one finding.
1399    #[test]
1400    fn test_add_cross_file_link_collapses_one_line_asking_one_file_once() {
1401        let link = |target: &str, fragment: &str, line: usize| CrossFileLinkIndex {
1402            target_path: target.to_string(),
1403            fragment: fragment.to_string(),
1404            line,
1405            column: 1,
1406            origin: LinkOrigin::Body,
1407        };
1408
1409        let mut index = FileIndex::new();
1410        index.add_cross_file_link(link("target.md?raw=true", "missing", 3));
1411        index.add_cross_file_link(link("target.md?plain=1", "missing", 3));
1412        assert_eq!(
1413            index.cross_file_links.len(),
1414            1,
1415            "one file, one fragment, one line is one entry, got: {:?}",
1416            index.cross_file_links
1417        );
1418
1419        index.add_cross_file_link(link("target.md", "other", 3));
1420        index.add_cross_file_link(link("target.md", "missing", 4));
1421        assert_eq!(index.cross_file_links.len(), 3);
1422    }
1423
1424    /// A link is a dependency on the file it names, so editing `b.md` re-lints
1425    /// the source whichever way that source spelled the destination. The query
1426    /// string is the case that gets this wrong: it is not part of a file name,
1427    /// nothing ever creates a file called `b.md?raw=true`, and a reverse
1428    /// dependency filed under that name is one no editor will ever look up.
1429    #[test]
1430    fn test_reverse_deps_ignore_a_query_string_on_the_destination() {
1431        let mut index = WorkspaceIndex::new();
1432
1433        let mut file_a = FileIndex::new();
1434        file_a.add_cross_file_link(CrossFileLinkIndex {
1435            target_path: "b.md?raw=true".to_string(),
1436            fragment: "section".to_string(),
1437            line: 10,
1438            column: 5,
1439            origin: LinkOrigin::Body,
1440        });
1441        index.update_file(Path::new("docs/a.md"), file_a);
1442
1443        assert_eq!(
1444            index.get_dependents(Path::new("docs/b.md")),
1445            vec![PathBuf::from("docs/a.md")],
1446            "editing docs/b.md must re-lint the file linking to it"
1447        );
1448
1449        // And the source stops depending on it once the link is gone, so the
1450        // stripped key is cleared by the same route it was created.
1451        index.update_file(Path::new("docs/a.md"), FileIndex::new());
1452        assert!(index.get_dependents(Path::new("docs/b.md")).is_empty());
1453    }
1454
1455    #[test]
1456    fn test_reverse_deps_basic() {
1457        let mut index = WorkspaceIndex::new();
1458
1459        // File A links to file B
1460        let mut file_a = FileIndex::new();
1461        file_a.add_cross_file_link(CrossFileLinkIndex {
1462            target_path: "b.md".to_string(),
1463            fragment: "section".to_string(),
1464            line: 10,
1465            column: 5,
1466            origin: LinkOrigin::Body,
1467        });
1468        index.update_file(Path::new("docs/a.md"), file_a);
1469
1470        // Check that B has A as a dependent
1471        let dependents = index.get_dependents(Path::new("docs/b.md"));
1472        assert_eq!(dependents.len(), 1);
1473        assert_eq!(dependents[0], PathBuf::from("docs/a.md"));
1474
1475        // A has no dependents
1476        let a_dependents = index.get_dependents(Path::new("docs/a.md"));
1477        assert!(a_dependents.is_empty());
1478    }
1479
1480    #[test]
1481    fn test_reverse_deps_multiple() {
1482        let mut index = WorkspaceIndex::new();
1483
1484        // Files A and C both link to B
1485        let mut file_a = FileIndex::new();
1486        file_a.add_cross_file_link(CrossFileLinkIndex {
1487            target_path: "../b.md".to_string(),
1488            fragment: "".to_string(),
1489            line: 1,
1490            column: 1,
1491            origin: LinkOrigin::Body,
1492        });
1493        index.update_file(Path::new("docs/sub/a.md"), file_a);
1494
1495        let mut file_c = FileIndex::new();
1496        file_c.add_cross_file_link(CrossFileLinkIndex {
1497            target_path: "b.md".to_string(),
1498            fragment: "".to_string(),
1499            line: 1,
1500            column: 1,
1501            origin: LinkOrigin::Body,
1502        });
1503        index.update_file(Path::new("docs/c.md"), file_c);
1504
1505        // B should have both A and C as dependents
1506        let dependents = index.get_dependents(Path::new("docs/b.md"));
1507        assert_eq!(dependents.len(), 2);
1508        assert!(dependents.contains(&PathBuf::from("docs/sub/a.md")));
1509        assert!(dependents.contains(&PathBuf::from("docs/c.md")));
1510    }
1511
1512    #[test]
1513    fn test_reverse_deps_update_clears_old() {
1514        let mut index = WorkspaceIndex::new();
1515
1516        // File A initially links to B
1517        let mut file_a = FileIndex::new();
1518        file_a.add_cross_file_link(CrossFileLinkIndex {
1519            target_path: "b.md".to_string(),
1520            fragment: "".to_string(),
1521            line: 1,
1522            column: 1,
1523            origin: LinkOrigin::Body,
1524        });
1525        index.update_file(Path::new("docs/a.md"), file_a);
1526
1527        // Verify B has A as dependent
1528        assert_eq!(index.get_dependents(Path::new("docs/b.md")).len(), 1);
1529
1530        // Update A to link to C instead of B
1531        let mut file_a_updated = FileIndex::new();
1532        file_a_updated.add_cross_file_link(CrossFileLinkIndex {
1533            target_path: "c.md".to_string(),
1534            fragment: "".to_string(),
1535            line: 1,
1536            column: 1,
1537            origin: LinkOrigin::Body,
1538        });
1539        index.update_file(Path::new("docs/a.md"), file_a_updated);
1540
1541        // B should no longer have A as dependent
1542        assert!(index.get_dependents(Path::new("docs/b.md")).is_empty());
1543
1544        // C should now have A as dependent
1545        let c_deps = index.get_dependents(Path::new("docs/c.md"));
1546        assert_eq!(c_deps.len(), 1);
1547        assert_eq!(c_deps[0], PathBuf::from("docs/a.md"));
1548    }
1549
1550    #[test]
1551    fn test_reverse_deps_remove_file() {
1552        let mut index = WorkspaceIndex::new();
1553
1554        // File A links to B
1555        let mut file_a = FileIndex::new();
1556        file_a.add_cross_file_link(CrossFileLinkIndex {
1557            target_path: "b.md".to_string(),
1558            fragment: "".to_string(),
1559            line: 1,
1560            column: 1,
1561            origin: LinkOrigin::Body,
1562        });
1563        index.update_file(Path::new("docs/a.md"), file_a);
1564
1565        // Verify B has A as dependent
1566        assert_eq!(index.get_dependents(Path::new("docs/b.md")).len(), 1);
1567
1568        // Remove file A
1569        index.remove_file(Path::new("docs/a.md"));
1570
1571        // B should no longer have any dependents
1572        assert!(index.get_dependents(Path::new("docs/b.md")).is_empty());
1573    }
1574
1575    #[test]
1576    fn test_normalize_path() {
1577        // Test .. handling
1578        let path = Path::new("docs/sub/../other.md");
1579        let normalized = normalize_relative_path(path);
1580        assert_eq!(normalized, PathBuf::from("docs/other.md"));
1581
1582        // Test . handling
1583        let path2 = Path::new("docs/./other.md");
1584        let normalized2 = normalize_relative_path(path2);
1585        assert_eq!(normalized2, PathBuf::from("docs/other.md"));
1586
1587        // Test multiple ..
1588        let path3 = Path::new("a/b/c/../../d.md");
1589        let normalized3 = normalize_relative_path(path3);
1590        assert_eq!(normalized3, PathBuf::from("a/d.md"));
1591    }
1592
1593    /// A `..` with no name in front of it is the path saying it leaves the
1594    /// directory it started in. Dropping it renames the file - `../notes.md`
1595    /// would become `notes.md`, a sibling that may well exist and is not the file
1596    /// the link names - so it survives normalization.
1597    #[test]
1598    fn normalize_keeps_a_traversal_that_leaves_its_own_root() {
1599        assert_eq!(
1600            normalize_relative_path(Path::new("../notes.md")),
1601            PathBuf::from("../notes.md")
1602        );
1603        assert_eq!(
1604            normalize_relative_path(Path::new("docs/../../notes.md")),
1605            PathBuf::from("../notes.md")
1606        );
1607        assert_eq!(
1608            normalize_relative_path(Path::new("../../a/./b/../notes.md")),
1609            PathBuf::from("../../a/notes.md")
1610        );
1611    }
1612
1613    /// There is nothing above a root to name, which is what the filesystem
1614    /// itself answers for `/..`.
1615    #[test]
1616    fn normalize_stops_a_traversal_at_a_root() {
1617        let root = if cfg!(windows) { "C:\\" } else { "/" };
1618        assert_eq!(
1619            normalize_relative_path(&Path::new(root).join("..").join("notes.md")),
1620            Path::new(root).join("notes.md")
1621        );
1622    }
1623
1624    #[test]
1625    fn test_clear_clears_reverse_deps() {
1626        let mut index = WorkspaceIndex::new();
1627
1628        // File A links to B
1629        let mut file_a = FileIndex::new();
1630        file_a.add_cross_file_link(CrossFileLinkIndex {
1631            target_path: "b.md".to_string(),
1632            fragment: "".to_string(),
1633            line: 1,
1634            column: 1,
1635            origin: LinkOrigin::Body,
1636        });
1637        index.update_file(Path::new("docs/a.md"), file_a);
1638
1639        // Verify B has A as dependent
1640        assert_eq!(index.get_dependents(Path::new("docs/b.md")).len(), 1);
1641
1642        // Clear the index
1643        index.clear();
1644
1645        // Both files and reverse deps should be cleared
1646        assert_eq!(index.file_count(), 0);
1647        assert!(index.get_dependents(Path::new("docs/b.md")).is_empty());
1648    }
1649
1650    #[test]
1651    fn test_is_file_stale() {
1652        let mut index = WorkspaceIndex::new();
1653
1654        // Non-existent file is always stale
1655        assert!(index.is_file_stale(Path::new("nonexistent.md"), "hash123"));
1656
1657        // Add a file with known hash
1658        let file_index = FileIndex::with_hash("hash123".to_string());
1659        index.insert_file(PathBuf::from("docs/test.md"), file_index);
1660
1661        // Same hash means not stale
1662        assert!(!index.is_file_stale(Path::new("docs/test.md"), "hash123"));
1663
1664        // Different hash means stale
1665        assert!(index.is_file_stale(Path::new("docs/test.md"), "different_hash"));
1666    }
1667
1668    #[cfg(feature = "native")]
1669    #[test]
1670    fn test_cache_roundtrip() {
1671        use std::fs;
1672
1673        // Create a temp directory
1674        let temp_dir = std::env::temp_dir().join("rumdl_test_cache_roundtrip");
1675        let _ = fs::remove_dir_all(&temp_dir);
1676        fs::create_dir_all(&temp_dir).unwrap();
1677
1678        // Create an index with some data
1679        let mut index = WorkspaceIndex::new();
1680
1681        let mut file1 = FileIndex::with_hash("abc123".to_string());
1682        file1.add_heading(HeadingIndex {
1683            text: "Test Heading".to_string(),
1684            auto_anchor: "test-heading".to_string(),
1685            custom_anchor: Some("test".to_string()),
1686            line: 1,
1687            is_setext: false,
1688        });
1689        file1.add_cross_file_link(CrossFileLinkIndex {
1690            target_path: "./other.md".to_string(),
1691            fragment: "section".to_string(),
1692            line: 5,
1693            column: 3,
1694            origin: LinkOrigin::Body,
1695        });
1696        index.update_file(Path::new("docs/file1.md"), file1);
1697
1698        let mut file2 = FileIndex::with_hash("def456".to_string());
1699        file2.add_heading(HeadingIndex {
1700            text: "Another Heading".to_string(),
1701            auto_anchor: "another-heading".to_string(),
1702            custom_anchor: None,
1703            line: 1,
1704            is_setext: false,
1705        });
1706        index.update_file(Path::new("docs/other.md"), file2);
1707
1708        // Save to cache
1709        index.save_to_cache(&temp_dir).expect("Failed to save cache");
1710
1711        // Verify cache file exists
1712        assert!(temp_dir.join("workspace_index.bin").exists());
1713
1714        // Load from cache
1715        let loaded = WorkspaceIndex::load_from_cache(&temp_dir).expect("Failed to load cache");
1716
1717        // Verify data matches
1718        assert_eq!(loaded.file_count(), 2);
1719        assert!(loaded.contains_file(Path::new("docs/file1.md")));
1720        assert!(loaded.contains_file(Path::new("docs/other.md")));
1721
1722        // Check file1 details
1723        let file1_loaded = loaded.get_file(Path::new("docs/file1.md")).unwrap();
1724        assert_eq!(file1_loaded.content_hash, "abc123");
1725        assert_eq!(file1_loaded.headings.len(), 1);
1726        assert_eq!(file1_loaded.headings[0].text, "Test Heading");
1727        assert_eq!(file1_loaded.headings[0].custom_anchor, Some("test".to_string()));
1728        assert_eq!(file1_loaded.cross_file_links.len(), 1);
1729        assert_eq!(file1_loaded.cross_file_links[0].target_path, "./other.md");
1730
1731        // Check reverse deps were serialized correctly
1732        let dependents = loaded.get_dependents(Path::new("docs/other.md"));
1733        assert_eq!(dependents.len(), 1);
1734        assert_eq!(dependents[0], PathBuf::from("docs/file1.md"));
1735
1736        // Clean up
1737        let _ = fs::remove_dir_all(&temp_dir);
1738    }
1739
1740    #[cfg(feature = "native")]
1741    #[test]
1742    fn test_cache_missing_file() {
1743        let temp_dir = std::env::temp_dir().join("rumdl_test_cache_missing");
1744        let _ = std::fs::remove_dir_all(&temp_dir);
1745
1746        // Should return None for non-existent cache
1747        let result = WorkspaceIndex::load_from_cache(&temp_dir);
1748        assert!(result.is_none());
1749    }
1750
1751    #[cfg(feature = "native")]
1752    #[test]
1753    fn test_cache_corrupted_file() {
1754        use std::fs;
1755
1756        let temp_dir = std::env::temp_dir().join("rumdl_test_cache_corrupted");
1757        let _ = fs::remove_dir_all(&temp_dir);
1758        fs::create_dir_all(&temp_dir).unwrap();
1759
1760        // Write corrupted data (too small for header)
1761        fs::write(temp_dir.join("workspace_index.bin"), b"bad").unwrap();
1762
1763        // Should return None for corrupted cache (and remove the file)
1764        let result = WorkspaceIndex::load_from_cache(&temp_dir);
1765        assert!(result.is_none());
1766
1767        // Corrupted file should be removed
1768        assert!(!temp_dir.join("workspace_index.bin").exists());
1769
1770        // Clean up
1771        let _ = fs::remove_dir_all(&temp_dir);
1772    }
1773
1774    #[cfg(feature = "native")]
1775    #[test]
1776    fn test_cache_invalid_magic() {
1777        use std::fs;
1778
1779        let temp_dir = std::env::temp_dir().join("rumdl_test_cache_invalid_magic");
1780        let _ = fs::remove_dir_all(&temp_dir);
1781        fs::create_dir_all(&temp_dir).unwrap();
1782
1783        // Write data with wrong magic header
1784        let mut data = Vec::new();
1785        data.extend_from_slice(b"XXXX"); // Wrong magic
1786        data.extend_from_slice(&1u32.to_le_bytes()); // Version 1
1787        data.extend_from_slice(&[0; 100]); // Some garbage data
1788        fs::write(temp_dir.join("workspace_index.bin"), &data).unwrap();
1789
1790        // Should return None for invalid magic
1791        let result = WorkspaceIndex::load_from_cache(&temp_dir);
1792        assert!(result.is_none());
1793
1794        // File should be removed
1795        assert!(!temp_dir.join("workspace_index.bin").exists());
1796
1797        // Clean up
1798        let _ = fs::remove_dir_all(&temp_dir);
1799    }
1800
1801    #[cfg(feature = "native")]
1802    #[test]
1803    fn test_cache_version_mismatch() {
1804        use std::fs;
1805
1806        let temp_dir = std::env::temp_dir().join("rumdl_test_cache_version_mismatch");
1807        let _ = fs::remove_dir_all(&temp_dir);
1808        fs::create_dir_all(&temp_dir).unwrap();
1809
1810        // Write data with correct magic but wrong version
1811        let mut data = Vec::new();
1812        data.extend_from_slice(b"RWSI"); // Correct magic
1813        data.extend_from_slice(&999u32.to_le_bytes()); // Future version
1814        data.extend_from_slice(&[0; 100]); // Some garbage data
1815        fs::write(temp_dir.join("workspace_index.bin"), &data).unwrap();
1816
1817        // Should return None for version mismatch
1818        let result = WorkspaceIndex::load_from_cache(&temp_dir);
1819        assert!(result.is_none());
1820
1821        // File should be removed to trigger rebuild
1822        assert!(!temp_dir.join("workspace_index.bin").exists());
1823
1824        // Clean up
1825        let _ = fs::remove_dir_all(&temp_dir);
1826    }
1827
1828    #[cfg(feature = "native")]
1829    #[test]
1830    fn test_cache_atomic_write() {
1831        use std::fs;
1832
1833        // Test that atomic writes work (no temp files left behind)
1834        let temp_dir = std::env::temp_dir().join("rumdl_test_cache_atomic");
1835        let _ = fs::remove_dir_all(&temp_dir);
1836        fs::create_dir_all(&temp_dir).unwrap();
1837
1838        let index = WorkspaceIndex::new();
1839        index.save_to_cache(&temp_dir).expect("Failed to save");
1840
1841        // Only the final cache file should exist, no temp files
1842        let entries: Vec<_> = fs::read_dir(&temp_dir).unwrap().collect();
1843        assert_eq!(entries.len(), 1);
1844        assert!(temp_dir.join("workspace_index.bin").exists());
1845
1846        // Clean up
1847        let _ = fs::remove_dir_all(&temp_dir);
1848    }
1849
1850    #[test]
1851    fn test_has_anchor_auto_generated() {
1852        let mut file_index = FileIndex::new();
1853        file_index.add_heading(HeadingIndex {
1854            text: "Installation Guide".to_string(),
1855            auto_anchor: "installation-guide".to_string(),
1856            custom_anchor: None,
1857            line: 1,
1858            is_setext: false,
1859        });
1860
1861        // Should find by auto-generated anchor
1862        assert!(file_index.has_anchor("installation-guide"));
1863
1864        // Case-insensitive matching
1865        assert!(file_index.has_anchor("Installation-Guide"));
1866        assert!(file_index.has_anchor("INSTALLATION-GUIDE"));
1867
1868        // Should not find non-existent anchor
1869        assert!(!file_index.has_anchor("nonexistent"));
1870    }
1871
1872    #[test]
1873    fn test_has_anchor_custom() {
1874        let mut file_index = FileIndex::new();
1875        file_index.add_heading(HeadingIndex {
1876            text: "Installation Guide".to_string(),
1877            auto_anchor: "installation-guide".to_string(),
1878            custom_anchor: Some("install".to_string()),
1879            line: 1,
1880            is_setext: false,
1881        });
1882
1883        // Should find by auto-generated anchor
1884        assert!(file_index.has_anchor("installation-guide"));
1885
1886        // Should also find by custom anchor
1887        assert!(file_index.has_anchor("install"));
1888        assert!(file_index.has_anchor("Install")); // case-insensitive
1889
1890        // Should not find non-existent anchor
1891        assert!(!file_index.has_anchor("nonexistent"));
1892    }
1893
1894    #[test]
1895    fn test_get_heading_by_anchor() {
1896        let mut file_index = FileIndex::new();
1897        file_index.add_heading(HeadingIndex {
1898            text: "Installation Guide".to_string(),
1899            auto_anchor: "installation-guide".to_string(),
1900            custom_anchor: Some("install".to_string()),
1901            line: 10,
1902            is_setext: false,
1903        });
1904        file_index.add_heading(HeadingIndex {
1905            text: "Configuration".to_string(),
1906            auto_anchor: "configuration".to_string(),
1907            custom_anchor: None,
1908            line: 20,
1909            is_setext: false,
1910        });
1911
1912        // Get by auto anchor
1913        let heading = file_index.get_heading_by_anchor("installation-guide");
1914        assert!(heading.is_some());
1915        assert_eq!(heading.unwrap().text, "Installation Guide");
1916        assert_eq!(heading.unwrap().line, 10);
1917
1918        // Get by custom anchor
1919        let heading = file_index.get_heading_by_anchor("install");
1920        assert!(heading.is_some());
1921        assert_eq!(heading.unwrap().text, "Installation Guide");
1922
1923        // Get second heading
1924        let heading = file_index.get_heading_by_anchor("configuration");
1925        assert!(heading.is_some());
1926        assert_eq!(heading.unwrap().text, "Configuration");
1927        assert_eq!(heading.unwrap().line, 20);
1928
1929        // Non-existent
1930        assert!(file_index.get_heading_by_anchor("nonexistent").is_none());
1931    }
1932
1933    #[test]
1934    fn test_anchor_lookup_many_headings() {
1935        // Test that O(1) lookup works with many headings
1936        let mut file_index = FileIndex::new();
1937
1938        // Add 100 headings
1939        for i in 0..100 {
1940            file_index.add_heading(HeadingIndex {
1941                text: format!("Heading {i}"),
1942                auto_anchor: format!("heading-{i}"),
1943                custom_anchor: Some(format!("h{i}")),
1944                line: i + 1,
1945                is_setext: false,
1946            });
1947        }
1948
1949        // Verify all can be found
1950        for i in 0..100 {
1951            assert!(file_index.has_anchor(&format!("heading-{i}")));
1952            assert!(file_index.has_anchor(&format!("h{i}")));
1953
1954            let heading = file_index.get_heading_by_anchor(&format!("heading-{i}"));
1955            assert!(heading.is_some());
1956            assert_eq!(heading.unwrap().line, i + 1);
1957        }
1958    }
1959
1960    // =============================================================================
1961    // Tests for extract_cross_file_links utility
1962    // =============================================================================
1963
1964    #[test]
1965    fn test_extract_cross_file_links_basic() {
1966        use crate::config::MarkdownFlavor;
1967
1968        let content = "# Test\n\nSee [link](./other.md) for info.\n";
1969        let ctx = LintContext::new(content, MarkdownFlavor::default(), None);
1970        let links = extract_cross_file_links(&ctx).relative;
1971
1972        assert_eq!(links.len(), 1);
1973        assert_eq!(links[0].target_path, "./other.md");
1974        assert_eq!(links[0].fragment, "");
1975        assert_eq!(links[0].line, 3);
1976        // "See [link](" = 11 chars, so column 12 is where "./other.md" starts
1977        assert_eq!(links[0].column, 12);
1978    }
1979
1980    #[test]
1981    fn test_extract_cross_file_links_with_fragment() {
1982        use crate::config::MarkdownFlavor;
1983
1984        let content = "Check [guide](./guide.md#install) here.\n";
1985        let ctx = LintContext::new(content, MarkdownFlavor::default(), None);
1986        let links = extract_cross_file_links(&ctx).relative;
1987
1988        assert_eq!(links.len(), 1);
1989        assert_eq!(links[0].target_path, "./guide.md");
1990        assert_eq!(links[0].fragment, "install");
1991        assert_eq!(links[0].line, 1);
1992        // "Check [guide](" = 14 chars, so column 15 is where "./guide.md" starts
1993        assert_eq!(links[0].column, 15);
1994    }
1995
1996    #[test]
1997    fn test_extract_cross_file_links_multiple_on_same_line() {
1998        use crate::config::MarkdownFlavor;
1999
2000        let content = "See [a](a.md) and [b](b.md) here.\n";
2001        let ctx = LintContext::new(content, MarkdownFlavor::default(), None);
2002        let links = extract_cross_file_links(&ctx).relative;
2003
2004        assert_eq!(links.len(), 2);
2005
2006        assert_eq!(links[0].target_path, "a.md");
2007        assert_eq!(links[0].line, 1);
2008        // "See [a](" = 8 chars, so column 9
2009        assert_eq!(links[0].column, 9);
2010
2011        assert_eq!(links[1].target_path, "b.md");
2012        assert_eq!(links[1].line, 1);
2013        // "See [a](a.md) and [b](" = 22 chars, so column 23
2014        assert_eq!(links[1].column, 23);
2015    }
2016
2017    #[test]
2018    fn test_extract_cross_file_links_angle_brackets() {
2019        use crate::config::MarkdownFlavor;
2020
2021        let content = "See [link](<path/with (parens).md>) here.\n";
2022        let ctx = LintContext::new(content, MarkdownFlavor::default(), None);
2023        let links = extract_cross_file_links(&ctx).relative;
2024
2025        assert_eq!(links.len(), 1);
2026        assert_eq!(links[0].target_path, "path/with (parens).md");
2027        assert_eq!(links[0].line, 1);
2028        // "See [link](<" = 12 chars, so column 13
2029        assert_eq!(links[0].column, 13);
2030    }
2031
2032    #[test]
2033    fn test_extract_cross_file_links_skips_external() {
2034        use crate::config::MarkdownFlavor;
2035
2036        let content = r#"
2037[external](https://example.com)
2038[mailto](mailto:test@example.com)
2039[local](./local.md)
2040[fragment](#section)
2041[absolute](/docs/page.md)
2042"#;
2043        let ctx = LintContext::new(content, MarkdownFlavor::default(), None);
2044        let extracted = extract_cross_file_links(&ctx);
2045
2046        // Only the local markdown link is a directory-relative link.
2047        assert_eq!(extracted.relative.len(), 1);
2048        assert_eq!(extracted.relative[0].target_path, "./local.md");
2049        // The root-relative link is captured separately, leading `/` stripped.
2050        assert_eq!(extracted.root_relative.len(), 1);
2051        assert_eq!(extracted.root_relative[0].target_path, "docs/page.md");
2052    }
2053
2054    #[test]
2055    fn test_extract_cross_file_links_root_relative() {
2056        use crate::config::MarkdownFlavor;
2057
2058        // Root-relative markdown links land in the root_relative bucket with the
2059        // leading `/` stripped; parent traversal and protocol-relative links are
2060        // excluded so they cannot escape a content root.
2061        let content = "[a](/guide.md#install)\n[b](/../escape.md)\n[c](//host/x.md)\n[d](/img/pic.png)\n";
2062        let ctx = LintContext::new(content, MarkdownFlavor::default(), None);
2063        let extracted = extract_cross_file_links(&ctx);
2064
2065        assert!(extracted.relative.is_empty(), "no directory-relative links here");
2066        assert_eq!(
2067            extracted
2068                .root_relative
2069                .iter()
2070                .map(|l| (l.target_path.as_str(), l.fragment.as_str()))
2071                .collect::<Vec<_>>(),
2072            vec![("guide.md", "install")],
2073            "only the safe root-relative markdown link is captured"
2074        );
2075    }
2076
2077    #[test]
2078    fn test_extract_cross_file_links_skips_non_markdown() {
2079        use crate::config::MarkdownFlavor;
2080
2081        let content = r#"
2082[image](./photo.png)
2083[doc](./readme.md)
2084[pdf](./document.pdf)
2085"#;
2086        let ctx = LintContext::new(content, MarkdownFlavor::default(), None);
2087        let links = extract_cross_file_links(&ctx).relative;
2088
2089        // Only markdown files are indexed for cross-file validation
2090        assert_eq!(links.len(), 1);
2091        assert_eq!(links[0].target_path, "./readme.md");
2092    }
2093
2094    #[test]
2095    fn test_extract_cross_file_links_skips_code_spans() {
2096        use crate::config::MarkdownFlavor;
2097
2098        let content = "Normal [link](./file.md) and `[code](./ignored.md)` here.\n";
2099        let ctx = LintContext::new(content, MarkdownFlavor::default(), None);
2100        let links = extract_cross_file_links(&ctx).relative;
2101
2102        // Only the link outside code span should be extracted
2103        assert_eq!(links.len(), 1);
2104        assert_eq!(links[0].target_path, "./file.md");
2105    }
2106
2107    #[test]
2108    fn test_extract_cross_file_links_with_query_params() {
2109        use crate::config::MarkdownFlavor;
2110
2111        let content = "See [doc](./file.md?raw=true) here.\n";
2112        let ctx = LintContext::new(content, MarkdownFlavor::default(), None);
2113        let links = extract_cross_file_links(&ctx).relative;
2114
2115        assert_eq!(links.len(), 1);
2116        // Query params should be stripped
2117        assert_eq!(links[0].target_path, "./file.md");
2118    }
2119
2120    #[test]
2121    fn test_extract_cross_file_links_empty_content() {
2122        use crate::config::MarkdownFlavor;
2123
2124        let content = "";
2125        let ctx = LintContext::new(content, MarkdownFlavor::default(), None);
2126        let links = extract_cross_file_links(&ctx).relative;
2127
2128        assert!(links.is_empty());
2129    }
2130
2131    #[test]
2132    fn test_extract_cross_file_links_no_links() {
2133        use crate::config::MarkdownFlavor;
2134
2135        let content = "# Just a heading\n\nSome text without links.\n";
2136        let ctx = LintContext::new(content, MarkdownFlavor::default(), None);
2137        let links = extract_cross_file_links(&ctx).relative;
2138
2139        assert!(links.is_empty());
2140    }
2141
2142    #[test]
2143    fn test_extract_cross_file_links_position_accuracy_issue_234() {
2144        // This test verifies the fix for GitHub issue #234
2145        // The LSP was reporting incorrect column positions for MD057 diagnostics
2146        use crate::config::MarkdownFlavor;
2147
2148        let content = r#"# Test Document
2149
2150Here is a [broken link](nonexistent-file.md) that should trigger MD057.
2151
2152And another [link](also-missing.md) on this line.
2153"#;
2154        let ctx = LintContext::new(content, MarkdownFlavor::default(), None);
2155        let links = extract_cross_file_links(&ctx).relative;
2156
2157        assert_eq!(links.len(), 2);
2158
2159        // First link: "Here is a [broken link](" = 24 chars, column 25
2160        assert_eq!(links[0].target_path, "nonexistent-file.md");
2161        assert_eq!(links[0].line, 3);
2162        assert_eq!(links[0].column, 25);
2163
2164        // Second link: "And another [link](" = 19 chars, column 20
2165        assert_eq!(links[1].target_path, "also-missing.md");
2166        assert_eq!(links[1].line, 5);
2167        assert_eq!(links[1].column, 20);
2168    }
2169}