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
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
/// Shared table detection and processing utilities for markdown linting rules
///
/// This module provides optimized table detection and processing functionality
/// that can be shared across multiple table-related rules (MD055, MD056, MD058).
/// Represents a table block in the document
#[derive(Debug, Clone)]
pub struct TableBlock {
    pub start_line: usize,
    pub end_line: usize,
    pub header_line: usize,
    pub delimiter_line: usize,
    pub content_lines: Vec<usize>,
    /// If the table is inside a list item, this contains:
    /// - The list marker prefix for the header line (e.g., "- ", "1. ")
    /// - The content indent (number of spaces for continuation lines)
    pub list_context: Option<ListTableContext>,
}

/// Context information for tables inside list items
#[derive(Debug, Clone)]
pub struct ListTableContext {
    /// The list marker prefix including any leading whitespace (e.g., "- ", "  1. ")
    pub list_prefix: String,
    /// Number of spaces for continuation lines to align with content
    pub content_indent: usize,
}

/// Shared table detection utilities
pub struct TableUtils;

impl TableUtils {
    /// Returns true if the line has at least one unescaped pipe separator outside inline code spans.
    ///
    /// This helps distinguish actual table separators from command/prose examples like
    /// `` `echo a | sed 's/a/b/'` `` where the pipe is fully inside inline code.
    fn has_unescaped_pipe_outside_inline_code(text: &str) -> bool {
        let chars: Vec<char> = text.chars().collect();
        let mut i = 0;
        let mut in_code = false;
        let mut code_delim_len = 0usize;

        while i < chars.len() {
            let ch = chars[i];

            if ch == '\\' && !in_code {
                // Skip escaped character (only outside code spans —
                // backslashes are literal inside code spans per CommonMark).
                i += if i + 1 < chars.len() { 2 } else { 1 };
                continue;
            }

            if ch == '`' {
                let mut run = 1usize;
                while i + run < chars.len() && chars[i + run] == '`' {
                    run += 1;
                }

                if in_code {
                    if run == code_delim_len {
                        in_code = false;
                        code_delim_len = 0;
                    }
                } else {
                    in_code = true;
                    code_delim_len = run;
                }

                i += run;
                continue;
            }

            if ch == '|' && !in_code {
                return true;
            }

            i += 1;
        }

        false
    }

    /// Check if a line looks like a potential table row
    pub fn is_potential_table_row(line: &str) -> bool {
        let trimmed = line.trim();
        if trimmed.is_empty() || !trimmed.contains('|') {
            return false;
        }

        // Skip lines that are clearly not table rows
        // Unordered list items with space or tab after marker
        if trimmed.starts_with("- ")
            || trimmed.starts_with("* ")
            || trimmed.starts_with("+ ")
            || trimmed.starts_with("-\t")
            || trimmed.starts_with("*\t")
            || trimmed.starts_with("+\t")
        {
            return false;
        }

        // Skip ordered list items: digits followed by . or ) then space/tab
        if let Some(first_non_digit) = trimmed.find(|c: char| !c.is_ascii_digit())
            && first_non_digit > 0
        {
            let after_digits = &trimmed[first_non_digit..];
            if after_digits.starts_with(". ")
                || after_digits.starts_with(".\t")
                || after_digits.starts_with(") ")
                || after_digits.starts_with(")\t")
            {
                return false;
            }
        }

        // Skip ATX headings (# through ######)
        if trimmed.starts_with('#') {
            let hash_count = trimmed.bytes().take_while(|&b| b == b'#').count();
            if hash_count <= 6 {
                let after_hashes = &trimmed[hash_count..];
                if after_hashes.is_empty() || after_hashes.starts_with(' ') || after_hashes.starts_with('\t') {
                    return false;
                }
            }
        }

        // For rows without explicit outer pipes, require a real separator outside
        // inline code spans to avoid prose/command false positives.
        let has_outer_pipes = trimmed.starts_with('|') && trimmed.ends_with('|');
        if !has_outer_pipes && !Self::has_unescaped_pipe_outside_inline_code(trimmed) {
            return false;
        }

        // Must have at least 2 parts when split by |
        let parts: Vec<&str> = trimmed.split('|').collect();
        if parts.len() < 2 {
            return false;
        }

        // Check if it looks like a table row by having reasonable content between pipes
        let mut valid_parts = 0;
        let mut total_non_empty_parts = 0;

        for part in &parts {
            let part_trimmed = part.trim();
            // Skip empty parts (from leading/trailing pipes)
            if part_trimmed.is_empty() {
                continue;
            }
            total_non_empty_parts += 1;

            // Count parts that look like table cells (reasonable content, no newlines)
            if !part_trimmed.contains('\n') {
                valid_parts += 1;
            }
        }

        // Check if all non-empty parts are valid (no newlines)
        if total_non_empty_parts > 0 && valid_parts != total_non_empty_parts {
            // Some cells contain newlines, not a valid table row
            return false;
        }

        // GFM allows tables with all empty cells (e.g., |||)
        // These are valid if they have proper table formatting (leading and trailing pipes)
        if total_non_empty_parts == 0 {
            // Empty cells are only valid with proper pipe formatting
            return trimmed.starts_with('|') && trimmed.ends_with('|') && parts.len() >= 3;
        }

        // GFM allows single-column tables, so >= 1 valid part is enough
        // when the line has proper table formatting (pipes)
        if trimmed.starts_with('|') && trimmed.ends_with('|') {
            // Properly formatted table row with pipes on both ends
            valid_parts >= 1
        } else {
            // For rows without proper pipe formatting, require at least 2 cells
            valid_parts >= 2
        }
    }

    /// Check if a line is a table delimiter row (e.g., |---|---|)
    pub fn is_delimiter_row(line: &str) -> bool {
        let trimmed = line.trim();
        if !trimmed.contains('|') || !trimmed.contains('-') {
            return false;
        }

        // Split by pipes and check each part
        let parts: Vec<&str> = trimmed.split('|').collect();
        let mut valid_delimiter_parts = 0;
        let mut total_non_empty_parts = 0;

        for part in &parts {
            let part_trimmed = part.trim();
            if part_trimmed.is_empty() {
                continue; // Skip empty parts from leading/trailing pipes
            }

            total_non_empty_parts += 1;

            // Check if this part looks like a delimiter (contains dashes and optionally colons)
            if part_trimmed.chars().all(|c| c == '-' || c == ':' || c.is_whitespace()) && part_trimmed.contains('-') {
                valid_delimiter_parts += 1;
            }
        }

        // All non-empty parts must be valid delimiters, and there must be at least one
        total_non_empty_parts > 0 && valid_delimiter_parts == total_non_empty_parts
    }

    /// Strip blockquote prefix from a line, returning the content without the prefix
    fn strip_blockquote_prefix(line: &str) -> &str {
        let trimmed = line.trim_start();
        if trimmed.starts_with('>') {
            // Strip all blockquote markers and following space
            let mut rest = trimmed;
            while rest.starts_with('>') {
                rest = rest.strip_prefix('>').unwrap_or(rest);
                rest = rest.trim_start_matches(' ');
            }
            rest
        } else {
            line
        }
    }

