rumdl 0.1.75

A fast Markdown linter written in Rust (Ru(st) MarkDown Linter)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
use rumdl_lib::config::{Config, MarkdownFlavor};
use rumdl_lib::inline_config::InlineConfig;
use rumdl_lib::lint;
use rumdl_lib::lint_context::LintContext;
use rumdl_lib::rule::Rule;
use rumdl_lib::rules::all_rules;

#[test]
fn test_markdownlint_disable_enable() {
    let content = r#"# Test Document

<!-- markdownlint-disable MD013 -->
This is a very long line that exceeds 80 characters and would normally trigger MD013 but is disabled by the comment above

<!-- markdownlint-enable MD013 -->
This is another very long line that exceeds 80 characters and should trigger MD013 because it was re-enabled
"#;

    let rules = all_rules(&Config::default());
    let warnings = lint(
        content,
        &rules,
        false,
        rumdl_lib::config::MarkdownFlavor::Standard,
        None,
        None,
    )
    .unwrap();

    // Find MD013 warnings
    let md013_warnings: Vec<_> = warnings
        .iter()
        .filter(|w| w.rule_name.as_ref().is_some_and(|n| *n == "MD013"))
        .collect();

    // Should have exactly one MD013 warning (line 7)
    assert_eq!(md013_warnings.len(), 1);
    assert_eq!(md013_warnings[0].line, 7);
}

#[test]
fn test_markdownlint_disable_line() {
    let content = r#"# Test Document

This is a very long line that exceeds 80 characters and would normally trigger MD013 <!-- markdownlint-disable-line MD013 -->

This is another very long line that exceeds 80 characters and should trigger MD013 because it's not disabled
"#;

    let rules = all_rules(&Config::default());
    let warnings = lint(
        content,
        &rules,
        false,
        rumdl_lib::config::MarkdownFlavor::Standard,
        None,
        None,
    )
    .unwrap();

    // Find MD013 warnings
    let md013_warnings: Vec<_> = warnings
        .iter()
        .filter(|w| w.rule_name.as_ref().is_some_and(|n| *n == "MD013"))
        .collect();

    // Should have exactly one MD013 warning (line 5, not line 3)
    assert_eq!(md013_warnings.len(), 1);
    assert_eq!(md013_warnings[0].line, 5);
}

#[test]
fn test_markdownlint_disable_next_line() {
    let content = r#"# Test Document

<!-- markdownlint-disable-next-line MD013 -->
This is a very long line that exceeds 80 characters and would normally trigger MD013 but is disabled

This is another very long line that exceeds 80 characters and should trigger MD013 because it's not disabled
"#;

    let rules = all_rules(&Config::default());
    let warnings = lint(
        content,
        &rules,
        false,
        rumdl_lib::config::MarkdownFlavor::Standard,
        None,
        None,
    )
    .unwrap();

    // Find MD013 warnings
    let md013_warnings: Vec<_> = warnings
        .iter()
        .filter(|w| w.rule_name.as_ref().is_some_and(|n| *n == "MD013"))
        .collect();

    // Should have exactly one MD013 warning (line 6, not line 4)
    assert_eq!(md013_warnings.len(), 1);
    assert_eq!(md013_warnings[0].line, 6);
}

