Skip to main content

rumdl_lib/rules/
mod.rs

1pub mod code_fence_utils;
2pub mod emphasis_style;
3pub mod front_matter_utils;
4pub mod heading_utils;
5pub mod strong_style;
6
7mod md001_heading_increment;
8mod md003_heading_style;
9pub mod md004_unordered_list_style;
10mod md005_list_indent;
11pub mod md007_ul_indent;
12mod md009_trailing_spaces;
13pub mod md010_no_hard_tabs;
14mod md011_no_reversed_links;
15pub mod md013_line_length;
16mod md014_commands_show_output;
17mod md024_no_duplicate_heading;
18mod md025_single_title;
19mod md026_no_trailing_punctuation;
20mod md027_multiple_spaces_blockquote;
21mod md028_no_blanks_blockquote;
22mod md029_ordered_list_prefix;
23pub mod md030_list_marker_space;
24mod md031_blanks_around_fences;
25mod md032_blanks_around_lists;
26mod md033_no_inline_html;
27mod md034_no_bare_urls;
28mod md035_hr_style;
29pub mod md036_no_emphasis_only_first;
30mod md037_spaces_around_emphasis;
31mod md038_no_space_in_code;
32mod md039_no_space_in_links;
33pub mod md040_fenced_code_language;
34mod md041_first_line_heading;
35mod md042_no_empty_links;
36mod md043_required_headings;
37mod md044_proper_names;
38mod md045_no_alt_text;
39mod md046_code_block_style;
40mod md047_single_trailing_newline;
41mod md048_code_fence_style;
42mod md049_emphasis_style;
43mod md050_strong_style;
44pub mod md051_link_fragments;
45mod md052_reference_links_images;
46mod md053_link_image_reference_definitions;
47mod md054_link_image_style;
48mod md055_table_pipe_style;
49mod md056_table_column_count;
50mod md058_blanks_around_tables;
51mod md059_link_text;
52mod md060_table_format;
53mod md061_forbidden_terms;
54mod md062_link_destination_whitespace;
55mod md063_heading_capitalization;
56mod md064_no_multiple_consecutive_spaces;
57mod md065_blanks_around_horizontal_rules;
58mod md066_footnote_validation;
59mod md067_footnote_definition_order;
60mod md068_empty_footnote_definition;
61mod md069_no_duplicate_list_markers;
62mod md070_nested_code_fence;
63mod md071_blank_line_after_frontmatter;
64mod md072_frontmatter_key_sort;
65mod md073_toc_validation;
66mod md074_mkdocs_nav;
67mod md075_orphaned_table_rows;
68mod md076_list_item_spacing;
69mod md077_list_continuation_indent;
70mod md078_missing_chunk_labels;
71mod md079_chunk_label_spaces;
72mod md080_heading_anchor_collision;
73mod md081_no_excessive_emphasis;
74mod md082_no_empty_sections;
75mod md083_mojibake;
76mod md084_invisible_characters;
77mod md085_paragraph_continuation_indent;
78mod md086_no_unclosed_comments;
79mod md087_unused_disable_comment;
80mod md088_quotes_dashes;
81
82pub use code_fence_utils::CodeFenceStyle;
83pub use md001_heading_increment::MD001HeadingIncrement;
84pub use md003_heading_style::MD003HeadingStyle;
85pub use md004_unordered_list_style::MD004UnorderedListStyle;
86pub use md004_unordered_list_style::UnorderedListStyle;
87pub use md005_list_indent::MD005ListIndent;
88pub use md007_ul_indent::MD007ULIndent;
89pub use md009_trailing_spaces::MD009TrailingSpaces;
90pub use md010_no_hard_tabs::{MD010Config, MD010NoHardTabs};
91pub use md011_no_reversed_links::MD011NoReversedLinks;
92pub use md013_line_length::MD013Config;
93pub use md013_line_length::MD013LineLength;
94pub use md014_commands_show_output::MD014CommandsShowOutput;
95pub use md024_no_duplicate_heading::MD024NoDuplicateHeading;
96pub use md025_single_title::MD025SingleTitle;
97pub use md026_no_trailing_punctuation::MD026NoTrailingPunctuation;
98pub use md027_multiple_spaces_blockquote::MD027MultipleSpacesBlockquote;
99pub use md028_no_blanks_blockquote::MD028NoBlanksBlockquote;
100pub use md029_ordered_list_prefix::{ListStyle, MD029OrderedListPrefix};
101pub use md030_list_marker_space::MD030ListMarkerSpace;
102pub use md031_blanks_around_fences::MD031BlanksAroundFences;
103pub use md032_blanks_around_lists::MD032BlanksAroundLists;
104pub use md033_no_inline_html::MD033NoInlineHtml;
105pub use md034_no_bare_urls::MD034NoBareUrls;
106pub use md035_hr_style::MD035HRStyle;
107pub use md036_no_emphasis_only_first::MD036NoEmphasisAsHeading;
108pub use md037_spaces_around_emphasis::MD037NoSpaceInEmphasis;
109pub use md038_no_space_in_code::MD038NoSpaceInCode;
110pub use md039_no_space_in_links::MD039NoSpaceInLinks;
111pub use md040_fenced_code_language::MD040FencedCodeLanguage;
112pub use md041_first_line_heading::MD041FirstLineHeading;
113pub use md042_no_empty_links::MD042NoEmptyLinks;
114pub use md043_required_headings::MD043RequiredHeadings;
115pub use md044_proper_names::MD044ProperNames;
116pub use md045_no_alt_text::MD045NoAltText;
117pub use md046_code_block_style::{CodeBlockStyle, MD046CodeBlockStyle};
118pub use md047_single_trailing_newline::MD047SingleTrailingNewline;
119pub use md048_code_fence_style::MD048CodeFenceStyle;
120pub use md049_emphasis_style::MD049EmphasisStyle;
121pub use md050_strong_style::MD050StrongStyle;
122pub use md051_link_fragments::MD051LinkFragments;
123pub use md052_reference_links_images::MD052ReferenceLinkImages;
124pub use md053_link_image_reference_definitions::MD053LinkImageReferenceDefinitions;
125pub use md054_link_image_style::MD054LinkImageStyle;
126pub use md055_table_pipe_style::MD055TablePipeStyle;
127pub use md056_table_column_count::MD056TableColumnCount;
128pub use md058_blanks_around_tables::MD058BlanksAroundTables;
129pub use md059_link_text::MD059LinkText;
130pub use md060_table_format::ColumnAlign;
131pub use md060_table_format::MD060Config;
132pub use md060_table_format::MD060TableFormat;
133pub use md061_forbidden_terms::MD061ForbiddenTerms;
134pub use md062_link_destination_whitespace::MD062LinkDestinationWhitespace;
135pub use md063_heading_capitalization::MD063HeadingCapitalization;
136pub use md064_no_multiple_consecutive_spaces::MD064NoMultipleConsecutiveSpaces;
137pub use md065_blanks_around_horizontal_rules::MD065BlanksAroundHorizontalRules;
138pub use md066_footnote_validation::MD066FootnoteValidation;
139pub use md067_footnote_definition_order::MD067FootnoteDefinitionOrder;
140pub use md068_empty_footnote_definition::MD068EmptyFootnoteDefinition;
141pub use md069_no_duplicate_list_markers::MD069NoDuplicateListMarkers;
142pub use md070_nested_code_fence::MD070NestedCodeFence;
143pub use md071_blank_line_after_frontmatter::MD071BlankLineAfterFrontmatter;
144pub use md072_frontmatter_key_sort::MD072FrontmatterKeySort;
145pub use md073_toc_validation::MD073TocValidation;
146pub use md074_mkdocs_nav::MD074MkDocsNav;
147pub use md075_orphaned_table_rows::MD075OrphanedTableRows;
148pub use md076_list_item_spacing::{ListItemSpacingStyle, MD076ListItemSpacing};
149pub use md077_list_continuation_indent::{ContinuationStyle, MD077ListContinuationIndent};
150pub use md078_missing_chunk_labels::MD078MissingChunkLabels;
151pub use md079_chunk_label_spaces::MD079ChunkLabelSpaces;
152pub use md080_heading_anchor_collision::MD080HeadingAnchorCollision;
153pub use md081_no_excessive_emphasis::MD081NoExcessiveEmphasis;
154pub use md082_no_empty_sections::MD082NoEmptySections;
155pub use md083_mojibake::MD083DetectMojibake;
156pub use md084_invisible_characters::MD084InvisibleCharacters;
157pub use md085_paragraph_continuation_indent::MD085ParagraphContinuationIndent;
158pub use md086_no_unclosed_comments::MD086NoUnclosedComments;
159pub use md087_unused_disable_comment::MD087UnusedDisableComment;
160pub use md088_quotes_dashes::MD088QuotesDashes;
161
162mod md012_no_multiple_blanks;
163pub use md012_no_multiple_blanks::MD012NoMultipleBlanks;
164
165mod md018_no_missing_space_atx;
166pub use md018_no_missing_space_atx::MD018NoMissingSpaceAtx;
167
168mod md019_no_multiple_space_atx;
169pub use md019_no_multiple_space_atx::MD019NoMultipleSpaceAtx;
170
171mod md020_no_missing_space_closed_atx;
172mod md021_no_multiple_space_closed_atx;
173pub use md020_no_missing_space_closed_atx::MD020NoMissingSpaceClosedAtx;
174pub use md021_no_multiple_space_closed_atx::MD021NoMultipleSpaceClosedAtx;
175
176pub(crate) mod md022_blanks_around_headings;
177pub use md022_blanks_around_headings::MD022BlanksAroundHeadings;
178
179mod md023_heading_start_left;
180pub use md023_heading_start_left::MD023HeadingStartLeft;
181
182mod md057_existing_relative_links;
183
184pub use md057_existing_relative_links::{AbsoluteLinksOption, MD057Config, MD057ExistingRelativeLinks};
185
186use crate::rule::Rule;
187
188/// Type alias for rule constructor functions
189type RuleCtor = fn(&crate::config::Config) -> Box<dyn Rule>;
190
191/// Entry in the rule registry, with metadata about the rule
192struct RuleEntry {
193    name: &'static str,
194    /// Readable name used in diagnostics, generated directives, and help.
195    primary_alias: &'static str,
196    ctor: RuleCtor,
197    /// Whether this rule requires explicit opt-in via extend-enable or enable=["ALL"]
198    opt_in: bool,
199}
200
201/// Registry of all available rules with their constructor functions
202/// This enables automatic inline config support - the engine can recreate
203/// any rule with a merged config without per-rule changes.
204///
205/// Rules marked `opt_in: true` are excluded from the default rule set and must
206/// be explicitly enabled via `extend-enable` or `enable = ["ALL"]`.
207const RULES: &[RuleEntry] = &[
208    RuleEntry {
209        name: "MD001",
210        primary_alias: "heading-increment",
211        ctor: MD001HeadingIncrement::from_config,
212        opt_in: false,
213    },
214    RuleEntry {
215        name: "MD003",
216        primary_alias: "heading-style",
217        ctor: MD003HeadingStyle::from_config,
218        opt_in: false,
219    },
220    RuleEntry {
221        name: "MD004",
222        primary_alias: "ul-style",
223        ctor: MD004UnorderedListStyle::from_config,
224        opt_in: false,
225    },
226    RuleEntry {
227        name: "MD005",
228        primary_alias: "list-indent",
229        ctor: MD005ListIndent::from_config,
230        opt_in: false,
231    },
232    RuleEntry {
233        name: "MD007",
234        primary_alias: "ul-indent",
235        ctor: MD007ULIndent::from_config,
236        opt_in: false,
237    },
238    RuleEntry {
239        name: "MD009",
240        primary_alias: "no-trailing-spaces",
241        ctor: MD009TrailingSpaces::from_config,
242        opt_in: false,
243    },
244    RuleEntry {
245        name: "MD010",
246        primary_alias: "no-hard-tabs",
247        ctor: MD010NoHardTabs::from_config,
248        opt_in: false,
249    },
250    RuleEntry {
251        name: "MD011",
252        primary_alias: "no-reversed-links",
253        ctor: MD011NoReversedLinks::from_config,
254        opt_in: false,
255    },
256    RuleEntry {
257        name: "MD012",
258        primary_alias: "no-multiple-blanks",
259        ctor: MD012NoMultipleBlanks::from_config,
260        opt_in: false,
261    },
262    RuleEntry {
263        name: "MD013",
264        primary_alias: "line-length",
265        ctor: MD013LineLength::from_config,
266        opt_in: false,
267    },
268    RuleEntry {
269        name: "MD014",
270        primary_alias: "commands-show-output",
271        ctor: MD014CommandsShowOutput::from_config,
272        opt_in: false,
273    },
274    RuleEntry {
275        name: "MD018",
276        primary_alias: "no-missing-space-atx",
277        ctor: MD018NoMissingSpaceAtx::from_config,
278        opt_in: false,
279    },
280    RuleEntry {
281        name: "MD019",
282        primary_alias: "no-multiple-space-atx",
283        ctor: MD019NoMultipleSpaceAtx::from_config,
284        opt_in: false,
285    },
286    RuleEntry {
287        name: "MD020",
288        primary_alias: "no-missing-space-closed-atx",
289        ctor: MD020NoMissingSpaceClosedAtx::from_config,
290        opt_in: false,
291    },
292    RuleEntry {
293        name: "MD021",
294        primary_alias: "no-multiple-space-closed-atx",
295        ctor: MD021NoMultipleSpaceClosedAtx::from_config,
296        opt_in: false,
297    },
298    RuleEntry {
299        name: "MD022",
300        primary_alias: "blanks-around-headings",
301        ctor: MD022BlanksAroundHeadings::from_config,
302        opt_in: false,
303    },
304    RuleEntry {
305        name: "MD023",
306        primary_alias: "heading-start-left",
307        ctor: MD023HeadingStartLeft::from_config,
308        opt_in: false,
309    },
310    RuleEntry {
311        name: "MD024",
312        primary_alias: "no-duplicate-heading",
313        ctor: MD024NoDuplicateHeading::from_config,
314        opt_in: false,
315    },
316    RuleEntry {
317        name: "MD025",
318        primary_alias: "single-title",
319        ctor: MD025SingleTitle::from_config,
320        opt_in: false,
321    },
322    RuleEntry {
323        name: "MD026",
324        primary_alias: "no-trailing-punctuation",
325        ctor: MD026NoTrailingPunctuation::from_config,
326        opt_in: false,
327    },
328    RuleEntry {
329        name: "MD027",
330        primary_alias: "no-multiple-space-blockquote",
331        ctor: MD027MultipleSpacesBlockquote::from_config,
332        opt_in: false,
333    },
334    RuleEntry {
335        name: "MD028",
336        primary_alias: "no-blanks-blockquote",
337        ctor: MD028NoBlanksBlockquote::from_config,
338        opt_in: false,
339    },
340    RuleEntry {
341        name: "MD029",
342        primary_alias: "ol-prefix",
343        ctor: MD029OrderedListPrefix::from_config,
344        opt_in: false,
345    },
346    RuleEntry {
347        name: "MD030",
348        primary_alias: "list-marker-space",
349        ctor: MD030ListMarkerSpace::from_config,
350        opt_in: false,
351    },
352    RuleEntry {
353        name: "MD031",
354        primary_alias: "blanks-around-fences",
355        ctor: MD031BlanksAroundFences::from_config,
356        opt_in: false,
357    },
358    RuleEntry {
359        name: "MD032",
360        primary_alias: "blanks-around-lists",
361        ctor: MD032BlanksAroundLists::from_config,
362        opt_in: false,
363    },
364    RuleEntry {
365        name: "MD033",
366        primary_alias: "no-inline-html",
367        ctor: MD033NoInlineHtml::from_config,
368        opt_in: false,
369    },
370    RuleEntry {
371        name: "MD034",
372        primary_alias: "no-bare-urls",
373        ctor: MD034NoBareUrls::from_config,
374        opt_in: false,
375    },
376    RuleEntry {
377        name: "MD035",
378        primary_alias: "hr-style",
379        ctor: MD035HRStyle::from_config,
380        opt_in: false,
381    },
382    RuleEntry {
383        name: "MD036",
384        primary_alias: "no-emphasis-as-heading",
385        ctor: MD036NoEmphasisAsHeading::from_config,
386        opt_in: false,
387    },
388    RuleEntry {
389        name: "MD037",
390        primary_alias: "no-space-in-emphasis",
391        ctor: MD037NoSpaceInEmphasis::from_config,
392        opt_in: false,
393    },
394    RuleEntry {
395        name: "MD038",
396        primary_alias: "no-space-in-code",
397        ctor: MD038NoSpaceInCode::from_config,
398        opt_in: false,
399    },
400    RuleEntry {
401        name: "MD039",
402        primary_alias: "no-space-in-links",
403        ctor: MD039NoSpaceInLinks::from_config,
404        opt_in: false,
405    },
406    RuleEntry {
407        name: "MD040",
408        primary_alias: "fenced-code-language",
409        ctor: MD040FencedCodeLanguage::from_config,
410        opt_in: false,
411    },
412    RuleEntry {
413        name: "MD041",
414        primary_alias: "first-line-heading",
415        ctor: MD041FirstLineHeading::from_config,
416        opt_in: false,
417    },
418    RuleEntry {
419        name: "MD042",
420        primary_alias: "no-empty-links",
421        ctor: MD042NoEmptyLinks::from_config,
422        opt_in: false,
423    },
424    RuleEntry {
425        name: "MD043",
426        primary_alias: "required-headings",
427        ctor: MD043RequiredHeadings::from_config,
428        opt_in: false,
429    },
430    RuleEntry {
431        name: "MD044",
432        primary_alias: "proper-names",
433        ctor: MD044ProperNames::from_config,
434        opt_in: false,
435    },
436    RuleEntry {
437        name: "MD045",
438        primary_alias: "no-alt-text",
439        ctor: MD045NoAltText::from_config,
440        opt_in: false,
441    },
442    RuleEntry {
443        name: "MD046",
444        primary_alias: "code-block-style",
445        ctor: MD046CodeBlockStyle::from_config,
446        opt_in: false,
447    },
448    RuleEntry {
449        name: "MD047",
450        primary_alias: "single-trailing-newline",
451        ctor: MD047SingleTrailingNewline::from_config,
452        opt_in: false,
453    },
454    RuleEntry {
455        name: "MD048",
456        primary_alias: "code-fence-style",
457        ctor: MD048CodeFenceStyle::from_config,
458        opt_in: false,
459    },
460    RuleEntry {
461        name: "MD049",
462        primary_alias: "emphasis-style",
463        ctor: MD049EmphasisStyle::from_config,
464        opt_in: false,
465    },
466    RuleEntry {
467        name: "MD050",
468        primary_alias: "strong-style",
469        ctor: MD050StrongStyle::from_config,
470        opt_in: false,
471    },
472    RuleEntry {
473        name: "MD051",
474        primary_alias: "link-fragments",
475        ctor: MD051LinkFragments::from_config,
476        opt_in: false,
477    },
478    RuleEntry {
479        name: "MD052",
480        primary_alias: "reference-links-images",
481        ctor: MD052ReferenceLinkImages::from_config,
482        opt_in: false,
483    },
484    RuleEntry {
485        name: "MD053",
486        primary_alias: "link-image-reference-definitions",
487        ctor: MD053LinkImageReferenceDefinitions::from_config,
488        opt_in: false,
489    },
490    RuleEntry {
491        name: "MD054",
492        primary_alias: "link-image-style",
493        ctor: MD054LinkImageStyle::from_config,
494        opt_in: false,
495    },
496    RuleEntry {
497        name: "MD055",
498        primary_alias: "table-pipe-style",
499        ctor: MD055TablePipeStyle::from_config,
500        opt_in: false,
501    },
502    RuleEntry {
503        name: "MD056",
504        primary_alias: "table-column-count",
505        ctor: MD056TableColumnCount::from_config,
506        opt_in: false,
507    },
508    RuleEntry {
509        name: "MD057",
510        primary_alias: "existing-relative-links",
511        ctor: MD057ExistingRelativeLinks::from_config,
512        opt_in: false,
513    },
514    RuleEntry {
515        name: "MD058",
516        primary_alias: "blanks-around-tables",
517        ctor: MD058BlanksAroundTables::from_config,
518        opt_in: false,
519    },
520    RuleEntry {
521        name: "MD059",
522        primary_alias: "descriptive-link-text",
523        ctor: MD059LinkText::from_config,
524        opt_in: false,
525    },
526    RuleEntry {
527        name: "MD060",
528        primary_alias: "table-format",
529        ctor: MD060TableFormat::from_config,
530        opt_in: true,
531    },
532    RuleEntry {
533        name: "MD061",
534        primary_alias: "forbidden-terms",
535        ctor: MD061ForbiddenTerms::from_config,
536        opt_in: false,
537    },
538    RuleEntry {
539        name: "MD062",
540        primary_alias: "link-destination-whitespace",
541        ctor: MD062LinkDestinationWhitespace::from_config,
542        opt_in: false,
543    },
544    RuleEntry {
545        name: "MD063",
546        primary_alias: "heading-capitalization",
547        ctor: MD063HeadingCapitalization::from_config,
548        opt_in: true,
549    },
550    RuleEntry {
551        name: "MD064",
552        primary_alias: "no-multiple-consecutive-spaces",
553        ctor: MD064NoMultipleConsecutiveSpaces::from_config,
554        opt_in: false,
555    },
556    RuleEntry {
557        name: "MD065",
558        primary_alias: "blanks-around-horizontal-rules",
559        ctor: MD065BlanksAroundHorizontalRules::from_config,
560        opt_in: false,
561    },
562    RuleEntry {
563        name: "MD066",
564        primary_alias: "footnote-validation",
565        ctor: MD066FootnoteValidation::from_config,
566        opt_in: false,
567    },
568    RuleEntry {
569        name: "MD067",
570        primary_alias: "footnote-definition-order",
571        ctor: MD067FootnoteDefinitionOrder::from_config,
572        opt_in: false,
573    },
574    RuleEntry {
575        name: "MD068",
576        primary_alias: "empty-footnote-definition",
577        ctor: MD068EmptyFootnoteDefinition::from_config,
578        opt_in: false,
579    },
580    RuleEntry {
581        name: "MD069",
582        primary_alias: "no-duplicate-list-markers",
583        ctor: MD069NoDuplicateListMarkers::from_config,
584        opt_in: false,
585    },
586    RuleEntry {
587        name: "MD070",
588        primary_alias: "nested-code-fence",
589        ctor: MD070NestedCodeFence::from_config,
590        opt_in: true,
591    },
592    RuleEntry {
593        name: "MD071",
594        primary_alias: "blank-line-after-frontmatter",
595        ctor: MD071BlankLineAfterFrontmatter::from_config,
596        opt_in: false,
597    },
598    RuleEntry {
599        name: "MD072",
600        primary_alias: "frontmatter-key-sort",
601        ctor: MD072FrontmatterKeySort::from_config,
602        opt_in: true,
603    },
604    RuleEntry {
605        name: "MD073",
606        primary_alias: "toc-validation",
607        ctor: MD073TocValidation::from_config,
608        opt_in: true,
609    },
610    RuleEntry {
611        name: "MD074",
612        primary_alias: "mkdocs-nav",
613        ctor: MD074MkDocsNav::from_config,
614        opt_in: true,
615    },
616    RuleEntry {
617        name: "MD075",
618        primary_alias: "orphaned-table-rows",
619        ctor: MD075OrphanedTableRows::from_config,
620        opt_in: false,
621    },
622    RuleEntry {
623        name: "MD076",
624        primary_alias: "list-item-spacing",
625        ctor: MD076ListItemSpacing::from_config,
626        opt_in: false,
627    },
628    RuleEntry {
629        name: "MD077",
630        primary_alias: "list-continuation-indent",
631        ctor: MD077ListContinuationIndent::from_config,
632        opt_in: false,
633    },
634    RuleEntry {
635        name: "MD078",
636        primary_alias: "missing-chunk-labels",
637        ctor: MD078MissingChunkLabels::from_config,
638        opt_in: false,
639    },
640    RuleEntry {
641        name: "MD079",
642        primary_alias: "chunk-label-spaces",
643        ctor: MD079ChunkLabelSpaces::from_config,
644        opt_in: false,
645    },
646    RuleEntry {
647        name: "MD080",
648        primary_alias: "heading-anchor-collision",
649        ctor: MD080HeadingAnchorCollision::from_config,
650        opt_in: true,
651    },
652    RuleEntry {
653        name: "MD081",
654        primary_alias: "no-excessive-emphasis",
655        ctor: MD081NoExcessiveEmphasis::from_config,
656        opt_in: false,
657    },
658    RuleEntry {
659        name: "MD082",
660        primary_alias: "no-empty-sections",
661        ctor: MD082NoEmptySections::from_config,
662        opt_in: true,
663    },
664    RuleEntry {
665        name: "MD083",
666        primary_alias: "mojibake",
667        ctor: MD083DetectMojibake::from_config,
668        opt_in: true,
669    },
670    RuleEntry {
671        name: "MD084",
672        primary_alias: "invisible-characters",
673        ctor: MD084InvisibleCharacters::from_config,
674        opt_in: true,
675    },
676    RuleEntry {
677        name: "MD085",
678        primary_alias: "paragraph-continuation-indent",
679        ctor: MD085ParagraphContinuationIndent::from_config,
680        opt_in: true,
681    },
682    RuleEntry {
683        name: "MD086",
684        primary_alias: "no-unclosed-comments",
685        ctor: MD086NoUnclosedComments::from_config,
686        opt_in: false,
687    },
688    RuleEntry {
689        name: "MD087",
690        primary_alias: "unused-disable-comment",
691        ctor: MD087UnusedDisableComment::from_config,
692        opt_in: true,
693    },
694    RuleEntry {
695        name: "MD088",
696        primary_alias: "quotes-dashes",
697        ctor: crate::rules::md088_quotes_dashes::MD088QuotesDashes::from_config,
698        opt_in: true,
699    },
700];
701
702/// Returns all rule instances (including opt-in) for config validation and CLI
703pub fn all_rules(config: &crate::config::Config) -> Vec<Box<dyn Rule>> {
704    RULES.iter().map(|entry| (entry.ctor)(config)).collect()
705}
706
707/// Returns the set of rule names that require explicit opt-in
708pub fn opt_in_rules() -> HashSet<&'static str> {
709    RULES
710        .iter()
711        .filter(|entry| entry.opt_in)
712        .map(|entry| entry.name)
713        .collect()
714}
715
716/// The readable alias owned by a canonical rule entry.
717pub(crate) fn primary_alias(name: &str) -> Option<&'static str> {
718    RULES
719        .binary_search_by_key(&name, |entry| entry.name)
720        .ok()
721        .map(|index| RULES[index].primary_alias)
722}
723
724pub(crate) fn rule_identity(index: usize) -> Option<(&'static str, &'static str)> {
725    RULES.get(index).map(|entry| (entry.name, entry.primary_alias))
726}
727
728/// Creates a single rule by name with the given config
729///
730/// This enables automatic inline config support - the engine can recreate
731/// any rule with a merged config without per-rule changes.
732///
733/// Returns None if the rule name is not found.
734pub fn create_rule_by_name(name: &str, config: &crate::config::Config) -> Option<Box<dyn Rule>> {
735    RULES
736        .iter()
737        .find(|entry| entry.name == name)
738        .map(|entry| (entry.ctor)(config))
739}
740
741// Filter rules based on config (moved from main.rs)
742// Note: This needs access to GlobalConfig from the config module.
743use crate::config::GlobalConfig;
744use std::collections::HashSet;
745
746/// Check whether the enable list contains the "all" keyword (case-insensitive).
747fn contains_all_keyword(list: &[String]) -> bool {
748    list.iter().any(|s| s.eq_ignore_ascii_case("all"))
749}
750
751/// Build a canonical-form `HashSet` from a rule-name list.
752///
753/// Rewrites every entry through `resolve_rule_name` so aliases
754/// (`"no-inline-html"`) match the same set as canonical IDs (`"MD033"`).
755/// The `"all"` keyword is preserved (case-folded to lowercase) so the
756/// special-case branches in `filter_rules` continue to work.
757fn canonical_rule_set(list: &[String]) -> HashSet<String> {
758    list.iter()
759        .map(|s| {
760            if s.eq_ignore_ascii_case("all") {
761                "all".to_string()
762            } else {
763                crate::config::resolve_rule_name(s)
764            }
765        })
766        .collect()
767}
768
769/// Filter `rules` according to `global_config.{enable,disable,extend_enable,extend_disable}`.
770///
771/// Rule-name entries may be either canonical IDs (`"MD033"`) or aliases
772/// (`"no-inline-html"`); both forms match identically. Canonical IDs are
773/// the norm — every `Config` produced through a mutation boundary
774/// (`From<SourcedConfig> for Config`, LSP `apply_lsp_settings_*`, WASM
775/// `to_config_with_warnings`) is canonicalised by
776/// `Config::canonicalize_rule_lists`. The defensive canonicalisation here
777/// keeps `filter_rules` correct for programmatic callers that build a
778/// `GlobalConfig` without going through those boundaries.
779pub fn filter_rules(rules: &[Box<dyn Rule>], global_config: &GlobalConfig) -> Vec<Box<dyn Rule>> {
780    let mut enabled_rules: Vec<Box<dyn Rule>> = Vec::new();
781    let disabled_rules: HashSet<String> = canonical_rule_set(&global_config.disable);
782    let opt_in_set = opt_in_rules();
783    let extend_enable_set: HashSet<String> = canonical_rule_set(&global_config.extend_enable);
784    let extend_disable_set: HashSet<String> = canonical_rule_set(&global_config.extend_disable);
785
786    let extend_enable_all = contains_all_keyword(&global_config.extend_enable);
787    let extend_disable_all = contains_all_keyword(&global_config.extend_disable);
788
789    // Helper: should this rule be removed by any disable source?
790    let is_disabled = |name: &str| -> bool {
791        disabled_rules.contains(name) || extend_disable_all || extend_disable_set.contains(name)
792    };
793
794    // Handle 'disable: ["all"]'
795    if disabled_rules.contains("all") {
796        // If 'enable' is also provided, only those rules are enabled, overriding "disable all"
797        if !global_config.enable.is_empty() {
798            if contains_all_keyword(&global_config.enable) {
799                // enable: ["ALL"] + disable: ["all"] cancel out → all rules enabled
800                for rule in rules {
801                    enabled_rules.push(dyn_clone::clone_box(&**rule));
802                }
803            } else {
804                let enabled_set: HashSet<String> = canonical_rule_set(&global_config.enable);
805                for rule in rules {
806                    if enabled_set.contains(rule.name()) {
807                        enabled_rules.push(dyn_clone::clone_box(&**rule));
808                    }
809                }
810            }
811        }
812        // If 'enable' is empty and 'disable: ["all"]', return empty vector.
813        return enabled_rules;
814    }
815
816    // If 'enable' is specified, only use those rules
817    if !global_config.enable.is_empty() || global_config.enable_is_explicit {
818        if contains_all_keyword(&global_config.enable) || extend_enable_all {
819            // enable: ["ALL"] or extend-enable: ["ALL"] → all rules including opt-in
820            for rule in rules {
821                if !is_disabled(rule.name()) {
822                    enabled_rules.push(dyn_clone::clone_box(&**rule));
823                }
824            }
825        } else {
826            // Merge enable set with extend-enable
827            let mut enabled_set: HashSet<String> = canonical_rule_set(&global_config.enable);
828            for name in &extend_enable_set {
829                enabled_set.insert(name.clone());
830            }
831            for rule in rules {
832                if enabled_set.contains(rule.name()) && !is_disabled(rule.name()) {
833                    enabled_rules.push(dyn_clone::clone_box(&**rule));
834                }
835            }
836        }
837    } else if extend_enable_all {
838        // No explicit enable, but extend-enable: ["ALL"] → all rules including opt-in
839        for rule in rules {
840            if !is_disabled(rule.name()) {
841                enabled_rules.push(dyn_clone::clone_box(&**rule));
842            }
843        }
844    } else {
845        // No explicit enable: use all non-opt-in rules + extend-enable, minus disable
846        for rule in rules {
847            let is_opt_in = opt_in_set.contains(rule.name());
848            let explicitly_extended = extend_enable_set.contains(rule.name());
849            if (!is_opt_in || explicitly_extended) && !is_disabled(rule.name()) {
850                enabled_rules.push(dyn_clone::clone_box(&**rule));
851            }
852        }
853    }
854
855    enabled_rules
856}
857
858/// Return `rules` with every rule excluded for `path` by `[per-file-ignores]`
859/// removed.
860///
861/// This is the lint/check-side counterpart to the fix coordinator's internal
862/// per-file-ignore handling: callers that lint a specific file run their rule
863/// set through this so diagnostics never report a rule the file has excluded,
864/// mirroring what the coordinator guarantees on the fix side. Returns a clone
865/// of `rules` unchanged when the file excludes nothing.
866pub fn filter_rules_for_file(
867    rules: &[Box<dyn Rule>],
868    config: &crate::config::Config,
869    path: &std::path::Path,
870) -> Vec<Box<dyn Rule>> {
871    let ignored = config.get_ignored_rules_for_file(path);
872    if ignored.is_empty() {
873        return rules.to_vec();
874    }
875    rules
876        .iter()
877        .filter(|rule| !ignored.contains(rule.name()))
878        .map(|r| dyn_clone::clone_box(&**r))
879        .collect()
880}
881
882#[cfg(test)]
883mod filter_rules_alias_tests {
884    use super::*;
885    use crate::config::Config;
886
887    /// `filter_rules` must accept aliases in `disable` even when the caller
888    /// builds a `GlobalConfig` directly (bypassing the canonicalisation
889    /// boundary in `From<SourcedConfig> for Config`). This is the public-API
890    /// guarantee that programmatic library callers depend on.
891    #[test]
892    fn alias_in_disable_filters_canonical_rule() {
893        let config = Config::default();
894        let rules = all_rules(&config);
895        let global = GlobalConfig {
896            disable: vec!["no-inline-html".to_string()],
897            ..Default::default()
898        };
899
900        let filtered = filter_rules(&rules, &global);
901        assert!(
902            !filtered.iter().any(|r| r.name() == "MD033"),
903            "filter_rules must drop MD033 when its alias `no-inline-html` is in disable, \
904             even on a hand-built GlobalConfig. Got: {:?}",
905            filtered.iter().map(|r| r.name()).collect::<Vec<_>>(),
906        );
907    }
908
909    /// Same guarantee for `extend_disable`.
910    #[test]
911    fn alias_in_extend_disable_filters_canonical_rule() {
912        let config = Config::default();
913        let rules = all_rules(&config);
914        let global = GlobalConfig {
915            extend_disable: vec!["line-length".to_string()],
916            ..Default::default()
917        };
918
919        let filtered = filter_rules(&rules, &global);
920        assert!(
921            !filtered.iter().any(|r| r.name() == "MD013"),
922            "filter_rules must drop MD013 when alias `line-length` is in extend_disable. Got: {:?}",
923            filtered.iter().map(|r| r.name()).collect::<Vec<_>>(),
924        );
925    }
926
927    /// `enable` aliases must select the canonical rule.
928    #[test]
929    fn alias_in_enable_selects_canonical_rule() {
930        let config = Config::default();
931        let rules = all_rules(&config);
932        let global = GlobalConfig {
933            enable: vec!["no-inline-html".to_string()],
934            ..Default::default()
935        };
936
937        let filtered = filter_rules(&rules, &global);
938        let names: Vec<&str> = filtered.iter().map(|r| r.name()).collect();
939        assert_eq!(
940            names,
941            vec!["MD033"],
942            "filter_rules must select only MD033 when alias `no-inline-html` is the sole enable. \
943             Got: {names:?}",
944        );
945    }
946
947    /// The canonical-IDs invariant is preserved: a config that is already
948    /// canonical produces the same filter result as one that uses aliases.
949    #[test]
950    fn alias_and_canonical_produce_identical_filter_result() {
951        let config = Config::default();
952        let rules = all_rules(&config);
953
954        let alias_global = GlobalConfig {
955            disable: vec!["no-inline-html".to_string(), "line-length".to_string()],
956            ..Default::default()
957        };
958        let canonical_global = GlobalConfig {
959            disable: vec!["MD033".to_string(), "MD013".to_string()],
960            ..Default::default()
961        };
962
963        let alias_names: Vec<&str> = filter_rules(&rules, &alias_global).iter().map(|r| r.name()).collect();
964        let canonical_names: Vec<&str> = filter_rules(&rules, &canonical_global)
965            .iter()
966            .map(|r| r.name())
967            .collect();
968        assert_eq!(alias_names, canonical_names);
969    }
970}