    /// Find all table blocks in the content with optimized detection
    /// This version accepts code_blocks and code_spans directly for use during LintContext construction
    pub fn find_table_blocks_with_code_info(
        content: &str,
        code_blocks: &[(usize, usize)],
        code_spans: &[crate::lint_context::CodeSpan],
        html_comment_ranges: &[crate::utils::skip_context::ByteRange],
    ) -> Vec<TableBlock> {
        let lines: Vec<&str> = content.lines().collect();
        let mut tables = Vec::new();
        let mut i = 0;

        // Pre-compute line positions for efficient code block checking
        let mut line_positions = Vec::with_capacity(lines.len());
        let mut pos = 0;
        for line in &lines {
            line_positions.push(pos);
            pos += line.len() + 1; // +1 for newline
        }

        // Stack of active list content indents for continuation table tracking.
        // Supports nested lists: when a child list is seen, we push; when we
        // dedent past a level, we pop back to the enclosing list.
        let mut list_indent_stack: Vec<usize> = Vec::new();

        while i < lines.len() {
            // Skip lines in code blocks, code spans, or HTML comments
            let line_start = line_positions[i];
            let in_code =
                crate::utils::code_block_utils::CodeBlockUtils::is_in_code_block_or_span(code_blocks, line_start) || {
                    // Binary search on sorted code spans
                    let idx = code_spans.partition_point(|span| span.byte_offset <= line_start);
                    idx > 0 && line_start < code_spans[idx - 1].byte_end
                };
            let in_html_comment = {
                // Binary search on sorted HTML comment ranges
                let idx = html_comment_ranges.partition_point(|range| range.start <= line_start);
                idx > 0 && line_start < html_comment_ranges[idx - 1].end
            };

            if in_code || in_html_comment {
                i += 1;
                continue;
            }

            // Strip blockquote prefix for table detection
            let line_content = Self::strip_blockquote_prefix(lines[i]);

            // Update active list tracking
            let (list_prefix, list_content, content_indent) = Self::extract_list_prefix(line_content);
            if !list_prefix.is_empty() {
                // Line has a list marker. Pop any deeper/equal levels, then push this one.
                while list_indent_stack.last().is_some_and(|&top| top >= content_indent) {
                    list_indent_stack.pop();
                }
                list_indent_stack.push(content_indent);
            } else if !line_content.trim().is_empty() {
                // Non-blank line without a marker: pop any levels we've dedented past
                let leading = line_content.len() - line_content.trim_start().len();
                while list_indent_stack.last().is_some_and(|&top| leading < top) {
                    list_indent_stack.pop();
                }
            }
            // Blank lines keep the stack unchanged (blank lines don't end list items)

            // Check if this is a list item that contains a table row on the same line,
            // or a continuation table indented under an active list item
            let (is_same_line_list_table, effective_content) =
                if !list_prefix.is_empty() && Self::is_potential_table_row_content(list_content) {
                    (true, list_content)
                } else {
                    (false, line_content)
                };

            // Detect continuation list tables: no marker on this line, but indented
            // under an active list item (e.g., "- Text\n  | h1 | h2 |")
            let continuation_indent = if !is_same_line_list_table && list_prefix.is_empty() {
                let leading = line_content.len() - line_content.trim_start().len();
                // Find the deepest list level this line is indented under
                list_indent_stack
                    .iter()
                    .rev()
                    .find(|&&indent| leading >= indent)
                    .copied()
            } else {
                None
            };

            let is_continuation_list_table = continuation_indent.is_some()
                && {
                    let indent = continuation_indent.unwrap();
                    let leading = line_content.len() - line_content.trim_start().len();
                    // Per CommonMark, 4+ spaces beyond content indent is a code block
                    leading < indent + 4
                }
                && Self::is_potential_table_row(effective_content);

            let is_any_list_table = is_same_line_list_table || is_continuation_list_table;

            // For continuation list tables, use the matched list indent
            let effective_content_indent = if is_same_line_list_table {
                content_indent
            } else if is_continuation_list_table {
                continuation_indent.unwrap()
            } else {
                0
            };

            // Look for potential table start
            if is_any_list_table || Self::is_potential_table_row(effective_content) {
                // For list tables (same-line or continuation), check indented continuation lines
                // For regular tables, check the next line directly
                let (next_line_content, delimiter_has_valid_indent) = if i + 1 < lines.len() {
                    let next_raw = Self::strip_blockquote_prefix(lines[i + 1]);
                    if is_any_list_table {
                        // Verify the delimiter line has proper indentation
                        let leading_spaces = next_raw.len() - next_raw.trim_start().len();
                        if leading_spaces >= effective_content_indent {
                            // Has proper indentation, strip it and check as delimiter
                            (
                                Self::strip_list_continuation_indent(next_raw, effective_content_indent),
                                true,
                            )
                        } else {
                            // Not enough indentation - not a list table
                            (next_raw, false)
                        }
                    } else {
                        (next_raw, true)
                    }
                } else {
                    ("", true)
                };

                // For list tables, only accept if delimiter has valid indentation
                let effective_is_list_table = is_any_list_table && delimiter_has_valid_indent;

                if i + 1 < lines.len() && Self::is_delimiter_row(next_line_content) {
                    // Found a table! Find its end
                    let table_start = i;
                    let header_line = i;
                    let delimiter_line = i + 1;
                    let mut table_end = i + 1; // Include the delimiter row
                    let mut content_lines = Vec::new();

                    // Continue while we have table rows
                    let mut j = i + 2;
                    while j < lines.len() {
                        let line = lines[j];
                        // Strip blockquote prefix for checking
                        let raw_content = Self::strip_blockquote_prefix(line);

                        // For list tables, strip expected indentation
                        let line_content = if effective_is_list_table {
                            Self::strip_list_continuation_indent(raw_content, effective_content_indent)
                        } else {
                            raw_content
                        };

                        if line_content.trim().is_empty() {
                            // Empty line ends the table
                            break;
                        }

                        // For list tables, the continuation line must have proper indentation
                        if effective_is_list_table {
                            let leading_spaces = raw_content.len() - raw_content.trim_start().len();
                            if leading_spaces < effective_content_indent {
                                // Not enough indentation - end of table
                                break;
                            }
                        }

                        if Self::is_potential_table_row(line_content) {
                            content_lines.push(j);
                            table_end = j;
                            j += 1;
                        } else {
                            // Non-table line ends the table
                            break;
                        }
                    }

                    let list_context = if effective_is_list_table {
                        if is_same_line_list_table {
                            // Same-line: prefix is the actual list marker (e.g., "- ")
                            Some(ListTableContext {
                                list_prefix: list_prefix.to_string(),
                                content_indent: effective_content_indent,
                            })
                        } else {
                            // Continuation: prefix is the indentation spaces
                            Some(ListTableContext {
                                list_prefix: " ".repeat(effective_content_indent),
                                content_indent: effective_content_indent,
                            })
                        }
                    } else {
                        None
                    };

                    tables.push(TableBlock {
                        start_line: table_start,
                        end_line: table_end,
                        header_line,
                        delimiter_line,
                        content_lines,
                        list_context,
                    });
                    i = table_end + 1;
                } else {
                    i += 1;
                }
            } else {
                i += 1;
            }
        }

        tables
    }

    /// Strip list continuation indentation from a line.
    /// For lines that are continuations of a list item's content, strip the expected indent.
    fn strip_list_continuation_indent(line: &str, expected_indent: usize) -> &str {
        let bytes = line.as_bytes();
        let mut spaces = 0;

        for &b in bytes {
            if b == b' ' {
                spaces += 1;
            } else if b == b'\t' {
                // Tab counts as up to 4 spaces, rounding up to next multiple of 4
                spaces = (spaces / 4 + 1) * 4;
            } else {
                break;
            }

            if spaces >= expected_indent {
                break;
            }
        }

        // Strip at most expected_indent characters
        let strip_count = spaces.min(expected_indent).min(line.len());
        // Count actual bytes to strip (handling tabs)
        let mut byte_count = 0;
        let mut counted_spaces = 0;
        for &b in bytes {
            if counted_spaces >= strip_count {
                break;
            }
            if b == b' ' {
                counted_spaces += 1;
                byte_count += 1;
            } else if b == b'\t' {
                counted_spaces = (counted_spaces / 4 + 1) * 4;
                byte_count += 1;
            } else {
                break;
            }
        }

        &line[byte_count..]
    }

