rumdl 0.1.51

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
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
/// Rule MD028: No blank lines inside blockquotes
///
/// This rule flags blank lines that appear to be inside a blockquote but lack the > marker.
/// It uses heuristics to distinguish between paragraph breaks within a blockquote
/// and intentional separators between distinct blockquotes.
///
/// GFM Alerts (GitHub Flavored Markdown) are automatically detected and excluded:
/// - `> [!NOTE]`, `> [!TIP]`, `> [!IMPORTANT]`, `> [!WARNING]`, `> [!CAUTION]`
///   These alerts MUST be separated by blank lines to render correctly on GitHub.
///
/// Obsidian Callouts are also supported when using the Obsidian flavor:
/// - Any `> [!TYPE]` pattern is recognized as a callout
/// - Foldable syntax is supported: `> [!NOTE]+` (expanded) or `> [!NOTE]-` (collapsed)
///
/// See [docs/md028.md](../../docs/md028.md) for full documentation, configuration, and examples.
use crate::config::MarkdownFlavor;
use crate::lint_context::LineInfo;
use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, RuleCategory, Severity};
use crate::utils::range_utils::calculate_line_range;

/// GFM Alert types supported by GitHub
/// Reference: https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts
const GFM_ALERT_TYPES: &[&str] = &["NOTE", "TIP", "IMPORTANT", "WARNING", "CAUTION"];

#[derive(Clone)]
pub struct MD028NoBlanksBlockquote;

impl MD028NoBlanksBlockquote {
    /// Check if a line is a blockquote line (has > markers)
    #[inline]
    fn is_blockquote_line(line: &str) -> bool {
        // Fast path: check for '>' character before doing any string operations
        if !line.as_bytes().contains(&b'>') {
            return false;
        }
        line.trim_start().starts_with('>')
    }

    /// Get the blockquote level (number of > markers) and leading whitespace
    /// Returns (level, whitespace_end_idx)
    fn get_blockquote_info(line: &str) -> (usize, usize) {
        let bytes = line.as_bytes();
        let mut i = 0;

        // Skip leading whitespace
        while i < bytes.len() && (bytes[i] == b' ' || bytes[i] == b'\t') {
            i += 1;
        }

        let whitespace_end = i;
        let mut level = 0;

        // Count '>' markers
        while i < bytes.len() {
            if bytes[i] == b'>' {
                level += 1;
                i += 1;
            } else if bytes[i] == b' ' || bytes[i] == b'\t' {
                i += 1;
            } else {
                break;
            }
        }

        (level, whitespace_end)
    }

    /// Check if a line is in a skip context (HTML comment, code block, HTML block, or frontmatter)
    #[inline]
    fn is_in_skip_context(line_infos: &[LineInfo], idx: usize) -> bool {
        if let Some(li) = line_infos.get(idx) {
            li.in_html_comment || li.in_code_block || li.in_html_block || li.in_front_matter
        } else {
            false
        }
    }

    /// Check if there's substantive content between two blockquote sections
    /// This helps distinguish between paragraph breaks and separate blockquotes.
    /// Lines in skip contexts (HTML comments, code blocks, frontmatter) count as
    /// separating content because they represent non-blockquote material between quotes.
    fn has_content_between(lines: &[&str], line_infos: &[LineInfo], start: usize, end: usize) -> bool {
        for (offset, line) in lines[start..end].iter().enumerate() {
            let idx = start + offset;
            // Non-blank lines in skip contexts (HTML comments, code blocks, frontmatter)
            // are separating content between blockquotes
            if Self::is_in_skip_context(line_infos, idx) {
                if !line.trim().is_empty() {
                    return true;
                }
                continue;
            }
            let trimmed = line.trim();
            // If there's any non-blank, non-blockquote content, these are separate quotes
            if !trimmed.is_empty() && !trimmed.starts_with('>') {
                return true;
            }
        }
        false
    }

    /// Check if a blockquote line is a GFM alert start
    /// GFM alerts have the format: `> [!TYPE]` where TYPE is NOTE, TIP, IMPORTANT, WARNING, or CAUTION
    /// Reference: https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts
    #[inline]
    fn is_gfm_alert_line(line: &str) -> bool {
        // Fast path: must contain '[!' pattern
        if !line.contains("[!") {
            return false;
        }

        // Extract content after the > marker(s)
        let trimmed = line.trim_start();
        if !trimmed.starts_with('>') {
            return false;
        }

        // Skip all > markers and whitespace to get to content
        let content = trimmed
            .trim_start_matches('>')
            .trim_start_matches([' ', '\t'])
            .trim_start_matches('>')
            .trim_start();

        // Check for GFM alert pattern: [!TYPE]
        if !content.starts_with("[!") {
            return false;
        }

        // Extract the alert type
        if let Some(end_bracket) = content.find(']') {
            let alert_type = &content[2..end_bracket];
            return GFM_ALERT_TYPES.iter().any(|&t| t.eq_ignore_ascii_case(alert_type));
        }

        false
    }

    /// Check if a blockquote line is an Obsidian callout
    /// Obsidian callouts have the format: `> [!TYPE]` where TYPE can be any string
    /// Obsidian also supports foldable callouts: `> [!TYPE]+` (expanded) or `> [!TYPE]-` (collapsed)
    /// Reference: https://help.obsidian.md/callouts
    #[inline]
    fn is_obsidian_callout_line(line: &str) -> bool {
        // Fast path: must contain '[!' pattern
        if !line.contains("[!") {
            return false;
        }

        // Extract content after the > marker(s)
        let trimmed = line.trim_start();
        if !trimmed.starts_with('>') {
            return false;
        }

        // Skip all > markers and whitespace to get to content
        let content = trimmed
            .trim_start_matches('>')
            .trim_start_matches([' ', '\t'])
            .trim_start_matches('>')
            .trim_start();

        // Check for Obsidian callout pattern: [!TYPE] or [!TYPE]+ or [!TYPE]-
        if !content.starts_with("[!") {
            return false;
        }

        // Find the closing bracket - must have at least one char for TYPE
        if let Some(end_bracket) = content.find(']') {
            // TYPE must be at least one character
            if end_bracket > 2 {
                // Verify the type contains only valid characters (alphanumeric, hyphen, underscore)
                let alert_type = &content[2..end_bracket];
                return !alert_type.is_empty()
                    && alert_type.chars().all(|c| c.is_alphanumeric() || c == '-' || c == '_');
            }
        }

        false
    }

