rumdl_lib/rules/
md021_no_multiple_space_closed_atx.rs

1/// Rule MD021: No multiple spaces inside closed ATX heading
2///
3/// See [docs/md021.md](../../docs/md021.md) for full documentation, configuration, and examples.
4use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, RuleCategory, Severity};
5use crate::utils::range_utils::{LineIndex, calculate_line_range};
6use crate::utils::regex_cache::get_cached_regex;
7
8// Regex patterns
9const CLOSED_ATX_MULTIPLE_SPACE_PATTERN_STR: &str = r"^(\s*)(#+)(\s+)(.*?)(\s+)(#+)\s*$";
10
11#[derive(Clone)]
12pub struct MD021NoMultipleSpaceClosedAtx;
13
14impl Default for MD021NoMultipleSpaceClosedAtx {
15    fn default() -> Self {
16        Self::new()
17    }
18}
19
20impl MD021NoMultipleSpaceClosedAtx {
21    pub fn new() -> Self {
22        Self
23    }
24
25    fn is_closed_atx_heading_with_multiple_spaces(&self, line: &str) -> bool {
26        if let Some(captures) = get_cached_regex(CLOSED_ATX_MULTIPLE_SPACE_PATTERN_STR)
27            .ok()
28            .and_then(|re| re.captures(line))
29        {
30            let start_spaces = captures.get(3).unwrap().as_str().len();
31            let end_spaces = captures.get(5).unwrap().as_str().len();
32            start_spaces > 1 || end_spaces > 1
33        } else {
34            false
35        }
36    }
37
38    fn fix_closed_atx_heading(&self, line: &str) -> String {
39        if let Some(captures) = get_cached_regex(CLOSED_ATX_MULTIPLE_SPACE_PATTERN_STR)
40            .ok()
41            .and_then(|re| re.captures(line))
42        {
43            let indentation = &captures[1];
44            let opening_hashes = &captures[2];
45            let content = &captures[4];
46            let closing_hashes = &captures[6];
47            format!(
48                "{}{} {} {}",
49                indentation,
50                opening_hashes,
51                content.trim(),
52                closing_hashes
53            )
54        } else {
55            line.to_string()
56        }
57    }
58
59    fn count_spaces(&self, line: &str) -> (usize, usize) {
60        if let Some(captures) = get_cached_regex(CLOSED_ATX_MULTIPLE_SPACE_PATTERN_STR)
61            .ok()
62            .and_then(|re| re.captures(line))
63        {
64            let start_spaces = captures.get(3).unwrap().as_str().len();
65            let end_spaces = captures.get(5).unwrap().as_str().len();
66            (start_spaces, end_spaces)
67        } else {
68            (0, 0)
69        }
70    }
71}
72
73impl Rule for MD021NoMultipleSpaceClosedAtx {
74    fn name(&self) -> &'static str {
75        "MD021"
76    }
77
78    fn description(&self) -> &'static str {
79        "Multiple spaces inside hashes on closed heading"
80    }
81
82    fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
83        let line_index = LineIndex::new(ctx.content.to_string());
84        let mut warnings = Vec::new();
85
86        // Check all closed ATX headings from cached info
87        for (line_num, line_info) in ctx.lines.iter().enumerate() {
88            if let Some(heading) = &line_info.heading {
89                // Skip headings indented 4+ spaces (they're code blocks)
90                if line_info.indent >= 4 {
91                    continue;
92                }
93
94                // Only check closed ATX headings
95                if matches!(heading.style, crate::lint_context::HeadingStyle::ATX) && heading.has_closing_sequence {
96                    let line = &line_info.content;
97
98                    // Check if line matches closed ATX pattern with multiple spaces
99                    if self.is_closed_atx_heading_with_multiple_spaces(line) {
100                        let captures = get_cached_regex(CLOSED_ATX_MULTIPLE_SPACE_PATTERN_STR)
101                            .ok()
102                            .and_then(|re| re.captures(line))
103                            .unwrap();
104                        let _indentation = captures.get(1).unwrap();
105                        let opening_hashes = captures.get(2).unwrap();
106                        let (start_spaces, end_spaces) = self.count_spaces(line);
107
108                        let message = if start_spaces > 1 && end_spaces > 1 {
109                            format!(
110                                "Multiple spaces ({} at start, {} at end) inside hashes on closed heading (with {} at start and end)",
111                                start_spaces,
112                                end_spaces,
113                                "#".repeat(opening_hashes.as_str().len())
114                            )
115                        } else if start_spaces > 1 {
116                            format!(
117                                "Multiple spaces ({}) after {} at start of closed heading",
118                                start_spaces,
119                                "#".repeat(opening_hashes.as_str().len())
120                            )
121                        } else {
122                            format!(
123                                "Multiple spaces ({}) before {} at end of closed heading",
124                                end_spaces,
125                                "#".repeat(opening_hashes.as_str().len())
126                            )
127                        };
128
129                        // Replace the entire line with the fixed version
130                        let (start_line, start_col, end_line, end_col) = calculate_line_range(line_num + 1, line);
131                        let replacement = self.fix_closed_atx_heading(line);
132
133                        warnings.push(LintWarning {
134                            rule_name: Some(self.name()),
135                            message,
136                            line: start_line,
137                            column: start_col,
138                            end_line,
139                            end_column: end_col,
140                            severity: Severity::Warning,
141                            fix: Some(Fix {
142                                range: line_index.line_col_to_byte_range_with_length(start_line, 1, line.len()),
143                                replacement,
144                            }),
145                        });
146                    }
147                }
148            }
149        }
150
151        Ok(warnings)
152    }
153
154    fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
155        let mut lines = Vec::new();
156
157        for line_info in ctx.lines.iter() {
158            let mut fixed = false;
159
160            if let Some(heading) = &line_info.heading {
161                // Skip headings indented 4+ spaces (they're code blocks)
162                if line_info.indent >= 4 {
163                    lines.push(line_info.content.clone());
164                    continue;
165                }
166
167                // Fix closed ATX headings with multiple spaces
168                if matches!(heading.style, crate::lint_context::HeadingStyle::ATX)
169                    && heading.has_closing_sequence
170                    && self.is_closed_atx_heading_with_multiple_spaces(&line_info.content)
171                {
172                    lines.push(self.fix_closed_atx_heading(&line_info.content));
173                    fixed = true;
174                }
175            }
176
177            if !fixed {
178                lines.push(line_info.content.clone());
179            }
180        }
181
182        // Reconstruct content preserving line endings
183        let mut result = lines.join("\n");
184        if ctx.content.ends_with('\n') && !result.ends_with('\n') {
185            result.push('\n');
186        }
187
188        Ok(result)
189    }
190
191    /// Get the category of this rule for selective processing
192    fn category(&self) -> RuleCategory {
193        RuleCategory::Heading
194    }
195
196    /// Check if this rule should be skipped
197    fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
198        ctx.content.is_empty() || !ctx.likely_has_headings()
199    }
200
201    fn as_any(&self) -> &dyn std::any::Any {
202        self
203    }
204
205    fn from_config(_config: &crate::config::Config) -> Box<dyn Rule>
206    where
207        Self: Sized,
208    {
209        Box::new(MD021NoMultipleSpaceClosedAtx::new())
210    }
211}
212
213#[cfg(test)]
214mod tests {
215    use super::*;
216    use crate::lint_context::LintContext;
217
218    #[test]
219    fn test_basic_functionality() {
220        let rule = MD021NoMultipleSpaceClosedAtx;
221
222        // Test with correct spacing
223        let content = "# Heading 1 #\n## Heading 2 ##\n### Heading 3 ###";
224        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
225        let result = rule.check(&ctx).unwrap();
226        assert!(result.is_empty());
227
228        // Test with multiple spaces
229        let content = "#  Heading 1 #\n## Heading 2 ##\n### Heading 3  ###";
230        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
231        let result = rule.check(&ctx).unwrap();
232        assert_eq!(result.len(), 2); // Should flag the two headings with multiple spaces
233        assert_eq!(result[0].line, 1);
234        assert_eq!(result[1].line, 3);
235    }
236}