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