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 md002_first_heading_h1;
13mod md003_heading_style;
14pub mod md004_unordered_list_style;
15mod md005_list_indent;
16mod md006_start_bullets;
17mod md007_ul_indent;
18mod md009_trailing_spaces;
19mod md010_no_hard_tabs;
20mod md011_no_reversed_links;
21pub mod md013_line_length;
22mod md014_commands_show_output;
23mod md024_no_duplicate_heading;
24mod md025_single_title;
25mod md026_no_trailing_punctuation;
26mod md027_multiple_spaces_blockquote;
27mod md028_no_blanks_blockquote;
28mod md029_ordered_list_prefix;
29pub mod md030_list_marker_space;
30mod md031_blanks_around_fences;
31mod md032_blanks_around_lists;
32mod md033_no_inline_html;
33mod md034_no_bare_urls;
34mod md035_hr_style;
35mod md036_no_emphasis_only_first;
36mod md037_spaces_around_emphasis;
37mod md038_no_space_in_code;
38mod md039_no_space_in_links;
39mod md040_fenced_code_language;
40mod md041_first_line_heading;
41mod md042_no_empty_links;
42mod md043_required_headings;
43mod md044_proper_names;
44mod md045_no_alt_text;
45mod md046_code_block_style;
46mod md047_single_trailing_newline;
47mod md048_code_fence_style;
48mod md049_emphasis_style;
49mod md050_strong_style;
50mod md051_link_fragments;
51mod md052_reference_links_images;
52mod md053_link_image_reference_definitions;
53mod md054_link_image_style;
54mod md055_table_pipe_style;
55mod md056_table_column_count;
56mod md058_blanks_around_tables;
57
58pub use md001_heading_increment::MD001HeadingIncrement;
59pub use md002_first_heading_h1::MD002FirstHeadingH1;
60pub use md003_heading_style::MD003HeadingStyle;
61pub use md004_unordered_list_style::MD004UnorderedListStyle;
62pub use md004_unordered_list_style::UnorderedListStyle;
63pub use md005_list_indent::MD005ListIndent;
64pub use md006_start_bullets::MD006StartBullets;
65pub use md007_ul_indent::MD007ULIndent;
66pub use md009_trailing_spaces::MD009TrailingSpaces;
67pub use md010_no_hard_tabs::MD010NoHardTabs;
68pub use md011_no_reversed_links::MD011NoReversedLinks;
69pub use md013_line_length::MD013LineLength;
70pub use md014_commands_show_output::MD014CommandsShowOutput;
71pub use md024_no_duplicate_heading::MD024NoDuplicateHeading;
72pub use md025_single_title::MD025SingleTitle;
73pub use md026_no_trailing_punctuation::MD026NoTrailingPunctuation;
74pub use md027_multiple_spaces_blockquote::MD027MultipleSpacesBlockquote;
75pub use md028_no_blanks_blockquote::MD028NoBlanksBlockquote;
76pub use md029_ordered_list_prefix::{ListStyle, MD029OrderedListPrefix};
77pub use md030_list_marker_space::MD030ListMarkerSpace;
78pub use md031_blanks_around_fences::MD031BlanksAroundFences;
79pub use md032_blanks_around_lists::MD032BlanksAroundLists;
80pub use md033_no_inline_html::MD033NoInlineHtml;
81pub use md034_no_bare_urls::MD034NoBareUrls;
82pub use md035_hr_style::MD035HRStyle;
83pub use md036_no_emphasis_only_first::MD036NoEmphasisAsHeading;
84pub use md037_spaces_around_emphasis::MD037NoSpaceInEmphasis;
85pub use md038_no_space_in_code::MD038NoSpaceInCode;
86pub use md039_no_space_in_links::MD039NoSpaceInLinks;
87pub use md040_fenced_code_language::MD040FencedCodeLanguage;
88pub use md041_first_line_heading::MD041FirstLineHeading;
89pub use md042_no_empty_links::MD042NoEmptyLinks;
90pub use md043_required_headings::MD043RequiredHeadings;
91pub use md044_proper_names::MD044ProperNames;
92pub use md045_no_alt_text::MD045NoAltText;
93pub use md046_code_block_style::MD046CodeBlockStyle;
94pub use md047_single_trailing_newline::MD047SingleTrailingNewline;
95pub use md048_code_fence_style::MD048CodeFenceStyle;
96pub use md049_emphasis_style::MD049EmphasisStyle;
97pub use md050_strong_style::MD050StrongStyle;
98pub use md051_link_fragments::MD051LinkFragments;
99pub use md052_reference_links_images::MD052ReferenceLinkImages;
100pub use md053_link_image_reference_definitions::MD053LinkImageReferenceDefinitions;
101pub use md054_link_image_style::MD054LinkImageStyle;
102pub use md055_table_pipe_style::MD055TablePipeStyle;
103pub use md056_table_column_count::MD056TableColumnCount;
104pub use md058_blanks_around_tables::MD058BlanksAroundTables;
105
106mod md012_no_multiple_blanks;
107pub use md012_no_multiple_blanks::MD012NoMultipleBlanks;
108
109mod md018_no_missing_space_atx;
110pub use md018_no_missing_space_atx::MD018NoMissingSpaceAtx;
111
112mod md019_no_multiple_space_atx;
113pub use md019_no_multiple_space_atx::MD019NoMultipleSpaceAtx;
114
115mod md020_no_missing_space_closed_atx;
116mod md021_no_multiple_space_closed_atx;
117pub use md020_no_missing_space_closed_atx::MD020NoMissingSpaceClosedAtx;
118pub use md021_no_multiple_space_closed_atx::MD021NoMultipleSpaceClosedAtx;
119
120mod md022_blanks_around_headings;
121pub use md022_blanks_around_headings::MD022BlanksAroundHeadings;
122
123mod md023_heading_start_left;
124pub use md023_heading_start_left::MD023HeadingStartLeft;
125
126mod md057_existing_relative_links;
127
128pub use md057_existing_relative_links::MD057ExistingRelativeLinks;
129
130use crate::rule::Rule;
131
132/// Returns all rule instances for config validation and CLI
133pub fn all_rules(config: &crate::config::Config) -> Vec<Box<dyn Rule>> {
134    type RuleCtor = fn(&crate::config::Config) -> Box<dyn Rule>;
135    const RULES: &[(&str, RuleCtor)] = &[
136        ("MD001", MD001HeadingIncrement::from_config),
137        ("MD002", MD002FirstHeadingH1::from_config),
138        ("MD003", MD003HeadingStyle::from_config),
139        ("MD004", MD004UnorderedListStyle::from_config),
140        ("MD005", MD005ListIndent::from_config),
141        ("MD006", MD006StartBullets::from_config),
142        ("MD007", MD007ULIndent::from_config),
143        ("MD009", MD009TrailingSpaces::from_config),
144        ("MD010", MD010NoHardTabs::from_config),
145        ("MD011", MD011NoReversedLinks::from_config),
146        ("MD012", MD012NoMultipleBlanks::from_config),
147        ("MD013", MD013LineLength::from_config),
148        ("MD014", MD014CommandsShowOutput::from_config),
149        ("MD018", MD018NoMissingSpaceAtx::from_config),
150        ("MD019", MD019NoMultipleSpaceAtx::from_config),
151        ("MD020", MD020NoMissingSpaceClosedAtx::from_config),
152        ("MD021", MD021NoMultipleSpaceClosedAtx::from_config),
153        ("MD022", MD022BlanksAroundHeadings::from_config),
154        ("MD023", MD023HeadingStartLeft::from_config),
155        ("MD024", MD024NoDuplicateHeading::from_config),
156        ("MD025", MD025SingleTitle::from_config),
157        ("MD026", MD026NoTrailingPunctuation::from_config),
158        ("MD027", MD027MultipleSpacesBlockquote::from_config),
159        ("MD028", MD028NoBlanksBlockquote::from_config),
160        ("MD029", MD029OrderedListPrefix::from_config),
161        ("MD030", MD030ListMarkerSpace::from_config),
162        ("MD031", MD031BlanksAroundFences::from_config),
163        ("MD032", MD032BlanksAroundLists::from_config),
164        ("MD033", MD033NoInlineHtml::from_config),
165        ("MD034", MD034NoBareUrls::from_config),
166        ("MD035", MD035HRStyle::from_config),
167        ("MD036", MD036NoEmphasisAsHeading::from_config),
168        ("MD037", MD037NoSpaceInEmphasis::from_config),
169        ("MD038", MD038NoSpaceInCode::from_config),
170        ("MD039", MD039NoSpaceInLinks::from_config),
171        ("MD040", MD040FencedCodeLanguage::from_config),
172        ("MD041", MD041FirstLineHeading::from_config),
173        ("MD042", MD042NoEmptyLinks::from_config),
174        ("MD043", MD043RequiredHeadings::from_config),
175        ("MD044", MD044ProperNames::from_config),
176        ("MD045", MD045NoAltText::from_config),
177        ("MD046", MD046CodeBlockStyle::from_config),
178        ("MD047", MD047SingleTrailingNewline::from_config),
179        ("MD048", MD048CodeFenceStyle::from_config),
180        ("MD049", MD049EmphasisStyle::from_config),
181        ("MD050", MD050StrongStyle::from_config),
182        ("MD051", MD051LinkFragments::from_config),
183        ("MD052", MD052ReferenceLinkImages::from_config),
184        ("MD053", MD053LinkImageReferenceDefinitions::from_config),
185        ("MD054", MD054LinkImageStyle::from_config),
186        ("MD055", MD055TablePipeStyle::from_config),
187        ("MD056", MD056TableColumnCount::from_config),
188        ("MD057", MD057ExistingRelativeLinks::from_config),
189        ("MD058", MD058BlanksAroundTables::from_config),
190    ];
191    RULES.iter().map(|(_, ctor)| ctor(config)).collect()
192}
193
194// Filter rules based on config (moved from main.rs)
195// Note: This needs access to GlobalConfig from the config module.
196use crate::config::GlobalConfig;
197use std::collections::HashSet;
198
199pub fn filter_rules(rules: &[Box<dyn Rule>], global_config: &GlobalConfig) -> Vec<Box<dyn Rule>> {
200    let mut enabled_rules: Vec<Box<dyn Rule>> = Vec::new();
201    let disabled_rules: HashSet<String> = global_config.disable.iter().cloned().collect();
202
203    // Handle 'disable: ["all"]'
204    if disabled_rules.contains("all") {
205        // If 'enable' is also provided, only those rules are enabled, overriding "disable all"
206        if !global_config.enable.is_empty() {
207            let enabled_set: HashSet<String> = global_config.enable.iter().cloned().collect();
208            for rule in rules {
209                if enabled_set.contains(rule.name()) {
210                    // Clone the rule (rules need to implement Clone or we need another approach)
211                    // For now, assuming rules are copyable/default constructible easily is complex.
212                    // Let's recreate the rule instance instead. This is brittle.
213                    // A better approach would involve rule registration and instantiation by name.
214                    // --> Reverting to filtering the input slice by cloning Box<dyn Rule>.
215                    enabled_rules.push(dyn_clone::clone_box(&**rule));
216                }
217            }
218        }
219        // If 'enable' is empty and 'disable: ["all"]', return empty vector.
220        return enabled_rules;
221    }
222
223    // If 'enable' is specified, only use those rules
224    if !global_config.enable.is_empty() {
225        let enabled_set: HashSet<String> = global_config.enable.iter().cloned().collect();
226        for rule in rules {
227            if enabled_set.contains(rule.name()) && !disabled_rules.contains(rule.name()) {
228                enabled_rules.push(dyn_clone::clone_box(&**rule));
229            }
230        }
231    } else {
232        // Otherwise, use all rules except the disabled ones
233        for rule in rules {
234            if !disabled_rules.contains(rule.name()) {
235                enabled_rules.push(dyn_clone::clone_box(&**rule));
236            }
237        }
238    }
239
240    enabled_rules
241}