    /// Find all table blocks in the content with optimized detection
    /// This is a backward-compatible wrapper that accepts LintContext
    pub fn find_table_blocks(content: &str, ctx: &crate::lint_context::LintContext) -> Vec<TableBlock> {
        Self::find_table_blocks_with_code_info(content, &ctx.code_blocks, &ctx.code_spans(), ctx.html_comment_ranges())
    }

    /// Count the number of cells in a table row
    pub fn count_cells(row: &str) -> usize {
        Self::count_cells_with_flavor(row, crate::config::MarkdownFlavor::Standard)
    }

    /// Count the number of cells in a table row with flavor-specific behavior
    ///
    /// Pipes inside code spans are treated as content, not cell delimiters.
    ///
    /// This function strips blockquote prefixes before counting cells, so it works
    /// correctly for tables inside blockquotes.
    pub fn count_cells_with_flavor(row: &str, flavor: crate::config::MarkdownFlavor) -> usize {
        // Strip blockquote prefix if present before counting cells
        let (_, content) = Self::extract_blockquote_prefix(row);
        Self::split_table_row_with_flavor(content, flavor).len()
    }

    /// Count the number of consecutive backslashes immediately preceding `pos` in `chars`.
    fn count_preceding_backslashes(chars: &[char], pos: usize) -> usize {
        let mut count = 0;
        let mut k = pos;
        while k > 0 {
            k -= 1;
            if chars[k] == '\\' {
                count += 1;
            } else {
                break;
            }
        }
        count
    }

    /// Mask pipes inside inline code blocks with a placeholder character.
    ///
    /// Backticks preceded by an odd number of backslashes are escaped (literal text)
    /// and do not open or close code spans. An even number of backslashes means the
    /// backslashes themselves are escaped, so the backtick is a real delimiter.
    pub fn mask_pipes_in_inline_code(text: &str) -> String {
        let mut result = String::new();
        let chars: Vec<char> = text.chars().collect();
        let mut i = 0;

        while i < chars.len() {
            if chars[i] == '`' {
                // A backtick preceded by an odd number of backslashes is escaped
                let preceding = Self::count_preceding_backslashes(&chars, i);
                if preceding % 2 != 0 {
                    // Escaped backtick -- treat as literal text, not a code span opener
                    result.push(chars[i]);
                    i += 1;
                    continue;
                }

                // Count consecutive backticks at start
                let start = i;
                let mut backtick_count = 0;
                while i < chars.len() && chars[i] == '`' {
                    backtick_count += 1;
                    i += 1;
                }

                // Look for matching closing backticks
                let mut found_closing = false;
                let mut j = i;

                while j < chars.len() {
                    if chars[j] == '`' {
                        // Per CommonMark spec, backslash escapes do NOT work inside code
                        // spans -- all characters including backslashes are literal. So we
                        // do NOT check count_preceding_backslashes here (only for the
                        // opening backtick above).

                        // Count potential closing backticks
                        let close_start = j;
                        let mut close_count = 0;
                        while j < chars.len() && chars[j] == '`' {
                            close_count += 1;
                            j += 1;
                        }

                        if close_count == backtick_count {
                            // Found matching closing backticks
                            found_closing = true;

                            // Valid inline code - add with pipes masked
                            result.extend(chars[start..i].iter());

                            for &ch in chars.iter().take(close_start).skip(i) {
                                if ch == '|' {
                                    result.push('_'); // Mask pipe with underscore
                                } else {
                                    result.push(ch);
                                }
                            }

                            result.extend(chars[close_start..j].iter());
                            i = j;
                            break;
                        }
                        // If not matching, continue searching (j is already past these backticks)
                    } else {
                        j += 1;
                    }
                }

                if !found_closing {
                    // No matching closing found, treat as regular text
                    result.extend(chars[start..i].iter());
                }
            } else {
                result.push(chars[i]);
                i += 1;
            }
        }

        result
    }

    /// Mask escaped pipes for accurate table cell parsing
    ///
    /// In GFM tables, escape handling happens BEFORE cell boundary detection:
    /// - `\|` → escaped pipe → masked (stays as cell content)
    /// - `\\|` → escaped backslash + pipe → NOT masked (pipe is a delimiter)
    ///
    /// This function only handles escaped pipes. Pipes inside inline code spans
    /// are handled separately by `mask_pipes_in_inline_code`.
    pub fn mask_pipes_for_table_parsing(text: &str) -> String {
        let mut result = String::new();
        let chars: Vec<char> = text.chars().collect();
        let mut i = 0;

        while i < chars.len() {
            if chars[i] == '\\' {
                if i + 1 < chars.len() && chars[i + 1] == '\\' {
                    // Escaped backslash: \\ → push both and continue
                    // The next character (if it's a pipe) will be a real delimiter
                    result.push('\\');
                    result.push('\\');
                    i += 2;
                } else if i + 1 < chars.len() && chars[i + 1] == '|' {
                    // Escaped pipe: \| → mask the pipe
                    result.push('\\');
                    result.push('_'); // Mask the pipe
                    i += 2;
                } else {
                    // Single backslash not followed by \ or | → just push it
                    result.push(chars[i]);
                    i += 1;
                }
            } else {
                result.push(chars[i]);
                i += 1;
            }
        }

        result
    }

    /// Split a table row into individual cell contents with flavor-specific behavior.
    ///
    /// Returns a Vec of cell content strings (not trimmed - preserves original spacing).
    /// This is the foundation for both cell counting and cell content extraction.
    ///
    /// Pipes inside code spans are treated as content, not cell delimiters.
    pub fn split_table_row_with_flavor(row: &str, _flavor: crate::config::MarkdownFlavor) -> Vec<String> {
        let trimmed = row.trim();

        if !trimmed.contains('|') {
            return Vec::new();
        }

        // First, mask escaped pipes (same for all flavors)
        let masked = Self::mask_pipes_for_table_parsing(trimmed);

        // Mask pipes inside inline code for all flavors
        let final_masked = Self::mask_pipes_in_inline_code(&masked);

        let has_leading = final_masked.starts_with('|');
        let has_trailing = final_masked.ends_with('|');

        let mut masked_content = final_masked.as_str();
        let mut orig_content = trimmed;

        if has_leading {
            masked_content = &masked_content[1..];
            orig_content = &orig_content[1..];
        }

        // Track whether we actually strip a trailing pipe
        let stripped_trailing = has_trailing && !masked_content.is_empty();
        if stripped_trailing {
            masked_content = &masked_content[..masked_content.len() - 1];
            orig_content = &orig_content[..orig_content.len() - 1];
        }

        // Handle edge cases for degenerate inputs
        if masked_content.is_empty() {
            if stripped_trailing {
                // "||" case: two pipes with empty content between = one empty cell
                return vec![String::new()];
            } else {
                // "|" case: single pipe, not a valid table row
                return Vec::new();
            }
        }

        let masked_parts: Vec<&str> = masked_content.split('|').collect();
        let mut cells = Vec::new();
        let mut pos = 0;

        for masked_cell in masked_parts {
            let cell_len = masked_cell.len();
            let orig_cell = if pos + cell_len <= orig_content.len() {
                &orig_content[pos..pos + cell_len]
            } else {
                masked_cell
            };
            cells.push(orig_cell.to_string());
            pos += cell_len + 1; // +1 for the pipe delimiter
        }

        cells
    }