    /// Check if a line is a callout/alert based on the flavor
    /// For Obsidian flavor: accepts any [!TYPE] pattern
    /// For other flavors: only accepts GFM alert types
    #[inline]
    fn is_callout_line(line: &str, flavor: MarkdownFlavor) -> bool {
        match flavor {
            MarkdownFlavor::Obsidian => Self::is_obsidian_callout_line(line),
            _ => Self::is_gfm_alert_line(line),
        }
    }

    /// Find the first line of a blockquote block starting from a given line
    /// Scans backwards to find where this blockquote block begins
    fn find_blockquote_start(lines: &[&str], line_infos: &[LineInfo], from_idx: usize) -> Option<usize> {
        if from_idx >= lines.len() {
            return None;
        }

        // Start from the given line and scan backwards
        let mut start_idx = from_idx;

        for i in (0..=from_idx).rev() {
            // Skip lines in skip contexts
            if Self::is_in_skip_context(line_infos, i) {
                continue;
            }

            let line = lines[i];

            // If it's a blockquote line, update start
            if Self::is_blockquote_line(line) {
                start_idx = i;
            } else if line.trim().is_empty() {
                // Blank line - check if previous content was blockquote
                // If we haven't found any blockquote yet, continue
                if start_idx == from_idx && !Self::is_blockquote_line(lines[from_idx]) {
                    continue;
                }
                // Otherwise, blank line ends this blockquote block
                break;
            } else {
                // Non-blockquote, non-blank line - this ends the blockquote block
                break;
            }
        }

        // Return start only if it's actually a blockquote line and not in a skip context
        if Self::is_blockquote_line(lines[start_idx]) && !Self::is_in_skip_context(line_infos, start_idx) {
            Some(start_idx)
        } else {
            None
        }
    }

    /// Check if a blockquote block (starting at given index) is a callout/alert
    /// For Obsidian flavor: accepts any [!TYPE] pattern
    /// For other flavors: only accepts GFM alert types
    fn is_callout_block(
        lines: &[&str],
        line_infos: &[LineInfo],
        blockquote_line_idx: usize,
        flavor: MarkdownFlavor,
    ) -> bool {
        // Find the start of this blockquote block
        if let Some(start_idx) = Self::find_blockquote_start(lines, line_infos, blockquote_line_idx) {
            // Check if the first line of the block is a callout/alert
            return Self::is_callout_line(lines[start_idx], flavor);
        }
        false
    }

    /// Analyze context to determine if quotes are likely the same or different
    fn are_likely_same_blockquote(
        lines: &[&str],
        line_infos: &[LineInfo],
        blank_idx: usize,
        flavor: MarkdownFlavor,
    ) -> bool {
        // Look for patterns that suggest these are the same blockquote:
        // 1. Only one blank line between them (multiple blanks suggest separation)
        // 2. Same indentation level
        // 3. No content between them
        // 4. Similar blockquote levels

        // Note: We flag ALL blank lines between blockquotes, matching markdownlint behavior.
        // Even multiple consecutive blank lines are flagged as they can be ambiguous
        // (some parsers treat them as one blockquote, others as separate blockquotes).

        // Find previous and next blockquote lines using fast byte scanning
        let mut prev_quote_idx = None;
        let mut next_quote_idx = None;

        // Scan backwards for previous blockquote, skipping lines in skip contexts
        for i in (0..blank_idx).rev() {
            if Self::is_in_skip_context(line_infos, i) {
                continue;
            }
            let line = lines[i];
            // Fast check: if no '>' character, skip
            if line.as_bytes().contains(&b'>') && Self::is_blockquote_line(line) {
                prev_quote_idx = Some(i);
                break;
            }
        }

        // Scan forwards for next blockquote, skipping lines in skip contexts
        for (i, line) in lines.iter().enumerate().skip(blank_idx + 1) {
            if Self::is_in_skip_context(line_infos, i) {
                continue;
            }
            // Fast check: if no '>' character, skip
            if line.as_bytes().contains(&b'>') && Self::is_blockquote_line(line) {
                next_quote_idx = Some(i);
                break;
            }
        }

        let (prev_idx, next_idx) = match (prev_quote_idx, next_quote_idx) {
            (Some(p), Some(n)) => (p, n),
            _ => return false,
        };

        // Callout/Alert check: If either blockquote is a callout/alert, treat them as
        // intentionally separate blockquotes. Callouts MUST be separated by blank lines
        // to render correctly.
        // For Obsidian flavor: any [!TYPE] is a callout
        // For other flavors: only GFM alert types (NOTE, TIP, IMPORTANT, WARNING, CAUTION)
        let prev_is_callout = Self::is_callout_block(lines, line_infos, prev_idx, flavor);
        let next_is_callout = Self::is_callout_block(lines, line_infos, next_idx, flavor);
        if prev_is_callout || next_is_callout {
            return false;
        }

        // Check for content between blockquotes
        if Self::has_content_between(lines, line_infos, prev_idx + 1, next_idx) {
            return false;
        }

        // Get blockquote info once per line to avoid repeated parsing
        let (prev_level, prev_whitespace_end) = Self::get_blockquote_info(lines[prev_idx]);
        let (next_level, next_whitespace_end) = Self::get_blockquote_info(lines[next_idx]);

        // Different levels suggest different contexts
        // But next_level > prev_level could be nested continuation
        if next_level < prev_level {
            return false;
        }

        // Check indentation consistency using byte indices
        let prev_line = lines[prev_idx];
        let next_line = lines[next_idx];
        let prev_indent = &prev_line[..prev_whitespace_end];
        let next_indent = &next_line[..next_whitespace_end];

        // Different indentation indicates separate blockquote contexts
        // Same indentation with no content between = same blockquote (blank line inside)
        prev_indent == next_indent
    }

