rumdl_lib/rules/
md019_no_multiple_space_atx.rs1use 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 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 for (line_num, line_info) in ctx.lines.iter().enumerate() {
42 if let Some(heading) = &line_info.heading {
43 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 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 let (start_line, start_col, end_line, end_col) = calculate_single_line_range(
56 line_num + 1, marker_pos + 1, space_count, );
60
61 let line_start_byte = ctx.line_index.get_line_start_byte(line_num + 1).unwrap_or(0);
63
64 let original_line = line_info.content(ctx.content);
66 let marker_byte_pos = line_start_byte + line_info.indent + heading.marker.len();
67
68 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(), }),
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 line_info in ctx.lines.iter() {
110 let mut fixed = false;
111
112 if let Some(heading) = &line_info.heading {
113 if matches!(heading.style, crate::lint_context::HeadingStyle::ATX) {
115 let line = line_info.content(ctx.content);
116 let trimmed = line.trim_start();
117
118 if trimmed.len() > heading.marker.len() {
119 let space_count = self.count_spaces_after_marker(trimmed, heading.marker.len());
120
121 if space_count > 1 {
122 let line = line_info.content(ctx.content);
124 let original_indent = &line[..line_info.indent];
125 lines.push(format!(
126 "{original_indent}{} {}",
127 heading.marker,
128 trimmed[heading.marker.len()..].trim_start()
129 ));
130 fixed = true;
131 }
132 }
133 }
134 }
135
136 if !fixed {
137 lines.push(line_info.content(ctx.content).to_string());
138 }
139 }
140
141 let mut result = lines.join("\n");
143 if ctx.content.ends_with('\n') && !result.ends_with('\n') {
144 result.push('\n');
145 }
146
147 Ok(result)
148 }
149
150 fn category(&self) -> RuleCategory {
152 RuleCategory::Heading
153 }
154
155 fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
157 ctx.content.is_empty() || !ctx.likely_has_headings()
158 }
159
160 fn as_any(&self) -> &dyn std::any::Any {
161 self
162 }
163
164 fn from_config(_config: &crate::config::Config) -> Box<dyn Rule>
165 where
166 Self: Sized,
167 {
168 Box::new(MD019NoMultipleSpaceAtx::new())
169 }
170}
171
172#[cfg(test)]
173mod tests {
174 use super::*;
175
176 #[test]
177 fn test_basic_functionality() {
178 let rule = MD019NoMultipleSpaceAtx::new();
179
180 let content = "# Multiple Spaces\n\nRegular content\n\n## More Spaces";
182 let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
183 let result = rule.check(&ctx).unwrap();
184 assert_eq!(result.len(), 2); assert_eq!(result[0].line, 1);
186 assert_eq!(result[1].line, 5);
187
188 let content = "# Single Space\n\n## Also correct";
190 let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
191 let result = rule.check(&ctx).unwrap();
192 assert!(
193 result.is_empty(),
194 "Properly formatted headings should not generate warnings"
195 );
196 }
197}