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
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
use crate::utils::fast_hash;
use crate::utils::regex_cache::{escape_regex, get_cached_regex};

use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, RuleCategory, Severity};
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex};

mod md044_config;
pub use md044_config::MD044Config;

type WarningPosition = (usize, usize, String); // (line, column, found_name)

/// Rule MD044: Proper names should be capitalized
///
/// See [docs/md044.md](../../docs/md044.md) for full documentation, configuration, and examples.
///
/// This rule is triggered when proper names are not capitalized correctly in the document.
/// For example, if you have defined "JavaScript" as a proper name, the rule will flag any
/// occurrences of "javascript" or "Javascript" as violations.
///
/// ## Purpose
///
/// Ensuring consistent capitalization of proper names improves document quality and
/// professionalism. This is especially important for technical documentation where
/// product names, programming languages, and technologies often have specific
/// capitalization conventions.
///
/// ## Configuration Options
///
/// The rule supports the following configuration options:
///
/// ```yaml
/// MD044:
///   names: []                # List of proper names to check for correct capitalization
///   code-blocks: false       # Whether to check code blocks (default: false)
/// ```
///
/// Example configuration:
///
/// ```yaml
/// MD044:
///   names: ["JavaScript", "Node.js", "TypeScript"]
///   code-blocks: true
/// ```
///
/// ## Performance Optimizations
///
/// This rule implements several performance optimizations:
///
/// 1. **Regex Caching**: Pre-compiles and caches regex patterns for each proper name
/// 2. **Content Caching**: Caches results based on content hashing for repeated checks
/// 3. **Efficient Text Processing**: Uses optimized algorithms to avoid redundant text processing
/// 4. **Smart Code Block Detection**: Efficiently identifies and optionally excludes code blocks
///
/// ## Edge Cases Handled
///
/// - **Word Boundaries**: Only matches complete words, not substrings within other words
/// - **Case Sensitivity**: Properly handles case-specific matching
/// - **Code Blocks**: Optionally checks code blocks (controlled by code-blocks setting)
/// - **Markdown Formatting**: Handles proper names within Markdown formatting elements
///
/// ## Fix Behavior
///
/// When fixing issues, this rule replaces incorrect capitalization with the correct form
/// as defined in the configuration.
///
/// Check if a trimmed line is an inline config comment from a linting tool.
/// Recognized tools: rumdl, markdownlint, Vale, and remark-lint.
fn is_inline_config_comment(trimmed: &str) -> bool {
    trimmed.starts_with("<!-- rumdl-")
        || trimmed.starts_with("<!-- markdownlint-")
        || trimmed.starts_with("<!-- vale off")
        || trimmed.starts_with("<!-- vale on")
        || (trimmed.starts_with("<!-- vale ") && trimmed.contains(" = "))
        || trimmed.starts_with("<!-- vale style")
        || trimmed.starts_with("<!-- lint disable ")
        || trimmed.starts_with("<!-- lint enable ")
        || trimmed.starts_with("<!-- lint ignore ")
}

#[derive(Clone)]
pub struct MD044ProperNames {
    config: MD044Config,
    // Cache the combined regex pattern string
    combined_pattern: Option<String>,
    // Precomputed lowercase name variants for fast pre-checks
    name_variants: Vec<String>,
    // Cache for name violations by content hash
    content_cache: Arc<Mutex<HashMap<u64, Vec<WarningPosition>>>>,
}

