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