#[test]
fn test_markdownlint_capture_restore() {
    let content = r#"# Test Document

<!-- markdownlint-disable MD013 MD009 -->
This is a very long line that exceeds 80 characters and would normally trigger MD013 but is disabled

<!-- markdownlint-capture -->
<!-- markdownlint-disable MD025 -->
# This heading would trigger MD025 but it's disabled
<!-- markdownlint-restore -->

This is another very long line that exceeds 80 characters and should not trigger MD013 (still disabled)
# This heading should trigger MD025 (was restored)
"#;

    let rules = all_rules(&Config::default());
    let warnings = lint(
        content,
        &rules,
        false,
        rumdl_lib::config::MarkdownFlavor::Standard,
        None,
        None,
    )
    .unwrap();

    // Find warnings by rule
    let md013_warnings: Vec<_> = warnings
        .iter()
        .filter(|w| w.rule_name.as_ref().is_some_and(|n| *n == "MD013"))
        .collect();
    let md025_warnings: Vec<_> = warnings
        .iter()
        .filter(|w| w.rule_name.as_ref().is_some_and(|n| *n == "MD025"))
        .collect();
    let md009_warnings: Vec<_> = warnings
        .iter()
        .filter(|w| w.rule_name.as_ref().is_some_and(|n| *n == "MD009"))
        .collect();

    // MD013 should be disabled throughout
    assert_eq!(md013_warnings.len(), 0);

    // MD009 should be disabled throughout (trailing spaces)
    assert_eq!(md009_warnings.len(), 0);

    // MD025 should only be disabled between capture and restore
    // Line 12 should have MD025 warning
    assert_eq!(md025_warnings.len(), 1);
    assert_eq!(md025_warnings[0].line, 12);
}

#[test]
fn test_global_disable_enable() {
    let content = r#"# Test Document

<!-- markdownlint-disable -->
This is a very long line that exceeds 80 characters and would normally trigger MD013 but all rules are disabled

# This would trigger MD025 (single title) but all rules are disabled

Trailing spaces here

<!-- markdownlint-enable -->
This is another very long line that exceeds 80 characters and should trigger MD013 because rules are re-enabled
"#;

    let rules = all_rules(&Config::default());
    let warnings = lint(
        content,
        &rules,
        false,
        rumdl_lib::config::MarkdownFlavor::Standard,
        None,
        None,
    )
    .unwrap();

    // All warnings should be from lines after the enable comment
    for warning in &warnings {
        assert!(
            warning.line >= 11,
            "Warning on line {} should have been disabled",
            warning.line
        );
    }

    // Should have at least one MD013 warning on line 11
    let md013_warnings: Vec<_> = warnings
        .iter()
        .filter(|w| w.rule_name.as_ref().is_some_and(|n| *n == "MD013"))
        .collect();
    assert!(!md013_warnings.is_empty());
}

#[test]
fn test_multiple_rules_in_comment() {
    let content = format!(
        "# Test Document\n\
         \n\
         <!-- markdownlint-disable MD013 MD009 -->\n\
         This is a very long line that exceeds 80 characters and would normally trigger MD013 but is disabled\n\
         \n\
         <!-- markdownlint-enable MD013 -->\n\
         This is another very long line that exceeds 80 characters and should trigger MD013 but MD009 is still disabled\n\
         \n\
         <!-- markdownlint-enable MD009 -->\n\
         Trailing spaces should now trigger MD009{trailing}\n",
        trailing = "   "
    );

    let rules = all_rules(&Config::default());
    let warnings = lint(
        &content,
        &rules,
        false,
        rumdl_lib::config::MarkdownFlavor::Standard,
        None,
        None,
    )
    .unwrap();

    // Find warnings by rule
    let md013_warnings: Vec<_> = warnings
        .iter()
        .filter(|w| w.rule_name.as_ref().is_some_and(|n| *n == "MD013"))
        .collect();
    let md009_warnings: Vec<_> = warnings
        .iter()
        .filter(|w| w.rule_name.as_ref().is_some_and(|n| *n == "MD009"))
        .collect();

    // MD013 on line 8 (after re-enable, long line)
    assert_eq!(md013_warnings.len(), 1);

    // MD009 on line 11 (after re-enable, trailing spaces)
    let inline_config = InlineConfig::from_content(&content);
    let md009_disabled_at_11 = inline_config.is_rule_disabled("MD009", 11);
    assert!(
        !md009_disabled_at_11,
        "MD009 should not be disabled at line 11, but it is!"
    );
    assert_eq!(md009_warnings.len(), 1);
}

