rumdl_lib/rules/
md043_required_headings.rs

1use crate::rule::{LintError, LintResult, LintWarning, Rule, RuleCategory, Severity};
2use crate::rule_config_serde::RuleConfig;
3use crate::utils::document_structure::{DocumentStructure, DocumentStructureExtensions};
4use crate::utils::range_utils::calculate_heading_range;
5use lazy_static::lazy_static;
6use regex::Regex;
7use serde::{Deserialize, Serialize};
8
9lazy_static! {
10    // Pattern for ATX headings
11    static ref ATX_HEADING: Regex = Regex::new(r"^(#+)\s+(.+)$").unwrap();
12    // Pattern for setext heading underlines
13    static ref SETEXT_UNDERLINE: Regex = Regex::new(r"^([=-]+)$").unwrap();
14}
15
16/// Configuration for MD043 rule
17#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
18#[serde(rename_all = "kebab-case")]
19pub struct MD043Config {
20    /// Required heading patterns
21    #[serde(default = "default_headings")]
22    pub headings: Vec<String>,
23    /// Case-sensitive matching (default: false)
24    #[serde(default = "default_match_case")]
25    pub match_case: bool,
26}
27
28impl Default for MD043Config {
29    fn default() -> Self {
30        Self {
31            headings: default_headings(),
32            match_case: default_match_case(),
33        }
34    }
35}
36
37fn default_headings() -> Vec<String> {
38    Vec::new()
39}
40
41fn default_match_case() -> bool {
42    false
43}
44
45impl RuleConfig for MD043Config {
46    const RULE_NAME: &'static str = "MD043";
47}
48
49/// Rule MD043: Required headings present
50///
51/// See [docs/md043.md](../../docs/md043.md) for full documentation, configuration, and examples.
52#[derive(Clone, Default)]
53pub struct MD043RequiredHeadings {
54    config: MD043Config,
55}
56
57impl MD043RequiredHeadings {
58    pub fn new(headings: Vec<String>) -> Self {
59        Self {
60            config: MD043Config {
61                headings,
62                match_case: default_match_case(),
63            },
64        }
65    }
66
67    /// Create a new instance with the given configuration
68    pub fn from_config_struct(config: MD043Config) -> Self {
69        Self { config }
70    }
71
72    /// Compare two headings based on the match_case configuration
73    fn headings_match(&self, expected: &str, actual: &str) -> bool {
74        if self.config.match_case {
75            expected == actual
76        } else {
77            expected.to_lowercase() == actual.to_lowercase()
78        }
79    }
80
81    fn extract_headings(&self, ctx: &crate::lint_context::LintContext) -> Vec<String> {
82        let mut result = Vec::new();
83
84        for line_info in &ctx.lines {
85            if let Some(heading) = &line_info.heading {
86                // Reconstruct the full heading format with the hash symbols
87                let full_heading = format!("{} {}", heading.marker, heading.text.trim());
88                result.push(full_heading);
89            }
90        }
91
92        result
93    }
94
95    fn is_heading(&self, line_index: usize, ctx: &crate::lint_context::LintContext) -> bool {
96        if line_index < ctx.lines.len() {
97            ctx.lines[line_index].heading.is_some()
98        } else {
99            false
100        }
101    }
102}
103
104impl Rule for MD043RequiredHeadings {
105    fn name(&self) -> &'static str {
106        "MD043"
107    }
108
109    fn description(&self) -> &'static str {
110        "Required heading structure"
111    }
112
113    fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
114        let mut warnings = Vec::new();
115        let actual_headings = self.extract_headings(ctx);
116
117        // If no required headings are specified, the rule is disabled
118        if self.config.headings.is_empty() {
119            return Ok(warnings);
120        }
121
122        // Check if headings match based on case sensitivity configuration
123        let headings_match = if actual_headings.len() != self.config.headings.len() {
124            false
125        } else {
126            actual_headings
127                .iter()
128                .zip(self.config.headings.iter())
129                .all(|(actual, expected)| self.headings_match(expected, actual))
130        };
131
132        if !headings_match {
133            // If no headings found but we have required headings, create a warning
134            if actual_headings.is_empty() && !self.config.headings.is_empty() {
135                warnings.push(LintWarning {
136                    rule_name: Some(self.name()),
137                    line: 1,
138                    column: 1,
139                    end_line: 1,
140                    end_column: 2,
141                    message: format!("Required headings not found: {:?}", self.config.headings),
142                    severity: Severity::Warning,
143                    fix: None,
144                });
145                return Ok(warnings);
146            }
147
148            // Create warnings for each heading that doesn't match
149            for (i, line_info) in ctx.lines.iter().enumerate() {
150                if self.is_heading(i, ctx) {
151                    // Calculate precise character range for the entire heading
152                    let (start_line, start_col, end_line, end_col) = calculate_heading_range(i + 1, &line_info.content);
153
154                    warnings.push(LintWarning {
155                        rule_name: Some(self.name()),
156                        line: start_line,
157                        column: start_col,
158                        end_line,
159                        end_column: end_col,
160                        message: "Heading structure does not match the required structure".to_string(),
161                        severity: Severity::Warning,
162                        fix: None, // Cannot automatically fix as we don't know the intended structure
163                    });
164                }
165            }
166
167            // If we have no warnings but headings don't match (could happen if we have no headings),
168            // add a warning at the beginning of the file
169            if warnings.is_empty() {
170                warnings.push(LintWarning {
171                    rule_name: Some(self.name()),
172                    line: 1,
173                    column: 1,
174                    end_line: 1,
175                    end_column: 2,
176                    message: format!(
177                        "Heading structure does not match required structure. Expected: {:?}, Found: {:?}",
178                        self.config.headings, actual_headings
179                    ),
180                    severity: Severity::Warning,
181                    fix: None,
182                });
183            }
184        }
185
186        Ok(warnings)
187    }
188
189    fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
190        let content = ctx.content;
191        // If no required headings are specified, return content as is
192        if self.config.headings.is_empty() {
193            return Ok(content.to_string());
194        }
195
196        let mut result = String::new();
197
198        // Add required headings
199        for (idx, heading) in self.config.headings.iter().enumerate() {
200            if idx > 0 {
201                result.push_str("\n\n");
202            }
203            result.push_str(heading);
204        }
205
206        Ok(result)
207    }
208
209    /// Optimized check using document structure
210    fn check_with_structure(
211        &self,
212        _ctx: &crate::lint_context::LintContext,
213        _structure: &DocumentStructure,
214    ) -> LintResult {
215        // Just use the regular check method which now uses cached headings
216        self.check(_ctx)
217    }
218
219    /// Get the category of this rule for selective processing
220    fn category(&self) -> RuleCategory {
221        RuleCategory::Heading
222    }
223
224    /// Check if this rule should be skipped
225    fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
226        // Skip if no heading requirements or content is empty
227        if self.config.headings.is_empty() || ctx.content.is_empty() {
228            return true;
229        }
230
231        // Check if any heading exists using cached information
232        let has_heading = ctx.lines.iter().any(|line| line.heading.is_some());
233
234        !has_heading
235    }
236
237    fn as_any(&self) -> &dyn std::any::Any {
238        self
239    }
240
241    fn default_config_section(&self) -> Option<(String, toml::Value)> {
242        let default_config = MD043Config::default();
243        let json_value = serde_json::to_value(&default_config).ok()?;
244        let toml_value = crate::rule_config_serde::json_to_toml_value(&json_value)?;
245        if let toml::Value::Table(table) = toml_value {
246            if !table.is_empty() {
247                Some((MD043Config::RULE_NAME.to_string(), toml::Value::Table(table)))
248            } else {
249                None
250            }
251        } else {
252            None
253        }
254    }
255
256    fn from_config(config: &crate::config::Config) -> Box<dyn Rule>
257    where
258        Self: Sized,
259    {
260        let rule_config = crate::rule_config_serde::load_rule_config::<MD043Config>(config);
261        Box::new(MD043RequiredHeadings::from_config_struct(rule_config))
262    }
263}
264
265impl DocumentStructureExtensions for MD043RequiredHeadings {
266    fn has_relevant_elements(
267        &self,
268        _ctx: &crate::lint_context::LintContext,
269        doc_structure: &DocumentStructure,
270    ) -> bool {
271        !doc_structure.heading_lines.is_empty() || !self.config.headings.is_empty()
272    }
273}
274
275#[cfg(test)]
276mod tests {
277    use super::*;
278    use crate::lint_context::LintContext;
279    use crate::utils::document_structure::document_structure_from_str;
280
281    #[test]
282    fn test_extract_headings_code_blocks() {
283        // Create rule with required headings (now with hash symbols)
284        let required = vec!["# Test Document".to_string(), "## Real heading 2".to_string()];
285        let rule = MD043RequiredHeadings::new(required);
286
287        // Test 1: Basic content with code block
288        let content = "# Test Document\n\nThis is regular content.\n\n```markdown\n# This is a heading in a code block\n## Another heading in code block\n```\n\n## Real heading 2\n\nSome content.";
289        let ctx = crate::lint_context::LintContext::new(content);
290        let actual_headings = rule.extract_headings(&ctx);
291        assert_eq!(
292            actual_headings,
293            vec!["# Test Document".to_string(), "## Real heading 2".to_string()],
294            "Should extract correct headings and ignore code blocks"
295        );
296
297        // Test 2: Content with invalid headings
298        let content = "# Test Document\n\nThis is regular content.\n\n```markdown\n# This is a heading in a code block\n## This should be ignored\n```\n\n## Not Real heading 2\n\nSome content.";
299        let ctx = crate::lint_context::LintContext::new(content);
300        let actual_headings = rule.extract_headings(&ctx);
301        assert_eq!(
302            actual_headings,
303            vec!["# Test Document".to_string(), "## Not Real heading 2".to_string()],
304            "Should extract actual headings including mismatched ones"
305        );
306    }
307
308    #[test]
309    fn test_with_document_structure() {
310        // Test with required headings (now with hash symbols)
311        let required = vec![
312            "# Introduction".to_string(),
313            "# Method".to_string(),
314            "# Results".to_string(),
315        ];
316        let rule = MD043RequiredHeadings::new(required);
317
318        // Test with matching headings
319        let content = "# Introduction\n\nContent\n\n# Method\n\nMore content\n\n# Results\n\nFinal content";
320        let structure = document_structure_from_str(content);
321        let warnings = rule
322            .check_with_structure(&LintContext::new(content), &structure)
323            .unwrap();
324        assert!(warnings.is_empty(), "Expected no warnings for matching headings");
325
326        // Test with mismatched headings
327        let content = "# Introduction\n\nContent\n\n# Results\n\nSkipped method";
328        let structure = document_structure_from_str(content);
329        let warnings = rule
330            .check_with_structure(&LintContext::new(content), &structure)
331            .unwrap();
332        assert!(!warnings.is_empty(), "Expected warnings for mismatched headings");
333
334        // Test with no headings but requirements exist
335        let content = "No headings here, just plain text";
336        let structure = document_structure_from_str(content);
337        let warnings = rule
338            .check_with_structure(&LintContext::new(content), &structure)
339            .unwrap();
340        assert!(!warnings.is_empty(), "Expected warnings when headings are missing");
341
342        // Test with setext headings - use the correct format (marker text)
343        let required_setext = vec![
344            "=========== Introduction".to_string(),
345            "------ Method".to_string(),
346            "======= Results".to_string(),
347        ];
348        let rule_setext = MD043RequiredHeadings::new(required_setext);
349        let content = "Introduction\n===========\n\nContent\n\nMethod\n------\n\nMore content\n\nResults\n=======\n\nFinal content";
350        let structure = document_structure_from_str(content);
351        let warnings = rule_setext
352            .check_with_structure(&LintContext::new(content), &structure)
353            .unwrap();
354        assert!(warnings.is_empty(), "Expected no warnings for matching setext headings");
355    }
356
357    #[test]
358    fn test_should_skip_no_false_positives() {
359        // Create rule with required headings
360        let required = vec!["Test".to_string()];
361        let rule = MD043RequiredHeadings::new(required);
362
363        // Test 1: Content with '#' character in normal text (not a heading)
364        let content = "This paragraph contains a # character but is not a heading";
365        assert!(
366            rule.should_skip(&LintContext::new(content)),
367            "Should skip content with # in normal text"
368        );
369
370        // Test 2: Content with code block containing heading-like syntax
371        let content = "Regular paragraph\n\n```markdown\n# This is not a real heading\n```\n\nMore text";
372        assert!(
373            rule.should_skip(&LintContext::new(content)),
374            "Should skip content with heading-like syntax in code blocks"
375        );
376
377        // Test 3: Content with list items using '-' character
378        let content = "Some text\n\n- List item 1\n- List item 2\n\nMore text";
379        assert!(
380            rule.should_skip(&LintContext::new(content)),
381            "Should skip content with list items using dash"
382        );
383
384        // Test 4: Content with horizontal rule that uses '---'
385        let content = "Some text\n\n---\n\nMore text below the horizontal rule";
386        assert!(
387            rule.should_skip(&LintContext::new(content)),
388            "Should skip content with horizontal rule"
389        );
390
391        // Test 5: Content with equals sign in normal text
392        let content = "This is a normal paragraph with equals sign x = y + z";
393        assert!(
394            rule.should_skip(&LintContext::new(content)),
395            "Should skip content with equals sign in normal text"
396        );
397
398        // Test 6: Content with dash/minus in normal text
399        let content = "This is a normal paragraph with minus sign x - y = z";
400        assert!(
401            rule.should_skip(&LintContext::new(content)),
402            "Should skip content with minus sign in normal text"
403        );
404    }
405
406    #[test]
407    fn test_should_skip_heading_detection() {
408        // Create rule with required headings
409        let required = vec!["Test".to_string()];
410        let rule = MD043RequiredHeadings::new(required);
411
412        // Test 1: Content with ATX heading
413        let content = "# This is a heading\n\nAnd some content";
414        assert!(
415            !rule.should_skip(&LintContext::new(content)),
416            "Should not skip content with ATX heading"
417        );
418
419        // Test 2: Content with Setext heading (equals sign)
420        let content = "This is a heading\n================\n\nAnd some content";
421        assert!(
422            !rule.should_skip(&LintContext::new(content)),
423            "Should not skip content with Setext heading (=)"
424        );
425
426        // Test 3: Content with Setext heading (dash)
427        let content = "This is a subheading\n------------------\n\nAnd some content";
428        assert!(
429            !rule.should_skip(&LintContext::new(content)),
430            "Should not skip content with Setext heading (-)"
431        );
432
433        // Test 4: Content with ATX heading with closing hashes
434        let content = "## This is a heading ##\n\nAnd some content";
435        assert!(
436            !rule.should_skip(&LintContext::new(content)),
437            "Should not skip content with ATX heading with closing hashes"
438        );
439    }
440
441    #[test]
442    fn test_config_match_case_sensitive() {
443        let config = MD043Config {
444            headings: vec!["# Introduction".to_string(), "# Method".to_string()],
445            match_case: true,
446        };
447        let rule = MD043RequiredHeadings::from_config_struct(config);
448
449        // Should fail with different case
450        let content = "# introduction\n\n# method";
451        let ctx = LintContext::new(content);
452        let result = rule.check(&ctx).unwrap();
453
454        assert!(
455            !result.is_empty(),
456            "Should detect case mismatch when match_case is true"
457        );
458    }
459
460    #[test]
461    fn test_config_match_case_insensitive() {
462        let config = MD043Config {
463            headings: vec!["# Introduction".to_string(), "# Method".to_string()],
464            match_case: false,
465        };
466        let rule = MD043RequiredHeadings::from_config_struct(config);
467
468        // Should pass with different case
469        let content = "# introduction\n\n# method";
470        let ctx = LintContext::new(content);
471        let result = rule.check(&ctx).unwrap();
472
473        assert!(result.is_empty(), "Should allow case mismatch when match_case is false");
474    }
475
476    #[test]
477    fn test_config_case_insensitive_mixed() {
478        let config = MD043Config {
479            headings: vec!["# Introduction".to_string(), "# METHOD".to_string()],
480            match_case: false,
481        };
482        let rule = MD043RequiredHeadings::from_config_struct(config);
483
484        // Should pass with mixed case variations
485        let content = "# INTRODUCTION\n\n# method";
486        let ctx = LintContext::new(content);
487        let result = rule.check(&ctx).unwrap();
488
489        assert!(
490            result.is_empty(),
491            "Should allow mixed case variations when match_case is false"
492        );
493    }
494
495    #[test]
496    fn test_config_case_sensitive_exact_match() {
497        let config = MD043Config {
498            headings: vec!["# Introduction".to_string(), "# Method".to_string()],
499            match_case: true,
500        };
501        let rule = MD043RequiredHeadings::from_config_struct(config);
502
503        // Should pass with exact case match
504        let content = "# Introduction\n\n# Method";
505        let ctx = LintContext::new(content);
506        let result = rule.check(&ctx).unwrap();
507
508        assert!(
509            result.is_empty(),
510            "Should pass with exact case match when match_case is true"
511        );
512    }
513
514    #[test]
515    fn test_default_config() {
516        let rule = MD043RequiredHeadings::default();
517
518        // Should be disabled with empty headings
519        let content = "# Any heading\n\n# Another heading";
520        let ctx = LintContext::new(content);
521        let result = rule.check(&ctx).unwrap();
522
523        assert!(result.is_empty(), "Should be disabled with default empty headings");
524    }
525
526    #[test]
527    fn test_default_config_section() {
528        let rule = MD043RequiredHeadings::default();
529        let config_section = rule.default_config_section();
530
531        assert!(config_section.is_some());
532        let (name, value) = config_section.unwrap();
533        assert_eq!(name, "MD043");
534
535        // Should contain both headings and match_case options with default values
536        if let toml::Value::Table(table) = value {
537            assert!(table.contains_key("headings"));
538            assert!(table.contains_key("match-case"));
539            assert_eq!(table["headings"], toml::Value::Array(vec![]));
540            assert_eq!(table["match-case"], toml::Value::Boolean(false));
541        } else {
542            panic!("Expected TOML table");
543        }
544    }
545
546    #[test]
547    fn test_headings_match_case_sensitive() {
548        let config = MD043Config {
549            headings: vec![],
550            match_case: true,
551        };
552        let rule = MD043RequiredHeadings::from_config_struct(config);
553
554        assert!(rule.headings_match("Test", "Test"));
555        assert!(!rule.headings_match("Test", "test"));
556        assert!(!rule.headings_match("test", "Test"));
557    }
558
559    #[test]
560    fn test_headings_match_case_insensitive() {
561        let config = MD043Config {
562            headings: vec![],
563            match_case: false,
564        };
565        let rule = MD043RequiredHeadings::from_config_struct(config);
566
567        assert!(rule.headings_match("Test", "Test"));
568        assert!(rule.headings_match("Test", "test"));
569        assert!(rule.headings_match("test", "Test"));
570        assert!(rule.headings_match("TEST", "test"));
571    }
572
573    #[test]
574    fn test_config_empty_headings() {
575        let config = MD043Config {
576            headings: vec![],
577            match_case: true,
578        };
579        let rule = MD043RequiredHeadings::from_config_struct(config);
580
581        // Should skip processing when no headings are required
582        let content = "# Any heading\n\n# Another heading";
583        let ctx = LintContext::new(content);
584        let result = rule.check(&ctx).unwrap();
585
586        assert!(result.is_empty(), "Should be disabled with empty headings list");
587    }
588
589    #[test]
590    fn test_fix_respects_configuration() {
591        let config = MD043Config {
592            headings: vec!["# Title".to_string(), "# Content".to_string()],
593            match_case: false,
594        };
595        let rule = MD043RequiredHeadings::from_config_struct(config);
596
597        let content = "Wrong content";
598        let ctx = LintContext::new(content);
599        let fixed = rule.fix(&ctx).unwrap();
600
601        let expected = "# Title\n\n# Content";
602        assert_eq!(fixed, expected);
603    }
604}