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