impl MD044ProperNames {
    pub fn new(names: Vec<String>, code_blocks: bool) -> Self {
        let config = MD044Config {
            names,
            code_blocks,
            html_elements: true, // Default to checking HTML elements
            html_comments: true, // Default to checking HTML comments
        };
        let combined_pattern = Self::create_combined_pattern(&config);
        let name_variants = Self::build_name_variants(&config);
        Self {
            config,
            combined_pattern,
            name_variants,
            content_cache: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    // Helper function for consistent ASCII normalization
    fn ascii_normalize(s: &str) -> String {
        s.replace(['é', 'è', 'ê', 'ë'], "e")
            .replace(['à', 'á', 'â', 'ä', 'ã', 'å'], "a")
            .replace(['ï', 'î', 'í', 'ì'], "i")
            .replace(['ü', 'ú', 'ù', 'û'], "u")
            .replace(['ö', 'ó', 'ò', 'ô', 'õ'], "o")
            .replace('ñ', "n")
            .replace('ç', "c")
    }

    pub fn from_config_struct(config: MD044Config) -> Self {
        let combined_pattern = Self::create_combined_pattern(&config);
        let name_variants = Self::build_name_variants(&config);
        Self {
            config,
            combined_pattern,
            name_variants,
            content_cache: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    // Create a combined regex pattern for all proper names
    fn create_combined_pattern(config: &MD044Config) -> Option<String> {
        if config.names.is_empty() {
            return None;
        }

        // Create patterns for all names and their variations
        let mut patterns: Vec<String> = config
            .names
            .iter()
            .flat_map(|name| {
                let mut variations = vec![];
                let lower_name = name.to_lowercase();

                // Add the lowercase version
                variations.push(escape_regex(&lower_name));

                // Add version without dots
                let lower_name_no_dots = lower_name.replace('.', "");
                if lower_name != lower_name_no_dots {
                    variations.push(escape_regex(&lower_name_no_dots));
                }

                // Add ASCII-normalized versions for common accented characters
                let ascii_normalized = Self::ascii_normalize(&lower_name);

                if ascii_normalized != lower_name {
                    variations.push(escape_regex(&ascii_normalized));

                    // Also add version without dots
                    let ascii_no_dots = ascii_normalized.replace('.', "");
                    if ascii_normalized != ascii_no_dots {
                        variations.push(escape_regex(&ascii_no_dots));
                    }
                }

                variations
            })
            .collect();

        // Sort patterns by length (longest first) to avoid shorter patterns matching within longer ones
        patterns.sort_by_key(|b| std::cmp::Reverse(b.len()));

        // Combine all patterns into a single regex with capture groups
        // Don't use \b as it doesn't work with Unicode - we'll check boundaries manually
        Some(format!(r"(?i)({})", patterns.join("|")))
    }

    fn build_name_variants(config: &MD044Config) -> Vec<String> {
        let mut variants = HashSet::new();
        for name in &config.names {
            let lower_name = name.to_lowercase();
            variants.insert(lower_name.clone());

            let lower_no_dots = lower_name.replace('.', "");
            if lower_name != lower_no_dots {
                variants.insert(lower_no_dots);
            }

            let ascii_normalized = Self::ascii_normalize(&lower_name);
            if ascii_normalized != lower_name {
                variants.insert(ascii_normalized.clone());

                let ascii_no_dots = ascii_normalized.replace('.', "");
                if ascii_normalized != ascii_no_dots {
                    variants.insert(ascii_no_dots);
                }
            }
        }

        variants.into_iter().collect()
    }

    // Find all name violations in the content and return positions.
    // `content_lower` is the pre-computed lowercase version of `content` to avoid redundant allocations.
    fn find_name_violations(
        &self,
        content: &str,
        ctx: &crate::lint_context::LintContext,
        content_lower: &str,
    ) -> Vec<WarningPosition> {
        // Early return: if no names configured or content is empty
        if self.config.names.is_empty() || content.is_empty() || self.combined_pattern.is_none() {
            return Vec::new();
        }

        // Early return: quick check if any of the configured names might be in content
        let has_potential_matches = self.name_variants.iter().any(|name| content_lower.contains(name));

        if !has_potential_matches {
            return Vec::new();
        }

        // Check if we have cached results
        let hash = fast_hash(content);
        {
            // Use a separate scope for borrowing to minimize lock time
            if let Ok(cache) = self.content_cache.lock()
                && let Some(cached) = cache.get(&hash)
            {
                return cached.clone();
            }
        }

        let mut violations = Vec::new();

        // Get the regex from global cache
        let combined_regex = match &self.combined_pattern {
            Some(pattern) => match get_cached_regex(pattern) {
                Ok(regex) => regex,
                Err(_) => return Vec::new(),
            },
            None => return Vec::new(),
        };

        // Use ctx.lines for better performance
        for (line_idx, line_info) in ctx.lines.iter().enumerate() {
            let line_num = line_idx + 1;
            let line = line_info.content(ctx.content);

            // Skip code fence lines (```language or ~~~language)
            let trimmed = line.trim_start();
            if trimmed.starts_with("```") || trimmed.starts_with("~~~") {
                continue;
            }

            // Skip if in code block (when code_blocks = false)
            if !self.config.code_blocks && line_info.in_code_block {
                continue;
            }

            // Skip if in HTML block (when html_elements = false)
            if !self.config.html_elements && line_info.in_html_block {
                continue;
            }

            // Skip HTML comments using pre-computed line flag
            if !self.config.html_comments && line_info.in_html_comment {
                continue;
            }

            // Skip JSX expressions and MDX comments (MDX flavor)
            if line_info.in_jsx_expression || line_info.in_mdx_comment {
                continue;
            }

            // Skip Obsidian comments (Obsidian flavor)
            if line_info.in_obsidian_comment {
                continue;
            }

            // For frontmatter lines, determine offset where checkable value content starts.
            // YAML keys should not be checked against proper names - only values.
            let fm_value_offset = if line_info.in_front_matter {
                Self::frontmatter_value_offset(line)
            } else {
                0
            };
            if fm_value_offset == usize::MAX {
                continue;
            }

            // Skip inline config comments (rumdl, markdownlint, Vale, remark-lint directives)
            if is_inline_config_comment(trimmed) {
                continue;
            }

            // Early return: skip lines that don't contain any potential matches
            let line_lower = line.to_lowercase();
            let has_line_matches = self.name_variants.iter().any(|name| line_lower.contains(name));

            if !has_line_matches {
                continue;
            }

            // Use the combined regex to find all matches in one pass
            for cap in combined_regex.find_iter(line) {
                let found_name = &line[cap.start()..cap.end()];

                // Check word boundaries manually for Unicode support
                let start_pos = cap.start();
                let end_pos = cap.end();

                // Skip matches in the key portion of frontmatter lines
                if start_pos < fm_value_offset {
                    continue;
                }

                // Skip matches inside HTML tag attributes (handles multi-line tags)
                let byte_pos = line_info.byte_offset + start_pos;
                if ctx.is_in_html_tag(byte_pos) {
                    continue;
                }

                if !Self::is_at_word_boundary(line, start_pos, true) || !Self::is_at_word_boundary(line, end_pos, false)
                {
                    continue; // Not at word boundary
                }

                // Skip if in inline code when code_blocks is false
                if !self.config.code_blocks {
                    if ctx.is_in_code_block_or_span(byte_pos) {
                        continue;
                    }
                    // pulldown-cmark doesn't parse markdown syntax inside HTML
                    // comments, HTML blocks, or frontmatter, so backtick-wrapped
                    // text isn't detected by is_in_code_block_or_span. Check directly.
                    if (line_info.in_html_comment || line_info.in_html_block || line_info.in_front_matter)
                        && Self::is_in_backtick_code_in_line(line, start_pos)
                    {
                        continue;
                    }
                }

                // Skip if in link URL or reference definition
                if Self::is_in_link(ctx, byte_pos) {
                    continue;
                }

                // Skip if inside an angle-bracket URL (e.g., <https://...>)
                // The link parser skips autolinks inside HTML comments,
                // so we detect them directly in the line text.
                if Self::is_in_angle_bracket_url(line, start_pos) {
                    continue;
                }

                // Find which proper name this matches
                if let Some(proper_name) = self.get_proper_name_for(found_name) {
                    // Only flag if it's not already correct
                    if found_name != proper_name {
                        violations.push((line_num, cap.start() + 1, found_name.to_string()));
                    }
                }
            }
        }

        // Store in cache (ignore if mutex is poisoned)
        if let Ok(mut cache) = self.content_cache.lock() {
            cache.insert(hash, violations.clone());
        }
        violations
    }

    /// Check if a byte position is within a link URL (not link text)
    ///
    /// Link text should be checked for proper names, but URLs should be skipped.
    /// For `[text](url)` - check text, skip url
    /// For `[text][ref]` - check text, skip reference portion
    /// For `[[text]]` (WikiLinks) - check text, skip brackets
    fn is_in_link(ctx: &crate::lint_context::LintContext, byte_pos: usize) -> bool {
        use pulldown_cmark::LinkType;

        // Binary search links (sorted by byte_offset) to find candidate containing byte_pos
        let link_idx = ctx.links.partition_point(|link| link.byte_offset <= byte_pos);
        if link_idx > 0 {
            let link = &ctx.links[link_idx - 1];
            if byte_pos < link.byte_end {
                // WikiLinks [[text]] start with '[[', regular links [text] start with '['
                let text_start = if matches!(link.link_type, LinkType::WikiLink { .. }) {
                    link.byte_offset + 2
                } else {
                    link.byte_offset + 1
                };
                let text_end = text_start + link.text.len();

                // If position is within the text portion, skip only if text is a URL
                if byte_pos >= text_start && byte_pos < text_end {
                    return Self::link_text_is_url(&link.text);
                }
                // Position is in the URL/reference portion, skip it
                return true;
            }
        }

        // Binary search images (sorted by byte_offset) to find candidate containing byte_pos
        let image_idx = ctx.images.partition_point(|img| img.byte_offset <= byte_pos);
        if image_idx > 0 {
            let image = &ctx.images[image_idx - 1];
            if byte_pos < image.byte_end {
                // Image starts with '![' so alt text starts at byte_offset + 2
                let alt_start = image.byte_offset + 2;
                let alt_end = alt_start + image.alt_text.len();

                // If position is within the alt text portion, don't skip
                if byte_pos >= alt_start && byte_pos < alt_end {
                    return false;
                }
                // Position is in the URL/reference portion, skip it
                return true;
            }
        }

        // Check pre-computed reference definitions
        ctx.is_in_reference_def(byte_pos)
    }

    /// Check if link text is a URL that should not have proper name corrections.
    /// Matches markdownlint behavior: skip text starting with `http://`, `https://`, or `www.`.
    fn link_text_is_url(text: &str) -> bool {
        let lower = text.trim().to_ascii_lowercase();
        lower.starts_with("http://") || lower.starts_with("https://") || lower.starts_with("www.")
    }

    /// Check if a position within a line falls inside an angle-bracket URL (`<scheme://...>`).
    ///
    /// The link parser skips autolinks inside HTML comments, so `ctx.links` won't
    /// contain them. This function detects angle-bracket URLs directly in the line
    /// text, covering both HTML comments and regular text as a safety net.
    fn is_in_angle_bracket_url(line: &str, pos: usize) -> bool {
        let bytes = line.as_bytes();
        let len = bytes.len();
        let mut i = 0;
        while i < len {
            if bytes[i] == b'<' {
                let after_open = i + 1;
                // Check for a valid URI scheme per CommonMark autolink spec:
                // scheme = [a-zA-Z][a-zA-Z0-9+.-]{0,31}
                // followed by ':'
                if after_open < len && bytes[after_open].is_ascii_alphabetic() {
                    let mut s = after_open + 1;
                    let scheme_max = (after_open + 32).min(len);
                    while s < scheme_max
                        && (bytes[s].is_ascii_alphanumeric()
                            || bytes[s] == b'+'
                            || bytes[s] == b'-'
                            || bytes[s] == b'.')
                    {
                        s += 1;
                    }
                    if s < len && bytes[s] == b':' {
                        // Valid scheme found; scan for closing '>' with no spaces or '<'
                        let mut j = s + 1;
                        let mut found_close = false;
                        while j < len {
                            match bytes[j] {
                                b'>' => {
                                    found_close = true;
                                    break;
                                }
                                b' ' | b'<' => break,
                                _ => j += 1,
                            }
                        }
                        if found_close && pos >= i && pos <= j {
                            return true;
                        }
                        if found_close {
                            i = j + 1;
                            continue;
                        }
                    }
                }
            }
            i += 1;
        }
        false
    }

    /// Check if a position within a line falls inside backtick-delimited code.
    ///
    /// pulldown-cmark does not parse markdown syntax inside HTML comments, so
    /// `ctx.is_in_code_block_or_span` returns false for backtick-wrapped text
    /// within comments. This function detects backtick code spans directly in
    /// the line text following CommonMark rules: a code span starts with N
    /// backticks and ends with exactly N backticks.
    fn is_in_backtick_code_in_line(line: &str, pos: usize) -> bool {
        let bytes = line.as_bytes();
        let len = bytes.len();
        let mut i = 0;
        while i < len {
            if bytes[i] == b'`' {
                // Count the opening backtick sequence length
                let open_start = i;
                while i < len && bytes[i] == b'`' {
                    i += 1;
                }
                let tick_len = i - open_start;

                // Scan forward for a closing sequence of exactly tick_len backticks
                while i < len {
                    if bytes[i] == b'`' {
                        let close_start = i;
                        while i < len && bytes[i] == b'`' {
                            i += 1;
                        }
                        if i - close_start == tick_len {
                            // Matched pair found; the code span content is between
                            // the end of the opening backticks and the start of the
                            // closing backticks (exclusive of the backticks themselves).
                            let content_start = open_start + tick_len;
                            let content_end = close_start;
                            if pos >= content_start && pos < content_end {
                                return true;
                            }
                            // Continue scanning after this pair
                            break;
                        }
                        // Not the right length; keep scanning
                    } else {
                        i += 1;
                    }
                }
            } else {
                i += 1;
            }
        }
        false
    }

    // Check if a character is a word boundary (handles Unicode)
    fn is_word_boundary_char(c: char) -> bool {
        !c.is_alphanumeric()
    }

    // Check if position is at a word boundary using byte-level lookups.
    fn is_at_word_boundary(content: &str, pos: usize, is_start: bool) -> bool {
        if is_start {
            if pos == 0 {
                return true;
            }
            match content[..pos].chars().next_back() {
                None => true,
                Some(c) => Self::is_word_boundary_char(c),
            }
        } else {
            if pos >= content.len() {
                return true;
            }
            match content[pos..].chars().next() {
                None => true,
                Some(c) => Self::is_word_boundary_char(c),
            }
        }
    }

    /// For a frontmatter line, return the byte offset where the checkable
    /// value portion starts. Returns `usize::MAX` if the entire line should be
    /// skipped (frontmatter delimiters, key-only lines, YAML comments, flow constructs).
    fn frontmatter_value_offset(line: &str) -> usize {
        let trimmed = line.trim();

        // Skip frontmatter delimiters and empty lines
        if trimmed == "---" || trimmed == "+++" || trimmed.is_empty() {
            return usize::MAX;
        }

        // Skip YAML comments
        if trimmed.starts_with('#') {
            return usize::MAX;
        }

        // YAML list item: "  - item" or "  - key: value"
        let stripped = line.trim_start();
        if let Some(after_dash) = stripped.strip_prefix("- ") {
            let leading = line.len() - stripped.len();
            // Check if the list item contains a mapping (e.g., "- key: value")
            if let Some(result) = Self::kv_value_offset(line, after_dash, leading + 2) {
                return result;
            }
            // Bare list item value (no colon) - check content after "- "
            return leading + 2;
        }
        if stripped == "-" {
            return usize::MAX;
        }

        // Key-value pair with colon separator (YAML): "key: value"
        if let Some(result) = Self::kv_value_offset(line, stripped, line.len() - stripped.len()) {
            return result;
        }

        // Key-value pair with equals separator (TOML): "key = value"
        if let Some(eq_pos) = line.find('=') {
            let after_eq = eq_pos + 1;
            if after_eq < line.len() && line.as_bytes()[after_eq] == b' ' {
                let value_start = after_eq + 1;
                let value_slice = &line[value_start..];
                let value_trimmed = value_slice.trim();
                if value_trimmed.is_empty() {
                    return usize::MAX;
                }
                // For quoted values, skip the opening quote character
                if (value_trimmed.starts_with('"') && value_trimmed.ends_with('"'))
                    || (value_trimmed.starts_with('\'') && value_trimmed.ends_with('\''))
                {
                    let quote_offset = value_slice.find(['"', '\'']).unwrap_or(0);
                    return value_start + quote_offset + 1;
                }
                return value_start;
            }
            // Equals with no space after or at end of line -> no value to check
            return usize::MAX;
        }

        // No separator found - continuation line or bare value, check the whole line
        0
    }

    /// Parse a key-value pair using colon separator within `content` that starts
    /// at `base_offset` in the original line. Returns `Some(offset)` if a colon
    /// separator is found, `None` if no colon is present.
    fn kv_value_offset(line: &str, content: &str, base_offset: usize) -> Option<usize> {
        let colon_pos = content.find(':')?;
        let abs_colon = base_offset + colon_pos;
        let after_colon = abs_colon + 1;
        if after_colon < line.len() && line.as_bytes()[after_colon] == b' ' {
            let value_start = after_colon + 1;
            let value_slice = &line[value_start..];
            let value_trimmed = value_slice.trim();
            if value_trimmed.is_empty() {
                return Some(usize::MAX);
            }
            // Skip flow mappings and flow sequences - too complex for heuristic parsing
            if value_trimmed.starts_with('{') || value_trimmed.starts_with('[') {
                return Some(usize::MAX);
            }
            // For quoted values, skip the opening quote character
            if (value_trimmed.starts_with('"') && value_trimmed.ends_with('"'))
                || (value_trimmed.starts_with('\'') && value_trimmed.ends_with('\''))
            {
                let quote_offset = value_slice.find(['"', '\'']).unwrap_or(0);
                return Some(value_start + quote_offset + 1);
            }
            return Some(value_start);
        }
        // Colon with no space after or at end of line -> no value to check
        Some(usize::MAX)
    }

    // Get the proper name that should be used for a found name
    fn get_proper_name_for(&self, found_name: &str) -> Option<String> {
        let found_lower = found_name.to_lowercase();

        // Iterate through the configured proper names
        for name in &self.config.names {
            let lower_name = name.to_lowercase();
            let lower_name_no_dots = lower_name.replace('.', "");

            // Direct match
            if found_lower == lower_name || found_lower == lower_name_no_dots {
                return Some(name.clone());
            }

            // Check ASCII-normalized version
            let ascii_normalized = Self::ascii_normalize(&lower_name);

            let ascii_no_dots = ascii_normalized.replace('.', "");

            if found_lower == ascii_normalized || found_lower == ascii_no_dots {
                return Some(name.clone());
            }
        }
        None
    }
}

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

    fn description(&self) -> &'static str {
        "Proper names should have the correct capitalization"
    }

    fn category(&self) -> RuleCategory {
        RuleCategory::Other
    }

    fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
        if self.config.names.is_empty() {
            return true;
        }
        // Quick check if any configured name variants exist (case-insensitive)
        let content_lower = if ctx.content.is_ascii() {
            ctx.content.to_ascii_lowercase()
        } else {
            ctx.content.to_lowercase()
        };
        !self.name_variants.iter().any(|name| content_lower.contains(name))
    }

    fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
        let content = ctx.content;
        if content.is_empty() || self.config.names.is_empty() || self.combined_pattern.is_none() {
            return Ok(Vec::new());
        }

        // Compute lowercase content once and reuse across all checks
        let content_lower = if content.is_ascii() {
            content.to_ascii_lowercase()
        } else {
            content.to_lowercase()
        };

        // Early return: use pre-computed name_variants for the quick check
        let has_potential_matches = self.name_variants.iter().any(|name| content_lower.contains(name));

        if !has_potential_matches {
            return Ok(Vec::new());
        }

        let line_index = &ctx.line_index;
        let violations = self.find_name_violations(content, ctx, &content_lower);

        let warnings = violations
            .into_iter()
            .filter_map(|(line, column, found_name)| {
                self.get_proper_name_for(&found_name).map(|proper_name| LintWarning {
                    rule_name: Some(self.name().to_string()),
                    line,
                    column,
                    end_line: line,
                    end_column: column + found_name.len(),
                    message: format!("Proper name '{found_name}' should be '{proper_name}'"),
                    severity: Severity::Warning,
                    fix: Some(Fix {
                        range: line_index.line_col_to_byte_range_with_length(line, column, found_name.len()),
                        replacement: proper_name,
                    }),
                })
            })
            .collect();

        Ok(warnings)
    }

    fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
        let content = ctx.content;
        if content.is_empty() || self.config.names.is_empty() {
            return Ok(content.to_string());
        }

        let content_lower = if content.is_ascii() {
            content.to_ascii_lowercase()
        } else {
            content.to_lowercase()
        };
        let violations = self.find_name_violations(content, ctx, &content_lower);
        if violations.is_empty() {
            return Ok(content.to_string());
        }

        // Process lines and build the fixed content
        let mut fixed_lines = Vec::new();

        // Group violations by line
        let mut violations_by_line: HashMap<usize, Vec<(usize, String)>> = HashMap::new();
        for (line_num, col_num, found_name) in violations {
            violations_by_line
                .entry(line_num)
                .or_default()
                .push((col_num, found_name));
        }

        // Sort violations within each line in reverse order
        for violations in violations_by_line.values_mut() {
            violations.sort_by_key(|b| std::cmp::Reverse(b.0));
        }

        // Process each line
        for (line_idx, line_info) in ctx.lines.iter().enumerate() {
            let line_num = line_idx + 1;

            // Skip lines where this rule is disabled by inline config
            if ctx.inline_config().is_rule_disabled(self.name(), line_num) {
                fixed_lines.push(line_info.content(ctx.content).to_string());
                continue;
            }

            if let Some(line_violations) = violations_by_line.get(&line_num) {
                // This line has violations, fix them
                let mut fixed_line = line_info.content(ctx.content).to_string();

                for (col_num, found_name) in line_violations {
                    if let Some(proper_name) = self.get_proper_name_for(found_name) {
                        let start_col = col_num - 1; // Convert to 0-based
                        let end_col = start_col + found_name.len();

                        if end_col <= fixed_line.len()
                            && fixed_line.is_char_boundary(start_col)
                            && fixed_line.is_char_boundary(end_col)
                        {
                            fixed_line.replace_range(start_col..end_col, &proper_name);
                        }
                    }
                }

                fixed_lines.push(fixed_line);
            } else {
                // No violations on this line, keep it as is
                fixed_lines.push(line_info.content(ctx.content).to_string());
            }
        }

        // Join lines with newlines, preserving the original ending
        let mut result = fixed_lines.join("\n");
        if content.ends_with('\n') && !result.ends_with('\n') {
            result.push('\n');
        }
        Ok(result)
    }

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

    fn default_config_section(&self) -> Option<(String, toml::Value)> {
        let json_value = serde_json::to_value(&self.config).ok()?;
        Some((
            self.name().to_string(),
            crate::rule_config_serde::json_to_toml_value(&json_value)?,
        ))
    }

    fn from_config(config: &crate::config::Config) -> Box<dyn Rule>
    where
        Self: Sized,
    {
        let rule_config = crate::rule_config_serde::load_rule_config::<MD044Config>(config);
        Box::new(Self::from_config_struct(rule_config))
    }
}

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

    fn create_context(content: &str) -> LintContext<'_> {
        LintContext::new(content, crate::config::MarkdownFlavor::Standard, None)
    }

    #[test]
    fn test_correctly_capitalized_names() {
        let rule = MD044ProperNames::new(
            vec![
                "JavaScript".to_string(),
                "TypeScript".to_string(),
                "Node.js".to_string(),
            ],
            true,
        );

        let content = "This document uses JavaScript, TypeScript, and Node.js correctly.";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty(), "Should not flag correctly capitalized names");
    }

    #[test]
    fn test_incorrectly_capitalized_names() {
        let rule = MD044ProperNames::new(vec!["JavaScript".to_string(), "TypeScript".to_string()], true);

        let content = "This document uses javascript and typescript incorrectly.";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 2, "Should flag two incorrect capitalizations");
        assert_eq!(result[0].message, "Proper name 'javascript' should be 'JavaScript'");
        assert_eq!(result[0].line, 1);
        assert_eq!(result[0].column, 20);
        assert_eq!(result[1].message, "Proper name 'typescript' should be 'TypeScript'");
        assert_eq!(result[1].line, 1);
        assert_eq!(result[1].column, 35);
    }

    #[test]
    fn test_names_at_beginning_of_sentences() {
        let rule = MD044ProperNames::new(vec!["JavaScript".to_string(), "Python".to_string()], true);

        let content = "javascript is a great language. python is also popular.";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 2, "Should flag names at beginning of sentences");
        assert_eq!(result[0].line, 1);
        assert_eq!(result[0].column, 1);
        assert_eq!(result[1].line, 1);
        assert_eq!(result[1].column, 33);
    }

    #[test]
    fn test_names_in_code_blocks_checked_by_default() {
        let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);

        let content = r#"Here is some text with JavaScript.

```javascript
// This javascript should be checked
const lang = "javascript";
```

