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/// Registry of all available rules with their constructor functions
167/// This enables automatic inline config support - the engine can recreate
168/// any rule with a merged config without per-rule opt-in
169const RULES: &[(&str, RuleCtor)] = &[
170    ("MD001", MD001HeadingIncrement::from_config),
171    ("MD003", MD003HeadingStyle::from_config),
172    ("MD004", MD004UnorderedListStyle::from_config),
173    ("MD005", MD005ListIndent::from_config),
174    ("MD007", MD007ULIndent::from_config),
175    ("MD009", MD009TrailingSpaces::from_config),
176    ("MD010", MD010NoHardTabs::from_config),
177    ("MD011", MD011NoReversedLinks::from_config),
178    ("MD012", MD012NoMultipleBlanks::from_config),
179    ("MD013", MD013LineLength::from_config),
180    ("MD014", MD014CommandsShowOutput::from_config),
181    ("MD018", MD018NoMissingSpaceAtx::from_config),
182    ("MD019", MD019NoMultipleSpaceAtx::from_config),
183    ("MD020", MD020NoMissingSpaceClosedAtx::from_config),
184    ("MD021", MD021NoMultipleSpaceClosedAtx::from_config),
185    ("MD022", MD022BlanksAroundHeadings::from_config),
186    ("MD023", MD023HeadingStartLeft::from_config),
187    ("MD024", MD024NoDuplicateHeading::from_config),
188    ("MD025", MD025SingleTitle::from_config),
189    ("MD026", MD026NoTrailingPunctuation::from_config),
190    ("MD027", MD027MultipleSpacesBlockquote::from_config),
191    ("MD028", MD028NoBlanksBlockquote::from_config),
192    ("MD029", MD029OrderedListPrefix::from_config),
193    ("MD030", MD030ListMarkerSpace::from_config),
194    ("MD031", MD031BlanksAroundFences::from_config),
195    ("MD032", MD032BlanksAroundLists::from_config),
196    ("MD033", MD033NoInlineHtml::from_config),
197    ("MD034", MD034NoBareUrls::from_config),
198    ("MD035", MD035HRStyle::from_config),
199    ("MD036", MD036NoEmphasisAsHeading::from_config),
200    ("MD037", MD037NoSpaceInEmphasis::from_config),
201    ("MD038", MD038NoSpaceInCode::from_config),
202    ("MD039", MD039NoSpaceInLinks::from_config),
203    ("MD040", MD040FencedCodeLanguage::from_config),
204    ("MD041", MD041FirstLineHeading::from_config),
205    ("MD042", MD042NoEmptyLinks::from_config),
206    ("MD043", MD043RequiredHeadings::from_config),
207    ("MD044", MD044ProperNames::from_config),
208    ("MD045", MD045NoAltText::from_config),
209    ("MD046", MD046CodeBlockStyle::from_config),
210    ("MD047", MD047SingleTrailingNewline::from_config),
211    ("MD048", MD048CodeFenceStyle::from_config),
212    ("MD049", MD049EmphasisStyle::from_config),
213    ("MD050", MD050StrongStyle::from_config),
214    ("MD051", MD051LinkFragments::from_config),
215    ("MD052", MD052ReferenceLinkImages::from_config),
216    ("MD053", MD053LinkImageReferenceDefinitions::from_config),
217    ("MD054", MD054LinkImageStyle::from_config),
218    ("MD055", MD055TablePipeStyle::from_config),
219    ("MD056", MD056TableColumnCount::from_config),
220    ("MD057", MD057ExistingRelativeLinks::from_config),
221    ("MD058", MD058BlanksAroundTables::from_config),
222    ("MD059", MD059LinkText::from_config),
223    ("MD060", MD060TableFormat::from_config),
224    ("MD061", MD061ForbiddenTerms::from_config),
225    ("MD062", MD062LinkDestinationWhitespace::from_config),
226    ("MD063", MD063HeadingCapitalization::from_config),
227    ("MD064", MD064NoMultipleConsecutiveSpaces::from_config),
228    ("MD065", MD065BlanksAroundHorizontalRules::from_config),
229    ("MD066", MD066FootnoteValidation::from_config),
230    ("MD067", MD067FootnoteDefinitionOrder::from_config),
231    ("MD068", MD068EmptyFootnoteDefinition::from_config),
232    ("MD069", MD069NoDuplicateListMarkers::from_config),
233    ("MD070", MD070NestedCodeFence::from_config),
234    ("MD071", MD071BlankLineAfterFrontmatter::from_config),
235    ("MD072", MD072FrontmatterKeySort::from_config),
236    ("MD073", MD073TocValidation::from_config),
237    ("MD074", MD074MkDocsNav::from_config),
238];
239
240/// Returns all rule instances for config validation and CLI
241pub fn all_rules(config: &crate::config::Config) -> Vec<Box<dyn Rule>> {
242    RULES.iter().map(|(_, ctor)| ctor(config)).collect()
243}
244
245/// Creates a single rule by name with the given config
246///
247/// This enables automatic inline config support - the engine can recreate
248/// any rule with a merged config without per-rule changes.
249///
250/// Returns None if the rule name is not found.
251pub fn create_rule_by_name(name: &str, config: &crate::config::Config) -> Option<Box<dyn Rule>> {
252    RULES
253        .iter()
254        .find(|(rule_name, _)| *rule_name == name)
255        .map(|(_, ctor)| ctor(config))
256}
257
258// Filter rules based on config (moved from main.rs)
259// Note: This needs access to GlobalConfig from the config module.
260use crate::config::GlobalConfig;
261use std::collections::HashSet;
262
263/// Check whether the enable list contains the "all" keyword (case-insensitive).
264fn enable_contains_all(enable: &[String]) -> bool {
265    enable.iter().any(|s| s.eq_ignore_ascii_case("all"))
266}
267
268pub fn filter_rules(rules: &[Box<dyn Rule>], global_config: &GlobalConfig) -> Vec<Box<dyn Rule>> {
269    let mut enabled_rules: Vec<Box<dyn Rule>> = Vec::new();
270    let disabled_rules: HashSet<String> = global_config.disable.iter().cloned().collect();
271
272    // Handle 'disable: ["all"]'
273    if disabled_rules.contains("all") {
274        // If 'enable' is also provided, only those rules are enabled, overriding "disable all"
275        if !global_config.enable.is_empty() {
276            if enable_contains_all(&global_config.enable) {
277                // enable: ["ALL"] + disable: ["all"] cancel out → all rules enabled
278                for rule in rules {
279                    enabled_rules.push(dyn_clone::clone_box(&**rule));
280                }
281            } else {
282                let enabled_set: HashSet<String> = global_config.enable.iter().cloned().collect();
283                for rule in rules {
284                    if enabled_set.contains(rule.name()) {
285                        enabled_rules.push(dyn_clone::clone_box(&**rule));
286                    }
287                }
288            }
289        }
290        // If 'enable' is empty and 'disable: ["all"]', return empty vector.
291        return enabled_rules;
292    }
293
294    // If 'enable' is specified, only use those rules
295    if !global_config.enable.is_empty() {
296        if enable_contains_all(&global_config.enable) {
297            // enable: ["ALL"] means all rules, then apply disable on top
298            for rule in rules {
299                if !disabled_rules.contains(rule.name()) {
300                    enabled_rules.push(dyn_clone::clone_box(&**rule));
301                }
302            }
303        } else {
304            let enabled_set: HashSet<String> = global_config.enable.iter().cloned().collect();
305            for rule in rules {
306                if enabled_set.contains(rule.name()) && !disabled_rules.contains(rule.name()) {
307                    enabled_rules.push(dyn_clone::clone_box(&**rule));
308                }
309            }
310        }
311    } else {
312        // Otherwise, use all rules except the disabled ones
313        for rule in rules {
314            if !disabled_rules.contains(rule.name()) {
315                enabled_rules.push(dyn_clone::clone_box(&**rule));
316            }
317        }
318    }
319
320    enabled_rules
321}