rumdl_lib/rules/
md001_heading_increment.rs

1use crate::HeadingStyle;
2use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, RuleCategory, Severity};
3use crate::rules::heading_utils::HeadingUtils;
4use crate::utils::range_utils::calculate_heading_range;
5
6/// Rule MD001: Heading levels should only increment by one level at a time
7///
8/// See [docs/md001.md](../../docs/md001.md) for full documentation, configuration, and examples.
9///
10/// This rule enforces a fundamental principle of document structure: heading levels
11/// should increase by exactly one level at a time to maintain a proper document hierarchy.
12///
13/// ## Purpose
14///
15/// Proper heading structure creates a logical document outline and improves:
16/// - Readability for humans
17/// - Accessibility for screen readers
18/// - Navigation in rendered documents
19/// - Automatic generation of tables of contents
20///
21/// ## Examples
22///
23/// ### Correct Heading Structure
24/// ```markdown
25/// # Heading 1
26/// ## Heading 2
27/// ### Heading 3
28/// ## Another Heading 2
29/// ```
30///
31/// ### Incorrect Heading Structure
32/// ```markdown
33/// # Heading 1
34/// ### Heading 3 (skips level 2)
35/// #### Heading 4
36/// ```
37///
38/// ## Behavior
39///
40/// This rule:
41/// - Tracks the heading level throughout the document
42/// - Validates that each new heading is at most one level deeper than the previous heading
43/// - Allows heading levels to decrease by any amount (e.g., going from ### to #)
44/// - Works with both ATX (`#`) and Setext (underlined) heading styles
45///
46/// ## Fix Behavior
47///
48/// When applying automatic fixes, this rule:
49/// - Changes the level of non-compliant headings to be one level deeper than the previous heading
50/// - Preserves the original heading style (ATX or Setext)
51/// - Maintains indentation and other formatting
52///
53/// ## Rationale
54///
55/// Skipping heading levels (e.g., from `h1` to `h3`) can confuse readers and screen readers
56/// by creating gaps in the document structure. Consistent heading increments create a proper
57/// hierarchical outline essential for well-structured documents.
58///
59#[derive(Debug, Default, Clone)]
60pub struct MD001HeadingIncrement;
61
62impl Rule for MD001HeadingIncrement {
63    fn name(&self) -> &'static str {
64        "MD001"
65    }
66
67    fn description(&self) -> &'static str {
68        "Heading levels should only increment by one level at a time"
69    }
70
71    fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
72        let mut warnings = Vec::new();
73        let mut prev_level: Option<usize> = None;
74
75        // Process headings using cached heading information
76        for (line_num, line_info) in ctx.lines.iter().enumerate() {
77            if let Some(heading) = &line_info.heading {
78                let level = heading.level as usize;
79
80                // Check if this heading level is more than one level deeper than the previous
81                if let Some(prev) = prev_level
82                    && level > prev + 1
83                {
84                    let indentation = line_info.indent;
85                    let heading_text = &heading.text;
86
87                    // Map heading style
88                    let style = match heading.style {
89                        crate::lint_context::HeadingStyle::ATX => HeadingStyle::Atx,
90                        crate::lint_context::HeadingStyle::Setext1 => HeadingStyle::Setext1,
91                        crate::lint_context::HeadingStyle::Setext2 => HeadingStyle::Setext2,
92                    };
93
94                    // Create a fix with the correct heading level
95                    let fixed_level = prev + 1;
96                    let replacement = HeadingUtils::convert_heading_style(heading_text, fixed_level as u32, style);
97
98                    // Calculate precise range: highlight the entire heading
99                    let line_content = line_info.content(ctx.content);
100                    let (start_line, start_col, end_line, end_col) =
101                        calculate_heading_range(line_num + 1, line_content);
102
103                    warnings.push(LintWarning {
104                        rule_name: Some(self.name().to_string()),
105                        line: start_line,
106                        column: start_col,
107                        end_line,
108                        end_column: end_col,
109                        message: format!("Expected heading level {}, but found heading level {}", prev + 1, level),
110                        severity: Severity::Warning,
111                        fix: Some(Fix {
112                            range: ctx.line_index.line_content_range(line_num + 1),
113                            replacement: format!("{}{}", " ".repeat(indentation), replacement),
114                        }),
115                    });
116                }
117
118                prev_level = Some(level);
119            }
120        }
121
122        Ok(warnings)
123    }
124
125    fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
126        let mut fixed_lines = Vec::new();
127        let mut prev_level: Option<usize> = None;
128
129        for line_info in ctx.lines.iter() {
130            if let Some(heading) = &line_info.heading {
131                let level = heading.level as usize;
132                let mut fixed_level = level;
133
134                // Check if this heading needs fixing
135                if let Some(prev) = prev_level
136                    && level > prev + 1
137                {
138                    fixed_level = prev + 1;
139                }
140
141                // Map heading style - when fixing, we may need to change Setext style based on level
142                let style = match heading.style {
143                    crate::lint_context::HeadingStyle::ATX => HeadingStyle::Atx,
144                    crate::lint_context::HeadingStyle::Setext1 => {
145                        if fixed_level == 1 {
146                            HeadingStyle::Setext1
147                        } else {
148                            HeadingStyle::Setext2
149                        }
150                    }
151                    crate::lint_context::HeadingStyle::Setext2 => {
152                        if fixed_level == 1 {
153                            HeadingStyle::Setext1
154                        } else {
155                            HeadingStyle::Setext2
156                        }
157                    }
158                };
159
160                let replacement = HeadingUtils::convert_heading_style(&heading.text, fixed_level as u32, style);
161                fixed_lines.push(format!("{}{}", " ".repeat(line_info.indent), replacement));
162
163                prev_level = Some(fixed_level);
164            } else {
165                fixed_lines.push(line_info.content(ctx.content).to_string());
166            }
167        }
168
169        let mut result = fixed_lines.join("\n");
170        if ctx.content.ends_with('\n') && !result.ends_with('\n') {
171            result.push('\n');
172        }
173        Ok(result)
174    }
175
176    fn category(&self) -> RuleCategory {
177        RuleCategory::Heading
178    }
179
180    fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
181        // Fast path: check if document likely has headings
182        if ctx.content.is_empty() || !ctx.likely_has_headings() {
183            return true;
184        }
185        // Verify headings actually exist
186        !ctx.lines.iter().any(|line| line.heading.is_some())
187    }
188
189    fn as_any(&self) -> &dyn std::any::Any {
190        self
191    }
192
193    fn from_config(_config: &crate::config::Config) -> Box<dyn Rule>
194    where
195        Self: Sized,
196    {
197        Box::new(MD001HeadingIncrement)
198    }
199}
200
201#[cfg(test)]
202mod tests {
203    use super::*;
204    use crate::lint_context::LintContext;
205
206    #[test]
207    fn test_basic_functionality() {
208        let rule = MD001HeadingIncrement;
209
210        // Test with valid headings
211        let content = "# Heading 1\n## Heading 2\n### Heading 3";
212        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
213        let result = rule.check(&ctx).unwrap();
214        assert!(result.is_empty());
215
216        // Test with invalid headings
217        let content = "# Heading 1\n### Heading 3\n#### Heading 4";
218        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
219        let result = rule.check(&ctx).unwrap();
220        assert_eq!(result.len(), 1);
221        assert_eq!(result[0].line, 2);
222    }
223}