    /// Check if a blank line is problematic (inside a blockquote)
    fn is_problematic_blank_line(
        lines: &[&str],
        line_infos: &[LineInfo],
        index: usize,
        flavor: MarkdownFlavor,
    ) -> Option<(usize, String)> {
        let current_line = lines[index];

        // Must be a blank line (no content, no > markers)
        if !current_line.trim().is_empty() || Self::is_blockquote_line(current_line) {
            return None;
        }

        // Use heuristics to determine if this blank line is inside a blockquote
        // or if it's an intentional separator between blockquotes
        if !Self::are_likely_same_blockquote(lines, line_infos, index, flavor) {
            return None;
        }

        // This blank line appears to be inside a blockquote
        // Find the appropriate fix using optimized parsing, skipping lines in skip contexts
        for i in (0..index).rev() {
            if Self::is_in_skip_context(line_infos, i) {
                continue;
            }
            let line = lines[i];
            // Fast check: if no '>' character, skip
            if line.as_bytes().contains(&b'>') && Self::is_blockquote_line(line) {
                let (level, whitespace_end) = Self::get_blockquote_info(line);
                let indent = &line[..whitespace_end];
                let mut fix = String::with_capacity(indent.len() + level);
                fix.push_str(indent);
                for _ in 0..level {
                    fix.push('>');
                }
                return Some((level, fix));
            }
        }

        None
    }
}

impl Default for MD028NoBlanksBlockquote {
    fn default() -> Self {
        Self
    }
}

