Skip to main content

rumdl_lib/rules/
mod.rs

1pub mod code_fence_utils;
2pub mod emphasis_style;
3pub mod front_matter_utils;
4pub mod heading_utils;
5pub mod list_utils;
6pub mod strong_style;
7
8mod md001_heading_increment;
9mod md003_heading_style;
10pub mod md004_unordered_list_style;
11mod md005_list_indent;
12mod md007_ul_indent;
13mod md009_trailing_spaces;
14mod md010_no_hard_tabs;
15mod md011_no_reversed_links;
16pub mod md013_line_length;
17mod md014_commands_show_output;
18mod md024_no_duplicate_heading;
19mod md025_single_title;
20mod md026_no_trailing_punctuation;
21mod md027_multiple_spaces_blockquote;
22mod md028_no_blanks_blockquote;
23mod md029_ordered_list_prefix;
24pub mod md030_list_marker_space;
25mod md031_blanks_around_fences;
26mod md032_blanks_around_lists;
27mod md033_no_inline_html;
28mod md034_no_bare_urls;
29mod md035_hr_style;
30pub mod md036_no_emphasis_only_first;
31mod md037_spaces_around_emphasis;
32mod md038_no_space_in_code;
33mod md039_no_space_in_links;
34pub mod md040_fenced_code_language;
35mod md041_first_line_heading;
36mod md042_no_empty_links;
37mod md043_required_headings;
38mod md044_proper_names;
39mod md045_no_alt_text;
40mod md046_code_block_style;
41mod md047_single_trailing_newline;
42mod md048_code_fence_style;
43mod md049_emphasis_style;
44mod md050_strong_style;
45mod md051_link_fragments;
46mod md052_reference_links_images;
47mod md053_link_image_reference_definitions;
48mod md054_link_image_style;
49mod md055_table_pipe_style;
50mod md056_table_column_count;
51mod md058_blanks_around_tables;
52mod md059_link_text;
53mod md060_table_format;
54mod md061_forbidden_terms;
55mod md062_link_destination_whitespace;
56mod md063_heading_capitalization;
57mod md064_no_multiple_consecutive_spaces;
58mod md065_blanks_around_horizontal_rules;
59mod md066_footnote_validation;
60mod md067_footnote_definition_order;
61mod md068_empty_footnote_definition;
62mod md069_no_duplicate_list_markers;
63mod md070_nested_code_fence;
64mod md071_blank_line_after_frontmatter;
65mod md072_frontmatter_key_sort;
66mod md073_toc_validation;
67mod md074_mkdocs_nav;
68mod md075_orphaned_table_rows;
69mod md076_list_item_spacing;
70mod md077_list_continuation_indent;
71
72pub use code_fence_utils::CodeFenceStyle;
73pub use md001_heading_increment::MD001HeadingIncrement;
74pub use md003_heading_style::MD003HeadingStyle;
75pub use md004_unordered_list_style::MD004UnorderedListStyle;
76pub use md004_unordered_list_style::UnorderedListStyle;
77pub use md005_list_indent::MD005ListIndent;
78pub use md007_ul_indent::MD007ULIndent;
79pub use md009_trailing_spaces::MD009TrailingSpaces;
80pub use md010_no_hard_tabs::MD010NoHardTabs;
81pub use md011_no_reversed_links::MD011NoReversedLinks;
82pub use md013_line_length::MD013Config;
83pub use md013_line_length::MD013LineLength;
84pub use md014_commands_show_output::MD014CommandsShowOutput;
85pub use md024_no_duplicate_heading::MD024NoDuplicateHeading;
86pub use md025_single_title::MD025SingleTitle;
87pub use md026_no_trailing_punctuation::MD026NoTrailingPunctuation;
88pub use md027_multiple_spaces_blockquote::MD027MultipleSpacesBlockquote;
89pub use md028_no_blanks_blockquote::MD028NoBlanksBlockquote;
90pub use md029_ordered_list_prefix::{ListStyle, MD029OrderedListPrefix};
91pub use md030_list_marker_space::MD030ListMarkerSpace;
92pub use md031_blanks_around_fences::MD031BlanksAroundFences;
93pub use md032_blanks_around_lists::MD032BlanksAroundLists;
94pub use md033_no_inline_html::MD033NoInlineHtml;
95pub use md034_no_bare_urls::MD034NoBareUrls;
96pub use md035_hr_style::MD035HRStyle;
97pub use md036_no_emphasis_only_first::MD036NoEmphasisAsHeading;
98pub use md037_spaces_around_emphasis::MD037NoSpaceInEmphasis;
99pub use md038_no_space_in_code::MD038NoSpaceInCode;
100pub use md039_no_space_in_links::MD039NoSpaceInLinks;
101pub use md040_fenced_code_language::MD040FencedCodeLanguage;
102pub use md041_first_line_heading::MD041FirstLineHeading;
103pub use md042_no_empty_links::MD042NoEmptyLinks;
104pub use md043_required_headings::MD043RequiredHeadings;
105pub use md044_proper_names::MD044ProperNames;
106pub use md045_no_alt_text::MD045NoAltText;
107pub use md046_code_block_style::{CodeBlockStyle, MD046CodeBlockStyle};
108pub use md047_single_trailing_newline::MD047SingleTrailingNewline;
109pub use md048_code_fence_style::MD048CodeFenceStyle;
110pub use md049_emphasis_style::MD049EmphasisStyle;
111pub use md050_strong_style::MD050StrongStyle;
112pub use md051_link_fragments::MD051LinkFragments;
113pub use md052_reference_links_images::MD052ReferenceLinkImages;
114pub use md053_link_image_reference_definitions::MD053LinkImageReferenceDefinitions;
115pub use md054_link_image_style::MD054LinkImageStyle;
116pub use md055_table_pipe_style::MD055TablePipeStyle;
117pub use md056_table_column_count::MD056TableColumnCount;
118pub use md058_blanks_around_tables::MD058BlanksAroundTables;
119pub use md059_link_text::MD059LinkText;
120pub use md060_table_format::ColumnAlign;
121pub use md060_table_format::MD060Config;
122pub use md060_table_format::MD060TableFormat;
123pub use md061_forbidden_terms::MD061ForbiddenTerms;
124pub use md062_link_destination_whitespace::MD062LinkDestinationWhitespace;
125pub use md063_heading_capitalization::MD063HeadingCapitalization;
126pub use md064_no_multiple_consecutive_spaces::MD064NoMultipleConsecutiveSpaces;
127pub use md065_blanks_around_horizontal_rules::MD065BlanksAroundHorizontalRules;
128pub use md066_footnote_validation::MD066FootnoteValidation;
129pub use md067_footnote_definition_order::MD067FootnoteDefinitionOrder;
130pub use md068_empty_footnote_definition::MD068EmptyFootnoteDefinition;
131pub use md069_no_duplicate_list_markers::MD069NoDuplicateListMarkers;
132pub use md070_nested_code_fence::MD070NestedCodeFence;
133pub use md071_blank_line_after_frontmatter::MD071BlankLineAfterFrontmatter;
134pub use md072_frontmatter_key_sort::MD072FrontmatterKeySort;
135pub use md073_toc_validation::MD073TocValidation;
136pub use md074_mkdocs_nav::MD074MkDocsNav;
137pub use md075_orphaned_table_rows::MD075OrphanedTableRows;
138pub use md076_list_item_spacing::{ListItemSpacingStyle, MD076ListItemSpacing};
139pub use md077_list_continuation_indent::MD077ListContinuationIndent;
140
141mod md012_no_multiple_blanks;
142pub use md012_no_multiple_blanks::MD012NoMultipleBlanks;
143
144mod md018_no_missing_space_atx;
145pub use md018_no_missing_space_atx::MD018NoMissingSpaceAtx;
146
147mod md019_no_multiple_space_atx;
148pub use md019_no_multiple_space_atx::MD019NoMultipleSpaceAtx;
149
150mod md020_no_missing_space_closed_atx;
151mod md021_no_multiple_space_closed_atx;
152pub use md020_no_missing_space_closed_atx::MD020NoMissingSpaceClosedAtx;
153pub use md021_no_multiple_space_closed_atx::MD021NoMultipleSpaceClosedAtx;
154
155pub(crate) mod md022_blanks_around_headings;
156pub use md022_blanks_around_headings::MD022BlanksAroundHeadings;
157
158mod md023_heading_start_left;
159pub use md023_heading_start_left::MD023HeadingStartLeft;
160
161mod md057_existing_relative_links;
162
163pub use md057_existing_relative_links::{AbsoluteLinksOption, MD057Config, MD057ExistingRelativeLinks};
164
165use crate::rule::Rule;
166
167/// Type alias for rule constructor functions
168type RuleCtor = fn(&crate::config::Config) -> Box<dyn Rule>;
169
170/// Entry in the rule registry, with metadata about the rule
171struct RuleEntry {
172    name: &'static str,
173    ctor: RuleCtor,
174    /// Whether this rule requires explicit opt-in via extend-enable or enable=["ALL"]
175    opt_in: bool,
176}
177
178/// Registry of all available rules with their constructor functions
179/// This enables automatic inline config support - the engine can recreate
180/// any rule with a merged config without per-rule changes.
181///
182/// Rules marked `opt_in: true` are excluded from the default rule set and must
183/// be explicitly enabled via `extend-enable` or `enable = ["ALL"]`.
184const RULES: &[RuleEntry] = &[
185    RuleEntry {
186        name: "MD001",
187        ctor: MD001HeadingIncrement::from_config,
188        opt_in: false,
189    },
190    RuleEntry {
191        name: "MD003",
192        ctor: MD003HeadingStyle::from_config,
193        opt_in: false,
194    },
195    RuleEntry {
196        name: "MD004",
197        ctor: MD004UnorderedListStyle::from_config,
198        opt_in: false,
199    },
200    RuleEntry {
201        name: "MD005",
202        ctor: MD005ListIndent::from_config,
203        opt_in: false,
204    },
205    RuleEntry {
206        name: "MD007",
207        ctor: MD007ULIndent::from_config,
208        opt_in: false,
209    },
210    RuleEntry {
211        name: "MD009",
212        ctor: MD009TrailingSpaces::from_config,
213        opt_in: false,
214    },
215    RuleEntry {
216        name: "MD010",
217        ctor: MD010NoHardTabs::from_config,
218        opt_in: false,
219    },
220    RuleEntry {
221        name: "MD011",
222        ctor: MD011NoReversedLinks::from_config,
223        opt_in: false,
224    },
225    RuleEntry {
226        name: "MD012",
227        ctor: MD012NoMultipleBlanks::from_config,
228        opt_in: false,
229    },
230    RuleEntry {
231        name: "MD013",
232        ctor: MD013LineLength::from_config,
233        opt_in: false,
234    },
235    RuleEntry {
236        name: "MD014",
237        ctor: MD014CommandsShowOutput::from_config,
238        opt_in: false,
239    },
240    RuleEntry {
241        name: "MD018",
242        ctor: MD018NoMissingSpaceAtx::from_config,
243        opt_in: false,
244    },
245    RuleEntry {
246        name: "MD019",
247        ctor: MD019NoMultipleSpaceAtx::from_config,
248        opt_in: false,
249    },
250    RuleEntry {
251        name: "MD020",
252        ctor: MD020NoMissingSpaceClosedAtx::from_config,
253        opt_in: false,
254    },
255    RuleEntry {
256        name: "MD021",
257        ctor: MD021NoMultipleSpaceClosedAtx::from_config,
258        opt_in: false,
259    },
260    RuleEntry {
261        name: "MD022",
262        ctor: MD022BlanksAroundHeadings::from_config,
263        opt_in: false,
264    },
265    RuleEntry {
266        name: "MD023",
267        ctor: MD023HeadingStartLeft::from_config,
268        opt_in: false,
269    },
270    RuleEntry {
271        name: "MD024",
272        ctor: MD024NoDuplicateHeading::from_config,
273        opt_in: false,
274    },
275    RuleEntry {
276        name: "MD025",
277        ctor: MD025SingleTitle::from_config,
278        opt_in: false,
279    },
280    RuleEntry {
281        name: "MD026",
282        ctor: MD026NoTrailingPunctuation::from_config,
283        opt_in: false,
284    },
285    RuleEntry {
286        name: "MD027",
287        ctor: MD027MultipleSpacesBlockquote::from_config,
288        opt_in: false,
289    },
290    RuleEntry {
291        name: "MD028",
292        ctor: MD028NoBlanksBlockquote::from_config,
293        opt_in: false,
294    },
295    RuleEntry {
296        name: "MD029",
297        ctor: MD029OrderedListPrefix::from_config,
298        opt_in: false,
299    },
300    RuleEntry {
301        name: "MD030",
302        ctor: MD030ListMarkerSpace::from_config,
303        opt_in: false,
304    },
305    RuleEntry {
306        name: "MD031",
307        ctor: MD031BlanksAroundFences::from_config,
308        opt_in: false,
309    },
310    RuleEntry {
311        name: "MD032",
312        ctor: MD032BlanksAroundLists::from_config,
313        opt_in: false,
314    },
315    RuleEntry {
316        name: "MD033",
317        ctor: MD033NoInlineHtml::from_config,
318        opt_in: false,
319    },
320    RuleEntry {
321        name: "MD034",
322        ctor: MD034NoBareUrls::from_config,
323        opt_in: false,
324    },
325    RuleEntry {
326        name: "MD035",
327        ctor: MD035HRStyle::from_config,
328        opt_in: false,
329    },
330    RuleEntry {
331        name: "MD036",
332        ctor: MD036NoEmphasisAsHeading::from_config,
333        opt_in: false,
334    },
335    RuleEntry {
336        name: "MD037",
337        ctor: MD037NoSpaceInEmphasis::from_config,
338        opt_in: false,
339    },
340    RuleEntry {
341        name: "MD038",
342        ctor: MD038NoSpaceInCode::from_config,
343        opt_in: false,
344    },
345    RuleEntry {
346        name: "MD039",
347        ctor: MD039NoSpaceInLinks::from_config,
348        opt_in: false,
349    },
350    RuleEntry {
351        name: "MD040",
352        ctor: MD040FencedCodeLanguage::from_config,
353        opt_in: false,
354    },
355    RuleEntry {
356        name: "MD041",
357        ctor: MD041FirstLineHeading::from_config,
358        opt_in: false,
359    },
360    RuleEntry {
361        name: "MD042",
362        ctor: MD042NoEmptyLinks::from_config,
363        opt_in: false,
364    },
365    RuleEntry {
366        name: "MD043",
367        ctor: MD043RequiredHeadings::from_config,
368        opt_in: false,
369    },
370    RuleEntry {
371        name: "MD044",
372        ctor: MD044ProperNames::from_config,
373        opt_in: false,
374    },
375    RuleEntry {
376        name: "MD045",
377        ctor: MD045NoAltText::from_config,
378        opt_in: false,
379    },
380    RuleEntry {
381        name: "MD046",
382        ctor: MD046CodeBlockStyle::from_config,
383        opt_in: false,
384    },
385    RuleEntry {
386        name: "MD047",
387        ctor: MD047SingleTrailingNewline::from_config,
388        opt_in: false,
389    },
390    RuleEntry {
391        name: "MD048",
392        ctor: MD048CodeFenceStyle::from_config,
393        opt_in: false,
394    },
395    RuleEntry {
396        name: "MD049",
397        ctor: MD049EmphasisStyle::from_config,
398        opt_in: false,
399    },
400    RuleEntry {
401        name: "MD050",
402        ctor: MD050StrongStyle::from_config,
403        opt_in: false,
404    },
405    RuleEntry {
406        name: "MD051",
407        ctor: MD051LinkFragments::from_config,
408        opt_in: false,
409    },
410    RuleEntry {
411        name: "MD052",
412        ctor: MD052ReferenceLinkImages::from_config,
413        opt_in: false,
414    },
415    RuleEntry {
416        name: "MD053",
417        ctor: MD053LinkImageReferenceDefinitions::from_config,
418        opt_in: false,
419    },
420    RuleEntry {
421        name: "MD054",
422        ctor: MD054LinkImageStyle::from_config,
423        opt_in: false,
424    },
425    RuleEntry {
426        name: "MD055",
427        ctor: MD055TablePipeStyle::from_config,
428        opt_in: false,
429    },
430    RuleEntry {
431        name: "MD056",
432        ctor: MD056TableColumnCount::from_config,
433        opt_in: false,
434    },
435    RuleEntry {
436        name: "MD057",
437        ctor: MD057ExistingRelativeLinks::from_config,
438        opt_in: false,
439    },
440    RuleEntry {
441        name: "MD058",
442        ctor: MD058BlanksAroundTables::from_config,
443        opt_in: false,
444    },
445    RuleEntry {
446        name: "MD059",
447        ctor: MD059LinkText::from_config,
448        opt_in: false,
449    },
450    RuleEntry {
451        name: "MD060",
452        ctor: MD060TableFormat::from_config,
453        opt_in: true,
454    },
455    RuleEntry {
456        name: "MD061",
457        ctor: MD061ForbiddenTerms::from_config,
458        opt_in: false,
459    },
460    RuleEntry {
461        name: "MD062",
462        ctor: MD062LinkDestinationWhitespace::from_config,
463        opt_in: false,
464    },
465    RuleEntry {
466        name: "MD063",
467        ctor: MD063HeadingCapitalization::from_config,
468        opt_in: true,
469    },
470    RuleEntry {
471        name: "MD064",
472        ctor: MD064NoMultipleConsecutiveSpaces::from_config,
473        opt_in: false,
474    },
475    RuleEntry {
476        name: "MD065",
477        ctor: MD065BlanksAroundHorizontalRules::from_config,
478        opt_in: false,
479    },
480    RuleEntry {
481        name: "MD066",
482        ctor: MD066FootnoteValidation::from_config,
483        opt_in: false,
484    },
485    RuleEntry {
486        name: "MD067",
487        ctor: MD067FootnoteDefinitionOrder::from_config,
488        opt_in: false,
489    },
490    RuleEntry {
491        name: "MD068",
492        ctor: MD068EmptyFootnoteDefinition::from_config,
493        opt_in: false,
494    },
495    RuleEntry {
496        name: "MD069",
497        ctor: MD069NoDuplicateListMarkers::from_config,
498        opt_in: false,
499    },
500    RuleEntry {
501        name: "MD070",
502        ctor: MD070NestedCodeFence::from_config,
503        opt_in: true,
504    },
505    RuleEntry {
506        name: "MD071",
507        ctor: MD071BlankLineAfterFrontmatter::from_config,
508        opt_in: false,
509    },
510    RuleEntry {
511        name: "MD072",
512        ctor: MD072FrontmatterKeySort::from_config,
513        opt_in: true,
514    },
515    RuleEntry {
516        name: "MD073",
517        ctor: MD073TocValidation::from_config,
518        opt_in: true,
519    },
520    RuleEntry {
521        name: "MD074",
522        ctor: MD074MkDocsNav::from_config,
523        opt_in: true,
524    },
525    RuleEntry {
526        name: "MD075",
527        ctor: MD075OrphanedTableRows::from_config,
528        opt_in: false,
529    },
530    RuleEntry {
531        name: "MD076",
532        ctor: MD076ListItemSpacing::from_config,
533        opt_in: false,
534    },
535    RuleEntry {
536        name: "MD077",
537        ctor: MD077ListContinuationIndent::from_config,
538        opt_in: false,
539    },
540];
541
542/// Returns all rule instances (including opt-in) for config validation and CLI
543pub fn all_rules(config: &crate::config::Config) -> Vec<Box<dyn Rule>> {
544    RULES.iter().map(|entry| (entry.ctor)(config)).collect()
545}
546
547/// Returns the set of rule names that require explicit opt-in
548pub fn opt_in_rules() -> HashSet<&'static str> {
549    RULES
550        .iter()
551        .filter(|entry| entry.opt_in)
552        .map(|entry| entry.name)
553        .collect()
554}
555
556/// Creates a single rule by name with the given config
557///
558/// This enables automatic inline config support - the engine can recreate
559/// any rule with a merged config without per-rule changes.
560///
561/// Returns None if the rule name is not found.
562pub fn create_rule_by_name(name: &str, config: &crate::config::Config) -> Option<Box<dyn Rule>> {
563    RULES
564        .iter()
565        .find(|entry| entry.name == name)
566        .map(|entry| (entry.ctor)(config))
567}
568
569// Filter rules based on config (moved from main.rs)
570// Note: This needs access to GlobalConfig from the config module.
571use crate::config::GlobalConfig;
572use std::collections::HashSet;
573
574/// Check whether the enable list contains the "all" keyword (case-insensitive).
575fn contains_all_keyword(list: &[String]) -> bool {
576    list.iter().any(|s| s.eq_ignore_ascii_case("all"))
577}
578
579pub fn filter_rules(rules: &[Box<dyn Rule>], global_config: &GlobalConfig) -> Vec<Box<dyn Rule>> {
580    let mut enabled_rules: Vec<Box<dyn Rule>> = Vec::new();
581    let disabled_rules: HashSet<String> = global_config.disable.iter().cloned().collect();
582    let opt_in_set = opt_in_rules();
583    let extend_enable_set: HashSet<String> = global_config.extend_enable.iter().cloned().collect();
584    let extend_disable_set: HashSet<String> = global_config.extend_disable.iter().cloned().collect();
585
586    let extend_enable_all = contains_all_keyword(&global_config.extend_enable);
587    let extend_disable_all = contains_all_keyword(&global_config.extend_disable);
588
589    // Helper: should this rule be removed by any disable source?
590    let is_disabled = |name: &str| -> bool {
591        disabled_rules.contains(name) || extend_disable_all || extend_disable_set.contains(name)
592    };
593
594    // Handle 'disable: ["all"]'
595    if disabled_rules.contains("all") {
596        // If 'enable' is also provided, only those rules are enabled, overriding "disable all"
597        if !global_config.enable.is_empty() {
598            if contains_all_keyword(&global_config.enable) {
599                // enable: ["ALL"] + disable: ["all"] cancel out → all rules enabled
600                for rule in rules {
601                    enabled_rules.push(dyn_clone::clone_box(&**rule));
602                }
603            } else {
604                let enabled_set: HashSet<String> = global_config.enable.iter().cloned().collect();
605                for rule in rules {
606                    if enabled_set.contains(rule.name()) {
607                        enabled_rules.push(dyn_clone::clone_box(&**rule));
608                    }
609                }
610            }
611        }
612        // If 'enable' is empty and 'disable: ["all"]', return empty vector.
613        return enabled_rules;
614    }
615
616    // If 'enable' is specified, only use those rules
617    if !global_config.enable.is_empty() || global_config.enable_is_explicit {
618        if contains_all_keyword(&global_config.enable) || extend_enable_all {
619            // enable: ["ALL"] or extend-enable: ["ALL"] → all rules including opt-in
620            for rule in rules {
621                if !is_disabled(rule.name()) {
622                    enabled_rules.push(dyn_clone::clone_box(&**rule));
623                }
624            }
625        } else {
626            // Merge enable set with extend-enable
627            let mut enabled_set: HashSet<String> = global_config.enable.iter().cloned().collect();
628            for name in &extend_enable_set {
629                enabled_set.insert(name.clone());
630            }
631            for rule in rules {
632                if enabled_set.contains(rule.name()) && !is_disabled(rule.name()) {
633                    enabled_rules.push(dyn_clone::clone_box(&**rule));
634                }
635            }
636        }
637    } else if extend_enable_all {
638        // No explicit enable, but extend-enable: ["ALL"] → all rules including opt-in
639        for rule in rules {
640            if !is_disabled(rule.name()) {
641                enabled_rules.push(dyn_clone::clone_box(&**rule));
642            }
643        }
644    } else {
645        // No explicit enable: use all non-opt-in rules + extend-enable, minus disable
646        for rule in rules {
647            let is_opt_in = opt_in_set.contains(rule.name());
648            let explicitly_extended = extend_enable_set.contains(rule.name());
649            if (!is_opt_in || explicitly_extended) && !is_disabled(rule.name()) {
650                enabled_rules.push(dyn_clone::clone_box(&**rule));
651            }
652        }
653    }
654
655    enabled_rules
656}