Skip to main content

rumdl_lib/rules/
md019_no_multiple_space_atx.rs

1/// Rule MD019: No multiple spaces after ATX heading marker
2///
3/// See [docs/md019.md](../../docs/md019.md) for full documentation, configuration, and examples.
4use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, RuleCategory, Severity};
5use crate::utils::range_utils::calculate_single_line_range;
6
7#[derive(Clone)]
8pub struct MD019NoMultipleSpaceAtx;
9
10impl Default for MD019NoMultipleSpaceAtx {
11    fn default() -> Self {
12        Self::new()
13    }
14}
15
16impl MD019NoMultipleSpaceAtx {
17    pub fn new() -> Self {
18        Self
19    }
20
21    /// Count spaces after the ATX marker
22    fn count_spaces_after_marker(&self, line: &str, marker_len: usize) -> usize {
23        let after_marker = &line[marker_len..];
24        after_marker.chars().take_while(|c| *c == ' ' || *c == '\t').count()
25    }
26}
27
28impl Rule for MD019NoMultipleSpaceAtx {
29    fn name(&self) -> &'static str {
30        "MD019"
31    }
32
33    fn description(&self) -> &'static str {
34        "Multiple spaces after hash in heading"
35    }
36
37    fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
38        let mut warnings = Vec::new();
39
40        // Check all ATX headings from cached info
41        for (line_num, line_info) in ctx.lines.iter().enumerate() {
42            if let Some(heading) = &line_info.heading {
43                // Only check ATX headings
44                if matches!(heading.style, crate::lint_context::HeadingStyle::ATX) {
45                    let line = line_info.content(ctx.content);
46                    let trimmed = line.trim_start();
47                    let marker_pos = line_info.indent + heading.marker.len();
48
49                    // Count spaces after marker
50                    if trimmed.len() > heading.marker.len() {
51                        let space_count = self.count_spaces_after_marker(trimmed, heading.marker.len());
52
53                        if space_count > 1 {
54                            // Calculate range for the extra spaces
55                            let (start_line, start_col, end_line, end_col) = calculate_single_line_range(
56                                line_num + 1,   // Convert to 1-indexed
57                                marker_pos + 1, // Start after marker (1-indexed)
58                                space_count,    // Length of all spaces (not just extra)
59                            );
60
61                            // Calculate byte range for just the extra spaces
62                            let line_start_byte = ctx.line_index.get_line_start_byte(line_num + 1).unwrap_or(0);
63
64                            // We need to work with the original line, not trimmed
65                            let original_line = line_info.content(ctx.content);
66                            let marker_byte_pos = line_start_byte + line_info.indent + heading.marker.len();
67
68                            // Get the actual byte length of the spaces/tabs after the marker
69                            let after_marker_start = line_info.indent + heading.marker.len();
70                            let after_marker = &original_line[after_marker_start..];
71                            let space_bytes = after_marker
72                                .as_bytes()
73                                .iter()
74                                .take_while(|&&b| b == b' ' || b == b'\t')
75                                .count();
76
77                            let extra_spaces_start = marker_byte_pos;
78                            let extra_spaces_end = marker_byte_pos + space_bytes;
79
80                            warnings.push(LintWarning {
81                                rule_name: Some(self.name().to_string()),
82                                message: format!(
83                                    "Multiple spaces ({}) after {} in heading",
84                                    space_count,
85                                    "#".repeat(heading.level as usize)
86                                ),
87                                line: start_line,
88                                column: start_col,
89                                end_line,
90                                end_column: end_col,
91                                severity: Severity::Warning,
92                                fix: Some(Fix {
93                                    range: extra_spaces_start..extra_spaces_end,
94                                    replacement: " ".to_string(), // Replace extra spaces with single space
95                                }),
96                            });
97                        }
98                    }
99                }
100            }
101        }
102
103        Ok(warnings)
104    }
105
106    fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
107        let mut lines = Vec::new();
108
109        for (i, line_info) in ctx.lines.iter().enumerate() {
110            let line_num = i + 1;
111            // If rule is disabled for this line, keep original
112            if ctx.inline_config().is_rule_disabled(self.name(), line_num) {
113                lines.push(line_info.content(ctx.content).to_string());
114                continue;
115            }
116
117            let mut fixed = false;
118
119            if let Some(heading) = &line_info.heading {
120                // Fix ATX headings with multiple spaces
121                if matches!(heading.style, crate::lint_context::HeadingStyle::ATX) {
122                    let line = line_info.content(ctx.content);
123                    let trimmed = line.trim_start();
124
125                    if trimmed.len() > heading.marker.len() {
126                        let space_count = self.count_spaces_after_marker(trimmed, heading.marker.len());
127
128                        if space_count > 1 {
129                            // Normalize to single space, preserving original indentation (including tabs)
130                            let line = line_info.content(ctx.content);
131                            let original_indent = &line[..line_info.indent];
132                            lines.push(format!(
133                                "{original_indent}{} {}",
134                                heading.marker,
135                                trimmed[heading.marker.len()..].trim_start()
136                            ));
137                            fixed = true;
138                        }
139                    }
140                }
141            }
142
143            if !fixed {
144                lines.push(line_info.content(ctx.content).to_string());
145            }
146        }
147
148        // Reconstruct content preserving line endings
149        let mut result = lines.join("\n");
150        if ctx.content.ends_with('\n') && !result.ends_with('\n') {
151            result.push('\n');
152        }
153
154        Ok(result)
155    }
156
157    /// Get the category of this rule for selective processing
158    fn category(&self) -> RuleCategory {
159        RuleCategory::Heading
160    }
161
162    /// Check if this rule should be skipped
163    fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
164        ctx.content.is_empty() || !ctx.likely_has_headings()
165    }
166
167    fn as_any(&self) -> &dyn std::any::Any {
168        self
169    }
170
171    fn from_config(_config: &crate::config::Config) -> Box<dyn Rule>
172    where
173        Self: Sized,
174    {
175        Box::new(MD019NoMultipleSpaceAtx::new())
176    }
177}
178
179#[cfg(test)]
180mod tests {
181    use super::*;
182
183    #[test]
184    fn test_basic_functionality() {
185        let rule = MD019NoMultipleSpaceAtx::new();
186
187        // Test with heading that has multiple spaces
188        let content = "#  Multiple Spaces\n\nRegular content\n\n##   More Spaces";
189        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
190        let result = rule.check(&ctx).unwrap();
191        assert_eq!(result.len(), 2); // Should flag both headings
192        assert_eq!(result[0].line, 1);
193        assert_eq!(result[1].line, 5);
194
195        // Test with proper headings
196        let content = "# Single Space\n\n## Also correct";
197        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
198        let result = rule.check(&ctx).unwrap();
199        assert!(
200            result.is_empty(),
201            "Properly formatted headings should not generate warnings"
202        );
203    }
204}