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