#[test]
fn test_rumdl_syntax_compatibility() {
    let content = r#"# Test Document

<!-- rumdl-disable MD013 -->
This is a very long line that exceeds 80 characters and would normally trigger MD013 but is disabled using rumdl syntax

<!-- rumdl-enable MD013 -->
This is another very long line that exceeds 80 characters and should trigger MD013 because it was re-enabled

This is a very long line that exceeds 80 characters but is disabled for this line only using rumdl syntax <!-- rumdl-disable-line MD013 -->

<!-- rumdl-disable-next-line MD013 -->
This is a very long line that exceeds 80 characters but is disabled by the previous line using rumdl syntax
"#;

    let rules = all_rules(&Config::default());
    let warnings = lint(
        content,
        &rules,
        false,
        rumdl_lib::config::MarkdownFlavor::Standard,
        None,
        None,
    )
    .unwrap();

    // Find MD013 warnings
    let md013_warnings: Vec<_> = warnings
        .iter()
        .filter(|w| w.rule_name.as_ref().is_some_and(|n| *n == "MD013"))
        .collect();

    // Should have exactly one MD013 warning (line 7)
    assert_eq!(md013_warnings.len(), 1);
    assert_eq!(md013_warnings[0].line, 7);
}

#[test]
fn test_inline_config_parsing() {
    let content = r#"# Test

<!-- markdownlint-disable MD001 -->
<!-- markdownlint-disable-line MD002 -->
<!-- markdownlint-disable-next-line MD003 -->
Text
<!-- markdownlint-capture -->
<!-- markdownlint-disable MD004 -->
<!-- markdownlint-restore -->
<!-- markdownlint-enable MD001 -->
"#;

    let config = InlineConfig::from_content(content);

    // MD001 should be disabled at line 5
    assert!(config.is_rule_disabled("MD001", 5));

    // MD002 should be disabled only at line 4
    assert!(config.is_rule_disabled("MD002", 4));
    assert!(!config.is_rule_disabled("MD002", 5));

    // MD003 should be disabled only at line 6 (next line after comment)
    assert!(config.is_rule_disabled("MD003", 6));
    assert!(!config.is_rule_disabled("MD003", 5));

    // MD004 should not be disabled after restore
    assert!(!config.is_rule_disabled("MD004", 10));

    // MD001 should be enabled after line 11
    assert!(!config.is_rule_disabled("MD001", 11));
}

#[test]
fn test_rumdl_capture_restore() {
    let content = r#"# Test Document

<!-- rumdl-disable MD013 MD009 -->
This is a very long line that exceeds 80 characters and would normally trigger MD013 but is disabled

<!-- rumdl-capture -->
<!-- rumdl-disable MD025 -->
# This heading would trigger MD025 but it's disabled
<!-- rumdl-restore -->
This is another very long line that exceeds 80 characters and should not trigger MD013 (still disabled)
# This heading should trigger MD025 (was restored)
"#;

    let rules = all_rules(&Config::default());
    let warnings = lint(
        content,
        &rules,
        false,
        rumdl_lib::config::MarkdownFlavor::Standard,
        None,
        None,
    )
    .unwrap();

    // Find warnings by rule
    let md013_warnings: Vec<_> = warnings
        .iter()
        .filter(|w| w.rule_name.as_ref().is_some_and(|n| *n == "MD013"))
        .collect();
    let md025_warnings: Vec<_> = warnings
        .iter()
        .filter(|w| w.rule_name.as_ref().is_some_and(|n| *n == "MD025"))
        .collect();
    let md009_warnings: Vec<_> = warnings
        .iter()
        .filter(|w| w.rule_name.as_ref().is_some_and(|n| *n == "MD009"))
        .collect();

    // MD013 should be disabled throughout
    assert_eq!(md013_warnings.len(), 0);

    // MD009 should be disabled throughout (trailing spaces)
    assert_eq!(md009_warnings.len(), 0);

    // MD025 should only be disabled between capture and restore
    // Line 11 should have MD025 warning
    assert_eq!(md025_warnings.len(), 1);
    assert_eq!(md025_warnings[0].line, 11);
}

