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