impl Rule for MD028NoBlanksBlockquote {
    fn name(&self) -> &'static str {
        "MD028"
    }

    fn description(&self) -> &'static str {
        "Blank line inside blockquote"
    }

    fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
        // Early return for content without blockquotes
        if !ctx.content.contains('>') {
            return Ok(Vec::new());
        }

        let mut warnings = Vec::new();

        // Get all lines
        let lines = ctx.raw_lines();

        // Pre-scan to find blank lines and blockquote lines for faster processing
        let mut blank_line_indices = Vec::new();
        let mut has_blockquotes = false;

        for (line_idx, line) in lines.iter().enumerate() {
            // Skip lines in non-markdown content contexts
            if line_idx < ctx.lines.len() {
                let li = &ctx.lines[line_idx];
                if li.in_code_block || li.in_html_comment || li.in_html_block || li.in_front_matter {
                    continue;
                }
            }

            if line.trim().is_empty() {
                blank_line_indices.push(line_idx);
            } else if Self::is_blockquote_line(line) {
                has_blockquotes = true;
            }
        }

        // If no blockquotes found, no need to check blank lines
        if !has_blockquotes {
            return Ok(Vec::new());
        }

        // Only check blank lines that could be problematic
        for &line_idx in &blank_line_indices {
            let line_num = line_idx + 1;

            // Check if this is a problematic blank line inside a blockquote
            if let Some((level, fix_content)) = Self::is_problematic_blank_line(lines, &ctx.lines, line_idx, ctx.flavor)
            {
                let line = lines[line_idx];
                let (start_line, start_col, end_line, end_col) = calculate_line_range(line_num, line);

                warnings.push(LintWarning {
                    rule_name: Some(self.name().to_string()),
                    message: format!("Blank line inside blockquote (level {level})"),
                    line: start_line,
                    column: start_col,
                    end_line,
                    end_column: end_col,
                    severity: Severity::Warning,
                    fix: Some(Fix {
                        range: ctx
                            .line_index
                            .line_col_to_byte_range_with_length(line_num, 1, line.len()),
                        replacement: fix_content,
                    }),
                });
            }
        }

        Ok(warnings)
    }

    fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
        let mut result = Vec::with_capacity(ctx.lines.len());
        let lines = ctx.raw_lines();

        for (line_idx, line) in lines.iter().enumerate() {
            // Skip lines where this rule is disabled by inline config
            if ctx.inline_config().is_rule_disabled(self.name(), line_idx + 1) {
                result.push(line.to_string());
                continue;
            }

            // Skip lines in non-markdown content contexts
            if line_idx < ctx.lines.len() {
                let li = &ctx.lines[line_idx];
                if li.in_code_block || li.in_html_comment || li.in_html_block || li.in_front_matter {
                    result.push(line.to_string());
                    continue;
                }
            }
            // Check if this blank line needs fixing
            if let Some((_, fix_content)) = Self::is_problematic_blank_line(lines, &ctx.lines, line_idx, ctx.flavor) {
                result.push(fix_content);
            } else {
                result.push(line.to_string());
            }
        }

        Ok(result.join("\n") + if ctx.content.ends_with('\n') { "\n" } else { "" })
    }

    /// Get the category of this rule for selective processing
    fn category(&self) -> RuleCategory {
        RuleCategory::Blockquote
    }

    /// Check if this rule should be skipped
    fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
        !ctx.likely_has_blockquotes()
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn from_config(_config: &crate::config::Config) -> Box<dyn Rule>
    where
        Self: Sized,
    {
        Box::new(MD028NoBlanksBlockquote)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::lint_context::LintContext;

    #[test]
    fn test_no_blockquotes() {
        let rule = MD028NoBlanksBlockquote;
        let content = "This is regular text\n\nWith blank lines\n\nBut no blockquotes";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty(), "Should not flag content without blockquotes");
    }

    #[test]
    fn test_valid_blockquote_no_blanks() {
        let rule = MD028NoBlanksBlockquote;
        let content = "> This is a blockquote\n> With multiple lines\n> But no blank lines";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty(), "Should not flag blockquotes without blank lines");
    }

    #[test]
    fn test_blockquote_with_empty_line_marker() {
        let rule = MD028NoBlanksBlockquote;
        // Lines with just > are valid and should NOT be flagged
        let content = "> First line\n>\n> Third line";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty(), "Should not flag lines with just > marker");
    }

    #[test]
    fn test_blockquote_with_empty_line_marker_and_space() {
        let rule = MD028NoBlanksBlockquote;
        // Lines with > and space are also valid
        let content = "> First line\n> \n> Third line";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty(), "Should not flag lines with > and space");
    }

    #[test]
    fn test_blank_line_in_blockquote() {
        let rule = MD028NoBlanksBlockquote;
        // Truly blank line (no >) inside blockquote should be flagged
        let content = "> First line\n\n> Third line";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 1, "Should flag truly blank line inside blockquote");
        assert_eq!(result[0].line, 2);
        assert!(result[0].message.contains("Blank line inside blockquote"));
    }

    #[test]
    fn test_multiple_blank_lines() {
        let rule = MD028NoBlanksBlockquote;
        let content = "> First\n\n\n> Fourth";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        // With proper indentation checking, both blank lines are flagged as they're within the same blockquote
        assert_eq!(result.len(), 2, "Should flag each blank line within the blockquote");
        assert_eq!(result[0].line, 2);
        assert_eq!(result[1].line, 3);
    }

    #[test]
    fn test_nested_blockquote_blank() {
        let rule = MD028NoBlanksBlockquote;
        let content = ">> Nested quote\n\n>> More nested";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].line, 2);
    }

    #[test]
    fn test_nested_blockquote_with_marker() {
        let rule = MD028NoBlanksBlockquote;
        // Lines with >> are valid
        let content = ">> Nested quote\n>>\n>> More nested";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty(), "Should not flag lines with >> marker");
    }

    #[test]
    fn test_fix_single_blank() {
        let rule = MD028NoBlanksBlockquote;
        let content = "> First\n\n> Third";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, "> First\n>\n> Third");
    }

    #[test]
    fn test_fix_nested_blank() {
        let rule = MD028NoBlanksBlockquote;
        let content = ">> Nested\n\n>> More";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, ">> Nested\n>>\n>> More");
    }

    #[test]
    fn test_fix_with_indentation() {
        let rule = MD028NoBlanksBlockquote;
        let content = "  > Indented quote\n\n  > More";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, "  > Indented quote\n  >\n  > More");
    }

    #[test]
    fn test_mixed_levels() {
        let rule = MD028NoBlanksBlockquote;
        // Blank lines between different levels
        let content = "> Level 1\n\n>> Level 2\n\n> Level 1 again";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        // Line 2 is a blank between > and >>, level 1 to level 2, considered inside level 1
        // Line 4 is a blank between >> and >, level 2 to level 1, NOT inside blockquote
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].line, 2);
    }

    #[test]
    fn test_blockquote_with_code_block() {
        let rule = MD028NoBlanksBlockquote;
        let content = "> Quote with code:\n> ```\n> code\n> ```\n>\n> More quote";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        // Line 5 has > marker, so it's not a blank line
        assert!(result.is_empty(), "Should not flag line with > marker");
    }

    #[test]
    fn test_category() {
        let rule = MD028NoBlanksBlockquote;
        assert_eq!(rule.category(), RuleCategory::Blockquote);
    }

    #[test]
    fn test_should_skip() {
        let rule = MD028NoBlanksBlockquote;
        let ctx1 = LintContext::new("No blockquotes here", crate::config::MarkdownFlavor::Standard, None);
        assert!(rule.should_skip(&ctx1));

        let ctx2 = LintContext::new("> Has blockquote", crate::config::MarkdownFlavor::Standard, None);
        assert!(!rule.should_skip(&ctx2));
    }

    #[test]
    fn test_empty_content() {
        let rule = MD028NoBlanksBlockquote;
        let content = "";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn test_blank_after_blockquote() {
        let rule = MD028NoBlanksBlockquote;
        let content = "> Quote\n\nNot a quote";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty(), "Blank line after blockquote ends is valid");
    }

    #[test]
    fn test_blank_before_blockquote() {
        let rule = MD028NoBlanksBlockquote;
        let content = "Not a quote\n\n> Quote";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty(), "Blank line before blockquote starts is valid");
    }

    #[test]
    fn test_preserve_trailing_newline() {
        let rule = MD028NoBlanksBlockquote;
        let content = "> Quote\n\n> More\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert!(fixed.ends_with('\n'));

        let content_no_newline = "> Quote\n\n> More";
        let ctx2 = LintContext::new(content_no_newline, crate::config::MarkdownFlavor::Standard, None);
        let fixed2 = rule.fix(&ctx2).unwrap();
        assert!(!fixed2.ends_with('\n'));
    }

    #[test]
    fn test_document_structure_extension() {
        let rule = MD028NoBlanksBlockquote;
        let ctx = LintContext::new("> test", crate::config::MarkdownFlavor::Standard, None);
        // Test that the rule works correctly with blockquotes
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty(), "Should not flag valid blockquote");

        // Test that rule skips content without blockquotes
        let ctx2 = LintContext::new("no blockquote", crate::config::MarkdownFlavor::Standard, None);
        assert!(rule.should_skip(&ctx2), "Should skip content without blockquotes");
    }

    #[test]
    fn test_deeply_nested_blank() {
        let rule = MD028NoBlanksBlockquote;
        let content = ">>> Deep nest\n\n>>> More deep";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 1);

        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, ">>> Deep nest\n>>>\n>>> More deep");
    }

    #[test]
    fn test_deeply_nested_with_marker() {
        let rule = MD028NoBlanksBlockquote;
        // Lines with >>> are valid
        let content = ">>> Deep nest\n>>>\n>>> More deep";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty(), "Should not flag lines with >>> marker");
    }

    #[test]
    fn test_complex_blockquote_structure() {
        let rule = MD028NoBlanksBlockquote;
        // Line with > is valid, not a blank line
        let content = "> Level 1\n> > Nested properly\n>\n> Back to level 1";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty(), "Should not flag line with > marker");
    }

    #[test]
    fn test_complex_with_blank() {
        let rule = MD028NoBlanksBlockquote;
        // Blank line between different nesting levels is not flagged
        // (going from >> back to > is a context change)
        let content = "> Level 1\n> > Nested\n\n> Back to level 1";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(
            result.len(),
            0,
            "Blank between different nesting levels is not inside blockquote"
        );
    }

    // ==================== GFM Alert Tests ====================
    // GitHub Flavored Markdown alerts use the syntax > [!TYPE] where TYPE is
    // NOTE, TIP, IMPORTANT, WARNING, or CAUTION. These alerts MUST be separated
    // by blank lines to render correctly on GitHub.
    // Reference: https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts

    #[test]
    fn test_gfm_alert_detection_note() {
        assert!(MD028NoBlanksBlockquote::is_gfm_alert_line("> [!NOTE]"));
        assert!(MD028NoBlanksBlockquote::is_gfm_alert_line("> [!NOTE] Additional text"));
        assert!(MD028NoBlanksBlockquote::is_gfm_alert_line(">  [!NOTE]"));
        assert!(MD028NoBlanksBlockquote::is_gfm_alert_line("> [!note]")); // case insensitive
        assert!(MD028NoBlanksBlockquote::is_gfm_alert_line("> [!Note]")); // mixed case
    }

    #[test]
    fn test_gfm_alert_detection_all_types() {
        // All five GFM alert types
        assert!(MD028NoBlanksBlockquote::is_gfm_alert_line("> [!NOTE]"));
        assert!(MD028NoBlanksBlockquote::is_gfm_alert_line("> [!TIP]"));
        assert!(MD028NoBlanksBlockquote::is_gfm_alert_line("> [!IMPORTANT]"));
        assert!(MD028NoBlanksBlockquote::is_gfm_alert_line("> [!WARNING]"));
        assert!(MD028NoBlanksBlockquote::is_gfm_alert_line("> [!CAUTION]"));
    }

    #[test]
    fn test_gfm_alert_detection_not_alert() {
        // These should NOT be detected as GFM alerts
        assert!(!MD028NoBlanksBlockquote::is_gfm_alert_line("> Regular blockquote"));
        assert!(!MD028NoBlanksBlockquote::is_gfm_alert_line("> [!INVALID]"));
        assert!(!MD028NoBlanksBlockquote::is_gfm_alert_line("> [NOTE]")); // missing !
        assert!(!MD028NoBlanksBlockquote::is_gfm_alert_line("> [!]")); // empty type
        assert!(!MD028NoBlanksBlockquote::is_gfm_alert_line("Regular text [!NOTE]")); // not blockquote
        assert!(!MD028NoBlanksBlockquote::is_gfm_alert_line("")); // empty
        assert!(!MD028NoBlanksBlockquote::is_gfm_alert_line("> ")); // empty blockquote
    }

    #[test]
    fn test_gfm_alerts_separated_by_blank_line() {
        // Issue #126 use case: Two GFM alerts separated by blank line should NOT be flagged
        let rule = MD028NoBlanksBlockquote;
        let content = "> [!TIP]\n> Here's a github tip\n\n> [!NOTE]\n> Here's a github note";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty(), "Should not flag blank line between GFM alerts");
    }

    #[test]
    fn test_gfm_alerts_all_five_types_separated() {
        // All five alert types in sequence, each separated by blank lines
        let rule = MD028NoBlanksBlockquote;
        let content = r#"> [!NOTE]
> Note content

> [!TIP]
> Tip content

> [!IMPORTANT]
> Important content

> [!WARNING]
> Warning content

> [!CAUTION]
> Caution content"#;
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Should not flag blank lines between any GFM alert types"
        );
    }

    #[test]
    fn test_gfm_alert_with_multiple_lines() {
        // GFM alert with multiple content lines, then another alert
        let rule = MD028NoBlanksBlockquote;
        let content = r#"> [!WARNING]
> This is a warning
> with multiple lines
> of content

> [!NOTE]
> This is a note"#;
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Should not flag blank line between multi-line GFM alerts"
        );
    }

    #[test]
    fn test_gfm_alert_followed_by_regular_blockquote() {
        // GFM alert followed by regular blockquote - should NOT flag
        let rule = MD028NoBlanksBlockquote;
        let content = "> [!TIP]\n> A helpful tip\n\n> Regular blockquote";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty(), "Should not flag blank line after GFM alert");
    }

    #[test]
    fn test_regular_blockquote_followed_by_gfm_alert() {
        // Regular blockquote followed by GFM alert - should NOT flag
        let rule = MD028NoBlanksBlockquote;
        let content = "> Regular blockquote\n\n> [!NOTE]\n> Important note";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty(), "Should not flag blank line before GFM alert");
    }

    #[test]
    fn test_regular_blockquotes_still_flagged() {
        // Regular blockquotes (not GFM alerts) should still be flagged
        let rule = MD028NoBlanksBlockquote;
        let content = "> First blockquote\n\n> Second blockquote";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(
            result.len(),
            1,
            "Should still flag blank line between regular blockquotes"
        );
    }

    #[test]
    fn test_gfm_alert_blank_line_within_same_alert() {
        // Blank line WITHIN a single GFM alert should still be flagged
        // (this is a missing > marker inside the alert)
        let rule = MD028NoBlanksBlockquote;
        let content = "> [!NOTE]\n> First paragraph\n\n> Second paragraph of same note";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        // The second > line is NOT a new alert, so this is a blank within the same blockquote
        // However, since the first blockquote is a GFM alert, and the second is just continuation,
        // this could be ambiguous. Current implementation: if first is alert, don't flag.
        // This is acceptable - user can use > marker on blank line if they want continuation.
        assert!(
            result.is_empty(),
            "GFM alert status propagates to subsequent blockquote lines"
        );
    }

    #[test]
    fn test_gfm_alert_case_insensitive() {
        let rule = MD028NoBlanksBlockquote;
        let content = "> [!note]\n> lowercase\n\n> [!TIP]\n> uppercase\n\n> [!Warning]\n> mixed";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty(), "GFM alert detection should be case insensitive");
    }

    #[test]
    fn test_gfm_alert_with_nested_blockquote() {
        // GFM alert doesn't support nesting, but test behavior
        let rule = MD028NoBlanksBlockquote;
        let content = "> [!NOTE]\n> > Nested quote inside alert\n\n> [!TIP]\n> Tip";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Should not flag blank between alerts even with nested content"
        );
    }

    #[test]
    fn test_gfm_alert_indented() {
        let rule = MD028NoBlanksBlockquote;
        // Indented GFM alerts (e.g., in a list context)
        let content = "  > [!NOTE]\n  > Indented note\n\n  > [!TIP]\n  > Indented tip";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty(), "Should not flag blank between indented GFM alerts");
    }

    #[test]
    fn test_gfm_alert_mixed_with_regular_content() {
        // Mixed document with GFM alerts and regular content
        let rule = MD028NoBlanksBlockquote;
        let content = r#"# Heading

Some paragraph.

> [!NOTE]
> Important note

More paragraph text.

> [!WARNING]
> Be careful!

Final text."#;
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "GFM alerts in mixed document should not trigger warnings"
        );
    }

    #[test]
    fn test_gfm_alert_fix_not_applied() {
        // When we have GFM alerts, fix should not modify the blank lines
        let rule = MD028NoBlanksBlockquote;
        let content = "> [!TIP]\n> Tip\n\n> [!NOTE]\n> Note";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, content, "Fix should not modify blank lines between GFM alerts");
    }

    #[test]
    fn test_gfm_alert_multiple_blank_lines_between() {
        // Multiple blank lines between GFM alerts should not be flagged
        let rule = MD028NoBlanksBlockquote;
        let content = "> [!NOTE]\n> Note\n\n\n> [!TIP]\n> Tip";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Should not flag multiple blank lines between GFM alerts"
        );
    }

    // ==================== Obsidian Callout Tests ====================
    // Obsidian callouts use the same > [!TYPE] syntax as GFM alerts, but support
    // any custom type (not just NOTE, TIP, IMPORTANT, WARNING, CAUTION).
    // They also support foldable callouts with + or - suffix.
    // Reference: https://help.obsidian.md/callouts

    #[test]
    fn test_obsidian_callout_detection() {
        // Obsidian callouts should be detected
        assert!(MD028NoBlanksBlockquote::is_obsidian_callout_line("> [!NOTE]"));
        assert!(MD028NoBlanksBlockquote::is_obsidian_callout_line("> [!info]"));
        assert!(MD028NoBlanksBlockquote::is_obsidian_callout_line("> [!todo]"));
        assert!(MD028NoBlanksBlockquote::is_obsidian_callout_line("> [!success]"));
        assert!(MD028NoBlanksBlockquote::is_obsidian_callout_line("> [!question]"));
        assert!(MD028NoBlanksBlockquote::is_obsidian_callout_line("> [!failure]"));
        assert!(MD028NoBlanksBlockquote::is_obsidian_callout_line("> [!danger]"));
        assert!(MD028NoBlanksBlockquote::is_obsidian_callout_line("> [!bug]"));
        assert!(MD028NoBlanksBlockquote::is_obsidian_callout_line("> [!example]"));
        assert!(MD028NoBlanksBlockquote::is_obsidian_callout_line("> [!quote]"));
        assert!(MD028NoBlanksBlockquote::is_obsidian_callout_line("> [!cite]"));
    }

    #[test]
    fn test_obsidian_callout_custom_types() {
        // Obsidian supports custom callout types
        assert!(MD028NoBlanksBlockquote::is_obsidian_callout_line("> [!custom]"));
        assert!(MD028NoBlanksBlockquote::is_obsidian_callout_line("> [!my-callout]"));
        assert!(MD028NoBlanksBlockquote::is_obsidian_callout_line("> [!my_callout]"));
        assert!(MD028NoBlanksBlockquote::is_obsidian_callout_line("> [!MyCallout]"));
        assert!(MD028NoBlanksBlockquote::is_obsidian_callout_line("> [!callout123]"));
    }

    #[test]
    fn test_obsidian_callout_foldable() {
        // Obsidian supports foldable callouts with + or -
        assert!(MD028NoBlanksBlockquote::is_obsidian_callout_line("> [!NOTE]+ Expanded"));
        assert!(MD028NoBlanksBlockquote::is_obsidian_callout_line(
            "> [!NOTE]- Collapsed"
        ));
        assert!(MD028NoBlanksBlockquote::is_obsidian_callout_line("> [!WARNING]+"));
        assert!(MD028NoBlanksBlockquote::is_obsidian_callout_line("> [!TIP]-"));
    }

    #[test]
    fn test_obsidian_callout_with_title() {
        // Obsidian callouts can have custom titles
        assert!(MD028NoBlanksBlockquote::is_obsidian_callout_line(
            "> [!NOTE] Custom Title"
        ));
        assert!(MD028NoBlanksBlockquote::is_obsidian_callout_line(
            "> [!WARNING]+ Be Careful!"
        ));
    }

    #[test]
    fn test_obsidian_callout_invalid() {
        // Invalid callout patterns
        assert!(!MD028NoBlanksBlockquote::is_obsidian_callout_line(
            "> Regular blockquote"
        ));
        assert!(!MD028NoBlanksBlockquote::is_obsidian_callout_line("> [NOTE]")); // missing !
        assert!(!MD028NoBlanksBlockquote::is_obsidian_callout_line("> [!]")); // empty type
        assert!(!MD028NoBlanksBlockquote::is_obsidian_callout_line(
            "Regular text [!NOTE]"
        )); // not blockquote
        assert!(!MD028NoBlanksBlockquote::is_obsidian_callout_line("")); // empty
    }

    #[test]
    fn test_obsidian_callouts_separated_by_blank_line() {
        // Obsidian callouts separated by blank line should NOT be flagged
        let rule = MD028NoBlanksBlockquote;
        let content = "> [!info]\n> Some info\n\n> [!todo]\n> A todo item";
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Should not flag blank line between Obsidian callouts"
        );
    }

    #[test]
    fn test_obsidian_custom_callouts_separated() {
        // Custom Obsidian callouts should also be recognized
        let rule = MD028NoBlanksBlockquote;
        let content = "> [!my-custom]\n> Custom content\n\n> [!another_custom]\n> More content";
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Should not flag blank line between custom Obsidian callouts"
        );
    }

    #[test]
    fn test_obsidian_foldable_callouts_separated() {
        // Foldable Obsidian callouts should also be recognized
        let rule = MD028NoBlanksBlockquote;
        let content = "> [!NOTE]+ Expanded\n> Content\n\n> [!WARNING]- Collapsed\n> Warning content";
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Should not flag blank line between foldable Obsidian callouts"
        );
    }

    #[test]
    fn test_obsidian_custom_not_recognized_in_standard_flavor() {
        // Custom callout types should NOT be recognized in Standard flavor
        // (only GFM alert types are recognized)
        let rule = MD028NoBlanksBlockquote;
        let content = "> [!info]\n> Info content\n\n> [!todo]\n> Todo content";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        // In Standard flavor, [!info] and [!todo] are NOT GFM alerts, so this is flagged
        assert_eq!(
            result.len(),
            1,
            "Custom callout types should be flagged in Standard flavor"
        );
    }

    #[test]
    fn test_obsidian_gfm_alerts_work_in_both_flavors() {
        // GFM alert types should work in both Standard and Obsidian flavors
        let rule = MD028NoBlanksBlockquote;
        let content = "> [!NOTE]\n> Note\n\n> [!WARNING]\n> Warning";

        // Standard flavor
        let ctx_standard = LintContext::new(content, MarkdownFlavor::Standard, None);
        let result_standard = rule.check(&ctx_standard).unwrap();
        assert!(result_standard.is_empty(), "GFM alerts should work in Standard flavor");

        // Obsidian flavor
        let ctx_obsidian = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let result_obsidian = rule.check(&ctx_obsidian).unwrap();
        assert!(
            result_obsidian.is_empty(),
            "GFM alerts should also work in Obsidian flavor"
        );
    }

    #[test]
    fn test_obsidian_callout_all_builtin_types() {
        // Test all built-in Obsidian callout types
        let rule = MD028NoBlanksBlockquote;
        let content = r#"> [!note]
> Note

> [!abstract]
> Abstract

> [!summary]
> Summary

> [!info]
> Info

> [!todo]
> Todo

> [!tip]
> Tip

> [!success]
> Success

> [!question]
> Question

> [!warning]
> Warning

> [!failure]
> Failure

> [!danger]
> Danger

> [!bug]
> Bug

> [!example]
> Example

> [!quote]
> Quote"#;
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty(), "All Obsidian callout types should be recognized");
    }

    #[test]
    fn test_obsidian_fix_not_applied_to_callouts() {
        // Fix should not modify blank lines between Obsidian callouts
        let rule = MD028NoBlanksBlockquote;
        let content = "> [!info]\n> Info\n\n> [!todo]\n> Todo";
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(
            fixed, content,
            "Fix should not modify blank lines between Obsidian callouts"
        );
    }

    #[test]
    fn test_obsidian_regular_blockquotes_still_flagged() {
        // Regular blockquotes (not callouts) should still be flagged in Obsidian flavor
        let rule = MD028NoBlanksBlockquote;
        let content = "> First blockquote\n\n> Second blockquote";
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(
            result.len(),
            1,
            "Regular blockquotes should still be flagged in Obsidian flavor"
        );
    }

    #[test]
    fn test_obsidian_callout_mixed_with_regular_blockquote() {
        // Callout followed by regular blockquote - should NOT flag (callout takes precedence)
        let rule = MD028NoBlanksBlockquote;
        let content = "> [!note]\n> Note content\n\n> Regular blockquote";
        let ctx = LintContext::new(content, MarkdownFlavor::Obsidian, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Should not flag blank after callout even if followed by regular blockquote"
        );
    }

    // ==================== HTML Comment Skip Tests ====================
    // Blockquote-like content inside HTML comments should not be linted.

    #[test]
    fn test_html_comment_blockquotes_not_flagged() {
        let rule = MD028NoBlanksBlockquote;
        let content = "## Responses\n\n<!--\n> First response text here.\n> <br>— Person One\n\n> Second response text here.\n> <br>— Person Two\n-->\n\nThe above responses are currently disabled.\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Should not flag blank lines inside HTML comments, got: {result:?}"
        );
    }

    #[test]
    fn test_fix_preserves_html_comment_content() {
        let rule = MD028NoBlanksBlockquote;
        let content = "<!--\n> First quote\n\n> Second quote\n-->\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, content, "Fix should not modify content inside HTML comments");
    }

    #[test]
    fn test_multiline_html_comment_with_blockquotes() {
        let rule = MD028NoBlanksBlockquote;
        let content = "# Title\n\n<!--\n> Quote A\n> Line 2\n\n> Quote B\n> Line 2\n\n> Quote C\n-->\n\nSome text\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Should not flag any blank lines inside HTML comments, got: {result:?}"
        );
    }

    #[test]
    fn test_blockquotes_outside_html_comment_still_flagged() {
        let rule = MD028NoBlanksBlockquote;
        let content = "> First quote\n\n> Second quote\n\n<!--\n> Commented quote A\n\n> Commented quote B\n-->\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        // The blank line between the first two blockquotes (outside comment) should be flagged
        // but none inside the HTML comment (lines 7 is the blank between commented quotes)
        for w in &result {
            assert!(
                w.line < 5,
                "Warning at line {} should not be inside HTML comment",
                w.line
            );
        }
        assert!(
            !result.is_empty(),
            "Should still flag blank line between blockquotes outside HTML comment"
        );
    }

    #[test]
    fn test_frontmatter_blockquote_like_content_not_flagged() {
        let rule = MD028NoBlanksBlockquote;
        let content = "---\n> not a real blockquote\n\n> also not real\n---\n\n# Title\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Should not flag content inside frontmatter, got: {result:?}"
        );
    }

    #[test]
    fn test_comment_boundary_does_not_leak_into_adjacent_blockquotes() {
        // A real blockquote before a comment should not be matched with
        // a blockquote inside the comment across the <!-- boundary
        let rule = MD028NoBlanksBlockquote;
        let content = "> real quote\n\n<!--\n> commented quote\n-->\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Should not match blockquotes across HTML comment boundaries, got: {result:?}"
        );
    }

    #[test]
    fn test_blockquote_after_comment_boundary_not_matched() {
        // A blockquote inside a comment should not be matched with
        // a blockquote after the comment across the --> boundary
        let rule = MD028NoBlanksBlockquote;
        let content = "<!--\n> commented quote\n-->\n\n> real quote\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Should not match blockquotes across HTML comment boundaries, got: {result:?}"
        );
    }

    #[test]
    fn test_fix_preserves_comment_boundary_content() {
        // Verify fix doesn't modify content when blockquotes straddle a comment boundary
        let rule = MD028NoBlanksBlockquote;
        let content = "> real quote\n\n<!--\n> commented quote A\n\n> commented quote B\n-->\n\n> another real quote\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(
            fixed, content,
            "Fix should not modify content when blockquotes are separated by comment boundaries"
        );
    }

    #[test]
    fn test_inline_html_comment_does_not_suppress_warning() {
        // Inline HTML comments on a blockquote line should NOT suppress warnings -
        // only multi-line HTML comment blocks should
        let rule = MD028NoBlanksBlockquote;
        let content = "> quote with <!-- inline comment -->\n\n> continuation\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        // This should still be flagged since the blockquotes are not inside an HTML comment block
        assert!(
            !result.is_empty(),
            "Should still flag blank lines between blockquotes with inline HTML comments"
        );
    }

    // ==================== Skip Context Scanning Tests ====================
    // Verify that backward/forward scanning in are_likely_same_blockquote()
    // and is_problematic_blank_line() properly skips lines in HTML comments,
    // code blocks, and frontmatter.

    #[test]
    fn test_comment_with_blockquote_markers_on_delimiters() {
        // The backward scan should not find blockquote lines on HTML comment
        // delimiter lines, preventing false positives
        let rule = MD028NoBlanksBlockquote;
        let content = "<!-- > not a real blockquote\n\n> also not real -->\n\n> real quote A\n\n> real quote B";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        // Only the blank between "real quote A" and "real quote B" (line 6) should be flagged
        assert_eq!(
            result.len(),
            1,
            "Should only warn about blank between real quotes, got: {result:?}"
        );
        assert_eq!(result[0].line, 6, "Warning should be on line 6 (between real quotes)");
    }

    #[test]
    fn test_commented_blockquote_between_real_blockquotes() {
        // A commented-out blockquote between two real blockquotes should act
        // as non-blockquote content, preventing them from being considered
        // the same blockquote
        let rule = MD028NoBlanksBlockquote;
        let content = "> real A\n\n<!-- > commented -->\n\n> real B";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Should NOT warn when non-blockquote content (HTML comment) separates blockquotes, got: {result:?}"
        );
    }

    #[test]
    fn test_code_block_with_blockquote_markers_between_real_blockquotes() {
        // Blockquote markers inside code blocks should be ignored by scanning
        let rule = MD028NoBlanksBlockquote;
        let content = "> real A\n\n```\n> not a blockquote\n```\n\n> real B";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Should NOT warn when code block with > markers separates blockquotes, got: {result:?}"
        );
    }

    #[test]
    fn test_frontmatter_with_blockquote_markers_does_not_cause_false_positive() {
        // Blockquote-like lines in frontmatter should be ignored by scanning
        let rule = MD028NoBlanksBlockquote;
        let content = "---\n> frontmatter value\n---\n\n> real quote A\n\n> real quote B";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        // Only the blank between the two real blockquotes should be flagged
        assert_eq!(
            result.len(),
            1,
            "Should only flag the blank between real quotes, got: {result:?}"
        );
        assert_eq!(result[0].line, 6, "Warning should be on line 6 (between real quotes)");
    }

    #[test]
    fn test_fix_does_not_modify_comment_separated_blockquotes() {
        // Fix should not add > markers when blockquotes are separated by HTML comments
        let rule = MD028NoBlanksBlockquote;
        let content = "> real A\n\n<!-- > commented -->\n\n> real B";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(
            fixed, content,
            "Fix should not modify content when blockquotes are separated by HTML comment"
        );
    }

    #[test]
    fn test_fix_works_correctly_with_comment_before_real_blockquotes() {
        // Fix should correctly handle the case where a comment with > markers
        // precedes two real blockquotes that have a blank between them
        let rule = MD028NoBlanksBlockquote;
        let content = "<!-- > not a real blockquote\n\n> also not real -->\n\n> real quote A\n\n> real quote B";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        // The blank between the two real quotes should be fixed
        assert!(
            fixed.contains("> real quote A\n>\n> real quote B"),
            "Fix should add > marker between real quotes, got: {fixed}"
        );
        // The content inside the comment should be untouched
        assert!(
            fixed.contains("<!-- > not a real blockquote"),
            "Fix should not modify comment content"
        );
    }

    #[test]
    fn test_html_block_with_angle_brackets_not_flagged() {
        // HTML blocks can contain `>` characters (e.g., in nested tags or template syntax)
        // that look like blockquote markers. These should be skipped.
        let rule = MD028NoBlanksBlockquote;
        let content = "<div>\n> not a real blockquote\n\n> also not real\n</div>";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        assert!(
            result.is_empty(),
            "Lines inside HTML blocks should not trigger MD028. Got: {result:?}"
        );
    }

    #[test]
    fn test_html_block_does_not_leak_into_adjacent_blockquotes() {
        // Blockquotes after an HTML block should still be checked
        let rule = MD028NoBlanksBlockquote;
        let content =
            "<details>\n<summary>Click</summary>\n> inside html block\n</details>\n\n> real quote A\n\n> real quote B";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        // Only the blank between "real quote A" and "real quote B" should be flagged
        assert_eq!(
            result.len(),
            1,
            "Expected 1 warning for blank between real blockquotes after HTML block. Got: {result:?}"
        );
    }
}