    /// Split a table row into individual cell contents using Standard/GFM behavior.
    pub fn split_table_row(row: &str) -> Vec<String> {
        Self::split_table_row_with_flavor(row, crate::config::MarkdownFlavor::Standard)
    }

    /// Determine the pipe style of a table row
    ///
    /// Handles tables inside blockquotes by stripping the blockquote prefix
    /// before analyzing the pipe style.
    pub fn determine_pipe_style(line: &str) -> Option<&'static str> {
        // Strip blockquote prefix if present before analyzing pipe style
        let content = Self::strip_blockquote_prefix(line);
        let trimmed = content.trim();
        if !trimmed.contains('|') {
            return None;
        }

        let has_leading = trimmed.starts_with('|');
        let has_trailing = trimmed.ends_with('|');

        match (has_leading, has_trailing) {
            (true, true) => Some("leading_and_trailing"),
            (true, false) => Some("leading_only"),
            (false, true) => Some("trailing_only"),
            (false, false) => Some("no_leading_or_trailing"),
        }
    }

    /// Extract blockquote prefix from a line, returning (prefix, content).
    ///
    /// This is useful for stripping the prefix before processing, then restoring it after.
    /// For example: `"> | H1 | H2 |"` returns `("> ", "| H1 | H2 |")`.
    pub fn extract_blockquote_prefix(line: &str) -> (&str, &str) {
        // Find where the actual content starts (after blockquote markers and spaces)
        let bytes = line.as_bytes();
        let mut pos = 0;

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

        // If no blockquote marker, return empty prefix
        if pos >= bytes.len() || bytes[pos] != b'>' {
            return ("", line);
        }

        // Skip all blockquote markers and spaces
        while pos < bytes.len() {
            if bytes[pos] == b'>' {
                pos += 1;
                // Skip optional space after >
                if pos < bytes.len() && bytes[pos] == b' ' {
                    pos += 1;
                }
            } else if bytes[pos] == b' ' || bytes[pos] == b'\t' {
                pos += 1;
            } else {
                break;
            }
        }

        // Split at the position where content starts
        (&line[..pos], &line[pos..])
    }

    /// Extract list marker prefix from a line, returning (prefix, content, content_indent).
    ///
    /// This handles unordered list markers (`-`, `*`, `+`) and ordered list markers (`1.`, `10)`, etc.)
    /// Returns:
    /// - prefix: The list marker including any leading whitespace and trailing space (e.g., "- ", "  1. ")
    /// - content: The content after the list marker
    /// - content_indent: The number of spaces needed for continuation lines to align with content
    ///
    /// For example:
    /// - `"- | H1 | H2 |"` returns `("- ", "| H1 | H2 |", 2)`
    /// - `"1. | H1 | H2 |"` returns `("1. ", "| H1 | H2 |", 3)`
    /// - `"  - table"` returns `("  - ", "table", 4)`
    ///
    /// Returns `("", line, 0)` if the line doesn't start with a list marker.
    pub fn extract_list_prefix(line: &str) -> (&str, &str, usize) {
        let bytes = line.as_bytes();

        // Skip leading whitespace
        let leading_spaces = bytes.iter().take_while(|&&b| b == b' ' || b == b'\t').count();
        let mut pos = leading_spaces;

        if pos >= bytes.len() {
            return ("", line, 0);
        }

        // Check for unordered list marker: -, *, +
        if matches!(bytes[pos], b'-' | b'*' | b'+') {
            pos += 1;

            // Must be followed by space or tab (or end of line for marker-only lines)
            if pos >= bytes.len() || bytes[pos] == b' ' || bytes[pos] == b'\t' {
                // Skip the space after marker if present
                if pos < bytes.len() && (bytes[pos] == b' ' || bytes[pos] == b'\t') {
                    pos += 1;
                }
                let content_indent = pos;
                return (&line[..pos], &line[pos..], content_indent);
            }
            // Not a list marker (e.g., "-word" or "--")
            return ("", line, 0);
        }

        // Check for ordered list marker: digits followed by . or ) then space
        if bytes[pos].is_ascii_digit() {
            let digit_start = pos;
            while pos < bytes.len() && bytes[pos].is_ascii_digit() {
                pos += 1;
            }

            // Must have at least one digit
            if pos > digit_start && pos < bytes.len() {
                // Check for . or ) followed by space/tab
                if bytes[pos] == b'.' || bytes[pos] == b')' {
                    pos += 1;
                    if pos >= bytes.len() || bytes[pos] == b' ' || bytes[pos] == b'\t' {
                        // Skip the space after marker if present
                        if pos < bytes.len() && (bytes[pos] == b' ' || bytes[pos] == b'\t') {
                            pos += 1;
                        }
                        let content_indent = pos;
                        return (&line[..pos], &line[pos..], content_indent);
                    }
                }
            }
        }

        ("", line, 0)
    }

    /// Extract the table row content from a line, stripping any list/blockquote prefix.
    ///
    /// This is useful for processing table rows that may be inside list items or blockquotes.
    /// The line_index indicates which line of the table this is (0 = header, 1 = delimiter, etc.)
    pub fn extract_table_row_content<'a>(line: &'a str, table_block: &TableBlock, line_index: usize) -> &'a str {
        // First strip blockquote prefix
        let (_, after_blockquote) = Self::extract_blockquote_prefix(line);

        // Then handle list prefix if present
        if let Some(ref list_ctx) = table_block.list_context {
            if line_index == 0 {
                // Header line: strip list prefix (handles both markers and indentation)
                after_blockquote
                    .strip_prefix(&list_ctx.list_prefix)
                    .unwrap_or_else(|| Self::extract_list_prefix(after_blockquote).1)
            } else {
                // Continuation lines: strip indentation
                Self::strip_list_continuation_indent(after_blockquote, list_ctx.content_indent)
            }
        } else {
            after_blockquote
        }
    }

    /// Check if the content after a list marker looks like a table row.
    /// This is used to detect tables that start on the same line as a list marker.
    pub fn is_list_item_with_table_row(line: &str) -> bool {
        let (prefix, content, _) = Self::extract_list_prefix(line);
        if prefix.is_empty() {
            return false;
        }

        // Check if the content after the list marker is a table row
        // It must start with | (proper table format within a list)
        let trimmed = content.trim();
        if !trimmed.starts_with('|') {
            return false;
        }

        // Use our table row detection on the content
        Self::is_potential_table_row_content(content)
    }

    /// Internal helper: Check if content (without list/blockquote prefix) looks like a table row.
    fn is_potential_table_row_content(content: &str) -> bool {
        Self::is_potential_table_row(content)
    }
}

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

    #[test]
    fn test_is_potential_table_row() {
        // Basic valid table rows
        assert!(TableUtils::is_potential_table_row("| Header 1 | Header 2 |"));
        assert!(TableUtils::is_potential_table_row("| Cell 1 | Cell 2 |"));
        assert!(TableUtils::is_potential_table_row("Cell 1 | Cell 2"));
        assert!(TableUtils::is_potential_table_row("| Cell |")); // Single-column tables are valid in GFM

        // Multiple cells
        assert!(TableUtils::is_potential_table_row("| A | B | C | D | E |"));

        // With whitespace
        assert!(TableUtils::is_potential_table_row("  | Indented | Table |  "));
        assert!(TableUtils::is_potential_table_row("| Spaces | Around |"));

        // Not table rows
        assert!(!TableUtils::is_potential_table_row("- List item"));
        assert!(!TableUtils::is_potential_table_row("* Another list"));
        assert!(!TableUtils::is_potential_table_row("+ Plus list"));
        assert!(!TableUtils::is_potential_table_row("Regular text"));
        assert!(!TableUtils::is_potential_table_row(""));
        assert!(!TableUtils::is_potential_table_row("   "));

        // Code blocks
        assert!(!TableUtils::is_potential_table_row("`code with | pipe`"));
        assert!(!TableUtils::is_potential_table_row("``multiple | backticks``"));
        assert!(!TableUtils::is_potential_table_row("Use ``a|b`` in prose"));
        assert!(TableUtils::is_potential_table_row("| `fenced` | Uses ``` and ~~~ |"));
        assert!(TableUtils::is_potential_table_row("`!foo && bar` | `(!foo) && bar`"));
        assert!(!TableUtils::is_potential_table_row("`echo a | sed 's/a/b/'`"));

        // Single pipe not enough
        assert!(!TableUtils::is_potential_table_row("Just one |"));
        assert!(!TableUtils::is_potential_table_row("| Just one"));

        // Very long cells are valid in tables (no length limit for cell content)
        let long_cell = "a".repeat(150);
        assert!(TableUtils::is_potential_table_row(&format!("| {long_cell} | b |")));

        // Cells with newlines
        assert!(!TableUtils::is_potential_table_row("| Cell with\nnewline | Other |"));

        // Empty cells (Issue #129)
        assert!(TableUtils::is_potential_table_row("|||")); // Two empty cells
        assert!(TableUtils::is_potential_table_row("||||")); // Three empty cells
        assert!(TableUtils::is_potential_table_row("| | |")); // Two empty cells with spaces
    }

    #[test]
    fn test_list_items_with_pipes_not_table_rows() {
        // Ordered list items should NOT be detected as table rows
        assert!(!TableUtils::is_potential_table_row("1. Item with | pipe"));
        assert!(!TableUtils::is_potential_table_row("10. Item with | pipe"));
        assert!(!TableUtils::is_potential_table_row("999. Item with | pipe"));
        assert!(!TableUtils::is_potential_table_row("1) Item with | pipe"));
        assert!(!TableUtils::is_potential_table_row("10) Item with | pipe"));

        // Unordered list items with tabs
        assert!(!TableUtils::is_potential_table_row("-\tItem with | pipe"));
        assert!(!TableUtils::is_potential_table_row("*\tItem with | pipe"));
        assert!(!TableUtils::is_potential_table_row("+\tItem with | pipe"));

        // Indented list items (the trim_start normalizes indentation)
        assert!(!TableUtils::is_potential_table_row("  - Indented | pipe"));
        assert!(!TableUtils::is_potential_table_row("    * Deep indent | pipe"));
        assert!(!TableUtils::is_potential_table_row("  1. Ordered indent | pipe"));

        // Task list items
        assert!(!TableUtils::is_potential_table_row("- [ ] task | pipe"));
        assert!(!TableUtils::is_potential_table_row("- [x] done | pipe"));

        // Multiple pipes in list items
        assert!(!TableUtils::is_potential_table_row("1. foo | bar | baz"));
        assert!(!TableUtils::is_potential_table_row("- alpha | beta | gamma"));

        // These SHOULD still be detected as potential table rows
        assert!(TableUtils::is_potential_table_row("| cell | cell |"));
        assert!(TableUtils::is_potential_table_row("cell | cell"));
        assert!(TableUtils::is_potential_table_row("| Header | Header |"));
    }

    #[test]
    fn test_atx_headings_with_pipes_not_table_rows() {
        // All 6 ATX heading levels with pipes
        assert!(!TableUtils::is_potential_table_row("# Heading | with pipe"));
        assert!(!TableUtils::is_potential_table_row("## Heading | with pipe"));
        assert!(!TableUtils::is_potential_table_row("### Heading | with pipe"));
        assert!(!TableUtils::is_potential_table_row("#### Heading | with pipe"));
        assert!(!TableUtils::is_potential_table_row("##### Heading | with pipe"));
        assert!(!TableUtils::is_potential_table_row("###### Heading | with pipe"));

        // Multiple pipes in headings
        assert!(!TableUtils::is_potential_table_row("### col1 | col2 | col3"));
        assert!(!TableUtils::is_potential_table_row("## a|b|c"));

        // Headings with tab after hashes
        assert!(!TableUtils::is_potential_table_row("#\tHeading | pipe"));
        assert!(!TableUtils::is_potential_table_row("##\tHeading | pipe"));

        // Heading with only hashes and pipe (empty heading text)
        assert!(!TableUtils::is_potential_table_row("# |"));
        assert!(!TableUtils::is_potential_table_row("## |"));

        // Indented headings (spaces before #)
        assert!(!TableUtils::is_potential_table_row("  ## Heading | pipe"));
        assert!(!TableUtils::is_potential_table_row("   ### Heading | pipe"));

        // Unicode content in headings (the original proptest failure case)
        assert!(!TableUtils::is_potential_table_row("#### ®aAA|ᯗ"));

        // 7+ hashes are NOT headings — should follow normal table detection
        // "####### text|pipe" has no space after 7 hashes if treated as non-heading
        // but with a space it still has 7+ hashes so not a heading
        assert!(TableUtils::is_potential_table_row("####### text | pipe"));

        // Hash without space is NOT a heading, so pipe detection applies
        assert!(TableUtils::is_potential_table_row("#nospc|pipe"));

        // These SHOULD still be detected as potential table rows
        assert!(TableUtils::is_potential_table_row("| # Header | Value |"));
        assert!(TableUtils::is_potential_table_row("text | #tag"));
    }

    #[test]
    fn test_is_delimiter_row() {
        // Basic delimiter rows
        assert!(TableUtils::is_delimiter_row("|---|---|"));
        assert!(TableUtils::is_delimiter_row("| --- | --- |"));
        assert!(TableUtils::is_delimiter_row("|:---|---:|"));
        assert!(TableUtils::is_delimiter_row("|:---:|:---:|"));

        // With varying dash counts
        assert!(TableUtils::is_delimiter_row("|-|--|"));
        assert!(TableUtils::is_delimiter_row("|-------|----------|"));

        // With whitespace
        assert!(TableUtils::is_delimiter_row("|  ---  |  ---  |"));
        assert!(TableUtils::is_delimiter_row("| :--- | ---: |"));

        // Multiple columns
        assert!(TableUtils::is_delimiter_row("|---|---|---|---|"));

        // Without leading/trailing pipes
        assert!(TableUtils::is_delimiter_row("--- | ---"));
        assert!(TableUtils::is_delimiter_row(":--- | ---:"));

        // Not delimiter rows
        assert!(!TableUtils::is_delimiter_row("| Header | Header |"));
        assert!(!TableUtils::is_delimiter_row("Regular text"));
        assert!(!TableUtils::is_delimiter_row(""));
        assert!(!TableUtils::is_delimiter_row("|||"));
        assert!(!TableUtils::is_delimiter_row("| | |"));

        // Must have dashes
        assert!(!TableUtils::is_delimiter_row("| : | : |"));
        assert!(!TableUtils::is_delimiter_row("|    |    |"));

        // Mixed content
        assert!(!TableUtils::is_delimiter_row("| --- | text |"));
        assert!(!TableUtils::is_delimiter_row("| abc | --- |"));
    }

    #[test]
    fn test_count_cells() {
        // Basic counts
        assert_eq!(TableUtils::count_cells("| Cell 1 | Cell 2 | Cell 3 |"), 3);
        assert_eq!(TableUtils::count_cells("Cell 1 | Cell 2 | Cell 3"), 3);
        assert_eq!(TableUtils::count_cells("| Cell 1 | Cell 2"), 2);
        assert_eq!(TableUtils::count_cells("Cell 1 | Cell 2 |"), 2);

        // Single cell
        assert_eq!(TableUtils::count_cells("| Cell |"), 1);
        assert_eq!(TableUtils::count_cells("Cell"), 0); // No pipe

        // Empty cells
        assert_eq!(TableUtils::count_cells("|  |  |  |"), 3);
        assert_eq!(TableUtils::count_cells("| | | |"), 3);

        // Many cells
        assert_eq!(TableUtils::count_cells("| A | B | C | D | E | F |"), 6);

        // Edge cases
        assert_eq!(TableUtils::count_cells("||"), 1); // One empty cell
        assert_eq!(TableUtils::count_cells("|||"), 2); // Two empty cells

        // No table
        assert_eq!(TableUtils::count_cells("Regular text"), 0);
        assert_eq!(TableUtils::count_cells(""), 0);
        assert_eq!(TableUtils::count_cells("   "), 0);

        // Whitespace handling
        assert_eq!(TableUtils::count_cells("  | A | B |  "), 2);
        assert_eq!(TableUtils::count_cells("|   A   |   B   |"), 2);
    }

    #[test]
    fn test_count_cells_with_escaped_pipes() {
        // Pipes inside code spans are treated as content, not cell delimiters.
        // To include a literal pipe outside code spans, escape it with \|.

        // Basic table structure
        assert_eq!(TableUtils::count_cells("| Challenge | Solution |"), 2);
        assert_eq!(TableUtils::count_cells("| A | B | C |"), 3);
        assert_eq!(TableUtils::count_cells("| One | Two |"), 2);

        // Escaped pipes: \| keeps the pipe as content
        assert_eq!(TableUtils::count_cells(r"| Command | echo \| grep |"), 2);
        assert_eq!(TableUtils::count_cells(r"| A | B \| C |"), 2); // B | C is one cell

        // Escaped pipes inside backticks
        assert_eq!(TableUtils::count_cells(r"| Command | `echo \| grep` |"), 2);

        // Double backslash + pipe: \\| means escaped backslash followed by pipe delimiter
        assert_eq!(TableUtils::count_cells(r"| A | B \\| C |"), 3); // \\| is NOT escaped pipe
        // Double backslash inside backticks: pipe is still masked by code span
        assert_eq!(TableUtils::count_cells(r"| A | `B \\| C` |"), 2);

        // Pipes inside code spans are content, not delimiters
        assert_eq!(TableUtils::count_cells("| Command | `echo | grep` |"), 2);
        assert_eq!(TableUtils::count_cells("| `code | one` | `code | two` |"), 2);
        assert_eq!(TableUtils::count_cells("| `single|pipe` |"), 1);

        // Regex example - pipes in code spans are masked
        assert_eq!(TableUtils::count_cells(r"| Hour formats | `^([0-1]?\d|2[0-3])` |"), 2);
        // Escaped pipe inside code is also masked (escape is redundant here)
        assert_eq!(TableUtils::count_cells(r"| Hour formats | `^([0-1]?\d\|2[0-3])` |"), 2);
    }

    #[test]
    fn test_determine_pipe_style() {
        // All pipe styles
        assert_eq!(
            TableUtils::determine_pipe_style("| Cell 1 | Cell 2 |"),
            Some("leading_and_trailing")
        );
        assert_eq!(
            TableUtils::determine_pipe_style("| Cell 1 | Cell 2"),
            Some("leading_only")
        );
        assert_eq!(
            TableUtils::determine_pipe_style("Cell 1 | Cell 2 |"),
            Some("trailing_only")
        );
        assert_eq!(
            TableUtils::determine_pipe_style("Cell 1 | Cell 2"),
            Some("no_leading_or_trailing")
        );

        // With whitespace
        assert_eq!(
            TableUtils::determine_pipe_style("  | Cell 1 | Cell 2 |  "),
            Some("leading_and_trailing")
        );
        assert_eq!(
            TableUtils::determine_pipe_style("  | Cell 1 | Cell 2  "),
            Some("leading_only")
        );

        // No pipes
        assert_eq!(TableUtils::determine_pipe_style("Regular text"), None);
        assert_eq!(TableUtils::determine_pipe_style(""), None);
        assert_eq!(TableUtils::determine_pipe_style("   "), None);

        // Single pipe cases
        assert_eq!(TableUtils::determine_pipe_style("|"), Some("leading_and_trailing"));
        assert_eq!(TableUtils::determine_pipe_style("| Cell"), Some("leading_only"));
        assert_eq!(TableUtils::determine_pipe_style("Cell |"), Some("trailing_only"));
    }

    #[test]
    fn test_find_table_blocks_simple() {
        let content = "| Header 1 | Header 2 |
|-----------|-----------|
| Cell 1    | Cell 2    |
| Cell 3    | Cell 4    |";

        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);

        let tables = TableUtils::find_table_blocks(content, &ctx);
        assert_eq!(tables.len(), 1);

        let table = &tables[0];
        assert_eq!(table.start_line, 0);
        assert_eq!(table.end_line, 3);
        assert_eq!(table.header_line, 0);
        assert_eq!(table.delimiter_line, 1);
        assert_eq!(table.content_lines, vec![2, 3]);
    }

    #[test]
    fn test_find_table_blocks_multiple() {
        let content = "Some text

| Table 1 | Col A |
|----------|-------|
| Data 1   | Val 1 |

More text

| Table 2 | Col 2 |
|----------|-------|
| Data 2   | Data  |";

        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);

        let tables = TableUtils::find_table_blocks(content, &ctx);
        assert_eq!(tables.len(), 2);

        // First table
        assert_eq!(tables[0].start_line, 2);
        assert_eq!(tables[0].end_line, 4);
        assert_eq!(tables[0].header_line, 2);
        assert_eq!(tables[0].delimiter_line, 3);
        assert_eq!(tables[0].content_lines, vec![4]);

        // Second table
        assert_eq!(tables[1].start_line, 8);
        assert_eq!(tables[1].end_line, 10);
        assert_eq!(tables[1].header_line, 8);
        assert_eq!(tables[1].delimiter_line, 9);
        assert_eq!(tables[1].content_lines, vec![10]);
    }

    #[test]
    fn test_find_table_blocks_no_content_rows() {
        let content = "| Header 1 | Header 2 |
|-----------|-----------|

Next paragraph";

        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);

        let tables = TableUtils::find_table_blocks(content, &ctx);
        assert_eq!(tables.len(), 1);

        let table = &tables[0];
        assert_eq!(table.start_line, 0);
        assert_eq!(table.end_line, 1); // Just header and delimiter
        assert_eq!(table.content_lines.len(), 0);
    }

    #[test]
    fn test_find_table_blocks_in_code_block() {
        let content = "```
| Not | A | Table |
|-----|---|-------|
| In  | Code | Block |
```

| Real | Table |
|------|-------|
| Data | Here  |";

        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);

        let tables = TableUtils::find_table_blocks(content, &ctx);
        assert_eq!(tables.len(), 1); // Only the table outside code block

        let table = &tables[0];
        assert_eq!(table.header_line, 6);
        assert_eq!(table.delimiter_line, 7);
    }

    #[test]
    fn test_find_table_blocks_no_tables() {
        let content = "Just regular text
No tables here
- List item with | pipe
* Another list item";

        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);

        let tables = TableUtils::find_table_blocks(content, &ctx);
        assert_eq!(tables.len(), 0);
    }

    #[test]
    fn test_find_table_blocks_malformed() {
        let content = "| Header without delimiter |
| This looks like table |
But no delimiter row

| Proper | Table |
|---------|-------|
| Data    | Here  |";

        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);

        let tables = TableUtils::find_table_blocks(content, &ctx);
        assert_eq!(tables.len(), 1); // Only the proper table
        assert_eq!(tables[0].header_line, 4);
    }

    #[test]
    fn test_edge_cases() {
        // Test empty content
        assert!(!TableUtils::is_potential_table_row(""));
        assert!(!TableUtils::is_delimiter_row(""));
        assert_eq!(TableUtils::count_cells(""), 0);
        assert_eq!(TableUtils::determine_pipe_style(""), None);

        // Test whitespace only
        assert!(!TableUtils::is_potential_table_row("   "));
        assert!(!TableUtils::is_delimiter_row("   "));
        assert_eq!(TableUtils::count_cells("   "), 0);
        assert_eq!(TableUtils::determine_pipe_style("   "), None);

        // Test single character
        assert!(!TableUtils::is_potential_table_row("|"));
        assert!(!TableUtils::is_delimiter_row("|"));
        assert_eq!(TableUtils::count_cells("|"), 0); // Need at least 2 parts

        // Test very long lines are valid table rows (no length limit)
        // Test both single-column and multi-column long lines
        let long_single = format!("| {} |", "a".repeat(200));
        assert!(TableUtils::is_potential_table_row(&long_single)); // Single-column table with long content

        let long_multi = format!("| {} | {} |", "a".repeat(200), "b".repeat(200));
        assert!(TableUtils::is_potential_table_row(&long_multi)); // Multi-column table with long content

        // Test unicode
        assert!(TableUtils::is_potential_table_row("| 你好 | 世界 |"));
        assert!(TableUtils::is_potential_table_row("| émoji | 🎉 |"));
        assert_eq!(TableUtils::count_cells("| 你好 | 世界 |"), 2);
    }

    #[test]
    fn test_table_block_struct() {
        let block = TableBlock {
            start_line: 0,
            end_line: 5,
            header_line: 0,
            delimiter_line: 1,
            content_lines: vec![2, 3, 4, 5],
            list_context: None,
        };

        // Test Debug trait
        let debug_str = format!("{block:?}");
        assert!(debug_str.contains("TableBlock"));
        assert!(debug_str.contains("start_line: 0"));

        // Test Clone trait
        let cloned = block.clone();
        assert_eq!(cloned.start_line, block.start_line);
        assert_eq!(cloned.end_line, block.end_line);
        assert_eq!(cloned.header_line, block.header_line);
        assert_eq!(cloned.delimiter_line, block.delimiter_line);
        assert_eq!(cloned.content_lines, block.content_lines);
        assert!(cloned.list_context.is_none());
    }

    #[test]
    fn test_split_table_row() {
        // Basic split
        let cells = TableUtils::split_table_row("| Cell 1 | Cell 2 | Cell 3 |");
        assert_eq!(cells.len(), 3);
        assert_eq!(cells[0].trim(), "Cell 1");
        assert_eq!(cells[1].trim(), "Cell 2");
        assert_eq!(cells[2].trim(), "Cell 3");

        // Without trailing pipe
        let cells = TableUtils::split_table_row("| Cell 1 | Cell 2");
        assert_eq!(cells.len(), 2);

        // Empty cells
        let cells = TableUtils::split_table_row("| | | |");
        assert_eq!(cells.len(), 3);

        // Single cell
        let cells = TableUtils::split_table_row("| Cell |");
        assert_eq!(cells.len(), 1);
        assert_eq!(cells[0].trim(), "Cell");

        // No pipes
        let cells = TableUtils::split_table_row("No pipes here");
        assert_eq!(cells.len(), 0);
    }

    #[test]
    fn test_split_table_row_with_escaped_pipes() {
        // Escaped pipes should be preserved in cell content
        let cells = TableUtils::split_table_row(r"| A | B \| C |");
        assert_eq!(cells.len(), 2);
        assert!(cells[1].contains(r"\|"), "Escaped pipe should be in cell content");

        // Double backslash + pipe is NOT escaped
        let cells = TableUtils::split_table_row(r"| A | B \\| C |");
        assert_eq!(cells.len(), 3);
    }

    #[test]
    fn test_split_table_row_with_flavor_mkdocs() {
        // MkDocs flavor: pipes in inline code are NOT cell delimiters
        let cells =
            TableUtils::split_table_row_with_flavor("| Type | `x | y` |", crate::config::MarkdownFlavor::MkDocs);
        assert_eq!(cells.len(), 2);
        assert!(
            cells[1].contains("`x | y`"),
            "Inline code with pipe should be single cell in MkDocs flavor"
        );

        // Multiple pipes in inline code
        let cells =
            TableUtils::split_table_row_with_flavor("| Type | `a | b | c` |", crate::config::MarkdownFlavor::MkDocs);
        assert_eq!(cells.len(), 2);
        assert!(cells[1].contains("`a | b | c`"));
    }

    #[test]
    fn test_split_table_row_with_flavor_standard() {
        // Pipes in inline code are NOT cell delimiters for any flavor
        let cells =
            TableUtils::split_table_row_with_flavor("| Type | `x | y` |", crate::config::MarkdownFlavor::Standard);
        assert_eq!(
            cells.len(),
            2,
            "Pipes in code spans should not be cell delimiters, got {cells:?}"
        );
        assert!(
            cells[1].contains("`x | y`"),
            "Inline code with pipe should be single cell"
        );
    }

    // === extract_blockquote_prefix tests ===

    #[test]
    fn test_extract_blockquote_prefix_no_blockquote() {
        // Regular table row without blockquote
        let (prefix, content) = TableUtils::extract_blockquote_prefix("| H1 | H2 |");
        assert_eq!(prefix, "");
        assert_eq!(content, "| H1 | H2 |");
    }

    #[test]
    fn test_extract_blockquote_prefix_single_level() {
        // Single blockquote level
        let (prefix, content) = TableUtils::extract_blockquote_prefix("> | H1 | H2 |");
        assert_eq!(prefix, "> ");
        assert_eq!(content, "| H1 | H2 |");
    }

    #[test]
    fn test_extract_blockquote_prefix_double_level() {
        // Double blockquote level
        let (prefix, content) = TableUtils::extract_blockquote_prefix(">> | H1 | H2 |");
        assert_eq!(prefix, ">> ");
        assert_eq!(content, "| H1 | H2 |");
    }

    #[test]
    fn test_extract_blockquote_prefix_triple_level() {
        // Triple blockquote level
        let (prefix, content) = TableUtils::extract_blockquote_prefix(">>> | H1 | H2 |");
        assert_eq!(prefix, ">>> ");
        assert_eq!(content, "| H1 | H2 |");
    }

    #[test]
    fn test_extract_blockquote_prefix_with_spaces() {
        // Blockquote with spaces between markers
        let (prefix, content) = TableUtils::extract_blockquote_prefix("> > | H1 | H2 |");
        assert_eq!(prefix, "> > ");
        assert_eq!(content, "| H1 | H2 |");
    }

    #[test]
    fn test_extract_blockquote_prefix_indented() {
        // Indented blockquote
        let (prefix, content) = TableUtils::extract_blockquote_prefix("  > | H1 | H2 |");
        assert_eq!(prefix, "  > ");
        assert_eq!(content, "| H1 | H2 |");
    }

    #[test]
    fn test_extract_blockquote_prefix_no_space_after() {
        // Blockquote without space after marker
        let (prefix, content) = TableUtils::extract_blockquote_prefix(">| H1 | H2 |");
        assert_eq!(prefix, ">");
        assert_eq!(content, "| H1 | H2 |");
    }

    #[test]
    fn test_determine_pipe_style_in_blockquote() {
        // determine_pipe_style should handle blockquotes correctly
        assert_eq!(
            TableUtils::determine_pipe_style("> | H1 | H2 |"),
            Some("leading_and_trailing")
        );
        assert_eq!(
            TableUtils::determine_pipe_style("> H1 | H2"),
            Some("no_leading_or_trailing")
        );
        assert_eq!(
            TableUtils::determine_pipe_style(">> | H1 | H2 |"),
            Some("leading_and_trailing")
        );
        assert_eq!(TableUtils::determine_pipe_style(">>> | H1 | H2"), Some("leading_only"));
    }

    #[test]
    fn test_list_table_delimiter_requires_indentation() {
        // Test case: list item contains pipe, but delimiter line is at column 1
        // This should NOT be detected as a list table since the delimiter has no indentation.
        // The result is a non-list table starting at line 0 (the list item becomes the header)
        // but list_context should be None.
        let content = "- List item with | pipe\n|---|---|\n| Cell 1 | Cell 2 |";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let tables = TableUtils::find_table_blocks(content, &ctx);

        // The table will be detected starting at line 0, but crucially it should NOT have
        // list_context set, meaning it won't be treated as a list-table for column count purposes
        assert_eq!(tables.len(), 1, "Should find exactly one table");
        assert!(
            tables[0].list_context.is_none(),
            "Should NOT have list context since delimiter has no indentation"
        );
    }

    #[test]
    fn test_list_table_with_properly_indented_delimiter() {
        // Test case: list item with table header, delimiter properly indented
        // This SHOULD be detected as a list table
        let content = "- | Header 1 | Header 2 |\n  |----------|----------|\n  | Cell 1   | Cell 2   |";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let tables = TableUtils::find_table_blocks(content, &ctx);

        // Should find exactly one list-table starting at line 0
        assert_eq!(tables.len(), 1, "Should find exactly one table");
        assert_eq!(tables[0].start_line, 0, "Table should start at list item line");
        assert!(
            tables[0].list_context.is_some(),
            "Should be a list table since delimiter is properly indented"
        );
    }

    #[test]
    fn test_mask_pipes_in_inline_code_regular_backticks() {
        // Regular backtick code span: pipe should be masked
        let result = TableUtils::mask_pipes_in_inline_code("| `code | here` |");
        assert_eq!(result, "| `code _ here` |");
    }

    #[test]
    fn test_mask_pipes_in_inline_code_escaped_backtick_not_code_span() {
        // Escaped backtick (\`) is literal text, not a code span opener.
        // The pipe should NOT be masked.
        let result = TableUtils::mask_pipes_in_inline_code(r"| \`not code | still pipe\` |");
        assert_eq!(result, r"| \`not code | still pipe\` |");
    }

    #[test]
    fn test_mask_pipes_in_inline_code_escaped_backslash_then_backtick() {
        // Escaped backslash (\\) followed by backtick: the backtick IS a code span opener.
        // The pipe inside the code span SHOULD be masked.
        let result = TableUtils::mask_pipes_in_inline_code(r"| \\`real code | masked\\` |");
        // \\` = escaped backslash + real backtick (code span opener)
        // The pipe between the backticks should be masked
        assert_eq!(result, r"| \\`real code _ masked\\` |");
    }

    #[test]
    fn test_mask_pipes_in_inline_code_triple_backslash_before_backtick() {
        // Three backslashes before backtick: odd count means backtick is escaped
        let result = TableUtils::mask_pipes_in_inline_code(r"| \\\`not code | pipe\\\` |");
        assert_eq!(result, r"| \\\`not code | pipe\\\` |");
    }

    #[test]
    fn test_mask_pipes_in_inline_code_four_backslashes_before_backtick() {
        // Four backslashes before backtick: even count means backtick is a real delimiter
        let result = TableUtils::mask_pipes_in_inline_code(r"| \\\\`code | here\\\\` |");
        assert_eq!(result, r"| \\\\`code _ here\\\\` |");
    }

    #[test]
    fn test_mask_pipes_in_inline_code_no_backslash() {
        // No backslashes at all: standard behavior, pipe inside code span is masked
        let result = TableUtils::mask_pipes_in_inline_code("before `a | b` after");
        assert_eq!(result, "before `a _ b` after");
    }

    #[test]
    fn test_mask_pipes_in_inline_code_no_code_span() {
        // No backticks at all: nothing should be masked
        let result = TableUtils::mask_pipes_in_inline_code("| col1 | col2 |");
        assert_eq!(result, "| col1 | col2 |");
    }

    #[test]
    fn test_mask_pipes_in_inline_code_backslash_before_closing_backtick() {
        // Per CommonMark spec, backslash escapes do NOT work inside code spans.
        // Inside a code span, `\` is a literal character. So `foo\` is a valid
        // code span containing "foo\", and the closing backtick is NOT escaped.
        //
        // Input: | `foo\` | bar |
        // The code span is `foo\` (backtick opens, backslash is literal, backtick closes).
        // The pipe after the code span is a real delimiter, producing 2 cells.
        // The pipe inside the code span should be left alone (there isn't one here).
        let result = TableUtils::mask_pipes_in_inline_code(r"| `foo\` | bar |");
        // The backslash before closing backtick is literal inside the code span,
        // so the code span closes at that backtick. The pipe between cells is NOT masked.
        assert_eq!(result, r"| `foo\` | bar |");
    }

    #[test]
    fn test_mask_pipes_in_inline_code_backslash_literal_with_pipe_inside() {
        // Code span contains a backslash and a pipe: `a\|b`
        // The backslash is literal inside the code span (CommonMark spec).
        // The pipe is inside the code span, so it should be masked.
        let result = TableUtils::mask_pipes_in_inline_code(r"| `a\|b` | col2 |");
        assert_eq!(result, r"| `a\_b` | col2 |");
    }

    #[test]
    fn test_count_preceding_backslashes() {
        let chars: Vec<char> = r"abc\\\`def".chars().collect();
        // Position of backtick is at index 6 (a=0, b=1, c=2, \=3, \=4, \=5, `=6)
        assert_eq!(TableUtils::count_preceding_backslashes(&chars, 6), 3);

        let chars2: Vec<char> = r"abc\\`def".chars().collect();
        // Position of backtick is at index 5
        assert_eq!(TableUtils::count_preceding_backslashes(&chars2, 5), 2);

        let chars3: Vec<char> = "`def".chars().collect();
        // Position of backtick is at index 0 -- no preceding chars
        assert_eq!(TableUtils::count_preceding_backslashes(&chars3, 0), 0);
    }

    #[test]
    fn test_has_unescaped_pipe_backslash_literal_in_code_span() {
        // Per CommonMark: backslashes are literal inside code spans.
        // `foo\` is a complete code span, so the pipe after it is outside code.
        assert!(TableUtils::has_unescaped_pipe_outside_inline_code(r"`foo\` | bar"));

        // Escaped backtick outside code span: \` is not a code span opener
        assert!(TableUtils::has_unescaped_pipe_outside_inline_code(r"\`foo | bar\`"));

        // Pipe inside code span should not count
        assert!(!TableUtils::has_unescaped_pipe_outside_inline_code(r"`foo | bar`"));
    }

    #[test]
    fn test_table_after_code_span_detected() {
        use crate::config::MarkdownFlavor;

        let content = "`code`\n\n| A | B |\n|---|---|\n| 1 | 2 |\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        assert!(!ctx.table_blocks.is_empty(), "Table after code span should be detected");
    }

    #[test]
    fn test_table_inside_html_comment_not_detected() {
        use crate::config::MarkdownFlavor;

        let content = "<!--\n| A | B |\n|---|---|\n| 1 | 2 |\n-->\n";
        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
        assert!(
            ctx.table_blocks.is_empty(),
            "Table inside HTML comment should not be detected"
        );
    }
}