#[test]
fn test_md009_simple() {
    let content = "Test  ";
    let rules = all_rules(&Config::default());
    let warnings = lint(
        content,
        &rules,
        false,
        rumdl_lib::config::MarkdownFlavor::Standard,
        None,
        None,
    )
    .unwrap();

    let md009_warnings: Vec<_> = warnings
        .iter()
        .filter(|w| w.rule_name.as_ref().is_some_and(|n| *n == "MD009"))
        .collect();

    assert_eq!(
        md009_warnings.len(),
        1,
        "Expected 1 MD009 warning but got {}",
        md009_warnings.len()
    );
}

#[test]
fn test_inline_config_with_alias_disable_enable() {
    // Test that aliases work in inline comments (e.g., line-length instead of MD013)
    let content = r#"# Test Document

<!-- rumdl-disable line-length -->
This is a very long line that exceeds 80 characters and would normally trigger MD013 but is disabled using the alias line-length

<!-- rumdl-enable line-length -->
This is another very long line that exceeds 80 characters and should trigger MD013 because it was re-enabled
"#;

    let rules = all_rules(&Config::default());
    let warnings = lint(
        content,
        &rules,
        false,
        rumdl_lib::config::MarkdownFlavor::Standard,
        None,
        None,
    )
    .unwrap();

    let md013_warnings: Vec<_> = warnings
        .iter()
        .filter(|w| w.rule_name.as_ref().is_some_and(|n| *n == "MD013"))
        .collect();

    // Should have exactly one MD013 warning (line 7)
    assert_eq!(
        md013_warnings.len(),
        1,
        "Expected 1 MD013 warning but got {}: {:?}",
        md013_warnings.len(),
        md013_warnings
    );
    assert_eq!(md013_warnings[0].line, 7);
}

#[test]
fn test_inline_config_with_alias_disable_next_line() {
    // Test that aliases work with disable-next-line
    let content = r#"# Test Document

<!-- rumdl-disable-next-line line-length -->
This is a very long line that exceeds 80 characters and would normally trigger MD013 but is disabled using the alias

This is another very long line that exceeds 80 characters and should trigger MD013 because it's not disabled
"#;

    let rules = all_rules(&Config::default());
    let warnings = lint(
        content,
        &rules,
        false,
        rumdl_lib::config::MarkdownFlavor::Standard,
        None,
        None,
    )
    .unwrap();

    let md013_warnings: Vec<_> = warnings
        .iter()
        .filter(|w| w.rule_name.as_ref().is_some_and(|n| *n == "MD013"))
        .collect();

    // Should have exactly one MD013 warning (line 6, not line 4)
    assert_eq!(md013_warnings.len(), 1);
    assert_eq!(md013_warnings[0].line, 6);
}

#[test]
fn test_inline_config_with_mixed_alias_and_rule_id() {
    // Test that mixing aliases and rule IDs works in the same comment
    // Note: Line 8 has trailing spaces at the end to trigger MD009
    let content = "# Test Document\n\n<!-- rumdl-disable line-length MD009 -->\nThis is a very long line that exceeds 80 characters and would normally trigger MD013\nLine with trailing spaces that would trigger MD009\n\n<!-- rumdl-enable MD013 no-trailing-spaces -->\nThis is another very long line that exceeds 80 characters and should trigger MD013   \n";

    let rules = all_rules(&Config::default());
    let warnings = lint(
        content,
        &rules,
        false,
        rumdl_lib::config::MarkdownFlavor::Standard,
        None,
        None,
    )
    .unwrap();

    let md013_warnings: Vec<_> = warnings
        .iter()
        .filter(|w| w.rule_name.as_ref().is_some_and(|n| *n == "MD013"))
        .collect();

    let md009_warnings: Vec<_> = warnings
        .iter()
        .filter(|w| w.rule_name.as_ref().is_some_and(|n| *n == "MD009"))
        .collect();

    // MD013 should only trigger on line 8 (after re-enable)
    assert_eq!(
        md013_warnings.len(),
        1,
        "Expected 1 MD013 warning but got {}",
        md013_warnings.len()
    );
    assert_eq!(md013_warnings[0].line, 8);

    // MD009 should only trigger on line 8 (after re-enable using alias no-trailing-spaces)
    assert_eq!(
        md009_warnings.len(),
        1,
        "Expected 1 MD009 warning but got {}",
        md009_warnings.len()
    );
    assert_eq!(md009_warnings[0].line, 8);
}

