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;
73
74pub use md001_heading_increment::MD001HeadingIncrement;
75pub use md003_heading_style::MD003HeadingStyle;
76pub use md004_unordered_list_style::MD004UnorderedListStyle;
77pub use md004_unordered_list_style::UnorderedListStyle;
78pub use md005_list_indent::MD005ListIndent;
79pub use md007_ul_indent::MD007ULIndent;
80pub use md009_trailing_spaces::MD009TrailingSpaces;
81pub use md010_no_hard_tabs::MD010NoHardTabs;
82pub use md011_no_reversed_links::MD011NoReversedLinks;
83pub use md013_line_length::MD013Config;
84pub use md013_line_length::MD013LineLength;
85pub use md014_commands_show_output::MD014CommandsShowOutput;
86pub use md024_no_duplicate_heading::MD024NoDuplicateHeading;
87pub use md025_single_title::MD025SingleTitle;
88pub use md026_no_trailing_punctuation::MD026NoTrailingPunctuation;
89pub use md027_multiple_spaces_blockquote::MD027MultipleSpacesBlockquote;
90pub use md028_no_blanks_blockquote::MD028NoBlanksBlockquote;
91pub use md029_ordered_list_prefix::{ListStyle, MD029OrderedListPrefix};
92pub use md030_list_marker_space::MD030ListMarkerSpace;
93pub use md031_blanks_around_fences::MD031BlanksAroundFences;
94pub use md032_blanks_around_lists::MD032BlanksAroundLists;
95pub use md033_no_inline_html::MD033NoInlineHtml;
96pub use md034_no_bare_urls::MD034NoBareUrls;
97pub use md035_hr_style::MD035HRStyle;
98pub use md036_no_emphasis_only_first::MD036NoEmphasisAsHeading;
99pub use md037_spaces_around_emphasis::MD037NoSpaceInEmphasis;
100pub use md038_no_space_in_code::MD038NoSpaceInCode;
101pub use md039_no_space_in_links::MD039NoSpaceInLinks;
102pub use md040_fenced_code_language::MD040FencedCodeLanguage;
103pub use md041_first_line_heading::MD041FirstLineHeading;
104pub use md042_no_empty_links::MD042NoEmptyLinks;
105pub use md043_required_headings::MD043RequiredHeadings;
106pub use md044_proper_names::MD044ProperNames;
107pub use md045_no_alt_text::MD045NoAltText;
108pub use md046_code_block_style::MD046CodeBlockStyle;
109pub use md047_single_trailing_newline::MD047SingleTrailingNewline;
110pub use md048_code_fence_style::MD048CodeFenceStyle;
111pub use md049_emphasis_style::MD049EmphasisStyle;
112pub use md050_strong_style::MD050StrongStyle;
113pub use md051_link_fragments::MD051LinkFragments;
114pub use md052_reference_links_images::MD052ReferenceLinkImages;
115pub use md053_link_image_reference_definitions::MD053LinkImageReferenceDefinitions;
116pub use md054_link_image_style::MD054LinkImageStyle;
117pub use md055_table_pipe_style::MD055TablePipeStyle;
118pub use md056_table_column_count::MD056TableColumnCount;
119pub use md058_blanks_around_tables::MD058BlanksAroundTables;
120pub use md059_link_text::MD059LinkText;
121pub use md060_table_format::ColumnAlign;
122pub use md060_table_format::MD060Config;
123pub use md060_table_format::MD060TableFormat;
124pub use md061_forbidden_terms::MD061ForbiddenTerms;
125pub use md062_link_destination_whitespace::MD062LinkDestinationWhitespace;
126pub use md063_heading_capitalization::MD063HeadingCapitalization;
127pub use md064_no_multiple_consecutive_spaces::MD064NoMultipleConsecutiveSpaces;
128pub use md065_blanks_around_horizontal_rules::MD065BlanksAroundHorizontalRules;
129pub use md066_footnote_validation::MD066FootnoteValidation;
130pub use md067_footnote_definition_order::MD067FootnoteDefinitionOrder;
131pub use md068_empty_footnote_definition::MD068EmptyFootnoteDefinition;
132pub use md069_no_duplicate_list_markers::MD069NoDuplicateListMarkers;
133pub use md070_nested_code_fence::MD070NestedCodeFence;
134pub use md071_blank_line_after_frontmatter::MD071BlankLineAfterFrontmatter;
135pub use md072_frontmatter_key_sort::MD072FrontmatterKeySort;
136pub use md073_toc_validation::MD073TocValidation;
137pub use md074_mkdocs_nav::MD074MkDocsNav;
138pub use md075_orphaned_table_rows::MD075OrphanedTableRows;
139pub use md076_list_item_spacing::{ListItemSpacingStyle, MD076ListItemSpacing};
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: false,
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];
536
537/// Returns all rule instances (including opt-in) for config validation and CLI
538pub fn all_rules(config: &crate::config::Config) -> Vec<Box<dyn Rule>> {
539    RULES.iter().map(|entry| (entry.ctor)(config)).collect()
540}
541
542/// Returns the set of rule names that require explicit opt-in
543pub fn opt_in_rules() -> HashSet<&'static str> {
544    RULES
545        .iter()
546        .filter(|entry| entry.opt_in)
547        .map(|entry| entry.name)
548        .collect()
549}
550
551/// Creates a single rule by name with the given config
552///
553/// This enables automatic inline config support - the engine can recreate
554/// any rule with a merged config without per-rule changes.
555///
556/// Returns None if the rule name is not found.
557pub fn create_rule_by_name(name: &str, config: &crate::config::Config) -> Option<Box<dyn Rule>> {
558    RULES
559        .iter()
560        .find(|entry| entry.name == name)
561        .map(|entry| (entry.ctor)(config))
562}
563
564// Filter rules based on config (moved from main.rs)
565// Note: This needs access to GlobalConfig from the config module.
566use crate::config::GlobalConfig;
567use std::collections::HashSet;
568
569/// Check whether the enable list contains the "all" keyword (case-insensitive).
570fn contains_all_keyword(list: &[String]) -> bool {
571    list.iter().any(|s| s.eq_ignore_ascii_case("all"))
572}
573
574pub fn filter_rules(rules: &[Box<dyn Rule>], global_config: &GlobalConfig) -> Vec<Box<dyn Rule>> {
575    let mut enabled_rules: Vec<Box<dyn Rule>> = Vec::new();
576    let disabled_rules: HashSet<String> = global_config.disable.iter().cloned().collect();
577    let opt_in_set = opt_in_rules();
578    let extend_enable_set: HashSet<String> = global_config.extend_enable.iter().cloned().collect();
579    let extend_disable_set: HashSet<String> = global_config.extend_disable.iter().cloned().collect();
580
581    let extend_enable_all = contains_all_keyword(&global_config.extend_enable);
582    let extend_disable_all = contains_all_keyword(&global_config.extend_disable);
583
584    // Helper: should this rule be removed by any disable source?
585    let is_disabled = |name: &str| -> bool {
586        disabled_rules.contains(name) || extend_disable_all || extend_disable_set.contains(name)
587    };
588
589    // Handle 'disable: ["all"]'
590    if disabled_rules.contains("all") {
591        // If 'enable' is also provided, only those rules are enabled, overriding "disable all"
592        if !global_config.enable.is_empty() {
593            if contains_all_keyword(&global_config.enable) {
594                // enable: ["ALL"] + disable: ["all"] cancel out → all rules enabled
595                for rule in rules {
596                    enabled_rules.push(dyn_clone::clone_box(&**rule));
597                }
598            } else {
599                let enabled_set: HashSet<String> = global_config.enable.iter().cloned().collect();
600                for rule in rules {
601                    if enabled_set.contains(rule.name()) {
602                        enabled_rules.push(dyn_clone::clone_box(&**rule));
603                    }
604                }
605            }
606        }
607        // If 'enable' is empty and 'disable: ["all"]', return empty vector.
608        return enabled_rules;
609    }
610
611    // If 'enable' is specified, only use those rules
612    if !global_config.enable.is_empty() || global_config.enable_is_explicit {
613        if contains_all_keyword(&global_config.enable) || extend_enable_all {
614            // enable: ["ALL"] or extend-enable: ["ALL"] → all rules including opt-in
615            for rule in rules {
616                if !is_disabled(rule.name()) {
617                    enabled_rules.push(dyn_clone::clone_box(&**rule));
618                }
619            }
620        } else {
621            // Merge enable set with extend-enable
622            let mut enabled_set: HashSet<String> = global_config.enable.iter().cloned().collect();
623            for name in &extend_enable_set {
624                enabled_set.insert(name.clone());
625            }
626            for rule in rules {
627                if enabled_set.contains(rule.name()) && !is_disabled(rule.name()) {
628                    enabled_rules.push(dyn_clone::clone_box(&**rule));
629                }
630            }
631        }
632    } else if extend_enable_all {
633        // No explicit enable, but extend-enable: ["ALL"] → all rules including opt-in
634        for rule in rules {
635            if !is_disabled(rule.name()) {
636                enabled_rules.push(dyn_clone::clone_box(&**rule));
637            }
638        }
639    } else {
640        // No explicit enable: use all non-opt-in rules + extend-enable, minus disable
641        for rule in rules {
642            let is_opt_in = opt_in_set.contains(rule.name());
643            let explicitly_extended = extend_enable_set.contains(rule.name());
644            if (!is_opt_in || explicitly_extended) && !is_disabled(rule.name()) {
645                enabled_rules.push(dyn_clone::clone_box(&**rule));
646            }
647        }
648    }
649
650    enabled_rules
651}