Skip to main content

rumdl_lib/rules/
mod.rs

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