#[test]
fn test_inline_config_alias_case_insensitive() {
    // Test that aliases are case-insensitive
    let content = r#"# Test Document

<!-- rumdl-disable LINE-LENGTH -->
This is a very long line that exceeds 80 characters and would normally trigger MD013 but is disabled using uppercase alias

<!-- rumdl-enable Line-Length -->
This is another very long line that exceeds 80 characters and should trigger MD013 because it was re-enabled
"#;

    let rules = all_rules(&Config::default());
    let warnings = lint(
        content,
        &rules,
        false,
        rumdl_lib::config::MarkdownFlavor::Standard,
        None,
        None,
    )
    .unwrap();

    let md013_warnings: Vec<_> = warnings
        .iter()
        .filter(|w| w.rule_name.as_ref().is_some_and(|n| *n == "MD013"))
        .collect();

    // Should have exactly one MD013 warning (line 7)
    assert_eq!(md013_warnings.len(), 1);
    assert_eq!(md013_warnings[0].line, 7);
}

// =============================================================================
// Fix mode + inline config integration tests (issue #501)
//
// These tests verify that --fix mode respects inline disable comments.
// The bug: fix coordinator had zero awareness of inline config, so rules
// would modify content even inside disable blocks.
// =============================================================================

/// Helper: create context from content and call rule.fix()
fn fix_with_rule(rule: &dyn Rule, content: &str) -> String {
    let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
    rule.fix(&ctx).unwrap()
}

// --- MD013: The exact bug reported in issue #501 (reflow ignores disable) ---

#[test]
fn test_fix_md013_reflow_respects_disable_enable() {
    use rumdl_lib::rules::MD013LineLength;
    use rumdl_lib::rules::md013_line_length::md013_config::MD013Config;
    use rumdl_lib::types::LineLength;

    let rule = MD013LineLength::from_config_struct(MD013Config {
        line_length: LineLength::from_const(80),
        reflow: true,
        ..Default::default()
    });

    let long = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
    let content = format!("# Test\n\n<!-- rumdl-disable MD013 -->\n{long}\n<!-- rumdl-enable MD013 -->\n\n{long}\n");

    let fixed = fix_with_rule(&rule, &content);
    let lines: Vec<&str> = fixed.lines().collect();

    // Line 4 (inside disable block) must be preserved exactly
    assert_eq!(lines[3], long, "Line inside disable block should not be reflowed");

    // Lines after the enable comment should be reflowed to <= 80 chars
    let non_disabled_lines: Vec<&str> = lines[6..].iter().copied().filter(|l| !l.is_empty()).collect();
    assert!(
        !non_disabled_lines.is_empty(),
        "Reflowed content should exist after enable comment"
    );
    for line in &non_disabled_lines {
        assert!(
            line.len() <= 80,
            "Reflowed line should be <= 80 chars, got {} chars: '{line}'",
            line.len()
        );
    }
}

#[test]
fn test_fix_md013_reflow_respects_disable_line() {
    use rumdl_lib::rules::MD013LineLength;
    use rumdl_lib::rules::md013_line_length::md013_config::MD013Config;
    use rumdl_lib::types::LineLength;

    let rule = MD013LineLength::from_config_struct(MD013Config {
        line_length: LineLength::from_const(80),
        reflow: true,
        ..Default::default()
    });

    let long = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore.";
    let content = format!("# Test\n\n{long} <!-- rumdl-disable-line MD013 -->\n\n{long}\n");

    let fixed = fix_with_rule(&rule, &content);
    let lines: Vec<&str> = fixed.lines().collect();

    // Line 3 (disabled via disable-line) should be preserved
    assert!(
        lines[2].len() > 80,
        "Disabled line should be preserved as-is, got: '{}'",
        lines[2]
    );

    // Line 5 (not disabled) should be reflowed
    assert!(
        lines[4].len() <= 80,
        "Non-disabled line should be reflowed to <= 80 chars, got: '{}'",
        lines[4]
    );
}

