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, crate::config::MarkdownFlavor::Standard);
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, crate::config::MarkdownFlavor::Standard);
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(
323                &LintContext::new(content, crate::config::MarkdownFlavor::Standard),
324                &structure,
325            )
326            .unwrap();
327        assert!(warnings.is_empty(), "Expected no warnings for matching headings");
328
329        // Test with mismatched headings
330        let content = "# Introduction\n\nContent\n\n# Results\n\nSkipped method";
331        let structure = document_structure_from_str(content);
332        let warnings = rule
333            .check_with_structure(
334                &LintContext::new(content, crate::config::MarkdownFlavor::Standard),
335                &structure,
336            )
337            .unwrap();
338        assert!(!warnings.is_empty(), "Expected warnings for mismatched headings");
339
340        // Test with no headings but requirements exist
341        let content = "No headings here, just plain text";
342        let structure = document_structure_from_str(content);
343        let warnings = rule
344            .check_with_structure(
345                &LintContext::new(content, crate::config::MarkdownFlavor::Standard),
346                &structure,
347            )
348            .unwrap();
349        assert!(!warnings.is_empty(), "Expected warnings when headings are missing");
350
351        // Test with setext headings - use the correct format (marker text)
352        let required_setext = vec![
353            "=========== Introduction".to_string(),
354            "------ Method".to_string(),
355            "======= Results".to_string(),
356        ];
357        let rule_setext = MD043RequiredHeadings::new(required_setext);
358        let content = "Introduction\n===========\n\nContent\n\nMethod\n------\n\nMore content\n\nResults\n=======\n\nFinal content";
359        let structure = document_structure_from_str(content);
360        let warnings = rule_setext
361            .check_with_structure(
362                &LintContext::new(content, crate::config::MarkdownFlavor::Standard),
363                &structure,
364            )
365            .unwrap();
366        assert!(warnings.is_empty(), "Expected no warnings for matching setext headings");
367    }
368
369    #[test]
370    fn test_should_skip_no_false_positives() {
371        // Create rule with required headings
372        let required = vec!["Test".to_string()];
373        let rule = MD043RequiredHeadings::new(required);
374
375        // Test 1: Content with '#' character in normal text (not a heading)
376        let content = "This paragraph contains a # character but is not a heading";
377        assert!(
378            rule.should_skip(&LintContext::new(content, crate::config::MarkdownFlavor::Standard)),
379            "Should skip content with # in normal text"
380        );
381
382        // Test 2: Content with code block containing heading-like syntax
383        let content = "Regular paragraph\n\n```markdown\n# This is not a real heading\n```\n\nMore text";
384        assert!(
385            rule.should_skip(&LintContext::new(content, crate::config::MarkdownFlavor::Standard)),
386            "Should skip content with heading-like syntax in code blocks"
387        );
388
389        // Test 3: Content with list items using '-' character
390        let content = "Some text\n\n- List item 1\n- List item 2\n\nMore text";
391        assert!(
392            rule.should_skip(&LintContext::new(content, crate::config::MarkdownFlavor::Standard)),
393            "Should skip content with list items using dash"
394        );
395
396        // Test 4: Content with horizontal rule that uses '---'
397        let content = "Some text\n\n---\n\nMore text below the horizontal rule";
398        assert!(
399            rule.should_skip(&LintContext::new(content, crate::config::MarkdownFlavor::Standard)),
400            "Should skip content with horizontal rule"
401        );
402
403        // Test 5: Content with equals sign in normal text
404        let content = "This is a normal paragraph with equals sign x = y + z";
405        assert!(
406            rule.should_skip(&LintContext::new(content, crate::config::MarkdownFlavor::Standard)),
407            "Should skip content with equals sign in normal text"
408        );
409
410        // Test 6: Content with dash/minus in normal text
411        let content = "This is a normal paragraph with minus sign x - y = z";
412        assert!(
413            rule.should_skip(&LintContext::new(content, crate::config::MarkdownFlavor::Standard)),
414            "Should skip content with minus sign in normal text"
415        );
416    }
417
418    #[test]
419    fn test_should_skip_heading_detection() {
420        // Create rule with required headings
421        let required = vec!["Test".to_string()];
422        let rule = MD043RequiredHeadings::new(required);
423
424        // Test 1: Content with ATX heading
425        let content = "# This is a heading\n\nAnd some content";
426        assert!(
427            !rule.should_skip(&LintContext::new(content, crate::config::MarkdownFlavor::Standard)),
428            "Should not skip content with ATX heading"
429        );
430
431        // Test 2: Content with Setext heading (equals sign)
432        let content = "This is a heading\n================\n\nAnd some content";
433        assert!(
434            !rule.should_skip(&LintContext::new(content, crate::config::MarkdownFlavor::Standard)),
435            "Should not skip content with Setext heading (=)"
436        );
437
438        // Test 3: Content with Setext heading (dash)
439        let content = "This is a subheading\n------------------\n\nAnd some content";
440        assert!(
441            !rule.should_skip(&LintContext::new(content, crate::config::MarkdownFlavor::Standard)),
442            "Should not skip content with Setext heading (-)"
443        );
444
445        // Test 4: Content with ATX heading with closing hashes
446        let content = "## This is a heading ##\n\nAnd some content";
447        assert!(
448            !rule.should_skip(&LintContext::new(content, crate::config::MarkdownFlavor::Standard)),
449            "Should not skip content with ATX heading with closing hashes"
450        );
451    }
452
453    #[test]
454    fn test_config_match_case_sensitive() {
455        let config = MD043Config {
456            headings: vec!["# Introduction".to_string(), "# Method".to_string()],
457            match_case: true,
458        };
459        let rule = MD043RequiredHeadings::from_config_struct(config);
460
461        // Should fail with different case
462        let content = "# introduction\n\n# method";
463        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
464        let result = rule.check(&ctx).unwrap();
465
466        assert!(
467            !result.is_empty(),
468            "Should detect case mismatch when match_case is true"
469        );
470    }
471
472    #[test]
473    fn test_config_match_case_insensitive() {
474        let config = MD043Config {
475            headings: vec!["# Introduction".to_string(), "# Method".to_string()],
476            match_case: false,
477        };
478        let rule = MD043RequiredHeadings::from_config_struct(config);
479
480        // Should pass with different case
481        let content = "# introduction\n\n# method";
482        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
483        let result = rule.check(&ctx).unwrap();
484
485        assert!(result.is_empty(), "Should allow case mismatch when match_case is false");
486    }
487
488    #[test]
489    fn test_config_case_insensitive_mixed() {
490        let config = MD043Config {
491            headings: vec!["# Introduction".to_string(), "# METHOD".to_string()],
492            match_case: false,
493        };
494        let rule = MD043RequiredHeadings::from_config_struct(config);
495
496        // Should pass with mixed case variations
497        let content = "# INTRODUCTION\n\n# method";
498        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
499        let result = rule.check(&ctx).unwrap();
500
501        assert!(
502            result.is_empty(),
503            "Should allow mixed case variations when match_case is false"
504        );
505    }
506
507    #[test]
508    fn test_config_case_sensitive_exact_match() {
509        let config = MD043Config {
510            headings: vec!["# Introduction".to_string(), "# Method".to_string()],
511            match_case: true,
512        };
513        let rule = MD043RequiredHeadings::from_config_struct(config);
514
515        // Should pass with exact case match
516        let content = "# Introduction\n\n# Method";
517        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
518        let result = rule.check(&ctx).unwrap();
519
520        assert!(
521            result.is_empty(),
522            "Should pass with exact case match when match_case is true"
523        );
524    }
525
526    #[test]
527    fn test_default_config() {
528        let rule = MD043RequiredHeadings::default();
529
530        // Should be disabled with empty headings
531        let content = "# Any heading\n\n# Another heading";
532        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
533        let result = rule.check(&ctx).unwrap();
534
535        assert!(result.is_empty(), "Should be disabled with default empty headings");
536    }
537
538    #[test]
539    fn test_default_config_section() {
540        let rule = MD043RequiredHeadings::default();
541        let config_section = rule.default_config_section();
542
543        assert!(config_section.is_some());
544        let (name, value) = config_section.unwrap();
545        assert_eq!(name, "MD043");
546
547        // Should contain both headings and match_case options with default values
548        if let toml::Value::Table(table) = value {
549            assert!(table.contains_key("headings"));
550            assert!(table.contains_key("match-case"));
551            assert_eq!(table["headings"], toml::Value::Array(vec![]));
552            assert_eq!(table["match-case"], toml::Value::Boolean(false));
553        } else {
554            panic!("Expected TOML table");
555        }
556    }
557
558    #[test]
559    fn test_headings_match_case_sensitive() {
560        let config = MD043Config {
561            headings: vec![],
562            match_case: true,
563        };
564        let rule = MD043RequiredHeadings::from_config_struct(config);
565
566        assert!(rule.headings_match("Test", "Test"));
567        assert!(!rule.headings_match("Test", "test"));
568        assert!(!rule.headings_match("test", "Test"));
569    }
570
571    #[test]
572    fn test_headings_match_case_insensitive() {
573        let config = MD043Config {
574            headings: vec![],
575            match_case: false,
576        };
577        let rule = MD043RequiredHeadings::from_config_struct(config);
578
579        assert!(rule.headings_match("Test", "Test"));
580        assert!(rule.headings_match("Test", "test"));
581        assert!(rule.headings_match("test", "Test"));
582        assert!(rule.headings_match("TEST", "test"));
583    }
584
585    #[test]
586    fn test_config_empty_headings() {
587        let config = MD043Config {
588            headings: vec![],
589            match_case: true,
590        };
591        let rule = MD043RequiredHeadings::from_config_struct(config);
592
593        // Should skip processing when no headings are required
594        let content = "# Any heading\n\n# Another heading";
595        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
596        let result = rule.check(&ctx).unwrap();
597
598        assert!(result.is_empty(), "Should be disabled with empty headings list");
599    }
600
601    #[test]
602    fn test_fix_respects_configuration() {
603        let config = MD043Config {
604            headings: vec!["# Title".to_string(), "# Content".to_string()],
605            match_case: false,
606        };
607        let rule = MD043RequiredHeadings::from_config_struct(config);
608
609        let content = "Wrong content";
610        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
611        let fixed = rule.fix(&ctx).unwrap();
612
613        let expected = "# Title\n\n# Content";
614        assert_eq!(fixed, expected);
615    }
616}