But this javascript should be flagged."#;

        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 3, "Should flag javascript inside and outside code blocks");
        assert_eq!(result[0].line, 4);
        assert_eq!(result[1].line, 5);
        assert_eq!(result[2].line, 8);
    }

    #[test]
    fn test_names_in_code_blocks_ignored_when_disabled() {
        let rule = MD044ProperNames::new(
            vec!["JavaScript".to_string()],
            false, // code_blocks = false means skip code blocks
        );

        let content = r#"```
javascript in code block
```"#;

        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(
            result.len(),
            0,
            "Should not flag javascript in code blocks when code_blocks is false"
        );
    }

    #[test]
    fn test_names_in_inline_code_checked_by_default() {
        let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);

        let content = "This is `javascript` in inline code and javascript outside.";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // When code_blocks=true, inline code should be checked
        assert_eq!(result.len(), 2, "Should flag javascript inside and outside inline code");
        assert_eq!(result[0].column, 10); // javascript in inline code
        assert_eq!(result[1].column, 41); // javascript outside
    }

    #[test]
    fn test_multiple_names_in_same_line() {
        let rule = MD044ProperNames::new(
            vec!["JavaScript".to_string(), "TypeScript".to_string(), "React".to_string()],
            true,
        );

        let content = "I use javascript, typescript, and react in my projects.";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 3, "Should flag all three incorrect names");
        assert_eq!(result[0].message, "Proper name 'javascript' should be 'JavaScript'");
        assert_eq!(result[1].message, "Proper name 'typescript' should be 'TypeScript'");
        assert_eq!(result[2].message, "Proper name 'react' should be 'React'");
    }

    #[test]
    fn test_case_sensitivity() {
        let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);

        let content = "JAVASCRIPT, Javascript, javascript, and JavaScript variations.";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 3, "Should flag all incorrect case variations");
        // JavaScript (correct) should not be flagged
        assert!(result.iter().all(|w| w.message.contains("should be 'JavaScript'")));
    }

    #[test]
    fn test_configuration_with_custom_name_list() {
        let config = MD044Config {
            names: vec!["GitHub".to_string(), "GitLab".to_string(), "DevOps".to_string()],
            code_blocks: true,
            html_elements: true,
            html_comments: true,
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = "We use github, gitlab, and devops for our workflow.";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 3, "Should flag all custom names");
        assert_eq!(result[0].message, "Proper name 'github' should be 'GitHub'");
        assert_eq!(result[1].message, "Proper name 'gitlab' should be 'GitLab'");
        assert_eq!(result[2].message, "Proper name 'devops' should be 'DevOps'");
    }

    #[test]
    fn test_empty_configuration() {
        let rule = MD044ProperNames::new(vec![], true);

        let content = "This has javascript and typescript but no configured names.";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert!(result.is_empty(), "Should not flag anything with empty configuration");
    }

    #[test]
    fn test_names_with_special_characters() {
        let rule = MD044ProperNames::new(
            vec!["Node.js".to_string(), "ASP.NET".to_string(), "C++".to_string()],
            true,
        );

        let content = "We use nodejs, asp.net, ASP.NET, and c++ in our stack.";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // nodejs should match Node.js (dotless variation)
        // asp.net should be flagged (wrong case)
        // ASP.NET should not be flagged (correct)
        // c++ should be flagged
        assert_eq!(result.len(), 3, "Should handle special characters correctly");

        let messages: Vec<&str> = result.iter().map(|w| w.message.as_str()).collect();
        assert!(messages.contains(&"Proper name 'nodejs' should be 'Node.js'"));
        assert!(messages.contains(&"Proper name 'asp.net' should be 'ASP.NET'"));
        assert!(messages.contains(&"Proper name 'c++' should be 'C++'"));
    }

    #[test]
    fn test_word_boundaries() {
        let rule = MD044ProperNames::new(vec!["Java".to_string(), "Script".to_string()], true);

        let content = "JavaScript is not java or script, but Java and Script are separate.";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // Should only flag lowercase "java" and "script" as separate words
        assert_eq!(result.len(), 2, "Should respect word boundaries");
        assert!(result.iter().any(|w| w.column == 19)); // "java" position
        assert!(result.iter().any(|w| w.column == 27)); // "script" position
    }

    #[test]
    fn test_fix_method() {
        let rule = MD044ProperNames::new(
            vec![
                "JavaScript".to_string(),
                "TypeScript".to_string(),
                "Node.js".to_string(),
            ],
            true,
        );

        let content = "I love javascript, typescript, and nodejs!";
        let ctx = create_context(content);
        let fixed = rule.fix(&ctx).unwrap();

        assert_eq!(fixed, "I love JavaScript, TypeScript, and Node.js!");
    }

    #[test]
    fn test_fix_multiple_occurrences() {
        let rule = MD044ProperNames::new(vec!["Python".to_string()], true);

        let content = "python is great. I use python daily. PYTHON is powerful.";
        let ctx = create_context(content);
        let fixed = rule.fix(&ctx).unwrap();

        assert_eq!(fixed, "Python is great. I use Python daily. Python is powerful.");
    }

    #[test]
    fn test_fix_checks_code_blocks_by_default() {
        let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);

        let content = r#"I love javascript.

```
const lang = "javascript";
```

More javascript here."#;

        let ctx = create_context(content);
        let fixed = rule.fix(&ctx).unwrap();

        let expected = r#"I love JavaScript.

```
const lang = "JavaScript";
```

More JavaScript here."#;

        assert_eq!(fixed, expected);
    }

    #[test]
    fn test_multiline_content() {
        let rule = MD044ProperNames::new(vec!["Rust".to_string(), "Python".to_string()], true);

        let content = r#"First line with rust.
Second line with python.
Third line with RUST and PYTHON."#;

        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 4, "Should flag all incorrect occurrences");
        assert_eq!(result[0].line, 1);
        assert_eq!(result[1].line, 2);
        assert_eq!(result[2].line, 3);
        assert_eq!(result[3].line, 3);
    }

    #[test]
    fn test_default_config() {
        let config = MD044Config::default();
        assert!(config.names.is_empty());
        assert!(!config.code_blocks);
        assert!(config.html_elements);
        assert!(config.html_comments);
    }

    #[test]
    fn test_default_config_checks_html_comments() {
        let config = MD044Config {
            names: vec!["JavaScript".to_string()],
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = "# Guide\n\n<!-- javascript mentioned here -->\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 1, "Default config should check HTML comments");
        assert_eq!(result[0].line, 3);
    }

    #[test]
    fn test_default_config_skips_code_blocks() {
        let config = MD044Config {
            names: vec!["JavaScript".to_string()],
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = "# Guide\n\n```\njavascript in code\n```\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 0, "Default config should skip code blocks");
    }

    #[test]
    fn test_standalone_html_comment_checked() {
        let config = MD044Config {
            names: vec!["Test".to_string()],
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = "# Heading\n\n<!-- this is a test example -->\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 1, "Should flag proper name in standalone HTML comment");
        assert_eq!(result[0].line, 3);
    }

    #[test]
    fn test_inline_config_comments_not_flagged() {
        let config = MD044Config {
            names: vec!["RUMDL".to_string()],
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        // Lines 1, 3, 4, 6 are inline config comments — should not be flagged.
        // Lines 2, 5 contain "rumdl" in regular text — flagged by rule.check(),
        // but would be suppressed by the linting engine's inline config filtering.
        let content = "<!-- rumdl-disable MD044 -->\nSome rumdl text here.\n<!-- rumdl-enable MD044 -->\n<!-- markdownlint-disable -->\nMore rumdl text.\n<!-- markdownlint-enable -->\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 2, "Should only flag body lines, not config comments");
        assert_eq!(result[0].line, 2);
        assert_eq!(result[1].line, 5);
    }

    #[test]
    fn test_html_comment_skipped_when_disabled() {
        let config = MD044Config {
            names: vec!["Test".to_string()],
            code_blocks: true,
            html_elements: true,
            html_comments: false,
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = "# Heading\n\n<!-- this is a test example -->\n\nRegular test here.\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(
            result.len(),
            1,
            "Should only flag 'test' outside HTML comment when html_comments=false"
        );
        assert_eq!(result[0].line, 5);
    }

    #[test]
    fn test_fix_corrects_html_comment_content() {
        let config = MD044Config {
            names: vec!["JavaScript".to_string()],
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = "# Guide\n\n<!-- javascript mentioned here -->\n";
        let ctx = create_context(content);
        let fixed = rule.fix(&ctx).unwrap();

        assert_eq!(fixed, "# Guide\n\n<!-- JavaScript mentioned here -->\n");
    }

    #[test]
    fn test_fix_does_not_modify_inline_config_comments() {
        let config = MD044Config {
            names: vec!["RUMDL".to_string()],
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = "<!-- rumdl-disable -->\nSome rumdl text.\n<!-- rumdl-enable -->\n";
        let ctx = create_context(content);
        let fixed = rule.fix(&ctx).unwrap();

        // Config comments should be untouched
        assert!(fixed.contains("<!-- rumdl-disable -->"));
        assert!(fixed.contains("<!-- rumdl-enable -->"));
        // Body text inside disable block should NOT be fixed (rule is disabled)
        assert!(
            fixed.contains("Some rumdl text."),
            "Line inside rumdl-disable block should not be modified by fix()"
        );
    }

    #[test]
    fn test_fix_respects_inline_disable_partial() {
        let config = MD044Config {
            names: vec!["RUMDL".to_string()],
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content =
            "<!-- rumdl-disable MD044 -->\nSome rumdl text.\n<!-- rumdl-enable MD044 -->\n\nSome rumdl text outside.\n";
        let ctx = create_context(content);
        let fixed = rule.fix(&ctx).unwrap();

        // Line inside disable block should be preserved
        assert!(
            fixed.contains("Some rumdl text.\n<!-- rumdl-enable"),
            "Line inside disable block should not be modified"
        );
        // Line outside disable block should be fixed
        assert!(
            fixed.contains("Some RUMDL text outside."),
            "Line outside disable block should be fixed"
        );
    }

    #[test]
    fn test_performance_with_many_names() {
        let mut names = vec![];
        for i in 0..50 {
            names.push(format!("ProperName{i}"));
        }

        let rule = MD044ProperNames::new(names, true);

        let content = "This has propername0, propername25, and propername49 incorrectly.";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 3, "Should handle many configured names efficiently");
    }

    #[test]
    fn test_large_name_count_performance() {
        // Verify MD044 can handle large numbers of names without regex limitations
        // This test confirms that fancy-regex handles large patterns well
        let names = (0..1000).map(|i| format!("ProperName{i}")).collect::<Vec<_>>();

        let rule = MD044ProperNames::new(names, true);

        // The combined pattern should be created successfully
        assert!(rule.combined_pattern.is_some());

        // Should be able to check content without errors
        let content = "This has propername0 and propername999 in it.";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // Should detect both incorrect names
        assert_eq!(result.len(), 2, "Should handle 1000 names without issues");
    }

    #[test]
    fn test_cache_behavior() {
        let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);

        let content = "Using javascript here.";
        let ctx = create_context(content);

        // First check
        let result1 = rule.check(&ctx).unwrap();
        assert_eq!(result1.len(), 1);

        // Second check should use cache
        let result2 = rule.check(&ctx).unwrap();
        assert_eq!(result2.len(), 1);

        // Results should be identical
        assert_eq!(result1[0].line, result2[0].line);
        assert_eq!(result1[0].column, result2[0].column);
    }

    #[test]
    fn test_html_comments_not_checked_when_disabled() {
        let config = MD044Config {
            names: vec!["JavaScript".to_string()],
            code_blocks: true,    // Check code blocks
            html_elements: true,  // Check HTML elements
            html_comments: false, // Don't check HTML comments
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = r#"Regular javascript here.
<!-- This javascript in HTML comment should be ignored -->
More javascript outside."#;

        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 2, "Should only flag javascript outside HTML comments");
        assert_eq!(result[0].line, 1);
        assert_eq!(result[1].line, 3);
    }

    #[test]
    fn test_html_comments_checked_when_enabled() {
        let config = MD044Config {
            names: vec!["JavaScript".to_string()],
            code_blocks: true,   // Check code blocks
            html_elements: true, // Check HTML elements
            html_comments: true, // Check HTML comments
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = r#"Regular javascript here.
<!-- This javascript in HTML comment should be checked -->
More javascript outside."#;

        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(
            result.len(),
            3,
            "Should flag all javascript occurrences including in HTML comments"
        );
    }

    #[test]
    fn test_multiline_html_comments() {
        let config = MD044Config {
            names: vec!["Python".to_string(), "JavaScript".to_string()],
            code_blocks: true,    // Check code blocks
            html_elements: true,  // Check HTML elements
            html_comments: false, // Don't check HTML comments
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = r#"Regular python here.
<!--
This is a multiline comment
with javascript and python
that should be ignored
-->
More javascript outside."#;

        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 2, "Should only flag names outside HTML comments");
        assert_eq!(result[0].line, 1); // python
        assert_eq!(result[1].line, 7); // javascript
    }

    #[test]
    fn test_fix_preserves_html_comments_when_disabled() {
        let config = MD044Config {
            names: vec!["JavaScript".to_string()],
            code_blocks: true,    // Check code blocks
            html_elements: true,  // Check HTML elements
            html_comments: false, // Don't check HTML comments
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = r#"javascript here.
<!-- javascript in comment -->
More javascript."#;

        let ctx = create_context(content);
        let fixed = rule.fix(&ctx).unwrap();

        let expected = r#"JavaScript here.
<!-- javascript in comment -->
More JavaScript."#;

        assert_eq!(
            fixed, expected,
            "Should not fix names inside HTML comments when disabled"
        );
    }

    #[test]
    fn test_proper_names_in_link_text_are_flagged() {
        let rule = MD044ProperNames::new(
            vec!["JavaScript".to_string(), "Node.js".to_string(), "Python".to_string()],
            true,
        );

        let content = r#"Check this [javascript documentation](https://javascript.info) for info.

Visit [node.js homepage](https://nodejs.org) and [python tutorial](https://python.org).

Real javascript should be flagged.

Also see the [typescript guide][ts-ref] for more.

Real python should be flagged too.

[ts-ref]: https://typescript.org/handbook"#;

        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // Link text should be checked, URLs should not be checked
        // Line 1: [javascript documentation] - "javascript" should be flagged
        // Line 3: [node.js homepage] - "node.js" should be flagged (matches "Node.js")
        // Line 3: [python tutorial] - "python" should be flagged
        // Line 5: standalone javascript
        // Line 9: standalone python
        assert_eq!(result.len(), 5, "Expected 5 warnings: 3 in link text + 2 standalone");

        // Verify line numbers for link text warnings
        let line_1_warnings: Vec<_> = result.iter().filter(|w| w.line == 1).collect();
        assert_eq!(line_1_warnings.len(), 1);
        assert!(
            line_1_warnings[0]
                .message
                .contains("'javascript' should be 'JavaScript'")
        );

        let line_3_warnings: Vec<_> = result.iter().filter(|w| w.line == 3).collect();
        assert_eq!(line_3_warnings.len(), 2); // node.js and python

        // Standalone warnings
        assert!(result.iter().any(|w| w.line == 5 && w.message.contains("'javascript'")));
        assert!(result.iter().any(|w| w.line == 9 && w.message.contains("'python'")));
    }

    #[test]
    fn test_link_urls_not_flagged() {
        let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);

        // URL contains "javascript" but should NOT be flagged
        let content = r#"[Link Text](https://javascript.info/guide)"#;

        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // URL should not be checked
        assert!(result.is_empty(), "URLs should not be checked for proper names");
    }

    #[test]
    fn test_proper_names_in_image_alt_text_are_flagged() {
        let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);

        let content = r#"Here is a ![javascript logo](javascript.png "javascript icon") image.

Real javascript should be flagged."#;

        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // Image alt text should be checked, URL and title should not be checked
        // Line 1: ![javascript logo] - "javascript" should be flagged
        // Line 3: standalone javascript
        assert_eq!(result.len(), 2, "Expected 2 warnings: 1 in alt text + 1 standalone");
        assert!(result[0].message.contains("'javascript' should be 'JavaScript'"));
        assert!(result[0].line == 1); // "![javascript logo]"
        assert!(result[1].message.contains("'javascript' should be 'JavaScript'"));
        assert!(result[1].line == 3); // "Real javascript should be flagged."
    }

    #[test]
    fn test_image_urls_not_flagged() {
        let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);

        // URL contains "javascript" but should NOT be flagged
        let content = r#"![Logo](https://javascript.info/logo.png)"#;

        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // Image URL should not be checked
        assert!(result.is_empty(), "Image URLs should not be checked for proper names");
    }

    #[test]
    fn test_reference_link_text_flagged_but_definition_not() {
        let rule = MD044ProperNames::new(vec!["JavaScript".to_string(), "TypeScript".to_string()], true);

        let content = r#"Check the [javascript guide][js-ref] for details.

Real javascript should be flagged.

[js-ref]: https://javascript.info/typescript/guide"#;

        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // Link text should be checked, reference definitions should not
        // Line 1: [javascript guide] - should be flagged
        // Line 3: standalone javascript - should be flagged
        // Line 5: reference definition - should NOT be flagged
        assert_eq!(result.len(), 2, "Expected 2 warnings: 1 in link text + 1 standalone");
        assert!(result.iter().any(|w| w.line == 1 && w.message.contains("'javascript'")));
        assert!(result.iter().any(|w| w.line == 3 && w.message.contains("'javascript'")));
    }

    #[test]
    fn test_reference_definitions_not_flagged() {
        let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);

        // Reference definition should NOT be flagged
        let content = r#"[js-ref]: https://javascript.info/guide"#;

        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // Reference definition URLs should not be checked
        assert!(result.is_empty(), "Reference definitions should not be checked");
    }

    #[test]
    fn test_wikilinks_text_is_flagged() {
        let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);

        // WikiLinks [[destination]] should have their text checked
        let content = r#"[[javascript]]

Regular javascript here.

[[JavaScript|display text]]"#;

        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // Line 1: [[javascript]] - should be flagged (WikiLink text)
        // Line 3: standalone javascript - should be flagged
        // Line 5: [[JavaScript|display text]] - correct capitalization, no flag
        assert_eq!(result.len(), 2, "Expected 2 warnings: 1 in WikiLink + 1 standalone");
        assert!(
            result
                .iter()
                .any(|w| w.line == 1 && w.column == 3 && w.message.contains("'javascript'"))
        );
        assert!(result.iter().any(|w| w.line == 3 && w.message.contains("'javascript'")));
    }

    #[test]
    fn test_url_link_text_not_flagged() {
        let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);

        // Link text that is itself a URL should not be flagged
        let content = r#"[https://github.com/org/repo](https://github.com/org/repo)

[http://github.com/org/repo](http://github.com/org/repo)

[www.github.com/org/repo](https://www.github.com/org/repo)"#;

        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert!(
            result.is_empty(),
            "URL-like link text should not be flagged, got: {result:?}"
        );
    }

    #[test]
    fn test_url_link_text_with_leading_space_not_flagged() {
        let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);

        // Leading/trailing whitespace in link text should be trimmed before URL check
        let content = r#"[ https://github.com/org/repo](https://github.com/org/repo)"#;

        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert!(
            result.is_empty(),
            "URL-like link text with leading space should not be flagged, got: {result:?}"
        );
    }

    #[test]
    fn test_url_link_text_uppercase_scheme_not_flagged() {
        let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);

        let content = r#"[HTTPS://GITHUB.COM/org/repo](https://github.com/org/repo)"#;

        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert!(
            result.is_empty(),
            "URL-like link text with uppercase scheme should not be flagged, got: {result:?}"
        );
    }

    #[test]
    fn test_non_url_link_text_still_flagged() {
        let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);

        // Link text that is NOT a URL should still be flagged
        let content = r#"[github.com/org/repo](https://github.com/org/repo)

[Visit github](https://github.com/org/repo)

[//github.com/org/repo](//github.com/org/repo)

[ftp://github.com/org/repo](ftp://github.com/org/repo)"#;

        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 4, "Non-URL link text should be flagged, got: {result:?}");
        assert!(result.iter().any(|w| w.line == 1)); // github.com (no protocol)
        assert!(result.iter().any(|w| w.line == 3)); // Visit github
        assert!(result.iter().any(|w| w.line == 5)); // //github.com (protocol-relative)
        assert!(result.iter().any(|w| w.line == 7)); // ftp://github.com
    }

    #[test]
    fn test_url_link_text_fix_not_applied() {
        let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);

        let content = "[https://github.com/org/repo](https://github.com/org/repo)\n";

        let ctx = create_context(content);
        let result = rule.fix(&ctx).unwrap();

        assert_eq!(result, content, "Fix should not modify URL-like link text");
    }

    #[test]
    fn test_mixed_url_and_regular_link_text() {
        let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);

        // Mix of URL link text (should skip) and regular text (should flag)
        let content = r#"[https://github.com/org/repo](https://github.com/org/repo)

Visit [github documentation](https://github.com/docs) for details.

[www.github.com/pricing](https://www.github.com/pricing)"#;

        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // Only line 3 should be flagged ("github documentation" is not a URL)
        assert_eq!(
            result.len(),
            1,
            "Only non-URL link text should be flagged, got: {result:?}"
        );
        assert_eq!(result[0].line, 3);
    }

    #[test]
    fn test_html_attribute_values_not_flagged() {
        // Matches inside HTML tag attributes (between `<` and `>`) are not flagged.
        // Attribute values are not prose — they hold URLs, class names, data values, etc.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
        let content = "# Heading\n\ntest\n\n<img src=\"www.example.test/test_image.png\">\n";
        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        // Nothing on line 5 should be flagged — everything is inside the `<img ...>` tag
        let line5_violations: Vec<_> = result.iter().filter(|w| w.line == 5).collect();
        assert!(
            line5_violations.is_empty(),
            "Should not flag anything inside HTML tag attributes: {line5_violations:?}"
        );

        // Plain text on line 3 is still flagged
        let line3_violations: Vec<_> = result.iter().filter(|w| w.line == 3).collect();
        assert_eq!(line3_violations.len(), 1, "Plain 'test' on line 3 should be flagged");
    }

    #[test]
    fn test_html_text_content_still_flagged() {
        // Text between HTML tags (not inside `<...>`) is still checked.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
        let content = "# Heading\n\n<a href=\"https://example.test/page\">test link</a>\n";
        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        // "example.test" in the href attribute → not flagged (inside `<...>`)
        // "test link" in the anchor text → flagged (between `>` and `<`)
        assert_eq!(
            result.len(),
            1,
            "Should flag only 'test' in anchor text, not in href: {result:?}"
        );
        assert_eq!(result[0].column, 37, "Should flag col 37 ('test link' in anchor text)");
    }

    #[test]
    fn test_html_attribute_various_not_flagged() {
        // All attribute types are ignored: src, href, alt, class, data-*, title, etc.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
        let content = concat!(
            "# Heading\n\n",
            "<img src=\"test.png\" alt=\"test image\">\n",
            "<span class=\"test-class\" data-test=\"value\">test content</span>\n",
        );
        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        // Only "test content" (between tags on line 4) should be flagged
        assert_eq!(
            result.len(),
            1,
            "Should flag only 'test content' between tags: {result:?}"
        );
        assert_eq!(result[0].line, 4);
    }

    #[test]
    fn test_plain_text_underscore_boundary_unchanged() {
        // Plain text (outside HTML tags) still uses original word boundary semantics where
        // underscore is a boundary character, matching markdownlint's behavior via AST splitting.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
        let content = "# Heading\n\ntest_image is here and just_test ends here\n";
        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        // Both "test_image" (test at start) and "just_test" (test at end) are flagged
        // because in plain text, "_" is a word boundary
        assert_eq!(
            result.len(),
            2,
            "Should flag 'test' in both 'test_image' and 'just_test': {result:?}"
        );
        let cols: Vec<usize> = result.iter().map(|w| w.column).collect();
        assert!(cols.contains(&1), "Should flag col 1 (test_image): {cols:?}");
        assert!(cols.contains(&29), "Should flag col 29 (just_test): {cols:?}");
    }

    #[test]
    fn test_frontmatter_yaml_keys_not_flagged() {
        // YAML keys in frontmatter should NOT be checked for proper name violations.
        // Only values should be checked.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "---\ntitle: Heading\ntest: Some Test value\n---\n\nTest\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // "test" in the YAML key (line 3) should NOT be flagged
        // "Test" in the YAML value (line 3) is correct capitalization, no flag
        // "Test" in body (line 6) is correct capitalization, no flag
        assert!(
            result.is_empty(),
            "Should not flag YAML keys or correctly capitalized values: {result:?}"
        );
    }

    #[test]
    fn test_frontmatter_yaml_values_flagged() {
        // Incorrectly capitalized names in YAML values should be flagged.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "---\ntitle: Heading\nkey: a test value\n---\n\nTest\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // "test" in the YAML value (line 3) SHOULD be flagged
        assert_eq!(result.len(), 1, "Should flag 'test' in YAML value: {result:?}");
        assert_eq!(result[0].line, 3);
        assert_eq!(result[0].column, 8); // "key: a " = 7 chars, then "test" at column 8
    }

    #[test]
    fn test_frontmatter_key_matches_name_not_flagged() {
        // A YAML key that happens to match a configured name should NOT be flagged.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "---\ntest: other value\n---\n\nBody text\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert!(
            result.is_empty(),
            "Should not flag YAML key that matches configured name: {result:?}"
        );
    }

    #[test]
    fn test_frontmatter_empty_value_not_flagged() {
        // YAML key with no value should be skipped entirely.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "---\ntest:\ntest: \n---\n\nBody text\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert!(
            result.is_empty(),
            "Should not flag YAML keys with empty values: {result:?}"
        );
    }

    #[test]
    fn test_frontmatter_nested_yaml_key_not_flagged() {
        // Nested/indented YAML keys should also be skipped.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "---\nparent:\n  test: nested value\n---\n\nBody text\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // "test" as a nested key should NOT be flagged
        assert!(result.is_empty(), "Should not flag nested YAML keys: {result:?}");
    }

    #[test]
    fn test_frontmatter_list_items_checked() {
        // YAML list items are values and should be checked for proper names.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "---\ntags:\n  - test\n  - other\n---\n\nBody text\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // "test" as a list item value SHOULD be flagged
        assert_eq!(result.len(), 1, "Should flag 'test' in YAML list item: {result:?}");
        assert_eq!(result[0].line, 3);
    }

    #[test]
    fn test_frontmatter_value_with_multiple_colons() {
        // For "key: value: more", key is before first colon.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "---\ntest: description: a test thing\n---\n\nBody text\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // "test" as key should NOT be flagged
        // "test" in value portion ("description: a test thing") SHOULD be flagged
        assert_eq!(
            result.len(),
            1,
            "Should flag 'test' in value after first colon: {result:?}"
        );
        assert_eq!(result[0].line, 2);
        assert!(result[0].column > 6, "Violation column should be in value portion");
    }

    #[test]
    fn test_frontmatter_does_not_affect_body() {
        // Body text after frontmatter should still be fully checked.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "---\ntitle: Heading\n---\n\ntest should be flagged here\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 1, "Should flag 'test' in body text: {result:?}");
        assert_eq!(result[0].line, 5);
    }

    #[test]
    fn test_frontmatter_fix_corrects_values_preserves_keys() {
        // Fix should correct YAML values but preserve keys.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "---\ntest: a test value\n---\n\ntest here\n";
        let ctx = create_context(content);
        let fixed = rule.fix(&ctx).unwrap();

        // Key "test" should remain lowercase; value "test" should become "Test"
        assert_eq!(fixed, "---\ntest: a Test value\n---\n\nTest here\n");
    }

    #[test]
    fn test_frontmatter_multiword_value_flagged() {
        // Multiple proper names in a single YAML value should all be flagged.
        let rule = MD044ProperNames::new(vec!["JavaScript".to_string(), "TypeScript".to_string()], true);

        let content = "---\ndescription: Learn javascript and typescript\n---\n\nBody\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 2, "Should flag both names in YAML value: {result:?}");
        assert!(result.iter().all(|w| w.line == 2));
    }

    #[test]
    fn test_frontmatter_yaml_comments_not_checked() {
        // YAML comments inside frontmatter should be skipped entirely.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "---\n# test comment\ntitle: Heading\n---\n\nBody text\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert!(result.is_empty(), "Should not flag names in YAML comments: {result:?}");
    }

    #[test]
    fn test_frontmatter_delimiters_not_checked() {
        // Frontmatter delimiter lines (--- or +++) should never be checked.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "---\ntitle: Heading\n---\n\ntest here\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // Only the body "test" on line 5 should be flagged
        assert_eq!(result.len(), 1, "Should only flag body text: {result:?}");
        assert_eq!(result[0].line, 5);
    }

    #[test]
    fn test_frontmatter_continuation_lines_checked() {
        // Continuation lines (indented, no colon) are value content and should be checked.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "---\ndescription: >\n  a test value\n  continued here\n---\n\nBody\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // "test" on the continuation line should be flagged
        assert_eq!(result.len(), 1, "Should flag 'test' in continuation line: {result:?}");
        assert_eq!(result[0].line, 3);
    }

    #[test]
    fn test_frontmatter_quoted_values_checked() {
        // Quoted YAML values should have their content checked (inside the quotes).
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "---\ntitle: \"a test title\"\n---\n\nBody\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 1, "Should flag 'test' in quoted YAML value: {result:?}");
        assert_eq!(result[0].line, 2);
    }

    #[test]
    fn test_frontmatter_single_quoted_values_checked() {
        // Single-quoted YAML values should have their content checked.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "---\ntitle: 'a test title'\n---\n\nBody\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(
            result.len(),
            1,
            "Should flag 'test' in single-quoted YAML value: {result:?}"
        );
        assert_eq!(result[0].line, 2);
    }

    #[test]
    fn test_frontmatter_fix_multiword_values() {
        // Fix should correct all proper names in frontmatter values.
        let rule = MD044ProperNames::new(vec!["JavaScript".to_string(), "TypeScript".to_string()], true);

        let content = "---\ndescription: Learn javascript and typescript\n---\n\nBody\n";
        let ctx = create_context(content);
        let fixed = rule.fix(&ctx).unwrap();

        assert_eq!(
            fixed,
            "---\ndescription: Learn JavaScript and TypeScript\n---\n\nBody\n"
        );
    }

    #[test]
    fn test_frontmatter_fix_preserves_yaml_structure() {
        // Fix should preserve YAML structure while correcting values.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "---\ntags:\n  - test\n  - other\ntitle: a test doc\n---\n\ntest body\n";
        let ctx = create_context(content);
        let fixed = rule.fix(&ctx).unwrap();

        assert_eq!(
            fixed,
            "---\ntags:\n  - Test\n  - other\ntitle: a Test doc\n---\n\nTest body\n"
        );
    }

    #[test]
    fn test_frontmatter_toml_delimiters_not_checked() {
        // TOML frontmatter with +++ delimiters should also be handled.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "+++\ntitle = \"a test title\"\n+++\n\ntest body\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // "title" as TOML key should NOT be flagged
        // "test" in TOML quoted value SHOULD be flagged (line 2)
        // "test" in body SHOULD be flagged (line 5)
        assert_eq!(result.len(), 2, "Should flag TOML value and body: {result:?}");
        let fm_violations: Vec<_> = result.iter().filter(|w| w.line == 2).collect();
        assert_eq!(fm_violations.len(), 1, "Should flag 'test' in TOML value: {result:?}");
        let body_violations: Vec<_> = result.iter().filter(|w| w.line == 5).collect();
        assert_eq!(body_violations.len(), 1, "Should flag body 'test': {result:?}");
    }

    #[test]
    fn test_frontmatter_toml_key_not_flagged() {
        // TOML keys should NOT be flagged, only values.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "+++\ntest = \"other value\"\n+++\n\nBody text\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert!(
            result.is_empty(),
            "Should not flag TOML key that matches configured name: {result:?}"
        );
    }

    #[test]
    fn test_frontmatter_toml_fix_preserves_keys() {
        // Fix should correct TOML values but preserve keys.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "+++\ntest = \"a test value\"\n+++\n\ntest here\n";
        let ctx = create_context(content);
        let fixed = rule.fix(&ctx).unwrap();

        // Key "test" should remain lowercase; value "test" should become "Test"
        assert_eq!(fixed, "+++\ntest = \"a Test value\"\n+++\n\nTest here\n");
    }

    #[test]
    fn test_frontmatter_list_item_mapping_key_not_flagged() {
        // In "- test: nested value", "test" is a YAML key within a list-item mapping.
        // The key should NOT be flagged; only the value should be checked.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "---\nitems:\n  - test: nested value\n---\n\nBody text\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert!(
            result.is_empty(),
            "Should not flag YAML key in list-item mapping: {result:?}"
        );
    }

    #[test]
    fn test_frontmatter_list_item_mapping_value_flagged() {
        // In "- key: test value", the value portion should be checked.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "---\nitems:\n  - key: a test value\n---\n\nBody text\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(
            result.len(),
            1,
            "Should flag 'test' in list-item mapping value: {result:?}"
        );
        assert_eq!(result[0].line, 3);
    }

    #[test]
    fn test_frontmatter_bare_list_item_still_flagged() {
        // Bare list items without a colon (e.g., "- test") are values and should be flagged.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "---\ntags:\n  - test\n  - other\n---\n\nBody text\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(result.len(), 1, "Should flag 'test' in bare list item: {result:?}");
        assert_eq!(result[0].line, 3);
    }

    #[test]
    fn test_frontmatter_flow_mapping_not_flagged() {
        // Flow mappings like {test: value} contain YAML keys that should not be flagged.
        // The entire flow construct should be skipped.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "---\nflow_map: {test: value, other: test}\n---\n\nBody text\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert!(
            result.is_empty(),
            "Should not flag names inside flow mappings: {result:?}"
        );
    }

    #[test]
    fn test_frontmatter_flow_sequence_not_flagged() {
        // Flow sequences like [test, other] should also be skipped.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "---\nitems: [test, other, test]\n---\n\nBody text\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert!(
            result.is_empty(),
            "Should not flag names inside flow sequences: {result:?}"
        );
    }

    #[test]
    fn test_frontmatter_list_item_mapping_fix_preserves_key() {
        // Fix should correct values in list-item mappings but preserve keys.
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "---\nitems:\n  - test: a test value\n---\n\ntest here\n";
        let ctx = create_context(content);
        let fixed = rule.fix(&ctx).unwrap();

        // "test" as list-item key should remain lowercase;
        // "test" in value portion should become "Test"
        assert_eq!(fixed, "---\nitems:\n  - test: a Test value\n---\n\nTest here\n");
    }

    #[test]
    fn test_frontmatter_backtick_code_not_flagged() {
        // Names inside backticks in frontmatter should NOT be flagged when code_blocks=false.
        let config = MD044Config {
            names: vec!["GoodApplication".to_string()],
            code_blocks: false,
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = "---\ntitle: \"`goodapplication` CLI\"\n---\n\nIntroductory `goodapplication` CLI text.\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // Neither the frontmatter nor the body backtick-wrapped name should be flagged
        assert!(
            result.is_empty(),
            "Should not flag names inside backticks in frontmatter or body: {result:?}"
        );
    }

    #[test]
    fn test_frontmatter_unquoted_backtick_code_not_flagged() {
        // Exact case from issue #513: unquoted YAML frontmatter with backticks
        let config = MD044Config {
            names: vec!["GoodApplication".to_string()],
            code_blocks: false,
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = "---\ntitle: `goodapplication` CLI\n---\n\nIntroductory `goodapplication` CLI text.\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert!(
            result.is_empty(),
            "Should not flag names inside backticks in unquoted YAML frontmatter: {result:?}"
        );
    }

    #[test]
    fn test_frontmatter_bare_name_still_flagged_with_backtick_nearby() {
        // Names outside backticks in frontmatter should still be flagged.
        let config = MD044Config {
            names: vec!["GoodApplication".to_string()],
            code_blocks: false,
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = "---\ntitle: goodapplication `goodapplication` CLI\n---\n\nBody\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // Only the bare "goodapplication" (before backticks) should be flagged
        assert_eq!(
            result.len(),
            1,
            "Should flag bare name but not backtick-wrapped name: {result:?}"
        );
        assert_eq!(result[0].line, 2);
        assert_eq!(result[0].column, 8); // "title: " = 7 chars, name at column 8
    }

    #[test]
    fn test_frontmatter_backtick_code_with_code_blocks_true() {
        // When code_blocks=true, names inside backticks ARE checked.
        let config = MD044Config {
            names: vec!["GoodApplication".to_string()],
            code_blocks: true,
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = "---\ntitle: \"`goodapplication` CLI\"\n---\n\nBody\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // With code_blocks=true, backtick-wrapped name SHOULD be flagged
        assert_eq!(
            result.len(),
            1,
            "Should flag backtick-wrapped name when code_blocks=true: {result:?}"
        );
        assert_eq!(result[0].line, 2);
    }

    #[test]
    fn test_frontmatter_fix_preserves_backtick_code() {
        // Fix should NOT change names inside backticks in frontmatter.
        let config = MD044Config {
            names: vec!["GoodApplication".to_string()],
            code_blocks: false,
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = "---\ntitle: \"`goodapplication` CLI\"\n---\n\nIntroductory `goodapplication` CLI text.\n";
        let ctx = create_context(content);
        let fixed = rule.fix(&ctx).unwrap();

        // Neither backtick-wrapped occurrence should be changed
        assert_eq!(
            fixed, content,
            "Fix should not modify names inside backticks in frontmatter"
        );
    }

    // --- Angle-bracket URL tests (issue #457) ---

    #[test]
    fn test_angle_bracket_url_in_html_comment_not_flagged() {
        // Angle-bracket URLs inside HTML comments should be skipped
        let config = MD044Config {
            names: vec!["Test".to_string()],
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = "---\ntitle: Level 1 heading\n---\n\n<https://www.example.test>\n\n<!-- This is a Test https://www.example.test -->\n<!-- This is a Test <https://www.example.test> -->\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // Line 7: "Test" in comment prose before bare URL -- already correct capitalization
        // Line 7: "test" in bare URL (not in angle brackets) -- but "test" is in URL domain, not prose.
        //   However, .example.test has "test" at a word boundary (after '.'), so it IS flagged.
        // Line 8: "Test" in comment prose -- correct capitalization, not flagged
        // Line 8: "test" in <https://www.example.test> -- inside angle-bracket URL, NOT flagged

        // The key assertion: line 8's angle-bracket URL should NOT produce a warning
        let line8_warnings: Vec<_> = result.iter().filter(|w| w.line == 8).collect();
        assert!(
            line8_warnings.is_empty(),
            "Should not flag names inside angle-bracket URLs in HTML comments: {line8_warnings:?}"
        );
    }

    #[test]
    fn test_bare_url_in_html_comment_still_flagged() {
        // Bare URLs (not in angle brackets) inside HTML comments should still be checked
        let config = MD044Config {
            names: vec!["Test".to_string()],
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = "<!-- This is a test https://www.example.test -->\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // "test" appears as prose text before URL and also in the bare URL domain
        // At minimum, the prose "test" should be flagged
        assert!(
            !result.is_empty(),
            "Should flag 'test' in prose text of HTML comment with bare URL"
        );
    }

    #[test]
    fn test_angle_bracket_url_in_regular_markdown_not_flagged() {
        // Angle-bracket URLs in regular markdown are already handled by the link parser,
        // but the angle-bracket check provides a safety net
        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);

        let content = "<https://www.example.test>\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert!(
            result.is_empty(),
            "Should not flag names inside angle-bracket URLs in regular markdown: {result:?}"
        );
    }

    #[test]
    fn test_multiple_angle_bracket_urls_in_one_comment() {
        let config = MD044Config {
            names: vec!["Test".to_string()],
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = "<!-- See <https://test.example.com> and <https://www.example.test> for details -->\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // Both URLs are inside angle brackets, so "test" inside them should NOT be flagged
        assert!(
            result.is_empty(),
            "Should not flag names inside multiple angle-bracket URLs: {result:?}"
        );
    }

    #[test]
    fn test_angle_bracket_non_url_still_flagged() {
        // <Test> is NOT a URL (no scheme), so is_in_angle_bracket_url does NOT protect it.
        // Whether it gets flagged depends on HTML tag detection, not on our URL check.
        assert!(
            !MD044ProperNames::is_in_angle_bracket_url("<test> which is not a URL.", 1),
            "is_in_angle_bracket_url should return false for non-URL angle brackets"
        );
    }

    #[test]
    fn test_angle_bracket_mailto_url_not_flagged() {
        let config = MD044Config {
            names: vec!["Test".to_string()],
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = "<!-- Contact <mailto:test@example.com> for help -->\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert!(
            result.is_empty(),
            "Should not flag names inside angle-bracket mailto URLs: {result:?}"
        );
    }

    #[test]
    fn test_angle_bracket_ftp_url_not_flagged() {
        let config = MD044Config {
            names: vec!["Test".to_string()],
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = "<!-- Download from <ftp://test.example.com/file> -->\n";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert!(
            result.is_empty(),
            "Should not flag names inside angle-bracket FTP URLs: {result:?}"
        );
    }

    #[test]
    fn test_angle_bracket_url_fix_preserves_url() {
        // Fix should not modify text inside angle-bracket URLs
        let config = MD044Config {
            names: vec!["Test".to_string()],
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = "<!-- test text <https://www.example.test> -->\n";
        let ctx = create_context(content);
        let fixed = rule.fix(&ctx).unwrap();

        // "test" in prose should be fixed, URL should be preserved
        assert!(
            fixed.contains("<https://www.example.test>"),
            "Fix should preserve angle-bracket URLs: {fixed}"
        );
        assert!(
            fixed.contains("Test text"),
            "Fix should correct prose 'test' to 'Test': {fixed}"
        );
    }

    #[test]
    fn test_is_in_angle_bracket_url_helper() {
        // Direct tests of the helper function
        let line = "text <https://example.test> more text";

        // Inside the URL
        assert!(MD044ProperNames::is_in_angle_bracket_url(line, 5)); // '<'
        assert!(MD044ProperNames::is_in_angle_bracket_url(line, 6)); // 'h'
        assert!(MD044ProperNames::is_in_angle_bracket_url(line, 15)); // middle of URL
        assert!(MD044ProperNames::is_in_angle_bracket_url(line, 26)); // '>'

        // Outside the URL
        assert!(!MD044ProperNames::is_in_angle_bracket_url(line, 0)); // 't' at start
        assert!(!MD044ProperNames::is_in_angle_bracket_url(line, 4)); // space before '<'
        assert!(!MD044ProperNames::is_in_angle_bracket_url(line, 27)); // space after '>'

        // Non-URL angle brackets
        assert!(!MD044ProperNames::is_in_angle_bracket_url("<notaurl>", 1));

        // mailto scheme
        assert!(MD044ProperNames::is_in_angle_bracket_url(
            "<mailto:test@example.com>",
            10
        ));

        // ftp scheme
        assert!(MD044ProperNames::is_in_angle_bracket_url(
            "<ftp://test.example.com>",
            10
        ));
    }

    #[test]
    fn test_is_in_angle_bracket_url_uppercase_scheme() {
        // RFC 3986: URI schemes are case-insensitive
        assert!(MD044ProperNames::is_in_angle_bracket_url(
            "<HTTPS://test.example.com>",
            10
        ));
        assert!(MD044ProperNames::is_in_angle_bracket_url(
            "<Http://test.example.com>",
            10
        ));
    }

    #[test]
    fn test_is_in_angle_bracket_url_uncommon_schemes() {
        // ssh scheme
        assert!(MD044ProperNames::is_in_angle_bracket_url(
            "<ssh://test@example.com>",
            10
        ));
        // file scheme
        assert!(MD044ProperNames::is_in_angle_bracket_url("<file:///test/path>", 10));
        // data scheme (no authority, just colon)
        assert!(MD044ProperNames::is_in_angle_bracket_url("<data:text/plain;test>", 10));
    }

    #[test]
    fn test_is_in_angle_bracket_url_unclosed() {
        // Unclosed angle bracket should NOT match
        assert!(!MD044ProperNames::is_in_angle_bracket_url(
            "<https://test.example.com",
            10
        ));
    }

    #[test]
    fn test_vale_inline_config_comments_not_flagged() {
        let config = MD044Config {
            names: vec!["Vale".to_string(), "JavaScript".to_string()],
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = "\
<!-- vale off -->
Some javascript text here.
<!-- vale on -->
<!-- vale Style.Rule = NO -->
More javascript text.
<!-- vale Style.Rule = YES -->
<!-- vale JavaScript.Grammar = NO -->
";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // Only the body text lines (2, 5) should be flagged for "javascript"
        assert_eq!(result.len(), 2, "Should only flag body lines, not Vale config comments");
        assert_eq!(result[0].line, 2);
        assert_eq!(result[1].line, 5);
    }

    #[test]
    fn test_remark_lint_inline_config_comments_not_flagged() {
        let config = MD044Config {
            names: vec!["JavaScript".to_string()],
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = "\
<!-- lint disable remark-lint-some-rule -->
Some javascript text here.
<!-- lint enable remark-lint-some-rule -->
<!-- lint ignore remark-lint-some-rule -->
More javascript text.
";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        assert_eq!(
            result.len(),
            2,
            "Should only flag body lines, not remark-lint config comments"
        );
        assert_eq!(result[0].line, 2);
        assert_eq!(result[1].line, 5);
    }

    #[test]
    fn test_fix_does_not_modify_vale_remark_lint_comments() {
        let config = MD044Config {
            names: vec!["JavaScript".to_string(), "Vale".to_string()],
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = "\
<!-- vale off -->
Some javascript text.
<!-- vale on -->
<!-- lint disable remark-lint-some-rule -->
More javascript text.
<!-- lint enable remark-lint-some-rule -->
";
        let ctx = create_context(content);
        let fixed = rule.fix(&ctx).unwrap();

        // Config directive lines must be preserved unchanged
        assert!(fixed.contains("<!-- vale off -->"));
        assert!(fixed.contains("<!-- vale on -->"));
        assert!(fixed.contains("<!-- lint disable remark-lint-some-rule -->"));
        assert!(fixed.contains("<!-- lint enable remark-lint-some-rule -->"));
        // Body text should be fixed
        assert!(fixed.contains("Some JavaScript text."));
        assert!(fixed.contains("More JavaScript text."));
    }

    #[test]
    fn test_mixed_tool_directives_all_skipped() {
        let config = MD044Config {
            names: vec!["JavaScript".to_string(), "Vale".to_string()],
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        let content = "\
<!-- rumdl-disable MD044 -->
Some javascript text.
<!-- markdownlint-disable -->
More javascript text.
<!-- vale off -->
Even more javascript text.
<!-- lint disable some-rule -->
Final javascript text.
<!-- rumdl-enable MD044 -->
<!-- markdownlint-enable -->
<!-- vale on -->
<!-- lint enable some-rule -->
";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // Only body text lines should be flagged (lines 2, 4, 6, 8)
        assert_eq!(
            result.len(),
            4,
            "Should only flag body lines, not any tool directive comments"
        );
        assert_eq!(result[0].line, 2);
        assert_eq!(result[1].line, 4);
        assert_eq!(result[2].line, 6);
        assert_eq!(result[3].line, 8);
    }

    #[test]
    fn test_vale_remark_lint_edge_cases_not_matched() {
        let config = MD044Config {
            names: vec!["JavaScript".to_string(), "Vale".to_string()],
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        // These are regular HTML comments, NOT tool directives:
        // - "<!-- vale -->" is not a valid Vale directive (no action keyword)
        // - "<!-- vale is a tool -->" starts with "vale" but is prose, not a directive
        // - "<!-- valedictorian javascript -->" does not start with "<!-- vale "
        // - "<!-- linting javascript tips -->" does not start with "<!-- lint "
        // - "<!-- vale javascript -->" starts with "vale" but has no action keyword
        // - "<!-- lint your javascript code -->" starts with "lint" but has no action keyword
        let content = "\
<!-- vale -->
<!-- vale is a tool for writing -->
<!-- valedictorian javascript -->
<!-- linting javascript tips -->
<!-- vale javascript -->
<!-- lint your javascript code -->
";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // Line 1: "<!-- vale -->" contains "vale" (wrong case for "Vale") -> flagged
        // Line 2: "<!-- vale is a tool for writing -->" contains "vale" -> flagged
        // Line 3: "<!-- valedictorian javascript -->" contains "javascript" -> flagged
        // Line 4: "<!-- linting javascript tips -->" contains "javascript" -> flagged
        // Line 5: "<!-- vale javascript -->" contains "vale" and "javascript" -> flagged for both
        // Line 6: "<!-- lint your javascript code -->" contains "javascript" -> flagged
        assert_eq!(
            result.len(),
            7,
            "Should flag proper names in non-directive HTML comments: got {result:?}"
        );
        assert_eq!(result[0].line, 1); // "vale" in <!-- vale -->
        assert_eq!(result[1].line, 2); // "vale" in <!-- vale is a tool -->
        assert_eq!(result[2].line, 3); // "javascript" in <!-- valedictorian javascript -->
        assert_eq!(result[3].line, 4); // "javascript" in <!-- linting javascript tips -->
        assert_eq!(result[4].line, 5); // "vale" in <!-- vale javascript -->
        assert_eq!(result[5].line, 5); // "javascript" in <!-- vale javascript -->
        assert_eq!(result[6].line, 6); // "javascript" in <!-- lint your javascript code -->
    }

    #[test]
    fn test_vale_style_directives_skipped() {
        let config = MD044Config {
            names: vec!["JavaScript".to_string(), "Vale".to_string()],
            ..MD044Config::default()
        };
        let rule = MD044ProperNames::from_config_struct(config);

        // These ARE valid Vale directives and should be skipped:
        let content = "\
<!-- vale style = MyStyle -->
<!-- vale styles = Style1, Style2 -->
<!-- vale MyRule.Name = YES -->
<!-- vale MyRule.Name = NO -->
Some javascript text.
";
        let ctx = create_context(content);
        let result = rule.check(&ctx).unwrap();

        // Only line 5 (body text) should be flagged
        assert_eq!(
            result.len(),
            1,
            "Should only flag body lines, not Vale style/rule directives: got {result:?}"
        );
        assert_eq!(result[0].line, 5);
    }

    // --- is_in_backtick_code_in_line unit tests ---

    #[test]
    fn test_backtick_code_single_backticks() {
        let line = "hello `world` bye";
        // 'w' is at index 7, inside the backtick span (content between backticks at 6 and 12)
        assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 7));
        // 'h' at index 0 is outside
        assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 0));
        // 'b' at index 14 is outside
        assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 14));
    }

    #[test]
    fn test_backtick_code_double_backticks() {
        let line = "a ``code`` b";
        // 'c' is at index 4, inside ``...``
        assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 4));
        // 'a' at index 0 is outside
        assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 0));
        // 'b' at index 11 is outside
        assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 11));
    }

    #[test]
    fn test_backtick_code_unclosed() {
        let line = "a `code b";
        // No closing backtick, so nothing is a code span
        assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 3));
    }

    #[test]
    fn test_backtick_code_mismatched_count() {
        // Single backtick opening, double backtick is not a match
        let line = "a `code`` b";
        // The single ` at index 2 doesn't match `` at index 7-8
        // So 'c' at index 3 is NOT in a code span
        assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 3));
    }

    #[test]
    fn test_backtick_code_multiple_spans() {
        let line = "`first` and `second`";
        // 'f' at index 1 (inside first span)
        assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 1));
        // 'a' at index 8 (between spans)
        assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 8));
        // 's' at index 13 (inside second span)
        assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 13));
    }

    #[test]
    fn test_backtick_code_on_backtick_boundary() {
        let line = "`code`";
        // Position 0 is the opening backtick itself, not inside the span
        assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 0));
        // Position 5 is the closing backtick, not inside the span
        assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 5));
        // Position 1-4 are inside the span
        assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 1));
        assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 4));
    }
}