rumdl_lib/rules/
md051_link_fragments.rs

1use crate::rule::{LintError, LintResult, LintWarning, Rule, Severity};
2use crate::utils::anchor_styles::AnchorStyle;
3use crate::utils::regex_cache::get_cached_regex;
4use lazy_static::lazy_static;
5use regex::Regex;
6use std::collections::HashSet;
7
8lazy_static! {
9    // Pre-compiled optimized patterns for quick checks
10    static ref QUICK_MARKDOWN_CHECK: Regex = Regex::new(r"[*_`\[\]]").unwrap();
11    // GitHub only strips asterisks (*), not underscores (_) - underscores are preserved
12    static ref EMPHASIS_PATTERN: Regex = Regex::new(r"\*+([^*]+)\*+").unwrap();
13    static ref CODE_PATTERN: Regex = Regex::new(r"`([^`]+)`").unwrap();
14    static ref LINK_PATTERN: Regex = Regex::new(r"\[([^\]]+)\]\(([^)]+)\)|\[([^\]]+)\]\[[^\]]*\]").unwrap();
15    static ref TOC_SECTION_START: Regex = Regex::new(r"(?i)^#+\s*(table\s+of\s+contents?|contents?|toc)\s*$").unwrap();
16    // HTML tags with id or name attributes (supports any HTML element, not just <a>)
17    // This pattern only captures the first id/name attribute in a tag
18    static ref HTML_ANCHOR_PATTERN: Regex = Regex::new(r#"\b(?:id|name)\s*=\s*["']([^"']+)["']"#).unwrap();
19}
20
21/// Rule MD051: Link fragments
22///
23/// See [docs/md051.md](../../docs/md051.md) for full documentation, configuration, and examples.
24///
25/// This rule validates that link anchors (the part after #) exist in the current document.
26/// Only applies to internal document links (like #heading), not to external URLs or cross-file links.
27#[derive(Clone)]
28pub struct MD051LinkFragments {
29    /// Anchor style to use for validation
30    anchor_style: AnchorStyle,
31}
32
33impl Default for MD051LinkFragments {
34    fn default() -> Self {
35        Self::new()
36    }
37}
38
39impl MD051LinkFragments {
40    pub fn new() -> Self {
41        Self {
42            anchor_style: AnchorStyle::GitHub,
43        }
44    }
45
46    /// Create with specific anchor style
47    pub fn with_anchor_style(style: AnchorStyle) -> Self {
48        Self { anchor_style: style }
49    }
50
51    /// Extract headings from cached LintContext information
52    /// Returns (markdown_anchors, html_anchors) where markdown_anchors are case-insensitive
53    /// and html_anchors are case-sensitive
54    fn extract_headings_from_context(
55        &self,
56        ctx: &crate::lint_context::LintContext,
57    ) -> (HashSet<String>, HashSet<String>) {
58        let mut markdown_headings = HashSet::with_capacity(32);
59        let mut html_anchors = HashSet::with_capacity(16);
60        let mut fragment_counts = std::collections::HashMap::new();
61        let mut in_toc = false;
62
63        // Single pass through lines, only processing lines with headings
64        for line_info in &ctx.lines {
65            // Skip front matter
66            if line_info.in_front_matter {
67                continue;
68            }
69
70            // Extract HTML anchor tags with id/name attributes
71            if !line_info.in_code_block {
72                // First, we need to handle multiple id attributes on same element
73                // HTML spec says only the first id attribute is used
74                // So we need to extract element by element, not all ids at once
75                let content = &line_info.content;
76                let mut pos = 0;
77                while pos < content.len() {
78                    // Find next HTML element
79                    if let Some(start) = content[pos..].find('<') {
80                        let tag_start = pos + start;
81                        if let Some(end) = content[tag_start..].find('>') {
82                            let tag_end = tag_start + end + 1;
83                            let tag = &content[tag_start..tag_end];
84
85                            // Extract only the FIRST id or name attribute from this tag
86                            // Use find() instead of captures_iter() to get only the first match
87                            if let Some(caps) = HTML_ANCHOR_PATTERN.find(tag) {
88                                // Now extract the actual id value from the first match
89                                let matched_text = caps.as_str();
90                                if let Some(caps) = HTML_ANCHOR_PATTERN.captures(matched_text)
91                                    && let Some(id_match) = caps.get(1)
92                                {
93                                    let id = id_match.as_str();
94                                    if !id.is_empty() {
95                                        // HTML anchors are case-sensitive
96                                        html_anchors.insert(id.to_string());
97                                    }
98                                }
99                            }
100                            pos = tag_end;
101                        } else {
102                            break;
103                        }
104                    } else {
105                        break;
106                    }
107                }
108            }
109
110            if let Some(heading) = &line_info.heading {
111                let line = &line_info.content;
112
113                // Check if we're entering a TOC section
114                let is_toc_heading = TOC_SECTION_START.is_match(line);
115
116                // If we were in TOC and hit another heading, we're out of TOC
117                if in_toc && !is_toc_heading {
118                    in_toc = false;
119                }
120
121                // Skip if we're inside a TOC section (but not the TOC heading itself)
122                if in_toc && !is_toc_heading {
123                    continue;
124                }
125
126                // If heading has a custom ID, add it as a valid anchor
127                if let Some(custom_id) = &heading.custom_id {
128                    markdown_headings.insert(custom_id.clone());
129                }
130
131                // ALWAYS generate the normal anchor too (for backward compatibility)
132                // This ensures both the custom ID and the generated anchor work
133                let fragment = self.anchor_style.generate_fragment(&heading.text);
134
135                if !fragment.is_empty() {
136                    // Handle duplicate fragments by appending numbers
137                    let final_fragment = if let Some(count) = fragment_counts.get_mut(&fragment) {
138                        let suffix = *count;
139                        *count += 1;
140                        format!("{fragment}-{suffix}")
141                    } else {
142                        fragment_counts.insert(fragment.clone(), 1);
143                        fragment
144                    };
145                    markdown_headings.insert(final_fragment);
146                }
147
148                // After processing the TOC heading, mark that we're in a TOC section
149                if is_toc_heading {
150                    in_toc = true;
151                }
152            }
153        }
154
155        (markdown_headings, html_anchors)
156    }
157
158    /// Fragment generation using GitHub.com's ACTUAL algorithm (verified against GitHub Gists)
159    /// This is the exact algorithm discovered by testing against GitHub.com itself, not third-party packages
160    #[inline]
161    pub fn heading_to_fragment_github_official(&self, heading: &str) -> String {
162        if heading.is_empty() {
163            return String::new();
164        }
165
166        // Strip markdown formatting first
167        let text = if QUICK_MARKDOWN_CHECK.is_match(heading) {
168            self.strip_markdown_formatting_fast(heading)
169        } else {
170            heading.to_string()
171        };
172
173        // GitHub.com's ACTUAL algorithm discovered through Gist testing:
174        // Based on analysis of https://gist.github.com/rvben/da6f7faf265f69fd8d6fd247ee526beb
175
176        let text = text.to_lowercase();
177        let mut result = String::with_capacity(text.len());
178        let chars: Vec<char> = text.chars().collect();
179        let len = chars.len();
180
181        let mut i = 0;
182        while i < len {
183            let c = chars[i];
184
185            // Handle multi-character patterns first (order matters)
186            if i + 2 < len && chars[i] == '-' && chars[i + 1] == '-' && chars[i + 2] == '>' {
187                // --> becomes ----
188                result.push_str("----");
189                i += 3;
190                continue;
191            }
192
193            if i + 2 < len && chars[i] == '<' && chars[i + 1] == '-' && chars[i + 2] == '>' {
194                // <-> becomes ---
195                result.push_str("---");
196                i += 3;
197                continue;
198            }
199
200            if i + 2 < len && chars[i] == '=' && chars[i + 1] == '=' && chars[i + 2] == '>' {
201                // ==> becomes --
202                result.push_str("--");
203                i += 3;
204                continue;
205            }
206
207            if i + 1 < len && chars[i] == '-' && chars[i + 1] == '>' {
208                // -> becomes ---
209                result.push_str("---");
210                i += 2;
211                continue;
212            }
213
214            // Single character processing
215            match c {
216                // Preserve all letters (including Unicode)
217                c if c.is_alphabetic() => result.push(c),
218
219                // Preserve all digits
220                c if c.is_ascii_digit() => result.push(c),
221
222                // Preserve underscores (critical difference from other implementations)
223                '_' => result.push(c),
224
225                // Preserve hyphens (including multiple consecutive)
226                '-' => result.push(c),
227
228                // Convert spaces to hyphens, but check for upcoming arrow patterns
229                ' ' => {
230                    // Check if this space is followed by an arrow pattern that we'll handle
231                    let followed_by_arrow =
232                        (i + 3 < len && chars[i + 1] == '-' && chars[i + 2] == '-' && chars[i + 3] == '>')
233                            || (i + 4 < len && chars[i + 1] == '<' && chars[i + 2] == '-' && chars[i + 3] == '>')
234                            || (i + 4 < len && chars[i + 1] == '=' && chars[i + 2] == '=' && chars[i + 3] == '>')
235                            || (i + 2 < len && chars[i + 1] == '-' && chars[i + 2] == '>');
236
237                    if !followed_by_arrow {
238                        result.push('-');
239                    }
240                    // If followed by arrow, skip the space (it gets absorbed into the arrow replacement)
241                }
242
243                // Special symbols become double hyphens
244                '&' => result.push_str("--"),
245                '©' => result.push_str("--"),
246
247                // Emojis become single hyphens (broader Unicode range for emojis)
248                c if (c as u32 >= 0x1F300 && c as u32 <= 0x1F9FF)
249                    || (c as u32 >= 0x1F600 && c as u32 <= 0x1F64F)
250                    || (c as u32 >= 0x2600 && c as u32 <= 0x26FF) =>
251                {
252                    result.push('-')
253                }
254
255                // Most other punctuation is removed (dots, colons, brackets, etc.)
256                _ => {
257                    // Skip all other characters (remove them)
258                }
259            }
260
261            i += 1;
262        }
263
264        result
265    }
266
267    /// Fragment generation following GitHub's official slugger algorithm
268    /// Uses the exact algorithm from github-slugger npm package used by GitHub.com
269    #[inline]
270    pub fn heading_to_fragment_github(&self, heading: &str) -> String {
271        if heading.is_empty() {
272            return String::new();
273        }
274
275        // Strip markdown formatting first
276        let text = if QUICK_MARKDOWN_CHECK.is_match(heading) {
277            self.strip_markdown_formatting_fast(heading)
278        } else {
279            heading.to_string()
280        };
281
282        // NOTE: GitHub does NOT trim whitespace - it preserves leading/trailing spaces
283        // and converts them to hyphens. This matches the official github-slugger behavior.
284
285        // GitHub's EXACT algorithm from github-slugger npm package:
286        // function slug(value, maintainCase) {
287        //   if (typeof value !== 'string') return ''
288        //   if (!maintainCase) value = value.toLowerCase()
289        //   return value.replace(regex, '').replace(/ /g, '-')
290        // }
291
292        // 1. Convert to lowercase (maintainCase = false)
293        let text = text.to_lowercase();
294
295        // 2. Apply GitHub's exact regex pattern to remove punctuation/symbols
296        // This is the EXACT regex from github-slugger/regex.js
297        // Note: In Rust, we need to use \u{} syntax for Unicode above \uFFFF
298        // The original JavaScript regex includes surrogate pairs that we convert to direct Unicode ranges
299        let github_regex = get_cached_regex(
300            r"[\x00-\x1F!-,\./:-@\[-\^`\{-\xA9\xAB-\xB4\xB6-\xB9\xBB-\xBF\xD7\xF7\u{02C2}-\u{02C5}\u{02D2}-\u{02DF}\u{02E5}-\u{02EB}\u{02ED}\u{02EF}-\u{02FF}\u{0375}\u{0378}\u{0379}\u{037E}\u{0380}-\u{0385}\u{0387}\u{038B}\u{038D}\u{03A2}\u{03F6}\u{0482}\u{0530}\u{0557}\u{0558}\u{055A}-\u{055F}\u{0589}-\u{0590}\u{05BE}\u{05C0}\u{05C3}\u{05C6}\u{05C8}-\u{05CF}\u{05EB}-\u{05EE}\u{05F3}-\u{060F}\u{061B}-\u{061F}\u{066A}-\u{066D}\u{06D4}\u{06DD}\u{06DE}\u{06E9}\u{06FD}\u{06FE}\u{0700}-\u{070F}\u{074B}\u{074C}\u{07B2}-\u{07BF}\u{07F6}-\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}\u{12C6}\u{12C7}\u{12D7}\u{1311}\u{1316}\u{1317}\u{135B}\u{135C}\u{1360}-\u{137F}\u{1390}-\u{139F}\u{13F6}\u{13F7}\u{13FE}-\u{1400}\u{166D}\u{166E}\u{1680}\u{169B}-\u{169F}\u{16EB}-\u{16ED}\u{16F9}-\u{16FF}\u{170D}\u{1715}-\u{171F}\u{1735}-\u{173F}\u{1754}-\u{175F}\u{176D}\u{1771}\u{1774}-\u{177F}\u{17D4}-\u{17D6}\u{17D8}-\u{17DB}\u{17DE}\u{17DF}\u{17EA}-\u{180A}\u{180E}\u{180F}\u{181A}-\u{181F}\u{1879}-\u{187F}\u{18AB}-\u{18AF}\u{18F6}-\u{18FF}\u{191F}\u{192C}-\u{192F}\u{193C}-\u{1945}\u{196E}\u{196F}\u{1975}-\u{197F}\u{19AC}-\u{19AF}\u{19CA}-\u{19CF}\u{19DA}-\u{19FF}\u{1A1C}-\u{1A1F}\u{1A5F}\u{1A7D}\u{1A7E}\u{1A8A}-\u{1A8F}\u{1A9A}-\u{1AA6}\u{1AA8}-\u{1AAF}\u{1AC1}-\u{1AFF}\u{1B4C}-\u{1B4F}\u{1B5A}-\u{1B6A}\u{1B74}-\u{1B7F}\u{1BF4}-\u{1BFF}\u{1C38}-\u{1C3F}\u{1C4A}-\u{1C4C}\u{1C7E}\u{1C7F}\u{1C89}-\u{1C8F}\u{1CBB}\u{1CBC}\u{1CC0}-\u{1CCF}\u{1CD3}\u{1CFB}-\u{1CFF}\u{1DFA}\u{1F16}\u{1F17}\u{1F1E}\u{1F1F}\u{1F46}\u{1F47}\u{1F4E}\u{1F4F}\u{1F58}\u{1F5A}\u{1F5C}\u{1F5E}\u{1F7E}\u{1F7F}\u{1FB5}\u{1FBD}\u{1FBF}-\u{1FC1}\u{1FC5}\u{1FCD}-\u{1FCF}\u{1FD4}\u{1FD5}\u{1FDC}-\u{1FDF}\u{1FED}-\u{1FF1}\u{1FF5}\u{1FFD}-\u{203E}\u{2041}-\u{2053}\u{2055}-\u{2070}\u{2072}-\u{207E}\u{2080}-\u{208F}\u{209D}-\u{20CF}\u{20F1}-\u{2101}\u{2103}-\u{2106}\u{2108}\u{2109}\u{2114}\u{2116}-\u{2118}\u{211E}-\u{2123}\u{2125}\u{2127}\u{2129}\u{212E}\u{213A}\u{213B}\u{2140}-\u{2144}\u{214A}-\u{214D}\u{214F}-\u{215F}\u{2189}-\u{24B5}\u{24EA}-\u{2BFF}\u{2C2F}\u{2C5F}\u{2CE5}-\u{2CEA}\u{2CF4}-\u{2CFF}\u{2D26}\u{2D28}-\u{2D2C}\u{2D2E}\u{2D2F}\u{2D68}-\u{2D6E}\u{2D70}-\u{2D7E}\u{2D97}-\u{2D9F}\u{2DA7}\u{2DAF}\u{2DB7}\u{2DBF}\u{2DC7}\u{2DCF}\u{2DD7}\u{2DDF}\u{2E00}-\u{2E2E}\u{2E30}-\u{3004}\u{3008}-\u{3020}\u{3030}\u{3036}\u{3037}\u{303D}-\u{3040}\u{3097}\u{3098}\u{309B}\u{309C}\u{30A0}\u{30FB}\u{3100}-\u{3104}\u{3130}\u{318F}-\u{319F}\u{31C0}-\u{31EF}\u{3200}-\u{33FF}\u{4DC0}-\u{4DFF}\u{9FFD}-\u{9FFF}\u{A48D}-\u{A4CF}\u{A4FE}\u{A4FF}\u{A60D}-\u{A60F}\u{A62C}-\u{A63F}\u{A673}\u{A67E}\u{A6F2}-\u{A716}\u{A720}\u{A721}\u{A789}\u{A78A}\u{A7C0}\u{A7C1}\u{A7CB}-\u{A7F4}\u{A828}-\u{A82B}\u{A82D}-\u{A83F}\u{A874}-\u{A87F}\u{A8C6}-\u{A8CF}\u{A8DA}-\u{A8DF}\u{A8F8}-\u{A8FA}\u{A8FC}\u{A92E}\u{A92F}\u{A954}-\u{A95F}\u{A97D}-\u{A97F}\u{A9C1}-\u{A9CE}\u{A9DA}-\u{A9DF}\u{A9FF}\u{AA37}-\u{AA3F}\u{AA4E}\u{AA4F}\u{AA5A}-\u{AA5F}\u{AA77}-\u{AA79}\u{AAC3}-\u{AADA}\u{AADE}\u{AADF}\u{AAF0}\u{AAF1}\u{AAF7}-\u{AB00}\u{AB07}\u{AB08}\u{AB0F}\u{AB10}\u{AB17}-\u{AB1F}\u{AB27}\u{AB2F}\u{AB5B}\u{AB6A}-\u{AB6F}\u{ABEB}\u{ABEE}\u{ABEF}\u{ABFA}-\u{ABFF}\u{D7A4}-\u{D7AF}\u{D7C7}-\u{D7CA}\u{D7FC}-\u{D7FF}\u{E000}-\u{F8FF}\u{FA6E}\u{FA6F}\u{FADA}-\u{FAFF}\u{FB07}-\u{FB12}\u{FB18}-\u{FB1C}\u{FB29}\u{FB37}\u{FB3D}\u{FB3F}\u{FB42}\u{FB45}\u{FBB2}-\u{FBD2}\u{FD3E}-\u{FD4F}\u{FD90}\u{FD91}\u{FDC8}-\u{FDEF}\u{FDFC}-\u{FDFF}\u{FE10}-\u{FE1F}\u{FE30}-\u{FE32}\u{FE35}-\u{FE4C}\u{FE50}-\u{FE6F}\u{FE75}\u{FEFD}-\u{FF0F}\u{FF1A}-\u{FF20}\u{FF3B}-\u{FF3E}\u{FF40}\u{FF5B}-\u{FF65}\u{FFBF}-\u{FFC1}\u{FFC8}\u{FFC9}\u{FFD0}\u{FFD1}\u{FFD8}\u{FFD9}\u{FFDD}-\u{FFFF}\u{1000C}\u{10027}\u{1003B}\u{1003E}\u{1004E}\u{1004F}\u{1005E}-\u{1007F}\u{100FB}-\u{1013F}\u{10175}-\u{101FC}\u{101FE}-\u{1027F}\u{1029D}-\u{1029F}\u{102D1}-\u{102DF}\u{102E1}-\u{102FF}\u{10320}-\u{1032C}\u{1034B}-\u{1034F}\u{1037B}-\u{1037F}\u{1039E}\u{1039F}\u{103C4}-\u{103C7}\u{103D0}\u{103D6}-\u{103FF}\u{1049E}\u{1049F}\u{104AA}-\u{104AF}\u{104D4}-\u{104D7}\u{104FC}-\u{104FF}\u{10528}-\u{1052F}\u{10564}-\u{105FF}\u{10737}-\u{1073F}\u{10756}-\u{1075F}\u{10768}-\u{107FF}\u{10806}\u{10807}\u{10809}\u{10836}\u{10839}-\u{1083B}\u{1083D}\u{1083E}\u{10856}-\u{1085F}\u{10877}-\u{1087F}\u{1089F}-\u{108DF}\u{108F3}\u{108F6}-\u{108FF}\u{10916}-\u{1091F}\u{1093A}-\u{1097F}\u{109B8}-\u{109BD}\u{109C0}-\u{109FF}\u{10A04}\u{10A07}-\u{10A0B}\u{10A14}\u{10A18}\u{10A36}\u{10A37}\u{10A3B}-\u{10A3E}\u{10A40}-\u{10A5F}\u{10A7D}-\u{10A7F}\u{10A9D}-\u{10ABF}\u{10AC8}\u{10AE7}-\u{10AFF}\u{10B36}-\u{10B3F}\u{10B56}-\u{10B5F}\u{10B73}-\u{10B7F}\u{10B92}-\u{10BFF}\u{10C49}-\u{10C7F}\u{10CB3}-\u{10CBF}\u{10CF3}-\u{10CFF}\u{10D28}-\u{10D2F}\u{10D3A}-\u{10E7F}\u{10EAA}\u{10EAD}-\u{10EAF}\u{10EB2}-\u{10EFF}\u{10F1D}-\u{10F26}\u{10F28}-\u{10F2F}\u{10F51}-\u{10FAF}\u{10FC5}-\u{10FDF}\u{10FF7}-\u{10FFF}\u{11047}-\u{11065}\u{11070}-\u{1107E}\u{110BB}-\u{110CF}\u{110E9}-\u{110EF}\u{110FA}-\u{110FF}\u{11135}\u{11140}-\u{11143}\u{11148}-\u{1114F}\u{11174}\u{11175}\u{11177}-\u{1117F}\u{111C5}-\u{111C8}\u{111CD}\u{111DB}\u{111DD}-\u{111FF}\u{11212}\u{11238}-\u{1123D}\u{1123F}-\u{1127F}\u{11287}\u{11289}\u{1128E}\u{1129E}\u{112A9}-\u{112AF}\u{112EB}-\u{112EF}\u{112FA}-\u{112FF}\u{11304}\u{1130D}\u{1130E}\u{11311}\u{11312}\u{11329}\u{11331}\u{11334}\u{1133A}\u{11345}\u{11346}\u{11349}\u{1134A}\u{1134E}\u{1134F}\u{11351}-\u{11356}\u{11358}-\u{1135C}\u{11364}\u{11365}\u{1136D}-\u{1136F}\u{11375}-\u{113FF}\u{1144B}-\u{1144F}\u{1145A}-\u{1145D}\u{1145F}-\u{1147F}\u{114C6}\u{114C8}-\u{114CF}\u{114DA}-\u{1157F}\u{115B6}\u{115B7}\u{115C1}-\u{115D7}\u{115DE}-\u{115FF}\u{11641}-\u{11643}\u{11645}-\u{1164F}\u{1165A}-\u{1167F}\u{116B9}-\u{116BF}\u{116CA}-\u{116FF}\u{1171B}-\u{1171C}\u{1172C}-\u{1172F}\u{1173A}-\u{117FF}\u{1183B}-\u{1189F}\u{118EA}-\u{118FE}\u{11900}-\u{1199F}\u{119A8}\u{119A9}\u{119D8}\u{119D9}\u{119E2}\u{119E5}-\u{119FF}\u{11A3F}-\u{11A46}\u{11A48}-\u{11A4F}\u{11A9A}-\u{11A9C}\u{11A9E}-\u{11ABF}\u{11AF9}-\u{11BFF}\u{11C09}\u{11C37}\u{11C41}-\u{11C4F}\u{11C5A}-\u{11C71}\u{11C90}\u{11C91}\u{11CA8}\u{11CB7}-\u{11CFF}\u{11D07}\u{11D0A}\u{11D37}-\u{11D39}\u{11D3B}\u{11D3E}\u{11D48}-\u{11D4F}\u{11D5A}-\u{11D5F}\u{11D66}\u{11D69}\u{11D8F}\u{11D92}\u{11D99}-\u{11D9F}\u{11DAA}-\u{11EDF}\u{11EF7}-\u{11FAF}\u{11FB1}-\u{11FFF}\u{1239A}-\u{123FF}\u{1246F}-\u{1247F}\u{12544}-\u{12FFF}\u{1342F}-\u{143FF}\u{14647}-\u{167FF}\u{16A39}-\u{16A3F}\u{16A5F}\u{16A6A}-\u{16ACF}\u{16AEE}\u{16AEF}\u{16AF5}-\u{16AFF}\u{16B37}-\u{16B3F}\u{16B44}-\u{16B4F}\u{16B5A}-\u{16B62}\u{16B78}-\u{16B7C}\u{16B90}-\u{16EFF}\u{16F4B}-\u{16F4E}\u{16F88}-\u{16F8E}\u{16FA0}-\u{16FDF}\u{16FE2}\u{16FE5}-\u{16FEF}\u{16FF2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BCA}\u{1FBF0}-\u{1FBF9}\u{1FC00}-\u{1FFFF}\u{2A6DE}-\u{2A6FF}\u{2B735}-\u{2B73F}\u{2B81E}\u{2B81F}\u{2CEA2}-\u{2CEAF}\u{2EBE1}-\u{2F7FF}\u{2FA1E}-\u{2FFFF}\u{3134B}-\u{E00FF}\u{E01F0}-\u{10FFFF}]"
301        ).expect("Valid GitHub regex pattern");
302
303        // Remove all punctuation and symbols matched by the regex
304        let result = github_regex.replace_all(&text, "");
305
306        // 3. Replace spaces with hyphens (/ /g, '-')
307        let with_hyphens = result.replace(' ', "-");
308
309        // 4. Apply kramdown-style hyphen consolidation for kramdown-gfm compatibility
310        Self::consolidate_hyphens_kramdown_style(&with_hyphens)
311    }
312
313    /// Fragment generation following kramdown GFM's EXACT algorithm
314    /// Based on comprehensive testing with kramdown 2.5.1 using official Ruby gems
315    /// This implementation matches the exact behavior verified through kramdown GFM processing
316    #[inline]
317    pub fn heading_to_fragment_jekyll_official(&self, heading: &str) -> String {
318        if heading.is_empty() {
319            return String::new();
320        }
321
322        // Strip markdown formatting first
323        let text = if QUICK_MARKDOWN_CHECK.is_match(heading) {
324            self.strip_markdown_formatting_fast(heading)
325        } else {
326            heading.to_string()
327        };
328
329        // Kramdown GFM's EXACT algorithm verified through official gem testing:
330
331        // 1. Convert to lowercase
332        let text = text.to_lowercase();
333
334        let mut result = String::with_capacity(text.len());
335
336        // First, do pattern-based replacements (verified kramdown behavior)
337        let mut processed_text = text.clone();
338
339        // Replace space-ampersand-space with double hyphen (verified behavior)
340        processed_text = processed_text.replace(" & ", "--");
341        processed_text = processed_text.replace(" © ", "--");
342
343        // Now process character by character
344        for c in processed_text.chars() {
345            // Single character processing based on verified kramdown behavior
346            match c {
347                // Preserve all letters (including Unicode) - verified behavior
348                c if c.is_alphabetic() => result.push(c),
349
350                // Preserve all digits - verified behavior
351                c if c.is_ascii_digit() => result.push(c),
352
353                // Preserve underscores (critical difference from GitHub) - verified behavior
354                '_' => result.push(c),
355
356                // Preserve hyphens (no special arrow handling) - verified behavior
357                '-' => result.push('-'),
358
359                // Convert spaces to hyphens - verified behavior
360                ' ' => result.push('-'),
361
362                // Remaining symbols that weren't pattern-replaced
363                '&' => result.push_str("--"), // For cases without surrounding spaces
364                '©' => result.push_str("--"), // For cases without surrounding spaces
365
366                // Emojis become single hyphens - verified behavior
367                c if (c as u32 >= 0x1F300 && c as u32 <= 0x1F9FF)
368                    || (c as u32 >= 0x1F600 && c as u32 <= 0x1F64F)
369                    || (c as u32 >= 0x2600 && c as u32 <= 0x26FF) =>
370                {
371                    result.push('-')
372                }
373
374                // Most punctuation is removed - verified behavior
375                ':' | '.' | '!' | '?' | '$' | '%' | '@' | '(' | ')' | '[' | ']' | '{' | '}' | '<' | '>' | '/'
376                | '\\' | '|' | '"' | '\'' | '`' | '~' | '^' | '*' | '+' | '=' | '#' | '°' => {
377                    // Remove these characters
378                }
379
380                // Unicode punctuation and symbols - handle em-dashes specifically
381                '—' | '–' => {
382                    // Em-dashes and en-dashes are removed (verified behavior)
383                }
384
385                // Handle ellipsis and other Unicode punctuation
386                '…' => {
387                    // Remove ellipsis
388                }
389
390                // Default: preserve other Unicode characters (verified behavior)
391                _ => {
392                    // Keep other Unicode characters as kramdown preserves them
393                    result.push(c);
394                }
395            }
396        }
397
398        // Remove leading non-letter characters until first letter (verified behavior)
399        let mut start_pos = 0;
400        for (pos, c) in result.char_indices() {
401            if c.is_alphabetic() {
402                start_pos = pos;
403                break;
404            }
405        }
406
407        let result = if start_pos > 0 {
408            result[start_pos..].to_string()
409        } else {
410            result
411        };
412
413        // Only remove trailing punctuation that isn't hyphens/underscores (verified behavior)
414        // Kramdown preserves trailing hyphens but removes other punctuation
415        let result = result
416            .trim_end_matches(|c: char| {
417                matches!(
418                    c,
419                    ':' | '.'
420                        | '!'
421                        | '?'
422                        | '$'
423                        | '%'
424                        | '@'
425                        | '('
426                        | ')'
427                        | '['
428                        | ']'
429                        | '{'
430                        | '}'
431                        | '<'
432                        | '>'
433                        | '/'
434                        | '\\'
435                        | '|'
436                        | '"'
437                        | '\''
438                        | '`'
439                        | '~'
440                        | '^'
441                        | '*'
442                        | '+'
443                        | '='
444                        | '#'
445                        | '°'
446                )
447            })
448            .to_string();
449
450        // If empty or only punctuation, return "section" (verified kramdown behavior)
451        if result.is_empty() || result.chars().all(|c| !c.is_alphanumeric() && c != '_') {
452            "section".to_string()
453        } else {
454            result
455        }
456    }
457
458    /// Strip markdown formatting from heading text (optimized for common patterns)
459    fn strip_markdown_formatting_fast(&self, text: &str) -> String {
460        let mut result = text.to_string();
461
462        // Strip emphasis (only asterisks, underscores are preserved per GitHub spec)
463        if result.contains('*') {
464            result = EMPHASIS_PATTERN.replace_all(&result, "$1").to_string();
465        }
466
467        // Strip inline code
468        if result.contains('`') {
469            result = CODE_PATTERN.replace_all(&result, "$1").to_string();
470        }
471
472        // Strip links (GitHub keeps both link text and URL)
473        if result.contains('[') {
474            result = LINK_PATTERN.replace_all(&result, "$1$2$3").to_string();
475        }
476
477        result
478    }
479
480    /// Fast check if URL is external (doesn't need to be validated)
481    #[inline]
482    fn is_external_url_fast(url: &str) -> bool {
483        // Quick prefix checks for common protocols
484        url.starts_with("http://")
485            || url.starts_with("https://")
486            || url.starts_with("ftp://")
487            || url.starts_with("mailto:")
488            || url.starts_with("tel:")
489            || url.starts_with("//")
490    }
491
492    /// Check if URL is a cross-file link (contains a file path before #)
493    #[inline]
494    fn is_cross_file_link(url: &str) -> bool {
495        if let Some(fragment_pos) = url.find('#') {
496            let path_part = &url[..fragment_pos];
497
498            // If there's no path part, it's just a fragment (#heading)
499            if path_part.is_empty() {
500                return false;
501            }
502
503            // Check for Liquid syntax used by Jekyll and other static site generators
504            // Liquid tags: {% ... %} for control flow and includes
505            // Liquid variables: {{ ... }} for outputting values
506            // These are template directives that reference external content and should be skipped
507            // We check for proper bracket order to avoid false positives
508            if let Some(tag_start) = path_part.find("{%")
509                && path_part[tag_start + 2..].contains("%}")
510            {
511                return true;
512            }
513            if let Some(var_start) = path_part.find("{{")
514                && path_part[var_start + 2..].contains("}}")
515            {
516                return true;
517            }
518
519            // Check if it's an absolute path (starts with /)
520            // These are links to other pages on the same site
521            if path_part.starts_with('/') {
522                return true;
523            }
524
525            // Check if it looks like a file path:
526            // - Contains a file extension (dot followed by letters)
527            // - Contains path separators
528            // - Contains relative path indicators
529            path_part.contains('.')
530                && (
531                    // Has file extension pattern (handle query parameters by splitting on them first)
532                    {
533                    let clean_path = path_part.split('?').next().unwrap_or(path_part);
534                    // Handle files starting with dot
535                    if let Some(after_dot) = clean_path.strip_prefix('.') {
536                        let dots_count = clean_path.matches('.').count();
537                        if dots_count == 1 {
538                            // Could be ".ext" (just extension) or ".hidden" (hidden file)
539                            // If it's a known file extension, treat as cross-file link
540                            !after_dot.is_empty() && after_dot.len() <= 10 &&
541                            after_dot.chars().all(|c| c.is_ascii_alphanumeric()) &&
542                            // Additional check: common file extensions are likely cross-file
543                            (after_dot.len() <= 4 || matches!(after_dot, "html" | "json" | "yaml" | "toml"))
544                        } else {
545                            // Hidden file with extension like ".hidden.txt"
546                            clean_path.split('.').next_back().is_some_and(|ext| {
547                                !ext.is_empty() && ext.len() <= 10 && ext.chars().all(|c| c.is_ascii_alphanumeric())
548                            })
549                        }
550                    } else {
551                        // Regular file path
552                        clean_path.split('.').next_back().is_some_and(|ext| {
553                            !ext.is_empty() && ext.len() <= 10 && ext.chars().all(|c| c.is_ascii_alphanumeric())
554                        })
555                    }
556                } ||
557                // Or contains path separators
558                path_part.contains('/') || path_part.contains('\\') ||
559                // Or starts with relative path indicators
560                path_part.starts_with("./") || path_part.starts_with("../")
561                )
562        } else {
563            false
564        }
565    }
566
567    /// Apply kramdown-style hyphen consolidation based on observed patterns
568    /// This is a pragmatic implementation to match the most common kramdown GFM cases
569    fn consolidate_hyphens_kramdown_style(text: &str) -> String {
570        // For the specific problematic cases, apply targeted fixes
571        // This handles the major kramdown GFM compatibility issues
572
573        // Pattern: ---- (4 hyphens) becomes -- (2 hyphens)
574        // Pattern: --- (3 hyphens) becomes - (1 hyphen)
575        // Pattern: -- (2 hyphens) stays -- (2 hyphens)
576        // Pattern: - (1 hyphen) stays - (1 hyphen)
577
578        let consolidated = text
579            .replace("----", "--")  // 4 → 2
580            .replace("---", "-"); // 3 → 1
581
582        // Remove leading and trailing hyphens (kramdown behavior)
583        consolidated.trim_matches('-').to_string()
584    }
585}
586
587impl Rule for MD051LinkFragments {
588    fn name(&self) -> &'static str {
589        "MD051"
590    }
591
592    fn description(&self) -> &'static str {
593        "Link fragments should reference valid headings"
594    }
595
596    fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
597        // Skip if no link fragments present
598        !ctx.content.contains("#")
599    }
600
601    fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
602        let mut warnings = Vec::new();
603        let content = ctx.content;
604
605        // Skip empty content
606        if content.is_empty() {
607            return Ok(warnings);
608        }
609
610        // Extract all valid heading anchors (markdown and HTML)
611        let (markdown_headings, html_anchors) = self.extract_headings_from_context(ctx);
612
613        // Find all links with fragments
614        let link_regex = get_cached_regex(r"\[([^\]]+)\]\(([^)]+)\)").unwrap();
615        let lines: Vec<&str> = content.lines().collect();
616
617        for (line_num, line) in lines.iter().enumerate() {
618            // Skip front matter
619            if ctx.lines[line_num].in_front_matter {
620                continue;
621            }
622
623            // Skip code blocks
624            if ctx.lines[line_num].in_code_block {
625                continue;
626            }
627
628            for cap in link_regex.captures_iter(line) {
629                if let Some(url_match) = cap.get(2) {
630                    let url = url_match.as_str();
631                    let full_match = cap.get(0).unwrap(); // Get the entire link match
632
633                    // Calculate byte position for this match within the entire content
634                    let line_byte_offset = if line_num == 0 {
635                        0
636                    } else {
637                        content.lines().take(line_num).map(|l| l.len() + 1).sum::<usize>() // +1 for newline
638                    };
639                    let match_byte_pos = line_byte_offset + full_match.start();
640
641                    // Skip links in code blocks or inline code spans
642                    if ctx.is_in_code_block_or_span(match_byte_pos) {
643                        continue;
644                    }
645
646                    // Check if this URL contains a fragment
647                    if url.contains('#') && !Self::is_external_url_fast(url) {
648                        // If it's a cross-file link, skip validation as the target file may not be in the current context
649                        if Self::is_cross_file_link(url) {
650                            continue;
651                        }
652
653                        // Extract fragment (everything after #)
654                        if let Some(fragment_pos) = url.find('#') {
655                            let fragment = &url[fragment_pos + 1..];
656
657                            // Handle Liquid template syntax with filters
658                            // If we're inside a Liquid variable ({{ ... }}), extract just the fragment part
659                            // before any filters (marked by |)
660                            if url.contains("{{") && fragment.contains('|') {
661                                // For patterns like {{ "/tags#alternative-data-streams" | relative_url }}
662                                // We want to skip this entirely as it's a Liquid template
663                                continue;
664                            }
665
666                            // Also check if the fragment ends with template syntax that wasn't caught
667                            if fragment.ends_with("}}") || fragment.ends_with("%}") {
668                                // This is likely part of a Liquid template, skip it
669                                continue;
670                            }
671
672                            // Skip empty fragments
673                            if fragment.is_empty() {
674                                continue;
675                            }
676
677                            // Check if fragment exists in document
678                            // HTML anchors are case-sensitive, Markdown anchors are case-insensitive
679                            let found = if html_anchors.contains(fragment) {
680                                // Found as case-sensitive HTML anchor
681                                true
682                            } else {
683                                // Check case-insensitive for Markdown anchors
684                                let fragment_lower = fragment.to_lowercase();
685                                markdown_headings.iter().any(|h| h.to_lowercase() == fragment_lower)
686                            };
687                            if !found {
688                                let column = full_match.start() + 1; // Point to start of entire link
689
690                                warnings.push(LintWarning {
691                                    rule_name: Some(self.name()),
692                                    message: format!("Link anchor '#{fragment}' does not exist in document headings"),
693                                    line: line_num + 1,
694                                    column,
695                                    end_line: line_num + 1,
696                                    end_column: full_match.end() + 1, // End of entire link
697                                    severity: Severity::Warning,
698                                    fix: None, // No auto-fix per industry standard
699                                });
700                            }
701                        }
702                    }
703                }
704            }
705        }
706
707        Ok(warnings)
708    }
709
710    fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
711        // MD051 does not provide auto-fix
712        // Link fragment corrections require human judgment to avoid incorrect fixes
713        Ok(ctx.content.to_string())
714    }
715
716    fn as_any(&self) -> &dyn std::any::Any {
717        self
718    }
719
720    fn from_config(config: &crate::config::Config) -> Box<dyn Rule>
721    where
722        Self: Sized,
723    {
724        // Config keys are normalized to kebab-case by the config system
725        let anchor_style = if let Some(rule_config) = config.rules.get("MD051") {
726            if let Some(style_str) = rule_config.values.get("anchor-style").and_then(|v| v.as_str()) {
727                match style_str.to_lowercase().as_str() {
728                    "kramdown" => AnchorStyle::Kramdown,
729                    "kramdown-gfm" => AnchorStyle::KramdownGfm,
730                    "jekyll" => AnchorStyle::KramdownGfm, // Backward compatibility alias
731                    _ => AnchorStyle::GitHub,
732                }
733            } else {
734                AnchorStyle::GitHub
735            }
736        } else {
737            AnchorStyle::GitHub
738        };
739
740        Box::new(MD051LinkFragments::with_anchor_style(anchor_style))
741    }
742
743    fn default_config_section(&self) -> Option<(String, toml::Value)> {
744        let value: toml::Value = toml::from_str(
745            r#"
746# Anchor generation style to match your target platform
747# Options: "github" (default), "kramdown-gfm", "kramdown"
748# Note: "jekyll" is accepted as an alias for "kramdown-gfm" (backward compatibility)
749anchor-style = "github"
750"#,
751        )
752        .ok()?;
753        Some(("MD051".to_string(), value))
754    }
755}
756
757#[cfg(test)]
758mod tests {}