#[test]
fn test_fix_md013_reflow_respects_disable_next_line() {
    use rumdl_lib::rules::MD013LineLength;
    use rumdl_lib::rules::md013_line_length::md013_config::MD013Config;
    use rumdl_lib::types::LineLength;

    let rule = MD013LineLength::from_config_struct(MD013Config {
        line_length: LineLength::from_const(80),
        reflow: true,
        ..Default::default()
    });

    let long = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore.";
    let content = format!("# Test\n\n<!-- rumdl-disable-next-line MD013 -->\n{long}\n\n{long}\n");

    let fixed = fix_with_rule(&rule, &content);
    let lines: Vec<&str> = fixed.lines().collect();

    // Line 4 (disabled by previous comment) should be preserved
    assert!(
        lines[3].len() > 80,
        "Line after disable-next-line should be preserved as-is"
    );

    // Line 6 (not disabled) should be reflowed
    let non_disabled: Vec<&str> = lines[5..].iter().copied().filter(|l| !l.is_empty()).collect();
    for line in &non_disabled {
        assert!(line.len() <= 80, "Non-disabled line should be reflowed, got: '{line}'");
    }
}

#[test]
fn test_fix_md013_reflow_respects_capture_restore() {
    use rumdl_lib::rules::MD013LineLength;
    use rumdl_lib::rules::md013_line_length::md013_config::MD013Config;
    use rumdl_lib::types::LineLength;

    let rule = MD013LineLength::from_config_struct(MD013Config {
        line_length: LineLength::from_const(80),
        reflow: true,
        ..Default::default()
    });

    let long = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore.";
    let content = format!(
        "# Test\n\n<!-- rumdl-disable MD013 -->\n{long}\n<!-- rumdl-capture -->\n<!-- rumdl-enable MD013 -->\n{long}\n<!-- rumdl-restore -->\n{long}\n"
    );

    let fixed = fix_with_rule(&rule, &content);
    let lines: Vec<&str> = fixed.lines().collect();

    // Line 4 (disabled) should be preserved
    assert_eq!(lines[3], long, "Initially disabled line should be preserved");

    // Line 7 (re-enabled between capture/restore) should be reflowed
    assert!(
        lines[6].len() <= 80,
        "Re-enabled line should be reflowed, got: '{}'",
        lines[6]
    );

    // Line 9 (after restore, back to disabled) should be preserved
    let last_long_idx = fixed.lines().count() - 1;
    let last_line = fixed.lines().last().unwrap();
    assert_eq!(
        last_line,
        long,
        "Line after restore (back to disabled) should be preserved (line {})",
        last_long_idx + 1
    );
}

// --- MD009: Category 2 rule (iterates lines directly in fix) ---

#[test]
fn test_fix_md009_respects_disable_enable() {
    use rumdl_lib::rules::MD009TrailingSpaces;

    let rule = MD009TrailingSpaces::new(2, false);

    let content =
        "# Test\n\n<!-- rumdl-disable MD009 -->\ntrailing   \n<!-- rumdl-enable MD009 -->\n\nalso trailing   \n";

    let fixed = fix_with_rule(&rule, content);
    let lines: Vec<&str> = fixed.lines().collect();

    // Line 4 (disabled) should keep trailing spaces
    assert!(
        lines[3].ends_with("   "),
        "Disabled line should keep trailing spaces, got: '{}'",
        lines[3]
    );

    // Line 7 (enabled) should have trailing spaces removed
    assert_eq!(lines[6], "also trailing", "Enabled line should be trimmed");
}

#[test]
fn test_fix_md009_strict_respects_disable_line() {
    use rumdl_lib::rules::MD009TrailingSpaces;

    let rule = MD009TrailingSpaces::new(2, true); // strict mode

    let content = "trailing   <!-- rumdl-disable-line MD009 -->\ntrailing   \n";

    let fixed = fix_with_rule(&rule, content);
    let lines: Vec<&str> = fixed.lines().collect();

    // Line 1 (disabled) should keep trailing spaces before the comment
    assert!(
        lines[0].contains("trailing   "),
        "Disabled line should keep trailing spaces, got: '{}'",
        lines[0]
    );

    // Line 2 (enabled, strict) should be fully trimmed
    assert_eq!(lines[1], "trailing");
}

// --- MD034: Category 1 rule (calls self.check() then filters in fix) ---

#[test]
fn test_fix_md034_respects_disable_enable() {
    use rumdl_lib::rules::MD034NoBareUrls;

    let rule = MD034NoBareUrls;

    let content = "# Test\n\n<!-- rumdl-disable MD034 -->\nVisit http://example.com for info\n<!-- rumdl-enable MD034 -->\n\nVisit http://other.com for info\n";

    let fixed = fix_with_rule(&rule, content);
    let lines: Vec<&str> = fixed.lines().collect();

    // Line 4 (disabled) should keep bare URL
    assert!(
        lines[3].contains("http://example.com") && !lines[3].contains("<http://example.com>"),
        "Disabled line should keep bare URL without angle brackets"
    );

    // Line 7 (enabled) should have URL wrapped
    assert!(
        lines[6].contains("<http://other.com>"),
        "Enabled line should wrap bare URL, got: '{}'",
        lines[6]
    );
}

// --- MD022: Heading spacing rule with context awareness ---

#[test]
fn test_fix_md022_respects_disable_enable() {
    use rumdl_lib::rules::MD022BlanksAroundHeadings;

    let rule = MD022BlanksAroundHeadings::default();

    // Two headings without required blank lines above.
    // First is disabled, second is not.
    let content = "# Top\n<!-- rumdl-disable MD022 -->\ntext\n## Disabled Heading\ntext\n<!-- rumdl-enable MD022 -->\ntext\n## Enabled Heading\ntext\n";

    let fixed = fix_with_rule(&rule, content);

    // The disabled heading should NOT have a blank line inserted above it
    assert!(
        fixed.contains("text\n## Disabled Heading"),
        "Disabled heading should not get blank line inserted above"
    );

    // The enabled heading should get a blank line inserted above
    assert!(
        fixed.contains("\n\n## Enabled Heading"),
        "Enabled heading should get blank line inserted, fixed content:\n{fixed}"
    );
}

// --- MD046: Complex state machine (fenced/indented conversion) ---

#[test]
fn test_fix_md046_respects_disable_enable() {
    use rumdl_lib::rules::MD046CodeBlockStyle;

    // Target style: indented (fenced blocks should be converted)
    let rule = MD046CodeBlockStyle::new(rumdl_lib::rules::CodeBlockStyle::Indented);

    let content = "# Test\n\n<!-- rumdl-disable MD046 -->\n```\npreserved fenced\n```\n<!-- rumdl-enable MD046 -->\n\n```\nconverted to indented\n```\n";

    let fixed = fix_with_rule(&rule, content);

    // Disabled fenced block should be preserved as-is
    assert!(
        fixed.contains("```\npreserved fenced\n```"),
        "Disabled fenced block should be preserved, got:\n{fixed}"
    );

    // Enabled fenced block should be converted to indented
    assert!(
        fixed.contains("    converted to indented"),
        "Enabled fenced block should be converted to indented, got:\n{fixed}"
    );
}

// --- Cross-cutting: globally disabled region should not be modified ---

#[test]
fn test_fix_global_disable_preserves_all_content() {
    // Test that a globally disabled region is untouched by any rule
    let content =
        "# Test\n\n<!-- rumdl-disable -->\nBare URL: http://example.com\nTrailing spaces   \n<!-- rumdl-enable -->\n";

    let rules_to_test: Vec<Box<dyn Rule>> = vec![
        Box::new(rumdl_lib::rules::MD009TrailingSpaces::new(2, false)),
        Box::new(rumdl_lib::rules::MD034NoBareUrls),
    ];

    for rule in &rules_to_test {
        let fixed = fix_with_rule(rule.as_ref(), content);
        assert_eq!(
            fixed,
            content,
            "Rule {} should not modify globally disabled content",
            rule.name()
        );
    }
}