rumdl 0.2.60

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
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
//! Text reflow utilities for MD013
//!
//! This module implements text wrapping/reflow functionality that preserves
//! Markdown elements like links, emphasis, code spans, etc.

use crate::utils::calculate_indentation_width_default;
use crate::utils::is_definition_list_item;
use crate::utils::mkdocs_attr_list::{ATTR_LIST_PATTERN, is_standalone_attr_list};
use crate::utils::mkdocs_snippets::is_snippet_block_delimiter;
use crate::utils::regex_cache::{
    DISPLAY_MATH_REGEX, EMAIL_PATTERN, EMOJI_SHORTCODE_REGEX, HTML_ENTITY_REGEX, HTML_TAG_PATTERN,
    HUGO_SHORTCODE_REGEX, INLINE_MATH_REGEX, WIKI_LINK_REGEX,
};
use crate::utils::sentence_utils::{
    get_abbreviations, is_cjk_char, is_cjk_sentence_ending, is_closing_quote, is_opening_quote,
    text_ends_with_abbreviation,
};
use pulldown_cmark::{BrokenLink, CowStr, Event, LinkType, Options, Parser, Tag, TagEnd};
use std::collections::HashSet;
use unicode_width::UnicodeWidthStr;

/// Length calculation mode for reflow
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum ReflowLengthMode {
    /// Count Unicode characters (grapheme clusters)
    Chars,
    /// Count visual display width (CJK = 2 columns, emoji = 2, etc.)
    #[default]
    Visual,
    /// Count raw bytes
    Bytes,
}

/// Calculate the display length of a string based on the length mode
fn display_len(s: &str, mode: ReflowLengthMode) -> usize {
    match mode {
        ReflowLengthMode::Chars => s.chars().count(),
        ReflowLengthMode::Visual => s.width(),
        ReflowLengthMode::Bytes => s.len(),
    }
}

/// Whitespace characters whose whole purpose is to forbid a line break:
/// no-break space (U+00A0), narrow no-break space (U+202F), and figure
/// space (U+2007).
fn is_non_breaking_space(c: char) -> bool {
    matches!(c, '\u{00A0}' | '\u{202F}' | '\u{2007}')
}

/// Whitespace on which reflow may break and rejoin lines. Non-breaking
/// spaces are excluded: they stay inside the surrounding token so they
/// survive reflow byte-for-byte and never become a wrap point (e.g. the
/// French `mot\u{00A0}:` pair or a `10\u{00A0}000` thousands separator).
fn is_breakable_whitespace(c: char) -> bool {
    c.is_whitespace() && !is_non_breaking_space(c)
}

/// Split text into wrappable tokens on breakable whitespace only.
fn split_breakable_words(text: &str) -> impl Iterator<Item = &str> {
    text.split(is_breakable_whitespace).filter(|word| !word.is_empty())
}

/// Whether an inline code span's content can be word-wrapped without altering it.
///
/// Interior whitespace in code spans is literal. Word-splitting collapses a run
/// of whitespace to a single space and cannot represent tabs, so wrapping would
/// corrupt content like `a    b` (four spaces) into `a b`. Only wrap when every
/// breakable-whitespace separator is already a single plain space; a lone
/// leading/trailing space still round-trips (CommonMark normalizes it and the
/// marker-padding path re-adds it for backtick-adjacent content).
fn code_span_wraps_losslessly(content: &str) -> bool {
    let mut prev_ws = false;
    for c in content.chars() {
        let ws = is_breakable_whitespace(c);
        if ws && (prev_ws || c != ' ') {
            return false;
        }
        prev_ws = ws;
    }
    true
}

/// How the inline structure nested inside a span's content constrains where a
/// line break may land.
struct NestedStructure {
    /// Ranges a break may never land inside, merged into outermost,
    /// non-overlapping ranges. The whitespace in a code span is literal, and
    /// the whitespace in a link destination or an HTML tag is structural, so
    /// replacing it with a newline rewrites the document. A link, image or
    /// attr list is held whole beyond that too, matching how the top level
    /// treats one.
    atomic: Vec<(usize, usize)>,
    /// The delimiter runs of nested emphasis, strong and strikethrough spans.
    /// These marker characters belong to a well-formed span, so they do not
    /// force the whole span to be kept whole, but they are not break points
    /// either: the prose between them breaks at whitespace as usual.
    markers: Vec<(usize, usize)>,
    /// Every link, image, wikilink and footnote reference the parse recognised,
    /// nested ones included, sorted by start. Where `atomic` folds a construct
    /// into the one enclosing it, this keeps each one's own start, so a sentence
    /// opener can be walked into a link whose text begins with an image.
    links: Vec<(usize, usize)>,
}

/// An emphasis, strong or strikethrough span whose end has not been seen yet.
struct OpenSpan {
    /// The span's full range, delimiters included.
    span: (usize, usize),
    /// Bounds of the content found inside it so far. What falls outside these
    /// but inside `span` is the delimiter run.
    content: Option<(usize, usize)>,
}

/// Record an event as content of every enclosing span still open, widening the
/// bounds that separate a span's delimiters from what sits between them.
fn note_span_content(open: &mut [OpenSpan], start: usize, end: usize) {
    for open_span in open.iter_mut() {
        if start >= open_span.span.0 && end <= open_span.span.1 {
            open_span.content = Some(match open_span.content {
                Some((known_start, known_end)) => (known_start.min(start), known_end.max(end)),
                None => (start, end),
            });
        }
    }
}

fn merge_ranges(mut ranges: Vec<(usize, usize)>) -> Vec<(usize, usize)> {
    // A nested construct is reported alongside its parent, so any range
    // starting at or before the current end is already covered by it.
    ranges.sort_unstable();
    let mut merged: Vec<(usize, usize)> = Vec::with_capacity(ranges.len());
    for (start, end) in ranges {
        match merged.last_mut() {
            Some(last) if start <= last.1 => last.1 = last.1.max(end),
            _ => merged.push((start, end)),
        }
    }
    merged
}

/// Classify the inline constructs nested inside a span's content.
fn nested_structure(content: &str, defined_references: Option<&HashSet<String>>, attr_lists: bool) -> NestedStructure {
    let mut options = Options::empty();
    options.insert(Options::ENABLE_STRIKETHROUGH);

    let mut atomic: Vec<(usize, usize)> = Vec::new();
    let mut markers: Vec<(usize, usize)> = Vec::new();
    let mut links: Vec<(usize, usize)> = Vec::new();
    // Emphasis-like spans whose end has not been seen yet, each with the bounds
    // of the content found inside it so far.
    let mut open: Vec<OpenSpan> = Vec::new();

    for (event, range) in Parser::new_ext(content, options).into_offset_iter() {
        let (start, end) = (range.start, range.end);
        // An `End` repeats the range its `Start` already contributed, and the
        // one closing a span covers that span whole, which would swallow its
        // own delimiters.
        if !matches!(event, Event::End(_)) {
            note_span_content(&mut open, start, end);
        }
        match event {
            Event::Start(Tag::Link { .. } | Tag::Image { .. }) => {
                atomic.push((start, end));
                links.push((start, end));
            }
            Event::Code(_) | Event::InlineHtml(_) => {
                atomic.push((start, end));
            }
            Event::Start(Tag::Emphasis | Tag::Strong | Tag::Strikethrough) => {
                open.push(OpenSpan {
                    span: (start, end),
                    content: None,
                });
            }
            Event::End(TagEnd::Emphasis | TagEnd::Strong | TagEnd::Strikethrough) => {
                if let Some(OpenSpan {
                    span: (span_start, span_end),
                    content,
                }) = open.pop()
                {
                    match content {
                        // Whatever sits outside the content is the delimiter run.
                        // Its length is read off the parse rather than assumed,
                        // since `~x~` and `~~x~~` are both strikethrough.
                        Some((content_start, content_end)) => {
                            markers.push((span_start, content_start));
                            markers.push((content_end, span_end));
                        }
                        // Nothing inside to anchor the delimiters against, so
                        // keep the span whole rather than guess where they end.
                        None => atomic.push((span_start, span_end)),
                    }
                }
            }
            _ => {}
        }
    }

    // Reference-style links and images (`[text][ref]`, `[text][]`, `[text]`) and
    // footnote references need the document's definitions to be recognised, which
    // the parse above has no access to. Reusing the walk the top level runs
    // keeps a link atomic under exactly the same conditions wherever it appears.
    // Nested constructs come along too: the reference image inside
    // `[![alt][img]](url)` is what that link's sentence opens with.
    for span in all_link_spans(content, defined_references) {
        atomic.push((span.start, span.end));
        links.push((span.start, span.end));
    }

    // Constructs pulldown does not model, but that `parse_elements` holds
    // atomic at the top level. Only those that can contain whitespace matter
    // here: an emoji shortcode or HTML entity has no break point inside it.
    for found in WIKI_LINK_REGEX.find_iter(content) {
        atomic.push((found.start(), found.end()));
        links.push((found.start(), found.end()));
    }
    for found in HUGO_SHORTCODE_REGEX
        .find_iter(content)
        .chain(DISPLAY_MATH_REGEX.find_iter(content))
    {
        atomic.push((found.start(), found.end()));
    }
    let mut from = 0;
    while let Ok(Some(found)) = INLINE_MATH_REGEX.find_from_pos(content, from) {
        atomic.push((found.start(), found.end()));
        from = found.end();
    }

    // A MkDocs/kramdown attr list (`{.class key="value"}`) holds interior
    // whitespace that is structural, so breaking inside one rewrites it. The
    // pattern anchors on `{`, so a non-overlapping sweep finds the same units
    // the top level does. Only when the flavor is enabled: otherwise `{a b}` is
    // literal prose and breaks like any other words, matching the top level.
    if attr_lists {
        for found in ATTR_LIST_PATTERN.find_iter(content) {
            atomic.push((found.start(), found.end()));
        }
    }

    // Both parses report an outermost inline link, so the same range can arrive
    // twice; a nested one arrives once, from the parse without definitions.
    links.sort_unstable();
    links.dedup();

    NestedStructure {
        atomic: merge_ranges(atomic),
        markers: merge_ranges(markers),
        links,
    }
}

/// Split an emphasis, strong or strikethrough span's content into the units
/// that may be placed on separate lines, or `None` when the span has to stay
/// atomic.
///
/// Breaking such a span at whitespace is safe: emphasis carries across a soft
/// line break, and a newline is whitespace just like the space it replaces, so
/// every delimiter keeps its flanking classification. Two things are not safe,
/// and this rules them out:
///
/// - A break inside a code span, link, image or HTML tag. Each is one
///   unbreakable unit, matching how they are already held atomic at the top
///   level, because the whitespace in one is literal or structural.
/// - A marker character that belongs to no well-formed nested span: a stray or
///   backslash-escaped `` ` ``, `*`, `_` or `~`. The content's structure is then
///   not fully modelled, so the span is kept whole rather than broken on a guess.
///   This matters: breaking `**a * b**` at its spaces would put a literal `*` at
///   the start of a line, turning it into a list item.
///
/// A nested emphasis, strong or strikethrough span is not one of those. The
/// whitespace inside it is ordinary prose whitespace, so it breaks like any
/// other, and only its delimiter runs are held together with the words they
/// flank.
fn breakable_units<'a>(
    content: &'a str,
    defined_references: Option<&HashSet<String>>,
    attr_lists: bool,
) -> Option<Vec<&'a str>> {
    // Plain prose cannot hold a nested construct, so every whitespace run is a
    // break point and the parse below can be skipped.
    if !content.contains(['`', '*', '_', '~', '[', '<', '$', '{']) {
        return Some(split_breakable_words(content).collect());
    }

    let NestedStructure { atomic, markers, .. } = nested_structure(content, defined_references, attr_lists);

    let mut units = Vec::new();
    let mut unit_start = None;
    let mut next_atomic = 0;
    let mut next_marker = 0;
    for (offset, ch) in content.char_indices() {
        while atomic.get(next_atomic).is_some_and(|&(_, end)| end <= offset) {
            next_atomic += 1;
        }
        if atomic.get(next_atomic).is_some_and(|&(start, _)| offset >= start) {
            // Inside an atomic construct: never a break point, and its markers
            // are accounted for.
            if unit_start.is_none() {
                unit_start = Some(offset);
            }
            continue;
        }
        while markers.get(next_marker).is_some_and(|&(_, end)| end <= offset) {
            next_marker += 1;
        }
        if matches!(ch, '`' | '*' | '_' | '~') && markers.get(next_marker).is_none_or(|&(start, _)| offset < start) {
            return None;
        }
        if is_breakable_whitespace(ch) {
            if let Some(start) = unit_start.take() {
                units.push(&content[start..offset]);
            }
        } else if unit_start.is_none() {
            unit_start = Some(offset);
        }
    }
    if let Some(start) = unit_start {
        units.push(&content[start..]);
    }
    Some(units)
}

/// Options for reflowing text
#[derive(Clone)]
pub struct ReflowOptions {
    /// Target line length
    pub line_length: usize,
    /// Whether to break on sentence boundaries when possible
    pub break_on_sentences: bool,
    /// Whether to preserve existing line breaks in paragraphs
    pub preserve_breaks: bool,
    /// Whether to enforce one sentence per line
    pub sentence_per_line: bool,
    /// Whether to use semantic line breaks (cascading split strategy)
    pub semantic_line_breaks: bool,
    /// Custom abbreviations for sentence detection
    /// Periods are optional - both "Dr" and "Dr." work the same
    /// Custom abbreviations are always added to the built-in defaults
    pub abbreviations: Option<Vec<String>>,
    /// How to measure string length for line-length comparisons
    pub length_mode: ReflowLengthMode,
    /// Whether to treat {#id .class key="value"} as atomic (unsplittable) elements.
    /// Enabled for MkDocs and Kramdown flavors.
    pub attr_lists: bool,
    /// Whether to treat MyST inline roles (`` {role}`content` ``) as atomic
    /// (unsplittable) elements. Enabled for the MyST flavor so the colon inside
    /// `{domain:role}` is never used as a clause-break point.
    pub myst_roles: bool,
    /// Whether to require uppercase after periods for sentence detection.
    /// When true (default), only "word. Capital" is a sentence boundary.
    /// When false, "word. lowercase" is also treated as a sentence boundary.
    /// Does not affect ! and ? which are always treated as sentence boundaries.
    pub require_sentence_capital: bool,
    /// Cap list continuation indent to this value when set.
    /// Used by mkdocs flavor where continuation is always 4 spaces
    /// regardless of checkbox markers.
    pub max_list_continuation_indent: Option<usize>,
    /// Defined reference labels for the surrounding document, used to decide
    /// whether a bare shortcut reference (`[text]`) is a real link (kept atomic
    /// during reflow) or literal bracketed prose (wrapped like normal text).
    ///
    /// `None` means no reference information is available: every shortcut is
    /// treated as atomic. This is the safe default - it never splits a real
    /// link, at the cost of also not wrapping literal bracketed prose.
    ///
    /// `Some(set)` enables definition-aware behavior: a shortcut is atomic only
    /// when its normalized label (see [`normalize_reference_label`]) is in the
    /// set. Full and collapsed reference links and reference images are always
    /// atomic regardless, because their `][ref]` / `[]` syntax is an explicit
    /// link signal that does not depend on a definition being in scope.
    pub defined_references: Option<HashSet<String>>,
    /// Whether to hold emphasis/strong/strikethrough and code spans atomic during reflow.
    /// When true (default), these spans are treated as atomic units.
    /// When false, they can be wrapped word-by-word like normal text.
    pub atomic_spans: bool,
    /// Which of the checker's line-length exemptions reflow mirrors when it
    /// measures a line. Empty measures the markdown as written.
    pub length_exemptions: LengthExemptions,
}

/// The line-length exemptions MD013's check applies, as far as reflow can mirror
/// them.
///
/// The checker tests each one against the budget separately, so a line is
/// forgiven when either reduced length fits, never when the two savings together
/// would fit. Reflow therefore has to keep them apart too: see
/// [`LineWidth`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct LengthExemptions {
    /// An inline `[text](url)` costs `[text]` and an inline `![alt](url)` costs
    /// `![alt]`. Reference, collapsed and shortcut forms are never exempt.
    pub link_urls: bool,
    /// An inline code span costs nothing, because it cannot be wrapped.
    pub code_spans: bool,
}

impl LengthExemptions {
    /// Whether any exemption is active. When none is, every width below reduces
    /// to the plain source width and the whole mechanism is inert.
    fn any(&self) -> bool {
        self.link_urls || self.code_spans
    }
}

impl Default for ReflowOptions {
    fn default() -> Self {
        Self {
            line_length: 80,
            break_on_sentences: true,
            preserve_breaks: false,
            sentence_per_line: false,
            semantic_line_breaks: false,
            abbreviations: None,
            length_mode: ReflowLengthMode::default(),
            attr_lists: false,
            myst_roles: false,
            require_sentence_capital: true,
            max_list_continuation_indent: None,
            defined_references: None,
            atomic_spans: true,
            length_exemptions: LengthExemptions::default(),
        }
    }
}

/// A line's width under each exemption the checker applies independently.
///
/// The checker forgives a line when the link-exempt length fits *or* the
/// code-exempt length fits, so the width that decides whether reflow may stop is
/// the smaller of the two, never one total with both savings taken out. Adding
/// widths is component-wise, which is what makes it usable as a running total.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
struct LineWidth {
    /// Width with inline link and image destinations discounted.
    link_exempt: usize,
    /// Width with inline code spans discounted.
    code_exempt: usize,
}

impl LineWidth {
    /// A span of text that no exemption touches, so both components are its
    /// full width.
    fn plain(width: usize) -> Self {
        Self {
            link_exempt: width,
            code_exempt: width,
        }
    }

    /// The width the checker measures this line at: whichever exemption helps
    /// more.
    fn effective(self) -> usize {
        self.link_exempt.min(self.code_exempt)
    }

    fn fits(self, line_length: usize) -> bool {
        self.effective() <= line_length
    }

    /// Whether nothing has been accumulated. Only an empty string measures zero
    /// under both exemptions: the cheapest an exempt link can be is `[]`, and a
    /// code span still costs its full width against the link exemption.
    fn is_empty(self) -> bool {
        self.link_exempt == 0 && self.code_exempt == 0
    }
}

impl std::ops::Add for LineWidth {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        Self {
            link_exempt: self.link_exempt + other.link_exempt,
            code_exempt: self.code_exempt + other.code_exempt,
        }
    }
}

impl std::ops::AddAssign for LineWidth {
    fn add_assign(&mut self, other: Self) {
        *self = *self + other;
    }
}

/// Normalize a reference label for definition matching: collapse internal
/// whitespace runs to a single space, trim, and lowercase (CommonMark-style
/// label matching). Both the defined labels and the shortcut references checked
/// against them are run through this function, so matching is case- and
/// whitespace-insensitive. Biasing toward matching keeps a real shortcut link
/// atomic even when its use and definition differ only in case or whitespace.
pub fn normalize_reference_label(label: &str) -> String {
    label.split_whitespace().collect::<Vec<_>>().join(" ").to_lowercase()
}

/// If `chars` starts at `start` with one or more consecutive footnote
/// references (`[^label]`, matching the same `[a-zA-Z0-9_-]+` label grammar as
/// `FOOTNOTE_REF` in `mkdocs_footnotes.rs`), return the position just past the
/// last one. Returns `None` if `start` is not the beginning of a footnote
/// reference, so a bare `[1]` or `[text]` never matches.
fn footnote_refs_end(chars: &[char], start: usize) -> Option<usize> {
    let mut pos = start;
    let mut found = false;

    loop {
        if chars.get(pos) != Some(&'[') || chars.get(pos + 1) != Some(&'^') {
            break;
        }
        let label_start = pos + 2;
        let mut label_end = label_start;
        while matches!(chars.get(label_end), Some(c) if c.is_ascii_alphanumeric() || *c == '_' || *c == '-') {
            label_end += 1;
        }
        if label_end == label_start || chars.get(label_end) != Some(&']') {
            break;
        }
        pos = label_end + 1;
        found = true;
    }

    found.then_some(pos)
}

/// Byte offset of each char in the text `chars` was collected from, with the
/// text's byte length as a final entry.
fn char_byte_offsets(chars: &[char]) -> Vec<usize> {
    let mut offsets = Vec::with_capacity(chars.len() + 1);
    let mut offset = 0;
    for c in chars {
        offsets.push(offset);
        offset += c.len_utf8();
    }
    offsets.push(offset);
    offsets
}

/// A text being split into sentences, with the views the boundary check reads
/// alongside it: its chars, each char's byte offset (plus the text's length as
/// a final entry), and the `links` list of its [`NestedStructure`], the
/// link-like constructs a sentence may open with, nested ones included.
struct SentenceText<'a> {
    text: &'a str,
    chars: &'a [char],
    char_offsets: &'a [usize],
    links: &'a [(usize, usize)],
}

impl SentenceText<'_> {
    /// Char index just past the link, image, wikilink or footnote reference
    /// that starts at `chars[pos]`, or `None` when no such construct starts
    /// there. The construct is one the parse behind `links` recognised, so a
    /// bracket the parse reads as text (`[Smith 2020]` with no such reference
    /// defined, `[text](unterminated`) opens nothing here either, and one
    /// nested in another (`[![alt](img)](url)`) is found at its own start. An
    /// Obsidian embed `![[note]]` is known to the parse from its `[[`, so the
    /// range starts one char after its `!`.
    fn link_end_at(&self, pos: usize) -> Option<usize> {
        let range_start = match self.chars.get(pos) {
            Some('[') => pos,
            Some('!') if self.chars.get(pos + 1) == Some(&'[') => match self.link_range_end_at(pos) {
                Some(end) => return Some(end),
                None => pos + 1,
            },
            _ => return None,
        };
        self.link_range_end_at(range_start)
    }

    /// Char index just past the link-like construct that starts at `chars[pos]`.
    fn link_range_end_at(&self, pos: usize) -> Option<usize> {
        let start = self.char_offsets[pos];
        let idx = self.links.binary_search_by_key(&start, |&(s, _)| s).ok()?;
        let end = self.links[idx].1;
        Some(self.char_offsets.binary_search(&end).unwrap_or_else(|i| i))
    }
}

/// Detect if a character position is a sentence boundary
/// Based on the approach from github.com/JoshuaKGoldberg/sentences-per-line
/// Supports both ASCII punctuation (. ! ?) and CJK punctuation (。 ! ?)
fn is_sentence_boundary(
    st: &SentenceText<'_>,
    pos: usize,
    abbreviations: &HashSet<String>,
    require_sentence_capital: bool,
) -> bool {
    let SentenceText { text, chars, .. } = *st;
    if pos + 1 >= chars.len() {
        return false;
    }
    let byte_offset_after_punct = st.char_offsets[pos + 1];

    let c = chars[pos];
    let next_char = chars[pos + 1];

    // Check for CJK sentence-ending punctuation (。, !, ?)
    // CJK punctuation doesn't require space or uppercase after it
    if is_cjk_sentence_ending(c) {
        // Skip any trailing emphasis/strikethrough markers
        let mut after_punct_pos = pos + 1;
        while after_punct_pos < chars.len()
            && (chars[after_punct_pos] == '*' || chars[after_punct_pos] == '_' || chars[after_punct_pos] == '~')
        {
            after_punct_pos += 1;
        }

        // Skip whitespace
        while after_punct_pos < chars.len() && chars[after_punct_pos].is_whitespace() {
            after_punct_pos += 1;
        }

        // Check if we have more content (any non-whitespace)
        if after_punct_pos >= chars.len() {
            return false;
        }

        // Same rule as after ASCII punctuation below: no sentence opens with
        // an ordered-list marker.
        if opens_ordered_list_marker(&chars[after_punct_pos..]) {
            return false;
        }

        // Skip leading emphasis/strikethrough markers
        while after_punct_pos < chars.len()
            && (chars[after_punct_pos] == '*' || chars[after_punct_pos] == '_' || chars[after_punct_pos] == '~')
        {
            after_punct_pos += 1;
        }

        if after_punct_pos >= chars.len() {
            return false;
        }

        // For CJK, we accept any character as the start of the next sentence
        // (no uppercase requirement, since CJK doesn't have case)
        return true;
    }

    // Check for ASCII sentence-ending punctuation
    if c != '.' && c != '!' && c != '?' {
        return false;
    }

    // A terminator immediately followed by a closing quote sits inside the
    // quotation, not after it.
    let inside_quotation = is_closing_quote(next_char);

    // Must be followed by space, closing quote, or emphasis/strikethrough marker followed by space
    let (_space_pos, after_space_pos) = if next_char == ' ' {
        // Normal case: punctuation followed by space
        (pos + 1, pos + 2)
    } else if is_closing_quote(next_char) && pos + 2 < chars.len() {
        // Sentence ends with quote - check what follows the quote
        if chars[pos + 2] == ' ' {
            // Just quote followed by space: 'sentence." '
            (pos + 2, pos + 3)
        } else if (chars[pos + 2] == '*' || chars[pos + 2] == '_') && pos + 3 < chars.len() && chars[pos + 3] == ' ' {
            // Quote followed by emphasis: 'sentence."* '
            (pos + 3, pos + 4)
        } else if (chars[pos + 2] == '*' || chars[pos + 2] == '_')
            && pos + 4 < chars.len()
            && chars[pos + 3] == chars[pos + 2]
            && chars[pos + 4] == ' '
        {
            // Quote followed by bold: 'sentence."** '
            (pos + 4, pos + 5)
        } else {
            return false;
        }
    } else if (next_char == '*' || next_char == '_') && pos + 2 < chars.len() && chars[pos + 2] == ' ' {
        // Sentence ends with emphasis: "sentence.* " or "sentence._ "
        (pos + 2, pos + 3)
    } else if (next_char == '*' || next_char == '_')
        && pos + 3 < chars.len()
        && chars[pos + 2] == next_char
        && chars[pos + 3] == ' '
    {
        // Sentence ends with bold: "sentence.** " or "sentence.__ "
        (pos + 3, pos + 4)
    } else if next_char == '~' && pos + 3 < chars.len() && chars[pos + 2] == '~' && chars[pos + 3] == ' ' {
        // Sentence ends with strikethrough: "sentence.~~ "
        (pos + 3, pos + 4)
    } else if next_char == '[' {
        // Sentence ends with one or more footnote references glued directly to
        // the punctuation, e.g. "sentence.[^1]" or "sentence.[^1][^2]". A bare
        // `[1]` or `[text]` doesn't match `footnote_refs_end` and falls through
        // to `return false` below, since that's link/citation-like text, not
        // footnote syntax.
        match footnote_refs_end(chars, pos + 1) {
            Some(end_pos) if chars.get(end_pos) == Some(&' ') => (end_pos, end_pos + 1),
            _ => return false,
        }
    } else {
        return false;
    };

    // Skip all whitespace after the space to find the start of the next sentence
    let mut next_char_pos = after_space_pos;
    while next_char_pos < chars.len() && chars[next_char_pos].is_whitespace() {
        next_char_pos += 1;
    }

    // Check if we reached the end of the string
    if next_char_pos >= chars.len() {
        return false;
    }

    // A sentence is not allowed to open with an ordered-list marker. Every
    // line this splitter produces ends a sentence, and text shaped `2. Do that`
    // right after such a line is a list item: to CommonMark when the number is
    // 1, and to MD032 (which reports a list item missing its blank line, in
    // any document that has a list) for any number. So `Do this. 2. Do that.`
    // keeps its enumerator on the line of the sentence before it, and the
    // enumerated text opens the next line. The CJK path above applies the
    // same rule.
    if opens_ordered_list_marker(&chars[next_char_pos..]) {
        return false;
    }

    // Skip leading emphasis/strikethrough markers, opening quotes and the
    // opener of a link, image or wikilink to find the actual first letter: a
    // sentence that starts with `[Link text](url)` starts with its text. A
    // bracket the parse reads as text is not skipped, so a citation like
    // `[Smith 2020]` or a footnote label opens no sentence.
    let mut first_letter_pos = next_char_pos;
    while first_letter_pos < chars.len() {
        let ch = chars[first_letter_pos];
        if let Some(end) = st.link_end_at(first_letter_pos) {
            first_letter_pos += link_opener_len(chars, first_letter_pos, end);
        } else if matches!(ch, '*' | '_' | '~') || is_opening_quote(ch) {
            first_letter_pos += 1;
        } else {
            break;
        }
    }

    // Check if we reached the end after skipping emphasis
    if first_letter_pos >= chars.len() {
        return false;
    }

    let first_char = chars[first_letter_pos];

    // A bare ! or ? ends a sentence unambiguously, unlike a period, which also
    // ends abbreviations and initials. Inside a quotation it is ambiguous
    // again: the question can belong to the quoted phrase rather than to the
    // sentence carrying it, as in `A "Is this a test?" guide`. A lowercase
    // word after the closing quote means that sentence continues.
    if c == '!' || c == '?' {
        return !inside_quotation || !require_sentence_capital || opens_sentence_in_strict_mode(first_char);
    }

    // Period-specific checks: periods are ambiguous (abbreviations, initials)
    // so we apply additional guards before accepting a sentence boundary. A
    // decimal such as `3.14` never reaches this point: the period must be
    // followed by a space to get here.

    if pos > 0 {
        // Check for common abbreviations
        if text_ends_with_abbreviation(&text[..byte_offset_after_punct], abbreviations) {
            return false;
        }

        // Check for single-letter initials (e.g., "J. K. Rowling")
        // A single uppercase letter before the period preceded by whitespace or start
        // is likely an initial, not a sentence ending.
        if chars[pos - 1].is_ascii_uppercase() && (pos == 1 || (pos >= 2 && chars[pos - 2].is_whitespace())) {
            return false;
        }
    }

    // In strict mode the next sentence must open with something a lowercase
    // continuation cannot. In relaxed mode, accept any character.
    if require_sentence_capital && !opens_sentence_in_strict_mode(first_char) {
        return false;
    }

    true
}

/// Whether `first_char` can open a sentence under `require-sentence-capital`.
///
/// The option exists to keep `word. lowercase` continuations such as
/// `etc. and` or `approx. ten` from being read as two sentences, so what it
/// requires is a first character that is not a lowercase letter: an uppercase
/// letter, a digit (`2nd try.`, `1976 was hot.`, `6:00 is early.`), or a CJK
/// character, none of which has a lowercase form.
fn opens_sentence_in_strict_mode(first_char: char) -> bool {
    first_char.is_uppercase() || first_char.is_numeric() || is_cjk_char(first_char)
}

/// Whether `chars` opens with an ordered-list marker: digits, `.` or `)`,
/// then a space or tab. Any number qualifies, and any number of digits:
/// CommonMark stops reading a marker at nine digits and only lets `1` open a
/// list mid-paragraph, but a line shaped this way reads as a list item to a
/// person and to MD032 alike, whatever the number.
fn opens_ordered_list_marker(chars: &[char]) -> bool {
    let digits = chars.iter().take_while(|c| c.is_ascii_digit()).count();
    digits > 0 && matches!(chars.get(digits), Some('.' | ')')) && matches!(chars.get(digits + 1), Some(' ' | '\t'))
}

/// Length in chars of the opener of the link, image, wikilink or footnote
/// reference occupying `chars[pos..end]`: the part before the text a reader
/// sees. `[` opens a link and `![` an image. A wikilink's `[[` opener runs
/// past its alias pipe, since `[[target|display]]` shows `display`; the pipe
/// is looked for inside the construct only, before its closing `]]`.
fn link_opener_len(chars: &[char], pos: usize, end: usize) -> usize {
    let open = if chars[pos] == '!' { pos + 1 } else { pos };
    let body = open + 1;
    if chars.get(body) != Some(&'[') {
        return body - pos;
    }
    let body = body + 1;
    let alias = chars[body..end.saturating_sub(2).max(body)]
        .iter()
        .position(|&c| c == '|')
        .map_or(body, |p| body + p + 1);
    alias - pos
}

/// Split text into sentences.
///
/// `defined_references` is the document's set of normalized reference labels,
/// which decides whether a bare `[text]` is a link (its label is defined) or
/// prose; `None` means the definitions are unknown and every shortcut is held
/// to be a link, so no real one is ever split. See
/// [`ReflowOptions::defined_references`].
pub fn split_into_sentences(text: &str, defined_references: Option<&HashSet<String>>) -> Vec<String> {
    let abbreviations = get_abbreviations(&None);
    split_into_sentences_with_set(text, &abbreviations, true, None, defined_references)
}

/// Internal function to split text into sentences with a pre-computed abbreviations set
/// Use this when calling multiple times in a loop to avoid repeatedly computing the set
///
/// `appended_span_start` is the byte offset at which an inline span the caller has
/// just appended begins, for callers that assemble a line one element at a time. A
/// sentence ending inside a span takes the closing marker with it, so a delimiter
/// run right after the punctuation joins the sentence that ends there. At that one
/// offset the run is the appended span's opening marker instead, and carrying it
/// back would leave the span with nothing to open it.
fn split_into_sentences_with_set(
    text: &str,
    abbreviations: &HashSet<String>,
    require_sentence_capital: bool,
    appended_span_start: Option<usize>,
    defined_references: Option<&HashSet<String>>,
) -> Vec<String> {
    let char_vec: Vec<char> = text.chars().collect();
    let char_offsets = char_byte_offsets(&char_vec);

    // The constructs a boundary must not fall inside, sorted and non-overlapping,
    // and the link-like ones a sentence may open with.
    let NestedStructure { atomic, links, .. } = sentence_structure(text, defined_references);
    let mut atomic_it = atomic.iter().peekable();
    let st = SentenceText {
        text,
        chars: &char_vec,
        char_offsets: &char_offsets,
        links: &links,
    };

    let mut sentences = Vec::new();
    let mut current_sentence = String::new();
    let mut pos = 0;

    while pos < char_vec.len() {
        let c = char_vec[pos];
        current_sentence.push(c);

        let byte_idx = char_offsets[pos];

        // Advance past every atomic range the current char start has left behind.
        while let Some(&&(_, end)) = atomic_it.peek() {
            if end <= byte_idx {
                atomic_it.next();
            } else {
                break;
            }
        }

        // True if the current character position falls inside an atomic construct.
        let in_atomic = atomic_it
            .peek()
            .is_some_and(|&&(start, end)| byte_idx >= start && byte_idx < end);

        if !in_atomic && is_sentence_boundary(&st, pos, abbreviations, require_sentence_capital) {
            // Consume any trailing footnote references glued to the punctuation
            if let Some(end_pos) = footnote_refs_end(&char_vec, pos + 1) {
                while pos + 1 < end_pos {
                    pos += 1;
                    current_sentence.push(char_vec[pos]);
                }
            }

            // Consume any trailing emphasis/strikethrough markers and quotes
            while pos + 1 < char_vec.len() {
                let next = char_vec[pos + 1];
                if matches!(next, '*' | '_' | '~') && Some(char_offsets[pos + 1]) == appended_span_start {
                    break;
                }
                if next == '*' || next == '_' || next == '~' || is_closing_quote(next) {
                    pos += 1;
                    current_sentence.push(char_vec[pos]);
                } else {
                    break;
                }
            }

            // Consume the space after the sentence
            if pos + 1 < char_vec.len() && char_vec[pos + 1] == ' ' {
                pos += 1; // skip space (not pushed to current_sentence)
            }

            sentences.push(current_sentence.trim().to_string());
            current_sentence.clear();
        }

        pos += 1;
    }

    // Add any remaining text as the last sentence
    if !current_sentence.trim().is_empty() {
        sentences.push(current_sentence.trim().to_string());
    }
    sentences
}

/// The inline structure of a text being split into sentences: the byte ranges
/// a boundary must not fall inside, and the link-like constructs a sentence
/// may open with.
///
/// A link's text, destination and title, an image's alt text, a wikilink's
/// target, a math span, an HTML tag's attributes and a code span each hold text
/// that reads like prose to the boundary check (`[First. Second](url)`) but is
/// one construct to the renderer, so a line break inside it rewrites the
/// document rather than its layout. These are the ranges `parse_elements`
/// holds atomic, computed here from the raw text so that the check counting a
/// line's sentences and the reflow splitting them agree on where a sentence
/// can end. A bare `[text]` is a link only when `defined_references` holds
/// its label, prose otherwise; without the definitions (`None`) it is held
/// atomic wherever it appears, which keeps a real shortcut link whole at the
/// price of leaving a bracketed prose aside on one line.
fn sentence_structure(text: &str, defined_references: Option<&HashSet<String>>) -> NestedStructure {
    // Every construct that can hold whitespace, and every link-like construct,
    // opens with one of these; plain prose skips the parse entirely.
    if !text.contains(['`', '[', '<', '$']) {
        return NestedStructure {
            atomic: Vec::new(),
            markers: Vec::new(),
            links: Vec::new(),
        };
    }
    nested_structure(text, defined_references, false)
}

/// Check if a line is a horizontal rule (---, ___, ***)
fn is_horizontal_rule(line: &str) -> bool {
    if line.len() < 3 {
        return false;
    }

    // Line must consist only of a single marker char (-, _, or *) plus spaces,
    // with at least 3 markers. Scan chars directly to avoid allocating a Vec.
    let mut chars = line.chars();
    let Some(first_char) = chars.next() else {
        return false;
    };
    if first_char != '-' && first_char != '_' && first_char != '*' {
        return false;
    }

    let mut non_space_count = 1usize; // first_char is a marker
    for c in chars {
        if c == ' ' {
            continue;
        }
        if c != first_char {
            return false;
        }
        non_space_count += 1;
    }
    non_space_count >= 3
}

/// Check if a line is a numbered list item (e.g., "1. ", "10. ")
fn is_numbered_list_item(line: &str) -> bool {
    let mut chars = line.chars();

    // Must start with a digit
    if !chars.next().is_some_and(char::is_numeric) {
        return false;
    }

    // Can have more digits
    while let Some(c) = chars.next() {
        if c == '.' {
            // After period, must have a space (consistent with list marker extraction)
            // "2019." alone is NOT treated as a list item to avoid false positives
            return chars.next() == Some(' ');
        }
        if !c.is_numeric() {
            return false;
        }
    }

    false
}

/// Check if a trimmed line is an unordered list item (-, *, + followed by space)
fn is_unordered_list_marker(s: &str) -> bool {
    matches!(s.as_bytes().first(), Some(b'-' | b'*' | b'+'))
        && !is_horizontal_rule(s)
        && (s.len() == 1 || s.as_bytes().get(1) == Some(&b' '))
}

/// Shared structural checks for block boundary detection.
/// Checks elements that only depend on the trimmed line content.
fn is_block_boundary_core(trimmed: &str) -> bool {
    trimmed.is_empty()
        || trimmed.starts_with('#')
        || trimmed.starts_with("```")
        || trimmed.starts_with("~~~")
        || trimmed.starts_with('>')
        || (trimmed.starts_with('[') && trimmed.contains("]:"))
        || is_horizontal_rule(trimmed)
        || is_unordered_list_marker(trimmed)
        || is_numbered_list_item(trimmed)
        || is_definition_list_item(trimmed)
        || trimmed.starts_with(":::")
}

/// Check if a trimmed line starts a new structural block element.
/// Used for paragraph boundary detection in `reflow_markdown()`.
fn is_block_boundary(trimmed: &str) -> bool {
    is_block_boundary_core(trimmed) || trimmed.starts_with('|')
}

/// Check if a line starts a new structural block for paragraph boundary detection
/// in `reflow_paragraph_at_line()`. Extends the core checks with indented code blocks
/// (≥4 spaces) and table row detection via `is_potential_table_row`.
fn is_paragraph_boundary(trimmed: &str, line: &str) -> bool {
    is_block_boundary_core(trimmed)
        || calculate_indentation_width_default(line) >= 4
        || crate::utils::table_utils::TableUtils::is_potential_table_row(line)
}

/// Check if a line ends with a hard break (either two spaces or backslash)
///
/// CommonMark supports two formats for hard line breaks:
/// 1. Two or more trailing spaces
/// 2. A backslash at the end of the line
fn has_hard_break(line: &str) -> bool {
    let line = line.strip_suffix('\r').unwrap_or(line);
    line.ends_with("  ") || line.ends_with('\\')
}

/// Check if text ends with sentence-terminating punctuation (. ! ?)
fn ends_with_sentence_punct(text: &str) -> bool {
    text.ends_with('.') || text.ends_with('!') || text.ends_with('?')
}

/// Trim trailing whitespace while preserving hard breaks (two trailing spaces or backslash)
///
/// Hard breaks in Markdown can be indicated by:
/// 1. Two trailing spaces before a newline (traditional)
/// 2. A backslash at the end of the line (mdformat style)
fn trim_preserving_hard_break(s: &str) -> String {
    // Strip trailing \r from CRLF line endings first to handle Windows files
    let s = s.strip_suffix('\r').unwrap_or(s);

    // Check for backslash hard break (mdformat style)
    if s.ends_with('\\') {
        // Preserve the backslash exactly as-is
        return s.to_string();
    }

    // Check if there are at least 2 trailing spaces (traditional hard break)
    if s.ends_with("  ") {
        // Find the position where non-space content ends
        let content_end = s.trim_end().len();
        if content_end == 0 {
            // String is all whitespace
            return String::new();
        }
        // Preserve exactly 2 trailing spaces for hard break
        format!("{}  ", &s[..content_end])
    } else {
        // No hard break, just trim all trailing whitespace
        s.trim_end().to_string()
    }
}

/// Parse markdown elements using the appropriate parser based on options.
fn parse_elements(text: &str, options: &ReflowOptions) -> Vec<Element> {
    parse_markdown_elements_inner(
        text,
        options.attr_lists,
        options.myst_roles,
        options.defined_references.as_ref(),
    )
}

/// Reflow a line, falling back to the input when the result would not preserve it.
///
/// Reflow redistributes whitespace: it decides where lines break, never which
/// characters a paragraph contains. So the sequence of non-whitespace characters
/// is invariant across a correct reflow, and any difference means the reflow
/// dropped, duplicated, reordered, or invented content. Returning the input
/// unchanged in that case costs a paragraph that stays unwrapped; the alternative
/// is writing corrupted prose into the user's file. A caller comparing its
/// replacement against the original then sees no change and reports nothing.
pub fn reflow_line(line: &str, options: &ReflowOptions) -> Vec<String> {
    let reflowed = reflow_line_unchecked(line, options);
    if preserves_content(line, &reflowed) {
        reflowed
    } else {
        vec![line.to_string()]
    }
}

/// Whether `reflowed` still holds `original`'s text: the same non-whitespace
/// characters in the same order, with every word boundary intact.
///
/// Reflow may add a boundary the input did not have, since wrapping a script
/// that writes without spaces has to break somewhere. Removing one is different:
/// it glues two words into a word the author never wrote.
fn preserves_content(original: &str, reflowed: &[String]) -> bool {
    let (original_text, original_breaks) = visible_text_and_breaks(original.chars());
    let (reflowed_text, reflowed_breaks) =
        visible_text_and_breaks(reflowed.iter().flat_map(|line| line.chars().chain(['\n'])));

    original_text == reflowed_text && contains_all(&reflowed_breaks, &original_breaks)
}

/// The non-whitespace characters of `text`, and for each interior run of
/// whitespace, how many characters precede it.
fn visible_text_and_breaks(text: impl Iterator<Item = char>) -> (String, Vec<usize>) {
    let mut visible = String::new();
    let mut breaks = Vec::new();
    let mut count = 0usize;
    let mut pending_break = false;

    for c in text {
        if c.is_whitespace() {
            pending_break = count > 0;
        } else {
            if pending_break {
                breaks.push(count);
                pending_break = false;
            }
            visible.push(c);
            count += 1;
        }
    }

    (visible, breaks)
}

/// Whether every value in `subset` appears in `superset`. Both are ascending.
fn contains_all(superset: &[usize], subset: &[usize]) -> bool {
    let mut candidates = superset.iter();
    subset
        .iter()
        .all(|wanted| candidates.by_ref().any(|found| found == wanted))
}

fn reflow_line_unchecked(line: &str, options: &ReflowOptions) -> Vec<String> {
    // For sentence-per-line mode, always process regardless of length
    if options.sentence_per_line {
        let elements = parse_elements(line, options);
        return merge_block_construct_continuations(reflow_elements_sentence_per_line(&elements, options));
    }

    // For semantic line breaks mode, use cascading split strategy
    if options.semantic_line_breaks {
        let elements = parse_elements(line, options);
        return merge_block_construct_continuations(reflow_elements_semantic(&elements, options));
    }

    // Quick check: if line is already short enough or no wrapping requested, return as-is
    // line_length = 0 means no wrapping (unlimited line length)
    if options.line_length == 0 || line_fits(line, options) {
        return vec![line.to_string()];
    }

    // Parse the markdown to identify elements
    let elements = parse_elements(line, options);

    // Reflow the elements into lines
    merge_block_construct_continuations(reflow_elements(&elements, options))
}

/// Represents a piece of content in the markdown
#[derive(Debug, Clone)]
enum Element {
    /// Plain text that can be wrapped
    Text(String),
    /// A complete markdown inline link [text](url)
    Link(String),
    /// A complete markdown reference link [text][ref]
    ReferenceLink(String),
    /// A complete markdown empty reference link [text][]
    EmptyReferenceLink(String),
    /// A complete markdown shortcut reference link [ref]
    ShortcutReference(String),
    /// A complete markdown inline image ![alt](url)
    InlineImage(String),
    /// A complete markdown reference image ![alt][ref]
    ReferenceImage(String),
    /// A complete markdown empty reference image ![alt][]
    EmptyReferenceImage(String),
    /// A clickable image badge
    LinkedImage(String),
    /// Footnote reference [^note]
    FootnoteReference(String),
    /// Strikethrough text ~~text~~ or ~text~ (GFM allows one or two tildes)
    Strikethrough {
        content: String,
        /// True if the original used a double-tilde (~~) marker, false for a single tilde (~)
        double: bool,
    },
    /// Wiki-style link [[wiki]] or [[wiki|text]]
    WikiLink(String),
    /// Inline math $math$
    InlineMath(String),
    /// Display math $$math$$
    DisplayMath(String),
    /// Emoji shortcode :emoji:
    EmojiShortcode(String),
    /// Autolink <https://...> or <mailto:...> or <user@domain.com>
    Autolink(String),
    /// HTML tag <tag> or </tag> or <tag/>
    HtmlTag(String),
    /// HTML entity &nbsp; or &#123;
    HtmlEntity(String),
    /// Hugo/Go template shortcode {{< ... >}} or {{% ... %}}
    HugoShortcode(String),
    /// MkDocs/kramdown attribute list {#id .class key="value"}
    AttrList(String),
    /// MyST inline role `` {role}`content` `` (or `` {domain:role}`content` ``).
    /// Stored as the raw matched text and rendered verbatim so it round-trips
    /// exactly; treated as atomic so it is never split mid-role.
    MystRole(String),
    /// Inline code `code`
    Code { content: String, marker: String },
    /// Bold text **text** or __text__
    Bold {
        content: String,
        /// True if underscore markers (__), false for asterisks (**)
        underscore: bool,
    },
    /// Italic text *text* or _text_
    Italic {
        content: String,
        /// True if underscore marker (_), false for asterisk (*)
        underscore: bool,
    },
}

impl Element {
    /// Whether the element's source form opens with `[` or `![`: a link,
    /// image, wikilink, footnote or shortcut reference. What the sentence
    /// splitter makes of that bracket decides whether the element can start a
    /// sentence, so the reflow defers to the splitter for these.
    fn opens_with_bracket(&self) -> bool {
        matches!(
            self,
            Element::Link(_)
                | Element::ReferenceLink(_)
                | Element::EmptyReferenceLink(_)
                | Element::ShortcutReference(_)
                | Element::FootnoteReference(_)
                | Element::InlineImage(_)
                | Element::ReferenceImage(_)
                | Element::EmptyReferenceImage(_)
                | Element::LinkedImage(_)
                | Element::WikiLink(_)
        )
    }
}

impl std::fmt::Display for Element {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Element::Text(s) => write!(f, "{s}"),
            Element::Link(s) => write!(f, "{s}"),
            Element::ReferenceLink(s) => write!(f, "{s}"),
            Element::EmptyReferenceLink(s) => write!(f, "{s}"),
            Element::ShortcutReference(s) => write!(f, "{s}"),
            Element::InlineImage(s) => write!(f, "{s}"),
            Element::ReferenceImage(s) => write!(f, "{s}"),
            Element::EmptyReferenceImage(s) => write!(f, "{s}"),
            Element::LinkedImage(s) => write!(f, "{s}"),
            Element::FootnoteReference(s) => write!(f, "{s}"),
            Element::Strikethrough { content, double } => {
                let marker = if *double { "~~" } else { "~" };
                write!(f, "{marker}{content}{marker}")
            }
            Element::WikiLink(s) => write!(f, "[[{s}]]"),
            Element::InlineMath(s) => write!(f, "${s}$"),
            Element::DisplayMath(s) => write!(f, "$${s}$$"),
            Element::EmojiShortcode(s) => write!(f, ":{s}:"),
            Element::Autolink(s) => write!(f, "{s}"),
            Element::HtmlTag(s) => write!(f, "{s}"),
            Element::HtmlEntity(s) => write!(f, "{s}"),
            Element::HugoShortcode(s) => write!(f, "{s}"),
            Element::AttrList(s) => write!(f, "{s}"),
            Element::MystRole(s) => write!(f, "{s}"),
            Element::Code { content, marker } => write!(f, "{marker}{content}{marker}"),
            Element::Bold { content, underscore } => {
                if *underscore {
                    write!(f, "__{content}__")
                } else {
                    write!(f, "**{content}**")
                }
            }
            Element::Italic { content, underscore } => {
                if *underscore {
                    write!(f, "_{content}_")
                } else {
                    write!(f, "*{content}*")
                }
            }
        }
    }
}

impl Element {
    fn display_len(&self, mode: ReflowLengthMode) -> usize {
        match self {
            Element::Text(s)
            | Element::Link(s)
            | Element::ReferenceLink(s)
            | Element::EmptyReferenceLink(s)
            | Element::ShortcutReference(s)
            | Element::InlineImage(s)
            | Element::ReferenceImage(s)
            | Element::EmptyReferenceImage(s)
            | Element::LinkedImage(s)
            | Element::FootnoteReference(s)
            | Element::Autolink(s)
            | Element::HtmlTag(s)
            | Element::HtmlEntity(s)
            | Element::HugoShortcode(s)
            | Element::AttrList(s)
            | Element::MystRole(s) => display_len(s, mode),
            Element::WikiLink(s) => display_len(s, mode) + 4,
            Element::InlineMath(s) => display_len(s, mode) + 2,
            Element::DisplayMath(s) => display_len(s, mode) + 4,
            Element::EmojiShortcode(s) => display_len(s, mode) + 2,
            Element::Strikethrough { content, double } => display_len(content, mode) + if *double { 4 } else { 2 },
            Element::Code { content, marker } => display_len(content, mode) + display_len(marker, mode) * 2,
            Element::Bold { content, .. } => display_len(content, mode) + 4,
            Element::Italic { content, .. } => display_len(content, mode) + 2,
        }
    }

    /// The width the checker measures this element at, under each exemption.
    ///
    /// An inline link costs `[text]` and an inline image `![alt]`, exactly as
    /// `MD013`'s check computes them; a code span costs nothing. Every other
    /// element, reference and shortcut link forms included, costs its full
    /// width, because the check does not exempt those either.
    ///
    /// An element whose text cannot be delimited is charged in full. Charging
    /// too much only makes reflow wrap a line the check would have forgiven;
    /// charging too little would leave a line the check reports.
    fn exempt_width(&self, mode: ReflowLengthMode, exemptions: LengthExemptions) -> LineWidth {
        let full = self.display_len(mode);
        let mut width = LineWidth::plain(full);
        match self {
            Element::Link(s) | Element::LinkedImage(s) if exemptions.link_urls => {
                if let Some(text) = bracketed_text(s, 0) {
                    width.link_exempt = (2 + display_len(text, mode)).min(full);
                }
            }
            Element::InlineImage(s) if exemptions.link_urls => {
                if let Some(alt) = bracketed_text(s, 1) {
                    width.link_exempt = (3 + display_len(alt, mode)).min(full);
                }
            }
            Element::Code { .. } if exemptions.code_spans => width.code_exempt = 0,
            _ => {}
        }
        width
    }
}

/// The source between the `[` at byte `open` and its matching `]`.
///
/// Bracket matching follows the document parser: a nested `[` raises the depth,
/// a backslash-escaped bracket is literal, and a bracket inside a code span is
/// literal too, so `[a \] b](url)` and ``[a `]` b](url)`` are each one link.
/// Returns `None` when `open` is not a `[` or the bracket never closes.
fn bracketed_text(s: &str, open: usize) -> Option<&str> {
    let bytes = s.as_bytes();
    if bytes.get(open) != Some(&b'[') {
        return None;
    }
    let mut depth = 0usize;
    let mut in_code_span = false;
    let mut escaped = false;
    for (i, &byte) in bytes.iter().enumerate().skip(open + 1) {
        if escaped {
            escaped = false;
            continue;
        }
        match byte {
            b'\\' => escaped = true,
            b'`' => in_code_span = !in_code_span,
            b'[' if !in_code_span => depth += 1,
            b']' if !in_code_span => match depth.checked_sub(1) {
                Some(next) => depth = next,
                None => return s.get(open + 1..i),
            },
            _ => {}
        }
    }
    None
}

/// An emphasis or formatting span parsed by pulldown-cmark
#[derive(Debug, Clone)]
struct EmphasisSpan {
    /// Byte offset where the emphasis starts (including markers)
    start: usize,
    /// Byte offset where the emphasis ends (after closing markers)
    end: usize,
    /// The content inside the emphasis markers
    content: String,
    /// Whether this is strong (bold) emphasis
    is_strong: bool,
    /// Whether this is strikethrough (~~text~~)
    is_strikethrough: bool,
    /// Whether the original used underscore markers (for emphasis only)
    uses_underscore: bool,
    /// For strikethrough spans, whether the original used a double-tilde (~~)
    /// marker rather than a single tilde (~). Meaningless for other spans.
    strikethrough_double: bool,
}

/// Extract emphasis and strikethrough spans from text using pulldown-cmark
///
/// This provides CommonMark-compliant emphasis parsing, correctly handling:
/// - Nested emphasis like `*text **bold** more*`
/// - Left/right flanking delimiter rules
/// - Underscore vs asterisk markers
/// - GFM strikethrough (~~text~~)
///
/// Returns spans sorted by start position.
fn extract_emphasis_and_code_spans(text: &str) -> (Vec<EmphasisSpan>, Vec<CodeSpan>) {
    // If neither marker is present, skip the parser entirely.
    let has_emphasis = text.contains(['*', '_', '~']);
    let has_code = text.contains('`');
    if !has_emphasis && !has_code {
        return (Vec::new(), Vec::new());
    }

    let mut emphasis_spans = Vec::new();
    let mut code_spans = Vec::new();

    let mut options = Options::empty();
    if has_emphasis {
        options.insert(Options::ENABLE_STRIKETHROUGH);
    }

    // Stacks to track nested formatting with their start positions
    let mut emphasis_stack: Vec<(usize, bool)> = Vec::new(); // (start_byte, uses_underscore)
    let mut strong_stack: Vec<(usize, bool)> = Vec::new();
    let mut strikethrough_stack: Vec<usize> = Vec::new();

    let parser = Parser::new_ext(text, options).into_offset_iter();

    for (event, range) in parser {
        match event {
            Event::Code(_) => {
                code_spans.push(CodeSpan {
                    start: range.start,
                    end: range.end,
                });
            }
            Event::Start(Tag::Emphasis) => {
                // Check if this uses underscore by looking at the original text
                let uses_underscore = text.get(range.start..range.start + 1) == Some("_");
                emphasis_stack.push((range.start, uses_underscore));
            }
            Event::End(TagEnd::Emphasis) => {
                if let Some((start_byte, uses_underscore)) = emphasis_stack.pop() {
                    let content_start = start_byte + 1;
                    let content_end = range.end - 1;
                    if content_end > content_start
                        && let Some(content) = text.get(content_start..content_end)
                    {
                        emphasis_spans.push(EmphasisSpan {
                            start: start_byte,
                            end: range.end,
                            content: content.to_string(),
                            is_strong: false,
                            is_strikethrough: false,
                            uses_underscore,
                            strikethrough_double: false,
                        });
                    }
                }
            }
            Event::Start(Tag::Strong) => {
                let uses_underscore = text.get(range.start..range.start + 2) == Some("__");
                strong_stack.push((range.start, uses_underscore));
            }
            Event::End(TagEnd::Strong) => {
                if let Some((start_byte, uses_underscore)) = strong_stack.pop() {
                    let content_start = start_byte + 2;
                    let content_end = range.end - 2;
                    if content_end > content_start
                        && let Some(content) = text.get(content_start..content_end)
                    {
                        emphasis_spans.push(EmphasisSpan {
                            start: start_byte,
                            end: range.end,
                            content: content.to_string(),
                            is_strong: true,
                            is_strikethrough: false,
                            uses_underscore,
                            strikethrough_double: false,
                        });
                    }
                }
            }
            Event::Start(Tag::Strikethrough) => {
                strikethrough_stack.push(range.start);
            }
            Event::End(TagEnd::Strikethrough) => {
                if let Some(start_byte) = strikethrough_stack.pop() {
                    let double = text.get(start_byte..start_byte + 2) == Some("~~");
                    let marker_len = if double { 2 } else { 1 };
                    let content_start = start_byte + marker_len;
                    let content_end = range.end - marker_len;
                    if content_end > content_start
                        && let Some(content) = text.get(content_start..content_end)
                    {
                        emphasis_spans.push(EmphasisSpan {
                            start: start_byte,
                            end: range.end,
                            content: content.to_string(),
                            is_strong: false,
                            is_strikethrough: true,
                            uses_underscore: false,
                            strikethrough_double: double,
                        });
                    }
                }
            }
            _ => {}
        }
    }

    emphasis_spans.sort_by_key(|s| s.start);
    (emphasis_spans, code_spans)
}

#[derive(Debug, Clone)]
struct CodeSpan {
    start: usize,
    end: usize,
}

#[derive(Debug, Clone)]
struct LinkSpan {
    start: usize,
    end: usize,
    link_type: Option<LinkType>,
    is_image: bool,
    is_footnote: bool,
    /// How many links or images enclose this one. The image in
    /// `[![alt](img)](url)` sits at depth 1.
    depth: usize,
}

/// The outermost links, images and footnote references in `text`, sorted by
/// start. The top level holds each of these whole, so a construct nested in
/// another is covered by the one enclosing it.
fn extract_link_spans(text: &str, defined_references: Option<&HashSet<String>>) -> Vec<LinkSpan> {
    let mut spans = all_link_spans(text, defined_references);
    spans.retain(|span| span.depth == 0);
    spans
}

/// Every link, image and footnote reference in `text`, nested ones included,
/// sorted by start.
fn all_link_spans(text: &str, defined_references: Option<&HashSet<String>>) -> Vec<LinkSpan> {
    // Links, images, and footnote references all open with `[`; skip the
    // parser entirely without one.
    if !text.contains('[') {
        return Vec::new();
    }

    let mut spans = Vec::new();
    let mut options = Options::empty();
    options.insert(Options::ENABLE_FOOTNOTES);

    // Reflow parses each paragraph in isolation, so the document's reference
    // definitions are never in scope. Without a broken-link callback,
    // pulldown-cmark would emit reference-style links (`[text][ref]`,
    // `[text][]`, `[text]`, `![alt][ref]`) as plain text, and reflow would wrap
    // their text mid-link. Resolving an unresolved reference to a dummy
    // destination makes pulldown emit the full link span so reflow treats it as
    // an atomic unit; the destination is unused because the element is rebuilt
    // verbatim from the source bytes.
    //
    // Full and collapsed references and reference images carry explicit
    // `][ref]` / `[]` syntax, so they are always resolved (atomic). A bare
    // shortcut `[text]` is ambiguous: it is only a real link when its label is
    // actually defined. With `Some(defined_references)` an undefined shortcut is
    // left unresolved (returns `None`) so it reflows as literal prose; with
    // `None` (no reference info) every shortcut stays atomic, which never splits
    // a real link.
    let resolve = move |link: BrokenLink<'_>| -> Option<(CowStr<'_>, CowStr<'_>)> {
        // The callback reports the syntactic reference type (`Shortcut` for a
        // bare `[text]`); the eventual emitted tag carries the `*Unknown`
        // variant. Only a bare shortcut is ambiguous - full and collapsed
        // references fall through and stay atomic.
        let atomic = match link.link_type {
            LinkType::Shortcut | LinkType::ShortcutUnknown => match defined_references {
                Some(defs) => defs.contains(&normalize_reference_label(link.reference.as_ref())),
                None => true,
            },
            _ => true,
        };
        atomic.then_some((CowStr::Borrowed(""), CowStr::Borrowed("")))
    };
    let parser = Parser::new_with_broken_link_callback(text, options, Some(resolve)).into_offset_iter();
    let mut stack = Vec::new();

    for (event, range) in parser {
        match event {
            Event::Start(Tag::Link { link_type, .. }) => {
                stack.push((range.start, Some(link_type), false));
            }
            Event::Start(Tag::Image { link_type, .. }) => {
                stack.push((range.start, Some(link_type), true));
            }
            Event::End(TagEnd::Link | TagEnd::Image) => {
                if let Some((start_byte, link_type, is_image)) = stack.pop() {
                    spans.push(LinkSpan {
                        start: start_byte,
                        end: range.end,
                        link_type,
                        is_image,
                        is_footnote: false,
                        depth: stack.len(),
                    });
                }
            }
            Event::FootnoteReference(_) => {
                spans.push(LinkSpan {
                    start: range.start,
                    end: range.end,
                    link_type: None,
                    is_image: false,
                    is_footnote: true,
                    depth: stack.len(),
                });
            }
            _ => {}
        }
    }

    spans.sort_by_key(|s| s.start);
    spans
}

/// If `text` starts with a MyST inline role (`` {name}`content` `` or
/// `` {domain:role}`content` ``), return the byte length of the whole role unit.
///
/// Mirrors the grammar in `lint_context::flavor_detection::detect_myst_role_ranges`:
/// a `{`, a name starting with an ASCII letter or `_` and continuing with
/// alphanumerics / `-` / `_` / `:` / `.`, a closing `}`, then a balanced inline
/// code span using one or more backticks. Returns `None` when any part is missing.
fn myst_role_len_at(text: &str, absolute_pos: usize, code_spans: &[CodeSpan]) -> Option<usize> {
    let bytes = text.as_bytes();
    if bytes.first() != Some(&b'{') {
        return None;
    }

    // Role name.
    let mut j = 1;
    match bytes.get(j) {
        Some(&b) if b.is_ascii_alphabetic() || b == b'_' => {}
        _ => return None,
    }
    while let Some(&b) = bytes.get(j) {
        if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b':' | b'.') {
            j += 1;
        } else {
            break;
        }
    }
    if bytes.get(j) != Some(&b'}') {
        return None;
    }
    j += 1; // past '}'

    // Must be immediately followed by an inline code span.
    let code_span_start = absolute_pos + j;
    if let Ok(idx) = code_spans.binary_search_by_key(&code_span_start, |span| span.start) {
        let span = &code_spans[idx];
        let code_span_len = span.end - span.start;
        return Some(j + code_span_len);
    }

    None
}

/// Byte length of an inline-math span (`$math$`) starting at the very
/// beginning of `s`, if one starts there.
///
/// Mirrors INLINE_MATH_REGEX (`(?<!\$)\$(?!\$)([^\$]+)\$(?!\$)`) with the
/// leading lookbehind dropped: callers probe only at a slice start, where
/// the lookbehind passes vacuously.
fn inline_math_len_at_start(s: &str) -> Option<usize> {
    let bytes = s.as_bytes();
    // Opening `$` not followed by another `$` (that would be display math).
    if bytes.first() != Some(&b'$') || bytes.get(1) == Some(&b'$') {
        return None;
    }
    // Content is `[^$]+`: everything up to the closing `$`. It is non-empty
    // whenever a closing `$` exists, because the byte at index 1 is not `$`.
    let close = 1 + s[1..].find('$')?;
    // Closing `$` not followed by another `$`.
    if bytes.get(close + 1) == Some(&b'$') {
        return None;
    }
    Some(close + 1)
}

/// Absolute byte offsets of a cached pattern match within the full input text.
#[derive(Clone, Copy, Debug)]
struct PatternMatch {
    start: usize,
    end: usize,
}

/// Lazily-computed earliest match of one pattern within the unparsed suffix.
///
/// `parse_markdown_elements_inner` probes every pattern on every loop
/// iteration; re-running each search against the whole remaining suffix made
/// pathological inputs quadratic. The cache keeps the previous result as
/// absolute offsets: until the parse cursor moves past a cached match, that
/// match is still the earliest one, so the search is skipped.
///
/// This is sound only for patterns whose match at a given position does not
/// depend on where the searched slice starts (no `^`, no lookbehind): for
/// those, a cached miss stays a miss and a cached hit stays the earliest hit
/// as the cursor advances. A start-sensitive pattern needs a dedicated probe
/// at the cursor first (see the inline-math call site).
#[derive(Clone, Copy)]
enum PatternCache {
    Unsearched,
    NotFound,
    Found(PatternMatch),
}

impl PatternCache {
    /// Returns the earliest match at or after `cursor` as offsets relative to
    /// `remaining` (the unparsed suffix starting at `cursor`), re-running
    /// `find` on the suffix only when the cached result no longer applies.
    fn earliest_in(
        &mut self,
        remaining: &str,
        cursor: usize,
        find: impl FnOnce(&str) -> Option<(usize, usize)>,
    ) -> Option<(usize, usize)> {
        let stale = match self {
            PatternCache::Found(pm) => pm.start < cursor,
            PatternCache::NotFound => false,
            PatternCache::Unsearched => true,
        };
        if stale {
            *self = match find(remaining) {
                Some((start, end)) => PatternCache::Found(PatternMatch {
                    start: cursor + start,
                    end: cursor + end,
                }),
                None => PatternCache::NotFound,
            };
        }
        match self {
            PatternCache::Found(pm) => Some((pm.start - cursor, pm.end - cursor)),
            _ => None,
        }
    }
}

/// Parse markdown elements from text preserving the raw syntax.
///
/// Detection order is critical:
/// 1. Linked images [![alt](img)](link) - must be detected first as atomic units
/// 2. Inline images ![alt](url) - before links to handle ! prefix
/// 3. Reference images ![alt][ref] - before reference links
/// 4. Inline links [text](url) - before reference links
/// 5. Reference links [text][ref] - before shortcut references
/// 6. Shortcut reference links [ref] - detected last to avoid false positives
/// 7. Other elements (code, bold, italic, MyST roles, etc.) - processed normally
fn parse_markdown_elements_inner(
    text: &str,
    attr_lists: bool,
    myst_roles: bool,
    defined_references: Option<&HashSet<String>>,
) -> Vec<Element> {
    let mut elements = Vec::new();
    let mut remaining = text;

    // Pre-extract emphasis spans, link spans, and code spans using pulldown-cmark.
    // Emphasis and code spans are extracted in a single shared parse to reduce cmark overhead.
    // Link spans must run as a separate parse because link resolution (the broken-link
    // callback) changes bracket collapses, which shifts delimiter range boundaries.
    let (emphasis_spans, code_spans) = extract_emphasis_and_code_spans(text);
    let link_spans = extract_link_spans(text, defined_references);

    // One cache per probed pattern to avoid an O(N^2) worst case on long
    // inputs; see PatternCache for the validity rules.
    let mut cached_wiki_link = PatternCache::Unsearched;
    let mut cached_display_math = PatternCache::Unsearched;
    let mut cached_inline_math = PatternCache::Unsearched;
    let mut cached_emoji = PatternCache::Unsearched;
    let mut cached_html_entity = PatternCache::Unsearched;
    let mut cached_hugo_shortcode = PatternCache::Unsearched;
    let mut cached_html_tag = PatternCache::Unsearched;
    let mut cached_next_curly = PatternCache::Unsearched;

    // Cursor indices into the sorted span lists: spans behind the parse cursor
    // can never match again, so each list is advanced monotonically instead of
    // rescanned from the start on every iteration.
    let mut link_span_idx = 0usize;
    let mut emphasis_span_idx = 0usize;
    let mut code_span_idx = 0usize;

    while !remaining.is_empty() {
        // Calculate current byte offset in original text
        let current_offset = text.len() - remaining.len();
        // Find the earliest occurrence of any markdown pattern
        // Store (start, end, pattern_name) to unify regex and span-list results
        let mut earliest_match: Option<(usize, usize, &str)> = None;

        // Find the earliest link span
        while link_span_idx < link_spans.len() && link_spans[link_span_idx].start < current_offset {
            link_span_idx += 1;
        }
        let next_link: Option<&LinkSpan> = link_spans.get(link_span_idx);

        if let Some(span) = next_link {
            let pos_in_remaining = span.start - current_offset;
            if earliest_match
                .as_ref()
                .is_none_or(|(start, _, _)| pos_in_remaining < *start)
            {
                let match_end = span.end - current_offset;
                earliest_match = Some((pos_in_remaining, match_end, "link_span"));
            }
        }

        // Check for wiki-style links - [[wiki]]
        if let Some((start, end)) = cached_wiki_link.earliest_in(remaining, current_offset, |suffix| {
            WIKI_LINK_REGEX.find(suffix).map(|m| (m.start(), m.end()))
        }) && earliest_match.as_ref().is_none_or(|(s, _, _)| start < *s)
        {
            earliest_match = Some((start, end, "wiki_link"));
        }

        // Check for display math first (before inline) - $$math$$
        if let Some((start, end)) = cached_display_math.earliest_in(remaining, current_offset, |suffix| {
            DISPLAY_MATH_REGEX.find(suffix).map(|m| (m.start(), m.end()))
        }) && earliest_match.as_ref().is_none_or(|(s, _, _)| start < *s)
        {
            earliest_match = Some((start, end, "display_math"));
        }

        // Check for inline math - $math$
        // INLINE_MATH_REGEX opens with the lookbehind `(?<!\$)`, which is
        // slice-start-sensitive: at the start of the searched slice there is
        // no preceding character, so the lookbehind trivially passes, while
        // the cached search, anchored earlier, saw the real `$` predecessor
        // and can have rejected the same position. Positions past the cursor
        // are unaffected by where the slice starts, so the cache stays valid
        // for them; only a match beginning exactly at the cursor can be
        // missing from it. When the cursor sits directly after a `$`, probe
        // for that one match in place, leaving the cache untouched. (Either
        // rescanning the suffix here or storing the probe hit in the cache is
        // quadratic on math-heavy inputs: each consumed span would trigger a
        // fresh scan of everything that follows.)
        let inline_math_probe = if current_offset > 0 && text.as_bytes()[current_offset - 1] == b'$' {
            inline_math_len_at_start(remaining).map(|len| (0, len))
        } else {
            None
        };
        if let Some((start, end)) = inline_math_probe.or_else(|| {
            cached_inline_math.earliest_in(remaining, current_offset, |suffix| {
                INLINE_MATH_REGEX
                    .find(suffix)
                    .ok()
                    .flatten()
                    .map(|m| (m.start(), m.end()))
            })
        }) && earliest_match.as_ref().is_none_or(|(s, _, _)| start < *s)
        {
            earliest_match = Some((start, end, "inline_math"));
        }

        // Check for emoji shortcodes - :emoji:
        if let Some((start, end)) = cached_emoji.earliest_in(remaining, current_offset, |suffix| {
            EMOJI_SHORTCODE_REGEX.find(suffix).map(|m| (m.start(), m.end()))
        }) && earliest_match.as_ref().is_none_or(|(s, _, _)| start < *s)
        {
            earliest_match = Some((start, end, "emoji"));
        }

        // Check for HTML entities - &nbsp; etc
        if let Some((start, end)) = cached_html_entity.earliest_in(remaining, current_offset, |suffix| {
            HTML_ENTITY_REGEX.find(suffix).map(|m| (m.start(), m.end()))
        }) && earliest_match.as_ref().is_none_or(|(s, _, _)| start < *s)
        {
            earliest_match = Some((start, end, "html_entity"));
        }

        // Check for Hugo shortcodes - {{< ... >}} or {{% ... %}}
        // Must be checked before other patterns to avoid false sentence breaks
        if let Some((start, end)) = cached_hugo_shortcode.earliest_in(remaining, current_offset, |suffix| {
            HUGO_SHORTCODE_REGEX.find(suffix).map(|m| (m.start(), m.end()))
        }) && earliest_match.as_ref().is_none_or(|(s, _, _)| start < *s)
        {
            earliest_match = Some((start, end, "hugo_shortcode"));
        }

        // Check for HTML tags - <tag> </tag> <tag/>
        // But exclude autolinks like <https://...> or <mailto:...> or email
        // autolinks <user@domain.com>: those are left for link_span handling.
        // The search skips past autolinks instead of giving up so the cache
        // lands on the first real tag; bailing out at an autolink would re-run
        // this scan from the same spot on every iteration.
        if let Some((start, end)) = cached_html_tag.earliest_in(remaining, current_offset, |suffix| {
            let mut from = 0;
            while let Some(m) = HTML_TAG_PATTERN.find(&suffix[from..]) {
                let (tag_start, tag_end) = (from + m.start(), from + m.end());
                let tag = &suffix[tag_start..tag_end];
                // Autolink starting with a protocol or mailto:?
                let is_url_autolink = tag.starts_with("<http://")
                    || tag.starts_with("<https://")
                    || tag.starts_with("<mailto:")
                    || tag.starts_with("<ftp://")
                    || tag.starts_with("<ftps://");
                // Email autolink (per CommonMark spec: <local@domain.tld>)?
                // Use centralized EMAIL_PATTERN for consistency with MD034 and other rules
                let is_email_autolink = {
                    let content = tag.trim_start_matches('<').trim_end_matches('>');
                    EMAIL_PATTERN.is_match(content)
                };
                if is_url_autolink || is_email_autolink {
                    from = tag_end;
                } else {
                    return Some((tag_start, tag_end));
                }
            }
            None
        }) && earliest_match.as_ref().is_none_or(|(s, _, _)| start < *s)
        {
            earliest_match = Some((start, end, "html_tag"));
        }

        // Find earliest non-link special characters
        let mut next_special = remaining.len();
        let mut special_type = "";
        let mut pulldown_emphasis: Option<&EmphasisSpan> = None;
        let mut attr_list_len: usize = 0;
        let mut myst_role_len: usize = 0;

        // Check for code spans using pulldown-cmark pre-extracted spans
        while code_span_idx < code_spans.len() && code_spans[code_span_idx].start < current_offset {
            code_span_idx += 1;
        }
        let next_code_span: Option<&CodeSpan> = code_spans.get(code_span_idx);
        if let Some(span) = next_code_span {
            let pos_in_remaining = span.start - current_offset;
            if pos_in_remaining < next_special {
                next_special = pos_in_remaining;
                special_type = "pulldown_code";
            }
        }

        // Position of the next `{`, shared by the MyST-role and attr-list
        // probes below
        let next_curly_pos = cached_next_curly
            .earliest_in(remaining, current_offset, |suffix| {
                suffix.find('{').map(|pos| (pos, pos + 1))
            })
            .map(|(start, _)| start);

        // Check for MyST inline roles - {role}`content` (e.g. {cite:p}`ref`).
        // Checked before the bare code-span handling so the role's trailing code
        // span is absorbed into the atomic role rather than split off, and before
        // attr lists since a role's `{` would otherwise be probed as an attr list.
        if myst_roles
            && let Some(pos) = next_curly_pos
            && pos < next_special
            && let Some(role_len) = myst_role_len_at(&remaining[pos..], current_offset + pos, &code_spans)
        {
            next_special = pos;
            special_type = "myst_role";
            myst_role_len = role_len;
        }

        // Check for MkDocs/kramdown attr lists - {#id .class key="value"}
        if attr_lists
            && let Some(pos) = next_curly_pos
            && pos < next_special
            && let Some(m) = ATTR_LIST_PATTERN.find(&remaining[pos..])
            && m.start() == 0
        {
            next_special = pos;
            special_type = "attr_list";
            attr_list_len = m.end();
        }

        // Check for emphasis using pulldown-cmark's pre-extracted spans
        while emphasis_span_idx < emphasis_spans.len() && emphasis_spans[emphasis_span_idx].start < current_offset {
            emphasis_span_idx += 1;
        }
        if let Some(span) = emphasis_spans.get(emphasis_span_idx) {
            let pos_in_remaining = span.start - current_offset;
            if pos_in_remaining < next_special {
                next_special = pos_in_remaining;
                special_type = "pulldown_emphasis";
                pulldown_emphasis = Some(span);
            }
        }

        // Determine which pattern to process first
        let should_process_markdown_link = if let Some((pos, _, _)) = earliest_match {
            pos < next_special
        } else {
            false
        };

        if should_process_markdown_link {
            let (pos, match_end, pattern_type) = earliest_match.unwrap();

            // Add any text before the match
            if pos > 0 {
                elements.push(Element::Text(remaining[..pos].to_string()));
            }

            // Process the matched pattern
            match pattern_type {
                "link_span" => {
                    let span = next_link.unwrap();
                    let raw_text = remaining[pos..match_end].to_string();
                    if span.is_footnote {
                        elements.push(Element::FootnoteReference(raw_text));
                    } else if span.is_image {
                        match span.link_type {
                            Some(LinkType::Inline) => elements.push(Element::InlineImage(raw_text)),
                            // `*Unknown` variants are produced when reflow's broken-link
                            // callback resolves a reference whose definition is out of scope.
                            Some(LinkType::Reference)
                            | Some(LinkType::ReferenceUnknown)
                            | Some(LinkType::Shortcut)
                            | Some(LinkType::ShortcutUnknown) => elements.push(Element::ReferenceImage(raw_text)),
                            Some(LinkType::Collapsed) | Some(LinkType::CollapsedUnknown) => {
                                elements.push(Element::EmptyReferenceImage(raw_text))
                            }
                            _ => elements.push(Element::InlineImage(raw_text)),
                        }
                    } else {
                        match span.link_type {
                            Some(LinkType::Inline) => {
                                if raw_text.starts_with('[') && raw_text.contains("![") {
                                    elements.push(Element::LinkedImage(raw_text));
                                } else {
                                    elements.push(Element::Link(raw_text));
                                }
                            }
                            // `*Unknown` variants are produced when reflow's broken-link
                            // callback resolves a reference whose definition is out of scope.
                            Some(LinkType::Reference) | Some(LinkType::ReferenceUnknown) => {
                                elements.push(Element::ReferenceLink(raw_text))
                            }
                            Some(LinkType::Collapsed) | Some(LinkType::CollapsedUnknown) => {
                                elements.push(Element::EmptyReferenceLink(raw_text))
                            }
                            Some(LinkType::Shortcut) | Some(LinkType::ShortcutUnknown) => {
                                elements.push(Element::ShortcutReference(raw_text))
                            }
                            Some(LinkType::Autolink) | Some(LinkType::Email) => {
                                elements.push(Element::Autolink(raw_text))
                            }
                            _ => elements.push(Element::Link(raw_text)),
                        }
                    }
                    remaining = &remaining[match_end..];
                }
                "wiki_link" => {
                    if let Some(caps) = WIKI_LINK_REGEX.captures(remaining) {
                        let content = caps.get(1).map_or("", |m| m.as_str());
                        elements.push(Element::WikiLink(content.to_string()));
                        remaining = &remaining[match_end..];
                    } else {
                        elements.push(Element::Text("[[".to_string()));
                        remaining = &remaining[2..];
                    }
                }
                "display_math" => {
                    if let Some(caps) = DISPLAY_MATH_REGEX.captures(remaining) {
                        let math = caps.get(1).map_or("", |m| m.as_str());
                        elements.push(Element::DisplayMath(math.to_string()));
                        remaining = &remaining[match_end..];
                    } else {
                        elements.push(Element::Text("$$".to_string()));
                        remaining = &remaining[2..];
                    }
                }
                "inline_math" => {
                    if let Ok(Some(caps)) = INLINE_MATH_REGEX.captures(remaining) {
                        let math = caps.get(1).map_or("", |m| m.as_str());
                        elements.push(Element::InlineMath(math.to_string()));
                        remaining = &remaining[match_end..];
                    } else {
                        elements.push(Element::Text("$".to_string()));
                        remaining = &remaining[1..];
                    }
                }
                "emoji" => {
                    if let Some(caps) = EMOJI_SHORTCODE_REGEX.captures(remaining) {
                        let emoji = caps.get(1).map_or("", |m| m.as_str());
                        elements.push(Element::EmojiShortcode(emoji.to_string()));
                        remaining = &remaining[match_end..];
                    } else {
                        elements.push(Element::Text(":".to_string()));
                        remaining = &remaining[1..];
                    }
                }
                "html_entity" => {
                    // HTML entities are captured whole
                    elements.push(Element::HtmlEntity(remaining[pos..match_end].to_string()));
                    remaining = &remaining[match_end..];
                }
                "hugo_shortcode" => {
                    // Hugo shortcodes are atomic elements - preserve them exactly
                    elements.push(Element::HugoShortcode(remaining[pos..match_end].to_string()));
                    remaining = &remaining[match_end..];
                }
                "html_tag" => {
                    // HTML tags are captured whole
                    elements.push(Element::HtmlTag(remaining[pos..match_end].to_string()));
                    remaining = &remaining[match_end..];
                }
                _ => unreachable!("unknown pattern type: {}", pattern_type),
            }
        } else {
            // Process non-link special characters

            // Add any text before the special character
            if next_special > 0 && next_special < remaining.len() {
                elements.push(Element::Text(remaining[..next_special].to_string()));
                remaining = &remaining[next_special..];
            }

            // Process the special element
            match special_type {
                "pulldown_code" => {
                    let span = next_code_span.unwrap();
                    let span_len = span.end - span.start;
                    let code_raw = &remaining[..span_len];
                    if let Some((content, marker)) = decompose_code_span(code_raw) {
                        elements.push(Element::Code {
                            content: content.to_string(),
                            marker: marker.to_string(),
                        });
                    } else {
                        elements.push(Element::Text(code_raw.to_string()));
                    }
                    remaining = &remaining[span_len..];
                }
                "attr_list" => {
                    elements.push(Element::AttrList(remaining[..attr_list_len].to_string()));
                    remaining = &remaining[attr_list_len..];
                }
                "myst_role" => {
                    elements.push(Element::MystRole(remaining[..myst_role_len].to_string()));
                    remaining = &remaining[myst_role_len..];
                }
                "pulldown_emphasis" => {
                    // Use pre-extracted emphasis/strikethrough span from pulldown-cmark
                    let span = pulldown_emphasis.expect("pulldown_emphasis must be set");
                    let span_len = span.end - span.start;
                    if span.is_strikethrough {
                        elements.push(Element::Strikethrough {
                            content: span.content.clone(),
                            double: span.strikethrough_double,
                        });
                    } else if span.is_strong {
                        elements.push(Element::Bold {
                            content: span.content.clone(),
                            underscore: span.uses_underscore,
                        });
                    } else {
                        elements.push(Element::Italic {
                            content: span.content.clone(),
                            underscore: span.uses_underscore,
                        });
                    }
                    remaining = &remaining[span_len..];
                }
                _ => {
                    // No special elements found, add all remaining text
                    elements.push(Element::Text(remaining.to_string()));
                    break;
                }
            }
        }
    }

    // Merge contiguous text elements to clean up the output.
    let mut merged_elements = Vec::new();
    for el in elements {
        match el {
            Element::Text(s) => {
                if let Some(Element::Text(last_s)) = merged_elements.last_mut() {
                    last_s.push_str(&s);
                } else {
                    merged_elements.push(Element::Text(s));
                }
            }
            other => merged_elements.push(other),
        }
    }
    merged_elements
}

/// The whitespace the source put in front of the element at `idx`, as reflow
/// should re-emit it.
///
/// A span carries no whitespace of its own, so the gap before it lives at the
/// end of the text element preceding it, which the sentence and clause paths
/// trim away as they accumulate. Reading the gap back from the source is what
/// keeps a standalone `-` from being glued onto the span after it: the
/// characters a line happens to end with cannot tell a dash that closes a word
/// from one that stands alone, and the same holds for a bracket or paren.
///
/// A run of breakable whitespace renders as one space and comes back as one. A
/// non-breaking space is a character the reader sees, so a gap containing one
/// is carried through exactly as written.
fn source_gap_before(elements: &[Element], idx: usize) -> &str {
    let Some(Element::Text(previous)) = idx.checked_sub(1).map(|prev| &elements[prev]) else {
        return "";
    };

    let gap = &previous[previous.trim_end_matches(char::is_whitespace).len()..];
    if gap.is_empty() {
        ""
    } else if gap.contains(is_non_breaking_space) {
        gap
    } else {
        " "
    }
}

/// Open `gap` before the element about to be appended, unless the line has
/// nothing for it to follow or already ends with whitespace of its own.
fn push_source_gap(current_line: &mut String, gap: &str) {
    if !gap.is_empty() && !current_line.is_empty() && !current_line.ends_with(char::is_whitespace) {
        current_line.push_str(gap);
    }
}

/// True when `text` consists solely of setext-underline or thematic-break
/// characters: a run of `=` or `-` (setext underline, any count, no internal
/// spaces) or 3+ `-`/`*`/`_` optionally separated by spaces (thematic break).
/// A paragraph-continuation line like this converts the previous line into a
/// heading or inserts a horizontal rule.
fn is_setext_or_thematic(text: &str) -> bool {
    let mut marker = 0u8;
    let mut count = 0usize;
    let mut has_space = false;
    for &b in text.as_bytes() {
        match b {
            b' ' | b'\t' => has_space = true,
            b'-' | b'=' | b'*' | b'_' => {
                if marker == 0 {
                    marker = b;
                } else if b != marker {
                    return false;
                }
                count += 1;
            }
            _ => return false,
        }
    }
    match marker {
        b'=' => !has_space,
        b'-' => !has_space || count >= 3,
        b'*' | b'_' => count >= 3,
        _ => false,
    }
}

/// True when `text`, placed at the start of a paragraph-continuation line,
/// would be re-parsed as opening a block construct - a list item (`- `, `* `,
/// `+ `, `1. `, `1) `), blockquote (`>`), ATX heading (`# `), code fence
/// (3+ backticks or tildes), thematic break, setext underline, footnote or
/// link-reference definition (`[^note]:`, `[label]: url`), or HTML block
/// (`<div>` and the other block-level tags rumdl's parser recognizes).
/// Reflow must never start a wrapped line with such content: prose that was
/// harmless mid-line becomes real block syntax at line start, silently
/// changing the document's structure (a `- ` clause becomes a nested list
/// item, a `# ` becomes a heading, a `[ref]: url` turns a dangling reference
/// elsewhere in the document into a live link, and so on).
fn starts_block_construct(text: &str) -> bool {
    let text = text.trim_start();
    let bytes = text.as_bytes();
    let Some(&first) = bytes.first() else {
        return false;
    };
    let marker_then_boundary = |len: usize| bytes.len() == len || bytes[len] == b' ' || bytes[len] == b'\t';
    match first {
        // A blockquote marker needs no following space
        b'>' => true,
        b'-' | b'*' | b'+' => marker_then_boundary(1) || is_setext_or_thematic(text),
        b'_' | b'=' => is_setext_or_thematic(text),
        b':' => is_definition_list_item(text) || text.starts_with(":::"),
        b'|' => true,
        b'#' => {
            let hashes = bytes.iter().take_while(|&&b| b == b'#').count();
            hashes <= 6 && marker_then_boundary(hashes)
        }
        b'`' => bytes.iter().take_while(|&&b| b == b'`').count() >= 3,
        b'~' => bytes.iter().take_while(|&&b| b == b'~').count() >= 3,
        // An ordered list is the one construct here that cannot always
        // interrupt a paragraph: it does so only when it is numbered 1 and its
        // first item has content. `7. item` and a bare `123456.` are prose to
        // the parser, so guarding them would refuse a legal wrap and leave an
        // unfixable long line. Leading zeros still make the number 1 (`01.`),
        // and a marker is at most 9 digits.
        b'0'..=b'9' => {
            let digits = bytes.iter().take_while(|b| b.is_ascii_digit()).count();
            digits <= 9
                && text[..digits].trim_start_matches('0') == "1"
                && bytes.len() > digits + 1
                && (bytes[digits] == b'.' || bytes[digits] == b')')
                && (bytes[digits + 1] == b' ' || bytes[digits + 1] == b'\t')
        }
        // Footnote/link-reference definition: `[label]:` anchored at line
        // start, meaning the label's own closing bracket is immediately
        // followed by a colon ("[ref]: url", "[^1]: note" - but not
        // "[a](b) [ref]:", whose first bracket is an inline link). rumdl's
        // parser recognizes definitions even on paragraph-continuation lines,
        // so hoisting one to line start reclassifies it (and can resolve
        // dangling references elsewhere in the document).
        b'[' => {
            let mut escaped = false;
            let mut label_close = None;
            for (i, &b) in bytes.iter().enumerate().skip(1) {
                if escaped {
                    escaped = false;
                } else if b == b'\\' {
                    escaped = true;
                } else if b == b']' {
                    label_close = Some(i);
                    break;
                }
            }
            label_close.is_some_and(|i| bytes.get(i + 1) == Some(&b':'))
        }
        // Block-level HTML tag per rumdl's parser (shared predicate, so the
        // guard cannot drift from what lint_context classifies as a block).
        b'<' => crate::utils::html_block::parse_html_block_start(text).is_some(),
        _ => false,
    }
}

/// Merge any reflowed continuation line that would open a block construct back
/// into the previous line. This is the safety net behind the per-break-site
/// guards: no matter which emitter produced the lines, a wrapped continuation
/// must never turn prose into a list item, heading, blockquote, code fence, or
/// horizontal rule. The first line keeps its position - it replaces the
/// paragraph's original start, where the source already established the
/// context. The merged line may exceed the configured width; a long line is
/// the correct failure direction, corrupted structure is not.
fn merge_block_construct_continuations(lines: Vec<String>) -> Vec<String> {
    let mut merged: Vec<String> = Vec::with_capacity(lines.len());
    for line in lines {
        merged.push(line);
        // A merge can itself produce an opener: a line holding just `1.` is
        // inert on its own, but absorbing a following `[ref]:` turns it into
        // `1. [ref]:`, a real list item. Keep folding until the tail is inert.
        while merged.len() > 1 && starts_block_construct(merged.last().expect("non-empty")) {
            let last = merged.pop().expect("non-empty");
            let prev = merged.last_mut().expect("len > 1");
            prev.push(' ');
            prev.push_str(last.trim_start());
        }
    }
    merged
}

/// Reflow elements for sentence-per-line mode
fn reflow_elements_sentence_per_line(elements: &[Element], options: &ReflowOptions) -> Vec<String> {
    let abbreviations = get_abbreviations(&options.abbreviations);
    let require_sentence_capital = options.require_sentence_capital;
    let mut lines = Vec::new();
    let mut current_line = String::new();

    for (idx, element) in elements.iter().enumerate() {
        // Text and emphasis are absorbed the same way. An emphasis span is
        // rendered back to its source form and then treated as ordinary text,
        // so a sentence boundary inside it breaks the line without closing and
        // reopening the markers: a line break inside a span is whitespace, and
        // whitespace is all a reflow is allowed to change.
        let is_span = matches!(
            element,
            Element::Italic { .. } | Element::Bold { .. } | Element::Strikethrough { .. }
        );
        let piece = match element {
            // Text already carries its own spacing from tokenization.
            Element::Text(text) => Some(text.clone()),
            Element::Italic { content, underscore } => Some(wrap_emphasis(
                content,
                if *underscore { "_" } else { "*" },
                &mut current_line,
                source_gap_before(elements, idx),
            )),
            Element::Bold { content, underscore } => Some(wrap_emphasis(
                content,
                if *underscore { "__" } else { "**" },
                &mut current_line,
                source_gap_before(elements, idx),
            )),
            Element::Strikethrough { content, double } => Some(wrap_emphasis(
                content,
                if *double { "~~" } else { "~" },
                &mut current_line,
                source_gap_before(elements, idx),
            )),
            _ => None,
        };

        if let Some(piece) = piece {
            // Where the piece lands in the combined line. A span begins with its
            // own opening marker, which the splitter must not read as the marker
            // closing the sentence in front of it.
            let appended_span_start = is_span.then_some(current_line.len());
            let combined = format!("{current_line}{piece}");
            // Use the pre-computed abbreviations set to avoid redundant computation
            let sentences = split_into_sentences_with_set(
                &combined,
                &abbreviations,
                require_sentence_capital,
                appended_span_start,
                options.defined_references.as_ref(),
            );

            // A bracketed element right after the piece may hold the sentence
            // in front of it open: `Claim ends here. [smith](url) continues.`
            // is one sentence to the splitter, since the link's text starts
            // with a lowercase letter, and so is `Claim ends here. [Smith 2020]`.
            // The splitter decides by reading the sentence with the element
            // appended, so the check counting sentences and this reflow agree
            // on where the line breaks. Every other kind of element closes the
            // sentence in front of it.
            let next_bracketed = elements
                .get(idx + 1)
                .filter(|next| next.opens_with_bracket())
                .map(|next| (source_gap_before(elements, idx + 1), next.to_string()));
            let closes_before_next = |sentence: &str| -> bool {
                let Some((gap, next_str)) = &next_bracketed else {
                    return true;
                };
                let mut probe = sentence.to_string();
                push_source_gap(&mut probe, gap);
                probe.push_str(next_str);
                let probe_sentences = split_into_sentences_with_set(
                    &probe,
                    &abbreviations,
                    require_sentence_capital,
                    None,
                    options.defined_references.as_ref(),
                );
                probe_sentences.last().is_some_and(|last| last == next_str)
            };

            if sentences.len() > 1 {
                // Accumulate rather than emit-and-overwrite: a sentence held
                // back for the next element must absorb what follows it, or the
                // text that follows would reach the output ahead of it.
                let mut pending = String::new();
                let last = sentences.len() - 1;
                for (i, sentence) in sentences.iter().enumerate() {
                    if !pending.is_empty() {
                        pending.push(' ');
                    }
                    pending.push_str(sentence);

                    // The splitter already decided every boundary except the
                    // final one, which is just the leftover tail. Hold a tail
                    // that no punctuation closed, and hold any piece ending in
                    // an abbreviation the splitter broke after regardless.
                    let closed = i < last || (ends_with_sentence_punct(&pending) && closes_before_next(&pending));
                    if closed && !text_ends_with_abbreviation(&pending, &abbreviations) {
                        lines.push(std::mem::take(&mut pending));
                    }
                }
                current_line = pending;
            } else {
                // Single sentence - check if it's complete
                let trimmed = combined.trim();

                // If the combined result is only whitespace, don't accumulate it.
                // This prevents leading spaces on subsequent elements when lines
                // are joined with spaces during reflow iteration.
                if trimmed.is_empty() {
                    continue;
                }

                let ends_with_sentence_punct = ends_with_sentence_punct(trimmed);

                if ends_with_sentence_punct
                    && !text_ends_with_abbreviation(trimmed, &abbreviations)
                    && closes_before_next(trimmed)
                {
                    // Complete single sentence - emit it (trimming only
                    // breakable whitespace so edge NBSPs survive)
                    lines.push(combined.trim_matches(is_breakable_whitespace).to_string());
                    current_line.clear();
                } else {
                    // Incomplete sentence - continue accumulating
                    current_line = combined;
                }
            }
        } else {
            // Non-text, non-emphasis elements (Code, Links, etc.)
            let element_str = format!("{element}");
            push_source_gap(&mut current_line, source_gap_before(elements, idx));
            current_line.push_str(&element_str);
        }
    }

    // Add any remaining content
    if !current_line.is_empty() {
        lines.push(current_line.trim_matches(is_breakable_whitespace).to_string());
    }
    lines
}

/// Restore an emphasis span to its source form, opening the gap the source had
/// in front of it. Unlike text elements, a span carries no surrounding
/// whitespace of its own.
fn wrap_emphasis(content: &str, marker: &str, current_line: &mut String, gap: &str) -> String {
    push_source_gap(current_line, gap);
    format!("{marker}{content}{marker}")
}

/// English break-words used for semantic line break splitting.
/// These are conjunctions and relative pronouns where a line break
/// reads naturally.
const BREAK_WORDS: &[&str] = &[
    "and",
    "or",
    "but",
    "nor",
    "yet",
    "so",
    "for",
    "which",
    "that",
    "because",
    "when",
    "if",
    "while",
    "where",
    "although",
    "though",
    "unless",
    "since",
    "after",
    "before",
    "until",
    "as",
    "once",
    "whether",
    "however",
    "therefore",
    "moreover",
    "furthermore",
    "nevertheless",
    "whereas",
];

/// Check if a character is clause punctuation for semantic line breaks
fn is_clause_punctuation(c: char) -> bool {
    matches!(c, ',' | ';' | ':' | '\u{2014}') // comma, semicolon, colon, em dash
}

/// Whether a clause-punctuation char at `chars[i]` is a legitimate break point.
///
/// A real clause boundary is followed by breakable whitespace (or ends the
/// text). Two reasons, and they agree: `,;:` with no following space sit
/// *inside* a token (`16:9`, `key:value`, a MyST role like `{cite:p}`), and a
/// line break renders as a space, so breaking where the source has none inserts
/// one. That holds for the em dash too: `cost—benefit` renders as one word,
/// `cost—\nbenefit` as two. A non-breaking space is not a boundary either: it
/// exists to forbid the break, so the scan keeps looking for an earlier one.
fn clause_break_allowed_after(chars: &[char], i: usize) -> bool {
    match chars.get(i + 1) {
        None => true,
        Some(next) => is_breakable_whitespace(*next),
    }
}

/// Find the closing `)` that balances the `(` at the start of `slice`.
///
/// `offset` is the byte position of the `(` in the original full-line string;
/// it is used to translate local byte positions into global positions for
/// element-span lookups.  Parens inside markdown element spans are skipped so
/// that, e.g., the closing `)` of an inline link does not prematurely end the
/// scan.  The char's *start* byte (not byte-after) is used for the span check
/// so that closing element delimiters — which sit exactly at the span's
/// exclusive-end boundary — are correctly excluded.
///
/// Returns `(end_local, inner)` where `end_local` is the byte offset within
/// `slice` just past the closing `)`, and `inner` is the content between the
/// outermost `(` and `)`.
fn paren_group_end<'a>(slice: &'a str, element_spans: &[ElementSpan], offset: usize) -> Option<(usize, &'a str)> {
    debug_assert!(slice.starts_with('('));
    let mut depth: i32 = 0;
    for (local_byte, c) in slice.char_indices() {
        let global_byte = offset + local_byte;
        // When depth > 0, skip parens that belong to a markdown element.
        // Use the char's start byte so that a closing element delimiter
        // (whose byte_after equals the span's exclusive end) is treated as
        // inside the element rather than outside it.
        if depth > 0 && is_inside_element(global_byte, element_spans) {
            continue;
        }
        match c {
            '(' => depth += 1,
            ')' => {
                depth -= 1;
                if depth == 0 {
                    let end = local_byte + 1;
                    let inner = &slice[1..local_byte];
                    return Some((end, inner));
                }
            }
            _ => {}
        }
    }
    None
}

/// Split a line at a parenthetical boundary for semantic line breaks.
///
/// Two strategies are tried in order:
///
/// 1. **Leading parenthetical** — if the line begins with `(`, isolate the
///    entire balanced group on this line and start the rest on the next.
///    This handles lines produced by a prior split that placed a `(` at the
///    very beginning.
///
/// 2. **Mid-line parenthetical** — find the rightmost balanced `(…)` whose
///    content spans multiple words and whose preceding text fits within
///    `[min_first_len, line_length]`.  Split just before the `(` so the
///    parenthetical begins the following line.
///
/// Parentheses that fall inside markdown element spans (links, code, etc.)
/// are ignored in both strategies.
fn split_at_parenthetical(
    text: &str,
    line_length: usize,
    element_spans: &[ElementSpan],
    length_mode: ReflowLengthMode,
) -> Option<(String, String)> {
    let min_first_len = ((line_length as f64) * MIN_SPLIT_RATIO) as usize;

    // Strategy 1: text starts with '(' — isolate the parenthetical as its own line.
    if text.starts_with('(')
        && let Some((end_local, inner)) = paren_group_end(text, element_spans, 0)
        && inner.contains(' ')
    {
        // Whatever follows the closing ')' up to the next breakable whitespace
        // belongs to the parenthetical: a break there would render as a space the
        // text does not have, and it would orphan the punctuation (`).`, `),`,
        // `)"`) at the head of the continuation line. Whitespace inside an inline
        // element is that element's own content, so a boundary landing there is
        // no boundary at all and the scan resumes past the element.
        let mut first_end = end_local;
        loop {
            first_end += text[first_end..]
                .char_indices()
                .take_while(|(_, c)| !is_breakable_whitespace(*c))
                .last()
                .map_or(0, |(idx, c)| idx + c.len_utf8());
            match element_containing(first_end, element_spans) {
                Some(span) => first_end = span.end,
                None => break,
            }
        }
        let rest_start = first_end;
        let first = &text[..first_end];
        // No MIN_SPLIT_RATIO check: a parenthetical unit is always a valid
        // semantic line regardless of its length.
        if measure(first, 0, element_spans, length_mode).fits(line_length) {
            let rest = text[rest_start..].trim_start();
            if !rest.is_empty() {
                return Some((first.to_string(), rest.to_string()));
            }
        }
    }

    // Strategy 2: find the rightmost multi-word '(' whose preceding text fits.
    let mut best_open_byte: Option<usize> = None;
    let mut pos = 0usize;
    while pos < text.len() {
        // '(' is ASCII so a single-byte comparison is safe in UTF-8.
        if text.as_bytes()[pos] != b'(' {
            let c = text[pos..].chars().next().unwrap();
            pos += c.len_utf8();
            continue;
        }
        // Skip '(' that are part of a markdown element (use start byte).
        if is_inside_element(pos, element_spans) {
            pos += 1;
            continue;
        }
        if let Some((end_local, inner)) = paren_group_end(&text[pos..], element_spans, pos) {
            let first = text[..pos].trim_end_matches(is_breakable_whitespace);
            let first_len = measure(first, 0, element_spans, length_mode).effective();
            // The '(' must follow breakable whitespace: splitting `f(a b)` into
            // `f` and `(a b)` would render as `f (a b)`.
            if first.len() < pos
                && !first.is_empty()
                && first_len >= min_first_len
                && first_len <= line_length
                && inner.contains(' ')
                && best_open_byte.is_none_or(|prev| pos > prev)
            {
                best_open_byte = Some(pos);
            }
            pos += end_local;
        } else {
            pos += 1;
        }
    }

    let open_byte = best_open_byte?;
    let first = text[..open_byte].trim_end_matches(is_breakable_whitespace).to_string();
    let rest = text[open_byte..].to_string();
    if first.is_empty() || rest.trim().is_empty() {
        return None;
    }
    Some((first, rest))
}

/// A non-Text element's byte span in a flat text representation, with the
/// columns each of the checker's exemptions forgives it.
///
/// The offsets exist so a split position can be kept out of an element; the
/// savings exist so a substring can be measured the way the checker measures it.
/// Both are computed from the same walk over the same elements, which is what
/// keeps them consistent.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct ElementSpan {
    start: usize,
    end: usize,
    full: usize,
    /// Columns the link/image URL exemption forgives, zero when it is off or
    /// does not apply to this element.
    link_saving: usize,
    /// Columns the code-span exemption forgives, on the same terms.
    code_saving: usize,
    /// Whether this element is hard atomic (cannot be broken even as fallback)
    is_hard: bool,
}

impl ElementSpan {
    /// A span covering `len` bytes from `start`, for an element whose full
    /// width is `full` and whose exempt widths are `width`.
    fn new(start: usize, len: usize, full: usize, width: LineWidth, is_hard: bool) -> Self {
        Self {
            start,
            end: start + len,
            full,
            link_saving: full - width.link_exempt,
            code_saving: full - width.code_exempt,
            is_hard,
        }
    }

    fn contains(&self, pos: usize) -> bool {
        pos > self.start && pos < self.end
    }

    fn within(&self, start: usize, end: usize) -> bool {
        self.start >= start && self.end <= end
    }

    fn exempt_width(&self) -> LineWidth {
        LineWidth {
            link_exempt: self.full - self.link_saving,
            code_exempt: self.full - self.code_saving,
        }
    }
}

/// Compute element spans for a flat text representation of elements.
///
/// The offsets are byte positions, so they are always measured in
/// [`ReflowLengthMode::Bytes`] regardless of how lines are measured; only the
/// savings depend on `mode` and the active exemptions.
fn compute_element_spans(
    elements: &[Element],
    mode: ReflowLengthMode,
    exemptions: LengthExemptions,
) -> Vec<ElementSpan> {
    let mut spans = Vec::new();
    let mut offset = 0;
    for element in elements {
        let len = element.display_len(ReflowLengthMode::Bytes);
        if !matches!(element, Element::Text(_)) {
            let full = element.display_len(mode);
            let width = element.exempt_width(mode, exemptions);
            let is_hard = match element {
                Element::Bold { content, .. }
                | Element::Italic { content, .. }
                | Element::Strikethrough { content, .. } => content.contains(['[', '`', '<', '$', '{']),
                _ => true,
            };
            spans.push(ElementSpan::new(offset, len, full, width, is_hard));
        }
        offset += len;
    }
    spans
}

/// Width of `text`, which sits at `[offset, offset + text.len())` of the line the
/// spans were computed for, under each exemption separately.
///
/// Only elements lying wholly inside the range are discounted. A split never
/// lands inside an element, so a partially covered element means the caller is
/// measuring something that is not a candidate line, and charging it in full is
/// the safe reading.
fn measure(text: &str, offset: usize, spans: &[ElementSpan], mode: ReflowLengthMode) -> LineWidth {
    let full = display_len(text, mode);
    let end = offset + text.len();
    let mut width = LineWidth::plain(full);
    for span in spans.iter().filter(|span| span.within(offset, end)) {
        width.link_exempt -= span.link_saving;
        width.code_exempt -= span.code_saving;
    }
    width
}

/// Width of a standalone line under each exemption.
///
/// Callers that already hold the line's element spans should use [`measure`];
/// this is for the sites that see only the finished line and so have to parse it.
fn line_width_components(line: &str, options: &ReflowOptions) -> LineWidth {
    let raw = display_len(line, options.length_mode);
    if !options.length_exemptions.any() {
        return LineWidth::plain(raw);
    }
    let elements = parse_markdown_elements_inner(
        line,
        options.attr_lists,
        options.myst_roles,
        options.defined_references.as_ref(),
    );
    let spans = compute_element_spans(&elements, options.length_mode, options.length_exemptions);
    measure(line, 0, &spans, options.length_mode)
}

/// Width of a standalone line as the checker measures it.
fn line_width(line: &str, options: &ReflowOptions) -> usize {
    line_width_components(line, options).effective()
}

/// Whether a standalone line fits the budget as the checker measures it.
///
/// A saving is never negative, so the exempt width never exceeds the raw width:
/// a line that already fits as written fits under any exemption too, and needs
/// no parse. That keeps the parse off the path most lines take.
fn line_fits(line: &str, options: &ReflowOptions) -> bool {
    display_len(line, options.length_mode) <= options.line_length || line_width(line, options) <= options.line_length
}

/// The non-Text element span that strictly contains `pos`, if any.
fn element_containing(pos: usize, spans: &[ElementSpan]) -> Option<ElementSpan> {
    spans.iter().copied().find(|span| span.contains(pos))
}

/// Check if a byte position falls inside any non-Text element span
fn is_inside_element(pos: usize, spans: &[ElementSpan]) -> bool {
    element_containing(pos, spans).is_some()
}

/// Minimum fraction of line_length that the first part of a split must occupy.
/// Prevents awkwardly short first lines like "A," or "Note:" on their own.
const MIN_SPLIT_RATIO: f64 = 0.3;

/// Split a line at the latest clause punctuation that keeps the first part
/// within `line_length`. Returns None if no valid split point exists or if
/// the split would create an unreasonably short first line.
fn split_at_clause_punctuation(
    text: &str,
    line_length: usize,
    element_spans: &[ElementSpan],
    length_mode: ReflowLengthMode,
) -> Option<(String, String)> {
    let chars: Vec<char> = text.chars().collect();
    let min_first_len = ((line_length as f64) * MIN_SPLIT_RATIO) as usize;

    // Find the char index where accumulated display width exceeds line_length.
    // An element the checker discounts is charged its reduced width and stepped
    // over whole, so the search window reaches as far as the exempt measure of a
    // prefix allows; scanning char by char through a discounted URL would stop
    // short of break points that are legal under it.
    let mut width_acc = LineWidth::default();
    let mut search_end_char = 0;
    let mut byte = 0usize;
    let mut idx = 0usize;
    while idx < chars.len() {
        let (advance_chars, advance_bytes, width) = match element_spans.iter().find(|s| s.start == byte) {
            Some(span) => {
                let source = &text[span.start..span.end];
                (
                    source.chars().count(),
                    source.len(),
                    measure(source, span.start, element_spans, length_mode),
                )
            }
            None => {
                let c = chars[idx];
                (
                    1,
                    c.len_utf8(),
                    LineWidth::plain(display_len(&c.to_string(), length_mode)),
                )
            }
        };
        if !(width_acc + width).fits(line_length) {
            break;
        }
        width_acc += width;
        byte += advance_bytes;
        idx += advance_chars;
        search_end_char = idx;
    }

    // Scan backwards tracking parenthesis depth to skip clause punctuation
    // inside plain-text parenthetical groups.  Scanning right-to-left means
    // ')' opens a depth level and '(' closes it.  Parens that belong to a
    // markdown element are excluded using the char's start byte (not byte-after)
    // so that closing element delimiters at the span boundary are correctly
    // treated as part of the element.
    let mut paren_depth: i32 = 0;
    let mut best_pos = None;
    for i in (0..search_end_char).rev() {
        // Start byte of char i (for paren element check)
        let byte_start: usize = chars[..i].iter().map(|c| c.len_utf8()).sum();
        // Byte just after char i (for clause punctuation element check — existing convention)
        let byte_after: usize = byte_start + chars[i].len_utf8();

        if !is_inside_element(byte_start, element_spans) {
            match chars[i] {
                ')' => paren_depth += 1,
                '(' => paren_depth = paren_depth.saturating_sub(1),
                _ => {}
            }
        }

        if paren_depth == 0
            && is_clause_punctuation(chars[i])
            && clause_break_allowed_after(&chars, i)
            && !is_inside_element(byte_after, element_spans)
        {
            best_pos = Some(i);
            break;
        }
    }

    let pos = best_pos?;

    // Reject splits that create very short first lines
    let first: String = chars[..=pos].iter().collect();
    if measure(&first, 0, element_spans, length_mode).effective() < min_first_len {
        return None;
    }

    // Split after the punctuation character
    let rest: String = chars[pos + 1..].iter().collect();
    let rest = rest.trim_start().to_string();

    if rest.is_empty() {
        return None;
    }

    Some((first, rest))
}

/// Compute plain-text paren-depth at each byte offset in `text`.
///
/// Returns a `Vec<i32>` of length `text.len()` where entry `i` is the
/// nesting depth at byte `i` — counting only `(` and `)` that fall
/// outside markdown element spans.  This lets callers quickly check
/// whether a byte position lies inside a plain-text parenthetical group.
fn paren_depth_map(text: &str, element_spans: &[ElementSpan]) -> Vec<i32> {
    let mut map = vec![0i32; text.len()];
    let mut depth = 0i32;
    for (byte, c) in text.char_indices() {
        if !is_inside_element(byte, element_spans) {
            match c {
                '(' => depth += 1,
                ')' => depth = depth.saturating_sub(1),
                _ => {}
            }
        }
        // Fill the depth value for every byte of this (possibly multi-byte) char.
        let end = (byte + c.len_utf8()).min(map.len());
        for slot in &mut map[byte..end] {
            *slot = depth;
        }
    }
    map
}

/// Return `true` if `line` is a complete, balanced, multi-word parenthetical
/// group — i.e. it starts with `(`, ends with `)` (possibly followed by the
/// punctuation `split_at_parenthetical` attaches to it), has balanced parens
/// throughout, and the inner content contains at least one space (matching the
/// ≥2-word threshold used by `split_at_parenthetical` when deciding to split).
///
/// Used to prevent the short-line merge step from collapsing intentional
/// parenthetical splits back into the previous line.
fn is_standalone_parenthetical(line: &str) -> bool {
    let trimmed = line.trim();
    if !trimmed.starts_with('(') {
        return false;
    }
    // Strip the attached tail to find the real end: everything after the last
    // ')' belongs to the group only when no whitespace separates it.
    let Some(close) = trimmed.rfind(')') else {
        return false;
    };
    if trimmed[close + 1..].contains(char::is_whitespace) {
        return false;
    }
    let core = &trimmed[..=close];
    // Inner content must span multiple words (same threshold as split_at_parenthetical).
    let inner = &core[1..core.len() - 1];
    if !inner.contains(' ') {
        return false;
    }
    // Verify the parens are balanced (depth returns to 0 at the last ')').
    let mut depth = 0i32;
    for c in core.chars() {
        match c {
            '(' => depth += 1,
            ')' => depth -= 1,
            _ => {}
        }
        if depth < 0 {
            return false;
        }
    }
    depth == 0
}

/// Split a line before the latest break-word that keeps the first part
/// within `line_length`. Returns None if no valid split point exists or if
/// the split would create an unreasonably short first line.
fn split_at_break_word(
    text: &str,
    line_length: usize,
    element_spans: &[ElementSpan],
    length_mode: ReflowLengthMode,
) -> Option<(String, String)> {
    let lower = text.to_lowercase();
    let min_first_len = ((line_length as f64) * MIN_SPLIT_RATIO) as usize;
    let mut best_split: Option<(usize, usize)> = None; // (byte_start, word_len_bytes)

    // Build a paren-depth map so we can skip break-words inside plain-text
    // parenthetical groups (matching the protection added to split_at_clause_punctuation).
    let depth_map = paren_depth_map(text, element_spans);

    for &word in BREAK_WORDS {
        let mut search_start = 0;
        while let Some(pos) = lower[search_start..].find(word) {
            let abs_pos = search_start + pos;

            // Verify it's a word boundary: preceded by space, followed by space
            let preceded_by_space = abs_pos == 0 || text.as_bytes().get(abs_pos - 1) == Some(&b' ');
            let followed_by_space = text.as_bytes().get(abs_pos + word.len()) == Some(&b' ');

            if preceded_by_space && followed_by_space {
                // The break goes BEFORE the word, so first part ends at abs_pos - 1
                let first_part = text[..abs_pos].trim_end();
                let first_part_len = measure(first_part, 0, element_spans, length_mode).effective();

                // Skip break-words inside plain-text parenthetical groups.
                let inside_paren = depth_map.get(abs_pos).is_some_and(|&d| d > 0);

                if first_part_len >= min_first_len
                    && first_part_len <= line_length
                    && !is_inside_element(abs_pos, element_spans)
                    && !inside_paren
                {
                    // Prefer the latest valid split point
                    if best_split.is_none_or(|(prev_pos, _)| abs_pos > prev_pos) {
                        best_split = Some((abs_pos, word.len()));
                    }
                }
            }

            search_start = abs_pos + word.len();
        }
    }

    let (byte_start, _word_len) = best_split?;

    let first = text[..byte_start].trim_end().to_string();
    let rest = text[byte_start..].to_string();

    if first.is_empty() || rest.trim().is_empty() {
        return None;
    }

    Some((first, rest))
}

/// Whether a proposed split takes the place of whitespace that `text` already has.
///
/// `first` is a prefix of `text` with its trailing whitespace removed and `rest`
/// a suffix with its leading whitespace removed, so the bytes between them are
/// exactly what the split consumed. That gap must be non-empty and hold nothing
/// but breakable whitespace the paragraph owns: the newline replacing it renders
/// as a single space, so an empty gap inserts a word boundary the author did not
/// write, and a gap holding anything else drops content. Whitespace inside an
/// inline element belongs to that element, where it is literal (a code span) or
/// structural (a link destination), never a place a line may break.
fn replaces_whitespace(text: &str, first: &str, rest: &str, element_spans: &[ElementSpan]) -> bool {
    if !text.starts_with(first) || !text.ends_with(rest) {
        return false;
    }
    let gap_end = text.len() - rest.len();
    gap_end > first.len()
        && text[first.len()..gap_end].chars().all(is_breakable_whitespace)
        && !element_spans
            .iter()
            .any(|span| first.len() < span.end && span.start < gap_end)
}

/// Cascade-split a line that exceeds line_length.
/// Tries parenthetical boundaries, then clause punctuation, then break-words,
/// then word wrap.
///
/// This is iterative rather than recursive so a single very long line (tens of
/// thousands of words) cannot overflow the stack. Each accepted split shrinks
/// the remaining text by a non-empty prefix, so the loop always makes progress.
/// The whole line is parsed into markdown elements once up front; every
/// remaining suffix reuses those element spans (re-based to the suffix offset)
/// instead of re-parsing, which keeps repeated element parsing out of the loop.
fn cascade_split_line(text: &str, options: &ReflowOptions) -> Vec<String> {
    let line_length = options.line_length;
    let length_mode = options.length_mode;
    let attr_lists = options.attr_lists;
    let myst_roles = options.myst_roles;
    let defined_references = options.defined_references.as_ref();
    if line_length == 0 || display_len(text, length_mode) <= line_length {
        return vec![text.to_string()];
    }

    let elements = parse_markdown_elements_inner(text, attr_lists, myst_roles, defined_references);
    let element_spans = compute_element_spans(&elements, length_mode, options.length_exemptions);

    // The raw width is over budget, but an exemption may still bring the line
    // under it, in which case the checker accepts it as written.
    if measure(text, 0, &element_spans, length_mode).fits(line_length) {
        return vec![text.to_string()];
    }

    // Element spans of the remaining suffix `text[start..]`, re-based so their
    // offsets are relative to the suffix. Split points never fall inside an
    // element, so every span lies wholly before or wholly at/after `start`.
    let rebased_spans = |start: usize| -> Vec<ElementSpan> {
        if start == 0 {
            return element_spans.clone();
        }
        element_spans
            .iter()
            .filter(|span| span.end > start)
            .map(|span| ElementSpan {
                start: span.start.saturating_sub(start),
                end: span.end.saturating_sub(start),
                ..*span
            })
            .collect()
    };

    let mut result = Vec::new();
    let mut start = 0usize;

    loop {
        let remaining = &text[start..];
        let spans = rebased_spans(start);
        if measure(remaining, 0, &spans, length_mode).fits(line_length) {
            result.push(remaining.to_string());
            return result;
        }

        // `rest` is always a suffix of `remaining` (the splitters only trim its
        // leading whitespace), so `remaining.len() - rest.len()` is the number of
        // bytes consumed, and the new absolute offset is `start + consumed`.
        //
        // Every candidate must stand in for whitespace the text already has: a
        // line break renders as a space, so one placed between two characters
        // that were adjacent changes the rendered paragraph. A strategy that
        // proposes such a split is skipped and the next one gets a turn.
        let at_whitespace = |candidate: Option<(String, String)>| {
            candidate.filter(|(first, rest)| replaces_whitespace(remaining, first, rest, &spans))
        };
        let split = at_whitespace(split_at_parenthetical(remaining, line_length, &spans, length_mode))
            .or_else(|| at_whitespace(split_at_clause_punctuation(remaining, line_length, &spans, length_mode)))
            .or_else(|| at_whitespace(split_at_break_word(remaining, line_length, &spans, length_mode)));

        if let Some((first, rest)) = split {
            let consumed = remaining.len().saturating_sub(rest.len());
            // Defensive: a zero-length advance would loop forever. Splitters only
            // return a non-empty `first`, so this never triggers, but guard anyway.
            if consumed == 0 {
                break;
            }
            result.push(first);
            start += consumed;
            continue;
        }

        // No semantic split point: word-wrap the remaining suffix and finish.
        break;
    }

    // Fallback: word wrap the still-oversized suffix using reflow_elements.
    let mut fallback_options = options.clone();
    fallback_options.break_on_sentences = false;
    fallback_options.preserve_breaks = false;
    fallback_options.sentence_per_line = false;
    fallback_options.semantic_line_breaks = false;
    fallback_options.require_sentence_capital = true;
    fallback_options.max_list_continuation_indent = None;
    fallback_options.defined_references = None;
    let remaining = &text[start..];
    let tail_elements = if start == 0 {
        elements
    } else {
        parse_markdown_elements_inner(remaining, attr_lists, myst_roles, defined_references)
    };
    result.extend(reflow_elements(&tail_elements, &fallback_options));
    result
}

/// Reflow elements using semantic line breaks strategy:
/// 1. Split at sentence boundaries (always)
/// 2. For lines exceeding line_length, cascade through clause punct → break-words → word wrap
fn reflow_elements_semantic(elements: &[Element], options: &ReflowOptions) -> Vec<String> {
    // Step 1: Split into sentences using existing sentence-per-line logic
    let sentence_lines = reflow_elements_sentence_per_line(elements, options);

    // Step 2: For each sentence line, apply cascading splits if it exceeds line_length
    // When line_length is 0 (unlimited), skip cascading — sentence splits only
    if options.line_length == 0 {
        return sentence_lines;
    }

    let mut result = Vec::new();
    for line in sentence_lines {
        if line_fits(&line, options) {
            result.push(line);
        } else {
            result.extend(cascade_split_line(&line, options));
        }
    }

    // Step 3: Merge very short trailing lines back into the previous line.
    // Word wrap can produce lines like "was" or "see" on their own, which reads poorly.
    let min_line_len = ((options.line_length as f64) * MIN_SPLIT_RATIO) as usize;
    let mut merged: Vec<String> = Vec::with_capacity(result.len());
    for line in result {
        if !merged.is_empty() && line_width(&line, options) < min_line_len && !line.trim().is_empty() {
            // Don't merge a line that is itself a standalone parenthetical group —
            // it was placed on its own line intentionally by split_at_parenthetical.
            if is_standalone_parenthetical(&line) {
                merged.push(line);
                continue;
            }

            // Don't merge across sentence boundaries — sentence splits are intentional
            let prev_ends_at_sentence = {
                let trimmed = merged.last().unwrap().trim_end();
                trimmed
                    .chars()
                    .rev()
                    .find(|c| !matches!(c, '"' | '\'' | '\u{201D}' | '\u{2019}' | ')' | ']'))
                    .is_some_and(|c| matches!(c, '.' | '!' | '?'))
            };

            if !prev_ends_at_sentence {
                let prev = merged.last_mut().unwrap();
                let combined = format!("{prev} {line}");
                // Only merge if the combined line fits within the limit
                if line_fits(&combined, options) {
                    *prev = combined;
                    continue;
                }
            }
        }
        merged.push(line);
    }
    merged
}

/// Find the last space in `line` that is safe to split at.
/// Safe spaces are those NOT inside rendered non-Text elements and whose
/// suffix would not open a block construct when placed at line start.
/// `element_spans` locates the non-Text elements in the line. Spans use
/// exclusive bounds (pos > start && pos < end) because element delimiters
/// (e.g., `[`, `]`, `(`, `)`, `<`, `>`, `` ` ``) are never spaces, so only
/// interior positions need protection. The scan keeps looking left past
/// construct-leading suffixes (e.g. a trailing `- `), so a usable earlier break
/// point is found instead of forcing an overlong line.
fn rfind_safe_space(
    line: &str,
    element_spans: &[ElementSpan],
    options: &ReflowOptions,
    relax_soft_spans: bool,
) -> Option<usize> {
    line.char_indices().rev().map(|(pos, _)| pos).find(|&pos| {
        line.as_bytes()[pos] == b' '
            && !is_inside_element_filtered(pos, element_spans, options, relax_soft_spans)
            && !starts_block_construct(&line[pos + 1..])
    })
}

fn is_inside_element_filtered(
    pos: usize,
    spans: &[ElementSpan],
    options: &ReflowOptions,
    relax_soft_spans: bool,
) -> bool {
    spans.iter().any(|span| {
        span.contains(pos)
            && (!relax_soft_spans
                || span.is_hard
                || (options.atomic_spans && span.exempt_width().fits(options.line_length)))
    })
}

/// A token that must not start a wrapped line, together with the width it
/// contributes and the separator that precedes it. The width travels with the
/// text because only the caller knows which construct produced it, and so which
/// exemption it earns.
#[derive(Clone, Copy)]
struct Attached<'a> {
    text: &'a str,
    width: LineWidth,
    separator: &'a str,
}

/// Break `current_line` one word earlier so `attach` never starts a wrapped
/// line: everything before the line's last safe space is emitted as a
/// finished line, and the carried word plus the separator plus the attached
/// text becomes the new current line. The returned byte length of the carried
/// word lets callers re-record a span for the attached text. Returns `None`
/// (line untouched) when the line has no safe break point.
///
/// The new width is the carried text measured through the spans it came with,
/// plus the attached width, so an exemption the carried text or the attached
/// token earns is preserved across the break instead of being re-derived from a
/// bare string.
///
/// The carried text keeps the element spans that fell inside it, rebased to the
/// new line. Dropping them would leave a later break blind to an element the
/// carried text still holds, and so free to split a link or a code span down
/// the middle.
fn break_before_attached(
    lines: &mut Vec<String>,
    current_line: &mut String,
    current_width: &mut LineWidth,
    element_spans: &mut Vec<ElementSpan>,
    attach: Attached<'_>,
    options: &ReflowOptions,
) -> Option<usize> {
    let length_mode = options.length_mode;
    let last_space = rfind_safe_space(current_line, element_spans, options, false)
        .or_else(|| rfind_safe_space(current_line, element_spans, options, true))?;
    let before = current_line[..last_space]
        .trim_end_matches(is_breakable_whitespace)
        .to_string();
    let after = current_line[last_space + 1..].to_string();
    let after_width = measure(&after, last_space + 1, element_spans, length_mode);
    lines.push(before);
    let carried = after.len();
    let Attached { text, width, separator } = attach;
    *current_line = format!("{after}{separator}{text}");
    *current_width = after_width + LineWidth::plain(display_len(separator, length_mode)) + width;
    rebase_spans_after_break(element_spans, last_space + 1);
    Some(carried)
}

/// Keep the spans that reach into the text starting at `carried_start` and move
/// them into that text's coordinates, discarding the ones that belong to the
/// line just emitted.
///
/// A span that starts before `carried_start` is clamped to 0 rather than
/// dropped. `rfind_safe_space` never breaks inside a span, so this cannot
/// normally happen; clamping keeps the whole prefix protected if it ever does,
/// where dropping the span would license a break inside an element.
fn rebase_spans_after_break(element_spans: &mut Vec<ElementSpan>, carried_start: usize) {
    element_spans.retain(|span| span.end > carried_start);
    for span in element_spans.iter_mut() {
        span.start = span.start.saturating_sub(carried_start);
        span.end -= carried_start;
    }
}

/// Reflow elements into lines that fit within the line length
fn reflow_elements(elements: &[Element], options: &ReflowOptions) -> Vec<String> {
    let mut lines = Vec::new();
    let mut current_line = String::new();
    // The line's width under each exemption the checker applies. With no
    // exemption active both components are the plain display width.
    let mut current_width = LineWidth::default();
    // Track byte spans of non-Text elements in current_line for safe splitting
    let mut current_line_element_spans: Vec<ElementSpan> = Vec::new();
    let length_mode = options.length_mode;
    let exemptions = options.length_exemptions;

    for (idx, element) in elements.iter().enumerate() {
        let element_len = element.display_len(length_mode);
        let element_width = element.exempt_width(length_mode, exemptions);
        let is_hard = match element {
            Element::Bold { content, .. }
            | Element::Italic { content, .. }
            | Element::Strikethrough { content, .. } => content.contains(['[', '`', '<', '$', '{']),
            _ => true,
        };

        // Determine adjacency from the original elements, not from current_line.
        // Elements are adjacent when there's no breakable whitespace between them
        // in the source (a non-breaking space stays inside the neighboring token,
        // so the pair must also stay attached):
        // - Text("v") → HugoShortcode("{{<...>}}") = adjacent (text has no trailing space)
        // - Text(" and ") → InlineLink("[a](url)") = NOT adjacent (text has trailing space)
        // - HugoShortcode("{{<...>}}") → Text(",") = adjacent (text has no leading space)
        // - Code("`x`") → Text("\u{00A0}:") = adjacent (only a non-breaking space between)
        let is_adjacent_to_prev = if idx > 0 {
            match (&elements[idx - 1], element) {
                (Element::Text(t), _) => !t.is_empty() && !t.ends_with(is_breakable_whitespace),
                (_, Element::Text(t)) => !t.is_empty() && !t.starts_with(is_breakable_whitespace),
                _ => true,
            }
        } else {
            false
        };

        // For text elements that might need breaking
        if let Element::Text(text) = element {
            // Check if original text had leading breakable whitespace
            let has_leading_space = text.starts_with(is_breakable_whitespace);
            // If this is a text element, always process it word by word
            let words: Vec<&str> = split_breakable_words(text).collect();

            for (i, word) in words.iter().enumerate() {
                // A bare word carries no construct the checker exempts.
                let word_width = LineWidth::plain(display_len(word, length_mode));
                // A token that is only punctuation (optionally led by a
                // non-breaking space, e.g. French "\u{00A0}:") must never be
                // hoisted to the start of a line. Tokens are never empty
                // (`split_breakable_words` filters), so `all` cannot be
                // vacuously true.
                let is_trailing_punct = word.chars().all(|c| {
                    matches!(c, ',' | '.' | ':' | ';' | '!' | '?' | ')' | ']' | '}') || is_non_breaking_space(c)
                });

                // First word of text adjacent to preceding non-text element
                // must stay attached (e.g., shortcode followed by punctuation or text)
                let is_first_adjacent = i == 0 && is_adjacent_to_prev;

                if is_first_adjacent {
                    // Attach directly without space, preventing line break
                    if !(current_width + word_width).fits(options.line_length)
                        && !current_width.is_empty()
                        && break_before_attached(
                            &mut lines,
                            &mut current_line,
                            &mut current_width,
                            &mut current_line_element_spans,
                            Attached {
                                text: word,
                                width: word_width,
                                separator: "",
                            },
                            options,
                        )
                        .is_some()
                    {
                        // Would exceed — broke before the adjacent group at the
                        // last safe space (element-aware, so links/code stay
                        // intact); with no safe break point the group is
                        // attached and the long line accepted.
                    } else {
                        current_line.push_str(word);
                        current_width += word_width;
                    }
                } else if !current_width.is_empty()
                    && !(current_width + LineWidth::plain(1) + word_width).fits(options.line_length)
                {
                    if is_trailing_punct {
                        // The overflowing token is bare punctuation, which must
                        // not start a line. Break one word earlier so the mark
                        // travels with the word it follows ("… mot :"), keeping
                        // the source space (French double punctuation requires
                        // it); with no safe earlier break point, accept the
                        // overlong line rather than rewrite content.
                        if break_before_attached(
                            &mut lines,
                            &mut current_line,
                            &mut current_width,
                            &mut current_line_element_spans,
                            Attached {
                                text: word,
                                width: word_width,
                                separator: " ",
                            },
                            options,
                        )
                        .is_none()
                        {
                            current_line.push(' ');
                            current_line.push_str(word);
                            current_width += LineWidth::plain(1) + word_width;
                        }
                    } else if !starts_block_construct(word) {
                        // Start a new line
                        lines.push(current_line.trim_matches(is_breakable_whitespace).to_string());
                        current_line = word.to_string();
                        current_width = word_width;
                        current_line_element_spans.clear();
                    } else if break_before_attached(
                        &mut lines,
                        &mut current_line,
                        &mut current_width,
                        &mut current_line_element_spans,
                        Attached {
                            text: word,
                            width: word_width,
                            separator: " ",
                        },
                        options,
                    )
                    .is_some()
                    {
                        // The overflowing word would open a block construct at line
                        // start. Broke one word earlier instead so the marker stays
                        // mid-line: "... and then" + "- clause" becomes "... and" +
                        // "then - clause".
                    } else {
                        // No safe earlier break point — keep the marker attached and
                        // accept the long line rather than corrupt the structure.
                        if i > 0 || has_leading_space {
                            current_line.push(' ');
                            current_width += LineWidth::plain(1);
                        }
                        current_line.push_str(word);
                        current_width += word_width;
                    }
                } else {
                    // Add a space wherever the source had breakable whitespace at
                    // this position. For the first word of a text run (i == 0)
                    // that means the run had a leading space — and reaching this
                    // branch already implies the word is not adjacent to the
                    // previous element, so the space is real. Later words
                    // (i > 0) always had whitespace before them: that is what
                    // separated them during tokenization. This holds for bare
                    // punctuation too ("ligne : la" keeps its French
                    // orthographic space): reflow moves line breaks, it does not
                    // rewrite characters. The no-space (adjacent) case is
                    // handled above by `is_first_adjacent`.
                    let add_space = !current_width.is_empty() && (i > 0 || has_leading_space);
                    if add_space {
                        current_line.push(' ');
                        current_width += LineWidth::plain(1);
                    }
                    current_line.push_str(word);
                    current_width += word_width;
                }
            }
        } else {
            let span_info = match element {
                Element::Italic { content, underscore } => {
                    Some((content.as_str(), if *underscore { "_" } else { "*" }, false))
                }
                Element::Bold { content, underscore } => {
                    Some((content.as_str(), if *underscore { "__" } else { "**" }, false))
                }
                Element::Strikethrough { content, double } => {
                    Some((content.as_str(), if *double { "~~" } else { "~" }, false))
                }
                Element::Code { content, marker } => Some((content.as_str(), marker.as_str(), true)),
                _ => None,
            };

            // A span that alone exceeds the line budget is broken even when
            // spans are atomic, since keeping it whole would leave a line that can
            // never fit. `breakable_units` decides where that is safe.
            let breakable: Option<Vec<&str>> = match span_info {
                Some((content, _, is_code)) => {
                    if is_code {
                        (!options.atomic_spans && code_span_wraps_losslessly(content))
                            .then(|| split_breakable_words(content).collect())
                    } else {
                        (!options.atomic_spans || element_len > options.line_length)
                            .then(|| breakable_units(content, options.defined_references.as_ref(), options.attr_lists))
                            .flatten()
                    }
                }
                None => None,
            };

            if let Some(words) = breakable {
                let (_, marker, is_code) = span_info.expect("breakable implies a span");
                let n = words.len();
                if n == 0 {
                    // Empty span — treat as atomic
                    let full = format!("{marker}{marker}");
                    let full_width = LineWidth::plain(display_len(&full, length_mode));
                    if !is_adjacent_to_prev && !current_width.is_empty() {
                        current_line.push(' ');
                        current_width += LineWidth::plain(1);
                    }
                    current_line.push_str(&full);
                    current_width += full_width;
                } else {
                    for (i, word) in words.iter().enumerate() {
                        let is_first = i == 0;
                        let is_last = i == n - 1;

                        let space_start = if is_first && is_code && word.starts_with('`') {
                            " "
                        } else {
                            ""
                        };
                        let space_end = if is_last && is_code && word.ends_with('`') {
                            " "
                        } else {
                            ""
                        };

                        let word_str: String = match (is_first, is_last) {
                            (true, true) => format!("{marker}{space_start}{word}{space_end}{marker}"),
                            (true, false) => format!("{marker}{space_start}{word}"),
                            (false, true) => format!("{word}{space_end}{marker}"),
                            (false, false) => word.to_string(),
                        };
                        let word_elements = parse_elements(&word_str, options);
                        let word_spans = compute_element_spans(&word_elements, length_mode, exemptions);
                        let word_width = measure(&word_str, 0, &word_spans, length_mode);

                        let needs_space = if is_first {
                            !is_adjacent_to_prev && !current_width.is_empty()
                        } else {
                            !current_width.is_empty()
                        };

                        if needs_space
                            && !(current_width + LineWidth::plain(1) + word_width).fits(options.line_length)
                            && !starts_block_construct(&word_str)
                        {
                            lines.push(current_line.trim_matches(is_breakable_whitespace).to_string());
                            current_line = word_str;
                            current_width = word_width;
                            current_line_element_spans.clear();
                            for span in word_spans {
                                current_line_element_spans.push(span);
                            }
                        } else {
                            let mut start_pos = current_line.len();
                            if needs_space {
                                current_line.push(' ');
                                current_width += LineWidth::plain(1);
                                start_pos += 1;
                            }
                            current_line.push_str(&word_str);
                            current_width += word_width;
                            for mut span in word_spans {
                                span.start += start_pos;
                                span.end += start_pos;
                                current_line_element_spans.push(span);
                            }
                        }
                    }
                }
            } else {
                // For non-text elements (code, links, references), treat as atomic units
                // These should never be broken across lines
                let element_str = format!("{element}");

                if is_adjacent_to_prev {
                    // Adjacent to preceding text — attach directly without space
                    if !(current_width + element_width).fits(options.line_length)
                        && let Some(carried) = break_before_attached(
                            &mut lines,
                            &mut current_line,
                            &mut current_width,
                            &mut current_line_element_spans,
                            Attached {
                                text: &element_str,
                                width: element_width,
                                separator: "",
                            },
                            options,
                        )
                    {
                        // Would exceed limit — broke before the adjacent word group
                        // at the last safe space (element-aware, so links/code stay
                        // intact). Record the element span in the new current_line.
                        current_line_element_spans.push(ElementSpan::new(
                            carried,
                            element_str.len(),
                            element_len,
                            element_width,
                            is_hard,
                        ));
                    } else {
                        let start = current_line.len();
                        current_line.push_str(&element_str);
                        current_width += element_width;
                        current_line_element_spans.push(ElementSpan::new(
                            start,
                            element_str.len(),
                            element_len,
                            element_width,
                            is_hard,
                        ));
                    }
                } else if !current_width.is_empty()
                    && !(current_width + LineWidth::plain(1) + element_width).fits(options.line_length)
                {
                    if !starts_block_construct(&element_str) {
                        // Not adjacent, would exceed — start new line
                        lines.push(current_line.trim_matches(is_breakable_whitespace).to_string());
                        current_line.clone_from(&element_str);
                        current_width = element_width;
                        current_line_element_spans.clear();
                        current_line_element_spans.push(ElementSpan::new(
                            0,
                            element_str.len(),
                            element_len,
                            element_width,
                            is_hard,
                        ));
                    } else if let Some(carried) = break_before_attached(
                        &mut lines,
                        &mut current_line,
                        &mut current_width,
                        &mut current_line_element_spans,
                        Attached {
                            text: &element_str,
                            width: element_width,
                            separator: " ",
                        },
                        options,
                    ) {
                        // The overflowing element would open a block construct at
                        // line start (e.g. an HtmlTag like `<div>`). Broke one word
                        // earlier instead so the element stays mid-line.
                        let start = carried + 1;
                        current_line_element_spans.push(ElementSpan::new(
                            start,
                            element_str.len(),
                            element_len,
                            element_width,
                            is_hard,
                        ));
                    } else {
                        // No safe earlier break point — keep the element attached
                        // and accept the long line rather than corrupt the structure.
                        let ends_with_opener =
                            current_line.ends_with('(') || current_line.ends_with('[') || current_line.ends_with('{');
                        if !ends_with_opener {
                            current_line.push(' ');
                            current_width += LineWidth::plain(1);
                        }
                        let start = current_line.len();
                        current_line.push_str(&element_str);
                        current_width += element_width;
                        current_line_element_spans.push(ElementSpan::new(
                            start,
                            element_str.len(),
                            element_len,
                            element_width,
                            is_hard,
                        ));
                    }
                } else {
                    // Not adjacent, fits — add with space
                    let ends_with_opener =
                        current_line.ends_with('(') || current_line.ends_with('[') || current_line.ends_with('{');
                    if !current_width.is_empty() && !ends_with_opener {
                        current_line.push(' ');
                        current_width += LineWidth::plain(1);
                    }
                    let start = current_line.len();
                    current_line.push_str(&element_str);
                    current_width += element_width;
                    current_line_element_spans.push(ElementSpan::new(
                        start,
                        element_str.len(),
                        element_len,
                        element_width,
                        is_hard,
                    ));
                }
            }
        }
    }

    // Don't forget the last line
    if !current_line.is_empty() {
        lines.push(current_line.trim_end_matches(is_breakable_whitespace).to_string());
    }

    lines
}

/// Reflow markdown content preserving structure
pub fn reflow_markdown(content: &str, options: &ReflowOptions) -> String {
    let lines: Vec<&str> = content.lines().collect();
    let mut result = Vec::new();
    let mut i = 0;

    while i < lines.len() {
        let line = lines[i];
        let trimmed = line.trim();

        // Preserve empty lines
        if trimmed.is_empty() {
            result.push(String::new());
            i += 1;
            continue;
        }

        // Preserve headings as-is
        if trimmed.starts_with('#') {
            result.push(line.to_string());
            i += 1;
            continue;
        }

        // Preserve Quarto/Pandoc div markers (:::) as-is
        if trimmed.starts_with(":::") {
            result.push(line.to_string());
            i += 1;
            continue;
        }

        // Preserve fenced code blocks
        if trimmed.starts_with("```") || trimmed.starts_with("~~~") {
            result.push(line.to_string());
            i += 1;
            // Copy lines until closing fence
            while i < lines.len() {
                result.push(lines[i].to_string());
                if lines[i].trim().starts_with("```") || lines[i].trim().starts_with("~~~") {
                    i += 1;
                    break;
                }
                i += 1;
            }
            continue;
        }

        // Preserve indented code blocks (4+ columns accounting for tab expansion)
        if calculate_indentation_width_default(line) >= 4 {
            // Collect all consecutive indented lines
            result.push(line.to_string());
            i += 1;
            while i < lines.len() {
                let next_line = lines[i];
                // Continue if next line is also indented or empty (empty lines in code blocks are ok)
                if calculate_indentation_width_default(next_line) >= 4 || next_line.trim().is_empty() {
                    result.push(next_line.to_string());
                    i += 1;
                } else {
                    break;
                }
            }
            continue;
        }

        // Preserve block quotes (but reflow their content)
        if trimmed.starts_with('>') {
            // find() returns byte position which is correct for str slicing
            // The unwrap is safe because we already verified trimmed starts with '>'
            let gt_pos = line.find('>').expect("'>' must exist since trimmed.starts_with('>')");
            let quote_prefix = line[0..=gt_pos].to_string();
            let quote_content = &line[quote_prefix.len()..].trim_start();

            let reflowed = reflow_line(quote_content, options);
            for reflowed_line in &reflowed {
                result.push(format!("{quote_prefix} {reflowed_line}"));
            }
            i += 1;
            continue;
        }

        // Preserve horizontal rules first (before checking for lists)
        if is_horizontal_rule(trimmed) {
            result.push(line.to_string());
            i += 1;
            continue;
        }

        // Preserve lists (but not horizontal rules)
        if is_unordered_list_marker(trimmed) || is_numbered_list_item(trimmed) {
            // Find the list marker and preserve indentation
            let indent = line.len() - line.trim_start().len();
            let indent_str = " ".repeat(indent);

            // For numbered lists, find the period and the space after it
            // For bullet lists, find the marker and the space after it
            let mut marker_end = indent;
            let mut content_start = indent;

            if trimmed.chars().next().is_some_and(char::is_numeric) {
                // Numbered list: find the period
                if let Some(period_pos) = line[indent..].find('.') {
                    marker_end = indent + period_pos + 1; // Include the period
                    content_start = marker_end;
                    // Skip any spaces after the period to find content start
                    // Use byte-based check since content_start is a byte index
                    // This is safe because space is ASCII (single byte)
                    while content_start < line.len() && line.as_bytes().get(content_start) == Some(&b' ') {
                        content_start += 1;
                    }
                }
            } else {
                // Bullet list: marker is single character
                marker_end = indent + 1; // Just the marker character
                content_start = marker_end;
                // Skip any spaces after the marker
                // Use byte-based check since content_start is a byte index
                // This is safe because space is ASCII (single byte)
                while content_start < line.len() && line.as_bytes().get(content_start) == Some(&b' ') {
                    content_start += 1;
                }
            }

            // Minimum indent for continuation lines (based on list marker, before checkbox)
            let min_continuation_indent = content_start;

            // Detect checkbox/task list markers: [ ], [x], [X]
            // GFM task lists work with both unordered and ordered lists
            let rest = &line[content_start..];
            if rest.starts_with("[ ] ") || rest.starts_with("[x] ") || rest.starts_with("[X] ") {
                marker_end = content_start + 3; // Include the checkbox `[ ]`
                content_start += 4; // Skip past `[ ] `
            }

            let marker = &line[indent..marker_end];

            // Collect all content for this list item (including continuation lines)
            // Preserve hard breaks (2 trailing spaces) while trimming excessive whitespace
            let mut list_content = vec![trim_preserving_hard_break(&line[content_start..])];
            i += 1;

            // Collect continuation lines (indented lines that are part of this list item)
            // Use the base marker indent (not checkbox-extended) for collection,
            // since users may indent continuations to the bullet level, not the checkbox level
            while i < lines.len() {
                let next_line = lines[i];
                let next_trimmed = next_line.trim();

                // Stop if we hit an empty line or another list item or special block
                if is_block_boundary(next_trimmed) {
                    break;
                }

                // Check if this line is indented (continuation of list item)
                let next_indent = next_line.len() - next_line.trim_start().len();
                if next_indent >= min_continuation_indent {
                    // This is a continuation line - add its content
                    // Preserve hard breaks while trimming excessive whitespace
                    let trimmed_start = next_line.trim_start();
                    list_content.push(trim_preserving_hard_break(trimmed_start));
                    i += 1;
                } else {
                    // Not indented enough, not part of this list item
                    break;
                }
            }

            // Join content, but respect hard breaks (lines ending with 2 spaces or backslash)
            // Hard breaks should prevent joining with the next line
            let combined_content = if options.preserve_breaks {
                list_content[0].clone()
            } else {
                // Check if any lines have hard breaks - if so, preserve the structure
                let has_hard_breaks = list_content.iter().any(|line| has_hard_break(line));
                if has_hard_breaks {
                    // Don't join lines with hard breaks - keep them separate with newlines
                    list_content.join("\n")
                } else {
                    // No hard breaks, safe to join with spaces
                    list_content.join(" ")
                }
            };

            // Calculate the proper indentation for continuation lines
            let trimmed_marker = marker;
            let continuation_spaces = if let Some(max_indent) = options.max_list_continuation_indent {
                // Cap the relative indent (past the nesting level) to max_indent,
                // then add back the nesting indent so nested items stay correct
                indent + (content_start - indent).min(max_indent)
            } else {
                content_start
            };

            // Adjust line length to account for list marker and space
            let prefix_length = indent + trimmed_marker.len() + 1;

            // Create adjusted options with reduced line length
            let adjusted_options = ReflowOptions {
                line_length: options.line_length.saturating_sub(prefix_length),
                ..options.clone()
            };

            let reflowed = reflow_line(&combined_content, &adjusted_options);
            for (j, reflowed_line) in reflowed.iter().enumerate() {
                if j == 0 {
                    result.push(format!("{indent_str}{trimmed_marker} {reflowed_line}"));
                } else {
                    // Continuation lines aligned with text after marker
                    let continuation_indent = " ".repeat(continuation_spaces);
                    result.push(format!("{continuation_indent}{reflowed_line}"));
                }
            }
            continue;
        }

        // Preserve tables
        if crate::utils::table_utils::TableUtils::is_potential_table_row(line) {
            result.push(line.to_string());
            i += 1;
            continue;
        }

        // Preserve reference definitions
        if trimmed.starts_with('[') && line.contains("]:") {
            result.push(line.to_string());
            i += 1;
            continue;
        }

        // Preserve definition list items (extended markdown)
        if is_definition_list_item(trimmed) {
            result.push(line.to_string());
            i += 1;
            continue;
        }

        // Check if this is a single line that doesn't need processing
        let mut is_single_line_paragraph = true;
        if i + 1 < lines.len() {
            let next_trimmed = lines[i + 1].trim();
            // Check if next line continues this paragraph
            if !is_block_boundary(next_trimmed) {
                is_single_line_paragraph = false;
            }
        }

        // If it's a single line that fits, just add it as-is
        if is_single_line_paragraph && line_fits(line, options) {
            result.push(line.to_string());
            i += 1;
            continue;
        }

        // For regular paragraphs, collect consecutive lines
        let mut paragraph_parts = Vec::new();
        let mut current_part = vec![line];
        i += 1;

        // If preserve_breaks is true, treat each line separately
        if options.preserve_breaks {
            // Don't collect consecutive lines - just reflow this single line
            let hard_break_type = if line.strip_suffix('\r').unwrap_or(line).ends_with('\\') {
                Some("\\")
            } else if line.ends_with("  ") {
                Some("  ")
            } else {
                None
            };
            let reflowed = reflow_line(line, options);

            // Preserve hard breaks (two trailing spaces or backslash)
            if let Some(break_marker) = hard_break_type {
                if !reflowed.is_empty() {
                    let mut reflowed_with_break = reflowed;
                    let last_idx = reflowed_with_break.len() - 1;
                    if !has_hard_break(&reflowed_with_break[last_idx]) {
                        reflowed_with_break[last_idx].push_str(break_marker);
                    }
                    result.extend(reflowed_with_break);
                }
            } else {
                result.extend(reflowed);
            }
        } else {
            // Original behavior: collect consecutive lines into a paragraph
            while i < lines.len() {
                let prev_line = if !current_part.is_empty() {
                    current_part.last().unwrap()
                } else {
                    ""
                };
                let next_line = lines[i];
                let next_trimmed = next_line.trim();

                // Stop at empty lines or special blocks
                if is_block_boundary(next_trimmed) {
                    break;
                }

                // Check if previous line ends with hard break (two spaces or backslash)
                // or is a complete sentence in sentence_per_line mode
                let prev_trimmed = prev_line.trim();
                let abbreviations = get_abbreviations(&options.abbreviations);
                let ends_with_sentence = (prev_trimmed.ends_with('.')
                    || prev_trimmed.ends_with('!')
                    || prev_trimmed.ends_with('?')
                    || prev_trimmed.ends_with(".*")
                    || prev_trimmed.ends_with("!*")
                    || prev_trimmed.ends_with("?*")
                    || prev_trimmed.ends_with("._")
                    || prev_trimmed.ends_with("!_")
                    || prev_trimmed.ends_with("?_")
                    // Quote-terminated sentences (straight and curly quotes)
                    || prev_trimmed.ends_with(".\"")
                    || prev_trimmed.ends_with("!\"")
                    || prev_trimmed.ends_with("?\"")
                    || prev_trimmed.ends_with(".'")
                    || prev_trimmed.ends_with("!'")
                    || prev_trimmed.ends_with("?'")
                    || prev_trimmed.ends_with(".\u{201D}")
                    || prev_trimmed.ends_with("!\u{201D}")
                    || prev_trimmed.ends_with("?\u{201D}")
                    || prev_trimmed.ends_with(".\u{2019}")
                    || prev_trimmed.ends_with("!\u{2019}")
                    || prev_trimmed.ends_with("?\u{2019}"))
                    && !text_ends_with_abbreviation(
                        prev_trimmed.trim_end_matches(['*', '_', '"', '\'', '\u{201D}', '\u{2019}']),
                        &abbreviations,
                    );

                if has_hard_break(prev_line) || (options.sentence_per_line && ends_with_sentence) {
                    // Start a new part after hard break or complete sentence
                    paragraph_parts.push(current_part.join(" "));
                    current_part = vec![next_line];
                } else {
                    current_part.push(next_line);
                }
                i += 1;
            }

            // Add the last part
            if !current_part.is_empty() {
                if current_part.len() == 1 {
                    // Single line, don't add trailing space
                    paragraph_parts.push(current_part[0].to_string());
                } else {
                    paragraph_parts.push(current_part.join(" "));
                }
            }

            // Reflow each part separately, preserving hard breaks
            for (j, part) in paragraph_parts.iter().enumerate() {
                let reflowed = reflow_line(part, options);
                result.extend(reflowed);

                // Preserve hard break by ensuring last line of part ends with hard break marker
                // Use two spaces as the default hard break format for reflows
                // But don't add hard breaks in sentence_per_line mode - lines are already separate
                if j < paragraph_parts.len() - 1 && !result.is_empty() && !options.sentence_per_line {
                    let last_idx = result.len() - 1;
                    if !has_hard_break(&result[last_idx]) {
                        result[last_idx].push_str("  ");
                    }
                }
            }
        }
    }

    // Preserve trailing newline if the original content had one
    let result_text = result.join("\n");
    if content.ends_with('\n') && !result_text.ends_with('\n') {
        format!("{result_text}\n")
    } else {
        result_text
    }
}

/// Information about a reflowed paragraph
#[derive(Debug, Clone)]
pub struct ParagraphReflow {
    /// Starting byte offset of the paragraph in the original content
    pub start_byte: usize,
    /// Ending byte offset of the paragraph in the original content
    pub end_byte: usize,
    /// The reflowed text for this paragraph
    pub reflowed_text: String,
}

/// A collected blockquote line used for style-preserving reflow.
///
/// The invariant `is_explicit == true` iff `prefix.is_some()` is enforced by the
/// constructors. Use [`BlockquoteLineData::explicit`] or [`BlockquoteLineData::lazy`]
/// rather than constructing the struct directly.
#[derive(Debug, Clone)]
pub struct BlockquoteLineData {
    /// Trimmed content without the `> ` prefix.
    pub(crate) content: String,
    /// Whether this line carries an explicit blockquote marker.
    pub(crate) is_explicit: bool,
    /// Full blockquote prefix (e.g. `"> "`, `"> > "`). `None` for lazy continuation lines.
    pub(crate) prefix: Option<String>,
}

impl BlockquoteLineData {
    /// Create an explicit (marker-bearing) blockquote line.
    pub fn explicit(content: String, prefix: String) -> Self {
        Self {
            content,
            is_explicit: true,
            prefix: Some(prefix),
        }
    }

    /// Create a lazy continuation line (no blockquote marker).
    pub fn lazy(content: String) -> Self {
        Self {
            content,
            is_explicit: false,
            prefix: None,
        }
    }
}

/// Style for blockquote continuation lines after reflow.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BlockquoteContinuationStyle {
    Explicit,
    Lazy,
}

/// Determine the continuation style for a blockquote paragraph from its collected lines.
///
/// The first line is always explicit (it carries the marker), so only continuation
/// lines (index 1+) are counted. Ties resolve to `Explicit`.
///
/// When the slice has only one element (no continuation lines to inspect), both
/// counts are zero and the tie-breaking rule returns `Explicit`.
pub fn blockquote_continuation_style(lines: &[BlockquoteLineData]) -> BlockquoteContinuationStyle {
    let mut explicit_count = 0usize;
    let mut lazy_count = 0usize;

    for line in lines.iter().skip(1) {
        if line.is_explicit {
            explicit_count += 1;
        } else {
            lazy_count += 1;
        }
    }

    if explicit_count > 0 && lazy_count == 0 {
        BlockquoteContinuationStyle::Explicit
    } else if lazy_count > 0 && explicit_count == 0 {
        BlockquoteContinuationStyle::Lazy
    } else if explicit_count >= lazy_count {
        BlockquoteContinuationStyle::Explicit
    } else {
        BlockquoteContinuationStyle::Lazy
    }
}

/// Determine the dominant blockquote prefix for a paragraph.
///
/// The most frequently occurring explicit prefix wins. Ties are broken by earliest
/// first appearance. Falls back to `fallback` when no explicit lines are present.
pub fn dominant_blockquote_prefix(lines: &[BlockquoteLineData], fallback: &str) -> String {
    let mut counts: std::collections::HashMap<String, (usize, usize)> = std::collections::HashMap::new();

    for (idx, line) in lines.iter().enumerate() {
        let Some(prefix) = line.prefix.as_ref() else {
            continue;
        };
        counts
            .entry(prefix.clone())
            .and_modify(|entry| entry.0 += 1)
            .or_insert((1, idx));
    }

    counts
        .into_iter()
        .max_by(|(_, (count_a, first_idx_a)), (_, (count_b, first_idx_b))| {
            count_a.cmp(count_b).then_with(|| first_idx_b.cmp(first_idx_a))
        })
        .map_or_else(|| fallback.to_string(), |(prefix, _)| prefix)
}

/// Whether a reflowed blockquote content line must carry an explicit prefix.
///
/// Lines that would start a new block structure (headings, fences, lists, etc.)
/// cannot safely use lazy continuation syntax.
pub(crate) fn should_force_explicit_blockquote_line(content_line: &str) -> bool {
    let trimmed = content_line.trim_start();
    trimmed.starts_with('>')
        || trimmed.starts_with('#')
        || trimmed.starts_with("```")
        || trimmed.starts_with("~~~")
        || is_unordered_list_marker(trimmed)
        || is_numbered_list_item(trimmed)
        || is_horizontal_rule(trimmed)
        || is_definition_list_item(trimmed)
        || (trimmed.starts_with('[') && trimmed.contains("]:"))
        || trimmed.starts_with(":::")
        || (trimmed.starts_with('<')
            && !trimmed.starts_with("<http")
            && !trimmed.starts_with("<https")
            && !trimmed.starts_with("<mailto:"))
}

/// Reflow blockquote content lines and apply continuation style.
///
/// Segments separated by hard breaks are reflowed independently. The output lines
/// receive blockquote prefixes according to `continuation_style`: the first line and
/// any line that would start a new block structure always get an explicit prefix;
/// other lines follow the detected style.
///
/// Returns the styled, reflowed lines (without a trailing newline).
pub fn reflow_blockquote_content(
    lines: &[BlockquoteLineData],
    explicit_prefix: &str,
    continuation_style: BlockquoteContinuationStyle,
    options: &ReflowOptions,
) -> Vec<String> {
    let content_strs: Vec<&str> = lines.iter().map(|l| l.content.as_str()).collect();
    let segments = split_into_segments_strs(&content_strs);
    let mut reflowed_content_lines: Vec<String> = Vec::new();

    for segment in segments {
        let hard_break_type = segment.last().and_then(|&line| {
            let line = line.strip_suffix('\r').unwrap_or(line);
            if line.ends_with('\\') {
                Some("\\")
            } else if line.ends_with("  ") {
                Some("  ")
            } else {
                None
            }
        });

        let pieces: Vec<&str> = segment
            .iter()
            .map(|&line| {
                if let Some(l) = line.strip_suffix('\\') {
                    l.trim_end()
                } else if let Some(l) = line.strip_suffix("  ") {
                    l.trim_end()
                } else {
                    line.trim_end()
                }
            })
            .collect();

        let segment_text = pieces.join(" ");
        let segment_text = segment_text.trim();
        if segment_text.is_empty() {
            continue;
        }

        let mut reflowed = reflow_line(segment_text, options);
        if let Some(break_marker) = hard_break_type
            && !reflowed.is_empty()
        {
            let last_idx = reflowed.len() - 1;
            if !has_hard_break(&reflowed[last_idx]) {
                reflowed[last_idx].push_str(break_marker);
            }
        }
        reflowed_content_lines.extend(reflowed);
    }

    let mut styled_lines: Vec<String> = Vec::new();
    for (idx, line) in reflowed_content_lines.iter().enumerate() {
        let force_explicit = idx == 0
            || continuation_style == BlockquoteContinuationStyle::Explicit
            || should_force_explicit_blockquote_line(line);
        if force_explicit {
            styled_lines.push(format!("{explicit_prefix}{line}"));
        } else {
            styled_lines.push(line.clone());
        }
    }

    styled_lines
}

fn is_blockquote_content_boundary(content: &str) -> bool {
    let trimmed = content.trim();
    trimmed.is_empty()
        || is_block_boundary(trimmed)
        || crate::utils::table_utils::TableUtils::is_potential_table_row(content)
        || trimmed.starts_with(":::")
        || crate::utils::is_template_directive_only(content)
        || is_standalone_attr_list(content)
        || is_snippet_block_delimiter(content)
}

fn split_into_segments_strs<'a>(lines: &[&'a str]) -> Vec<Vec<&'a str>> {
    let mut segments = Vec::new();
    let mut current = Vec::new();

    for &line in lines {
        current.push(line);
        if has_hard_break(line) {
            segments.push(current);
            current = Vec::new();
        }
    }

    if !current.is_empty() {
        segments.push(current);
    }

    segments
}

fn reflow_blockquote_paragraph_at_line(
    content: &str,
    lines: &[&str],
    target_idx: usize,
    options: &ReflowOptions,
) -> Option<ParagraphReflow> {
    let mut anchor_idx = target_idx;
    let mut target_level = if let Some(parsed) = crate::utils::blockquote::parse_blockquote_prefix(lines[target_idx]) {
        parsed.nesting_level
    } else {
        let mut found = None;
        let mut idx = target_idx;
        loop {
            if lines[idx].trim().is_empty() {
                break;
            }
            if let Some(parsed) = crate::utils::blockquote::parse_blockquote_prefix(lines[idx]) {
                found = Some((idx, parsed.nesting_level));
                break;
            }
            if idx == 0 {
                break;
            }
            idx -= 1;
        }
        let (idx, level) = found?;
        anchor_idx = idx;
        level
    };

    // Expand backward to capture prior quote content at the same nesting level.
    let mut para_start = anchor_idx;
    while para_start > 0 {
        let prev_idx = para_start - 1;
        let prev_line = lines[prev_idx];

        if prev_line.trim().is_empty() {
            break;
        }

        if let Some(parsed) = crate::utils::blockquote::parse_blockquote_prefix(prev_line) {
            if parsed.nesting_level != target_level || is_blockquote_content_boundary(parsed.content) {
                break;
            }
            para_start = prev_idx;
            continue;
        }

        let prev_lazy = prev_line.trim_start();
        if is_blockquote_content_boundary(prev_lazy) {
            break;
        }
        para_start = prev_idx;
    }

    // Lazy continuation cannot precede the first explicit marker.
    while para_start < lines.len() {
        let Some(parsed) = crate::utils::blockquote::parse_blockquote_prefix(lines[para_start]) else {
            para_start += 1;
            continue;
        };
        target_level = parsed.nesting_level;
        break;
    }

    if para_start >= lines.len() || para_start > target_idx {
        return None;
    }

    // Collect explicit lines at target level and lazy continuation lines.
    // Each entry is (original_line_idx, BlockquoteLineData).
    let mut collected: Vec<(usize, BlockquoteLineData)> = Vec::new();
    let mut idx = para_start;
    while idx < lines.len() {
        if !collected.is_empty() && has_hard_break(&collected[collected.len() - 1].1.content) {
            break;
        }

        let line = lines[idx];
        if line.trim().is_empty() {
            break;
        }

        if let Some(parsed) = crate::utils::blockquote::parse_blockquote_prefix(line) {
            if parsed.nesting_level != target_level || is_blockquote_content_boundary(parsed.content) {
                break;
            }
            collected.push((
                idx,
                BlockquoteLineData::explicit(trim_preserving_hard_break(parsed.content), parsed.prefix.to_string()),
            ));
            idx += 1;
            continue;
        }

        let lazy_content = line.trim_start();
        if is_blockquote_content_boundary(lazy_content) {
            break;
        }

        collected.push((idx, BlockquoteLineData::lazy(trim_preserving_hard_break(lazy_content))));
        idx += 1;
    }

    if collected.is_empty() {
        return None;
    }

    let para_end = collected[collected.len() - 1].0;
    if target_idx < para_start || target_idx > para_end {
        return None;
    }

    let line_data: Vec<BlockquoteLineData> = collected.iter().map(|(_, d)| d.clone()).collect();

    let fallback_prefix = line_data
        .iter()
        .find_map(|d| d.prefix.clone())
        .unwrap_or_else(|| "> ".to_string());
    let explicit_prefix = dominant_blockquote_prefix(&line_data, &fallback_prefix);
    let continuation_style = blockquote_continuation_style(&line_data);

    let adjusted_line_length = options
        .line_length
        .saturating_sub(display_len(&explicit_prefix, options.length_mode))
        .max(1);

    let adjusted_options = ReflowOptions {
        line_length: adjusted_line_length,
        ..options.clone()
    };

    let styled_lines = reflow_blockquote_content(&line_data, &explicit_prefix, continuation_style, &adjusted_options);

    if styled_lines.is_empty() {
        return None;
    }

    // Calculate byte offsets.
    let mut start_byte = 0;
    for line in lines.iter().take(para_start) {
        start_byte += line.len() + 1;
    }

    let mut end_byte = start_byte;
    for line in lines.iter().take(para_end + 1).skip(para_start) {
        end_byte += line.len() + 1;
    }

    let includes_trailing_newline = para_end != lines.len() - 1 || content.ends_with('\n');
    if !includes_trailing_newline {
        end_byte -= 1;
    }

    let reflowed_joined = styled_lines.join("\n");
    let reflowed_text = if includes_trailing_newline {
        if reflowed_joined.ends_with('\n') {
            reflowed_joined
        } else {
            format!("{reflowed_joined}\n")
        }
    } else if reflowed_joined.ends_with('\n') {
        reflowed_joined.trim_end_matches('\n').to_string()
    } else {
        reflowed_joined
    };

    Some(ParagraphReflow {
        start_byte,
        end_byte,
        reflowed_text,
    })
}

/// Reflow a single paragraph at the specified line number
///
/// This function finds the paragraph containing the given line number,
/// reflows it according to the specified line length, and returns
/// information about the paragraph location and its reflowed text.
///
/// # Arguments
///
/// * `content` - The full document content
/// * `line_number` - The 1-based line number within the paragraph to reflow
/// * `line_length` - The target line length for reflowing
///
/// # Returns
///
/// Returns `Some(ParagraphReflow)` if a paragraph was found and reflowed,
/// or `None` if the line number is out of bounds or the content at that
/// line shouldn't be reflowed (e.g., code blocks, headings, etc.)
pub fn reflow_paragraph_at_line(content: &str, line_number: usize, line_length: usize) -> Option<ParagraphReflow> {
    reflow_paragraph_at_line_with_mode(content, line_number, line_length, ReflowLengthMode::default())
}

/// Reflow a paragraph at the given line with a specific length mode.
pub fn reflow_paragraph_at_line_with_mode(
    content: &str,
    line_number: usize,
    line_length: usize,
    length_mode: ReflowLengthMode,
) -> Option<ParagraphReflow> {
    let options = ReflowOptions {
        line_length,
        length_mode,
        ..Default::default()
    };
    reflow_paragraph_at_line_with_options(content, line_number, &options)
}

/// Reflow a paragraph at the given line using the provided options.
///
/// This is the canonical implementation used by both the rule's fix mode and the
/// LSP "Reflow paragraph" action. Passing a fully configured `ReflowOptions` allows
/// the LSP action to respect user-configured reflow mode, abbreviations, etc.
///
/// # Returns
///
/// Returns `Some(ParagraphReflow)` with byte offsets and reflowed text, or `None`
/// if the line is out of bounds or sits inside a non-reflow-able construct.
pub fn reflow_paragraph_at_line_with_options(
    content: &str,
    line_number: usize,
    options: &ReflowOptions,
) -> Option<ParagraphReflow> {
    if line_number == 0 {
        return None;
    }

    let lines: Vec<&str> = content.lines().collect();

    // Check if line number is valid (1-based)
    if line_number > lines.len() {
        return None;
    }

    let target_idx = line_number - 1; // Convert to 0-based
    let target_line = lines[target_idx];
    let trimmed = target_line.trim();

    // Handle blockquote paragraphs (including lazy continuation lines) with
    // style-preserving output.
    if let Some(blockquote_reflow) = reflow_blockquote_paragraph_at_line(content, &lines, target_idx, options) {
        return Some(blockquote_reflow);
    }

    // Don't reflow special blocks
    if is_paragraph_boundary(trimmed, target_line) {
        return None;
    }

    // Find paragraph start - scan backward until blank line or special block
    let mut para_start = target_idx;
    while para_start > 0 {
        let prev_idx = para_start - 1;
        let prev_line = lines[prev_idx];
        let prev_trimmed = prev_line.trim();

        // Stop at blank line or special blocks
        if is_paragraph_boundary(prev_trimmed, prev_line) {
            break;
        }

        para_start = prev_idx;
    }

    // Find paragraph end - scan forward until blank line or special block
    let mut para_end = target_idx;
    while para_end + 1 < lines.len() {
        let next_idx = para_end + 1;
        let next_line = lines[next_idx];
        let next_trimmed = next_line.trim();

        // Stop at blank line or special blocks
        if is_paragraph_boundary(next_trimmed, next_line) {
            break;
        }

        para_end = next_idx;
    }

    // Extract paragraph lines
    let paragraph_lines = &lines[para_start..=para_end];

    // Calculate byte offsets
    let mut start_byte = 0;
    for line in lines.iter().take(para_start) {
        start_byte += line.len() + 1; // +1 for newline
    }

    let mut end_byte = start_byte;
    for line in paragraph_lines {
        end_byte += line.len() + 1; // +1 for newline
    }

    // Track whether the byte range includes a trailing newline
    // (it doesn't if this is the last line and the file doesn't end with newline)
    let includes_trailing_newline = para_end != lines.len() - 1 || content.ends_with('\n');

    // Adjust end_byte if the last line doesn't have a newline
    if !includes_trailing_newline {
        end_byte -= 1;
    }

    // Join paragraph lines and reflow
    let paragraph_text = paragraph_lines.join("\n");

    // Reflow the paragraph using reflow_markdown to handle it properly
    let reflowed = reflow_markdown(&paragraph_text, options);

    // Ensure reflowed text matches whether the byte range includes a trailing newline
    // This is critical: if the range includes a newline, the replacement must too,
    // otherwise the next line will get appended to the reflowed paragraph
    let reflowed_text = if includes_trailing_newline {
        // Range includes newline - ensure reflowed text has one
        if reflowed.ends_with('\n') {
            reflowed
        } else {
            format!("{reflowed}\n")
        }
    } else {
        // Range doesn't include newline - ensure reflowed text doesn't have one
        if reflowed.ends_with('\n') {
            reflowed.trim_end_matches('\n').to_string()
        } else {
            reflowed
        }
    };

    Some(ParagraphReflow {
        start_byte,
        end_byte,
        reflowed_text,
    })
}
/// Decomposes a raw inline code span string into its inner content and backtick marker.
///
/// For example, `decompose_code_span("`code`")` returns `Some(("code", "`"))`.
/// If the input is not a valid code span (e.g., it doesn't start and end with the
/// same number of backticks), returns `None`.
fn decompose_code_span(raw: &str) -> Option<(&str, &str)> {
    let marker_len = raw.bytes().take_while(|&b| b == b'`').count();
    if marker_len == 0 {
        return None;
    }
    let marker = &raw[..marker_len];
    if raw.len() < marker_len * 2 {
        return None;
    }
    let content = &raw[marker_len..raw.len() - marker_len];
    Some((content, marker))
}

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

    /// `preserves_content` is the last line of defense against a reflow writing
    /// corrupted prose into a file, so it has to actually reject the ways a
    /// reflow can go wrong - not merely accept the ways it can go right.
    #[test]
    fn preserves_content_accepts_whitespace_changes_and_rejects_the_rest() {
        let accepted: &[(&str, &[&str])] = &[
            ("one two three", &["one two three"]),
            ("one two three", &["one two", "three"]),
            ("one two three", &["one", "two", "three"]),
            // Collapsing runs of whitespace and dropping trailing whitespace
            ("one   two  ", &["one two"]),
            // A script written without spaces has to break somewhere
            ("日本語のテキスト", &["日本語の", "テキスト"]),
            // Markers move to the line their content moved to
            ("_First. Second._", &["_First.", "Second._"]),
        ];
        for (original, reflowed) in accepted {
            let reflowed: Vec<String> = reflowed.iter().map(ToString::to_string).collect();
            assert!(
                preserves_content(original, &reflowed),
                "{original:?} -> {reflowed:?} only moves whitespace"
            );
        }

        let rejected: &[(&str, &[&str])] = &[
            // Dropped
            ("one two three", &["one two"]),
            // Invented
            ("one two", &["one two three"]),
            // Reordered
            ("one two", &["two one"]),
            // Duplicated
            ("_First. Second._", &["_First._", "_Second._"]),
            // Two words glued into one
            ("alpha and beta", &["alpha", "andbeta"]),
            // A space deleted around punctuation
            ("mot suivant : autre", &["mot suivant: autre"]),
        ];
        for (original, reflowed) in rejected {
            let reflowed: Vec<String> = reflowed.iter().map(ToString::to_string).collect();
            assert!(
                !preserves_content(original, &reflowed),
                "{original:?} -> {reflowed:?} changes the text, not just its line breaks"
            );
        }
    }

    /// A rejected reflow leaves the line alone rather than writing the damage.
    #[test]
    fn reflow_line_falls_back_to_the_input_when_content_would_change() {
        let options = ReflowOptions {
            line_length: 40,
            ..Default::default()
        };
        let line = "one two three four five six seven eight nine ten";

        assert!(preserves_content(line, &reflow_line(line, &options)));
        assert_eq!(reflow_line(line, &options), reflow_line_unchecked(line, &options));
    }

    #[test]
    fn cascade_split_line_handles_a_very_long_line_without_overflowing() {
        // A single line of thousands of words once drove `cascade_split_line`
        // into deep recursion (stack overflow / hang). The iterative version
        // must complete and split it into many lines that each fit the width and
        // that together preserve every word. The test finishing at all is the
        // core assertion (no stack overflow); the content checks guard behavior.
        let words: Vec<String> = (0..4000).map(|i| format!("word{i}")).collect();
        let line = words.join(" ");

        let options = ReflowOptions {
            line_length: 80,
            length_mode: ReflowLengthMode::Chars,
            ..Default::default()
        };
        let out = cascade_split_line(&line, &options);

        assert!(out.len() > 1, "a very long line should split into many lines");
        for segment in &out {
            assert!(
                display_len(segment, ReflowLengthMode::Chars) <= 80 || !segment.contains(' '),
                "each wrapped line should fit the width (or be a single unbreakable token)"
            );
        }
        // Every original word survives, in order.
        let rejoined = out.join(" ");
        let original_words: Vec<&str> = line.split(' ').collect();
        let result_words: Vec<&str> = rejoined.split_whitespace().collect();
        assert_eq!(original_words, result_words, "reflow must preserve all words in order");
    }

    /// Unit test for private helper function text_ends_with_abbreviation()
    ///
    /// This test stays inline because it tests a private function.
    /// All other tests (public API, integration tests) are in tests/utils/text_reflow_test.rs
    #[test]
    fn test_helper_function_text_ends_with_abbreviation() {
        // Test the helper function directly
        let abbreviations = get_abbreviations(&None);

        // True cases - built-in abbreviations (titles and i.e./e.g.)
        assert!(text_ends_with_abbreviation("Dr.", &abbreviations));
        assert!(text_ends_with_abbreviation("word Dr.", &abbreviations));
        assert!(text_ends_with_abbreviation("e.g.", &abbreviations));
        assert!(text_ends_with_abbreviation("i.e.", &abbreviations));
        assert!(text_ends_with_abbreviation("Mr.", &abbreviations));
        assert!(text_ends_with_abbreviation("Mrs.", &abbreviations));
        assert!(text_ends_with_abbreviation("Ms.", &abbreviations));
        assert!(text_ends_with_abbreviation("Prof.", &abbreviations));

        // False cases - NOT in built-in list (etc doesn't always have period)
        assert!(!text_ends_with_abbreviation("etc.", &abbreviations));
        assert!(!text_ends_with_abbreviation("paradigms.", &abbreviations));
        assert!(!text_ends_with_abbreviation("programs.", &abbreviations));
        assert!(!text_ends_with_abbreviation("items.", &abbreviations));
        assert!(!text_ends_with_abbreviation("systems.", &abbreviations));
        assert!(!text_ends_with_abbreviation("Dr?", &abbreviations)); // question mark, not period
        assert!(!text_ends_with_abbreviation("Mr!", &abbreviations)); // exclamation, not period
        assert!(!text_ends_with_abbreviation("paradigms?", &abbreviations)); // question mark
        assert!(!text_ends_with_abbreviation("word", &abbreviations)); // no punctuation
        assert!(!text_ends_with_abbreviation("", &abbreviations)); // empty string
    }

    #[test]
    fn test_footnote_after_period_splits_sentence() {
        // A footnote reference glued to the period (no space) must not swallow
        // the sentence boundary; the reference stays attached to the sentence
        // it annotates.
        let text = "First sentence.[^1] Second sentence.";
        let sentences = split_into_sentences(text, None);
        assert_eq!(
            sentences,
            vec!["First sentence.[^1]".to_string(), "Second sentence.".to_string()],
            "footnote glued to the period should keep the boundary and stay attached to the first sentence"
        );
    }

    #[test]
    fn test_multiple_consecutive_footnotes_after_period_splits_sentence() {
        // Multiple footnote references glued back-to-back after the period.
        let text = "Notes here.[^1][^2] Second sentence.";
        let sentences = split_into_sentences(text, None);
        assert_eq!(
            sentences,
            vec!["Notes here.[^1][^2]".to_string(), "Second sentence.".to_string()]
        );
    }

    #[test]
    fn test_footnote_before_period_still_splits_sentence() {
        // Control: a footnote reference before the period was already followed
        // by a space, so this boundary worked before this fix and must keep
        // working.
        let text = "Annotation here[^1]. Second sentence.";
        let sentences = split_into_sentences(text, None);
        assert_eq!(
            sentences,
            vec!["Annotation here[^1].".to_string(), "Second sentence.".to_string()]
        );
    }

    #[test]
    fn test_mid_sentence_footnote_does_not_split() {
        // A footnote reference not glued to sentence-ending punctuation must not
        // introduce a spurious boundary at the bracket itself.
        let text = "The system word[^1] more words. Next sentence.";
        let sentences = split_into_sentences(text, None);
        assert_eq!(
            sentences,
            vec![
                "The system word[^1] more words.".to_string(),
                "Next sentence.".to_string()
            ]
        );
    }

    #[test]
    fn test_bare_numeric_bracket_after_period_does_not_split() {
        // A bare `[1]` is link/citation-like text, not footnote syntax; the fix
        // is scoped to `[^label]` only.
        let text = "Citation here.[1] Second sentence.";
        let sentences = split_into_sentences(text, None);
        assert_eq!(
            sentences,
            vec![text.to_string()],
            "a bare numeric bracket must not be treated as a sentence boundary"
        );
    }

    #[test]
    fn test_footnote_glued_to_following_word_does_not_split() {
        // No whitespace after the footnote reference means there is nowhere a
        // next sentence can start, so this must not be treated as a boundary.
        let text = "First sentence.[^1]Continued glued text.";
        let sentences = split_into_sentences(text, None);
        assert_eq!(sentences, vec![text.to_string()]);
    }

    #[test]
    fn test_footnote_at_end_of_text_is_preserved() {
        // A footnote reference at the very end of the text has nothing after it
        // to split off; it is preserved as part of the single trailing sentence.
        let text = "Sentence.[^1]";
        let sentences = split_into_sentences(text, None);
        assert_eq!(sentences, vec![text.to_string()]);
    }

    #[test]
    fn test_abbreviation_before_footnote_does_not_split() {
        // The existing abbreviation guard must still apply when a footnote
        // reference immediately follows the abbreviation's period.
        let text = "See the notes, e.g.[^1] this one.";
        let sentences = split_into_sentences(text, None);
        assert_eq!(
            sentences,
            vec![text.to_string()],
            "e.g. is an abbreviation, not a sentence boundary"
        );
    }

    #[test]
    fn sentence_boundary_never_falls_inside_an_atomic_construct() {
        // Each construct holds text that reads like a sentence boundary
        // (`. ` followed by a capital) but is one unit to the renderer: a
        // break inside it rewrites the document. The boundary after each
        // construct is real and must still split, so a construct that simply
        // silenced the splitter would fail here too.
        let cases = [
            "Prefix [link. Still link](https://example.com) tail. Next sentence.",
            "Prefix [target](<https://example.com/First. Second>) tail. Next sentence.",
            "Prefix [text](url \"Title. More\") tail. Next sentence.",
            "Prefix ![alt. Alt](img.png) tail. Next sentence.",
            "Prefix [ref text. More][ref] tail. Next sentence.",
            "Prefix [collapsed. More][] tail. Next sentence.",
            "Prefix [[Page name. Title]] tail. Next sentence.",
            "Prefix $x. Y$ tail. Next sentence.",
            "Prefix $$x. Y$$ tail. Next sentence.",
            "Prefix <span title=\"A. B\">x</span> tail. Next sentence.",
            "Prefix `code. Still code` tail. Next sentence.",
        ];
        for text in cases {
            let sentences = split_into_sentences(text, None);
            let (head, tail) = text.rsplit_once(" tail. ").expect("case has a tail");
            assert_eq!(
                sentences,
                vec![format!("{head} tail."), tail.to_string()],
                "input {text:?}"
            );
        }

        // A bare `[text]` is a link only when its label is defined. Defined,
        // or with the definitions unknown, it is held whole like the rest;
        // known undefined, it is prose and the boundary inside it is real.
        let text = "Prefix [shortcut. More] tail. Next sentence.";
        let whole = vec![
            "Prefix [shortcut. More] tail.".to_string(),
            "Next sentence.".to_string(),
        ];
        let defined = HashSet::from(["shortcut. more".to_string()]);
        assert_eq!(split_into_sentences(text, Some(&defined)), whole);
        assert_eq!(split_into_sentences(text, None), whole);
        assert_eq!(
            split_into_sentences(text, Some(&HashSet::new())),
            vec!["Prefix [shortcut.", "More] tail.", "Next sentence."]
        );
    }

    #[test]
    fn a_sentence_may_open_with_a_link_or_image() {
        // The next sentence's first letter sits behind the link opener; a
        // capital there is a capital start. The reflow already emits the text
        // before the link as its own line, so the check has to count the same
        // boundary or it never asks for that split.
        for text in [
            "Opening sentence. [First. Second](https://example.com)",
            "Opening sentence. ![First. Second](img.png)",
            "Opening sentence. [[First. Second]]",
            "Opening sentence. [[first-note|First. Second]]",
            "Opening sentence. [Ref link][ref]",
            // A link whose text is an image opens with the image's alt text,
            // one construct inside another; each is walked into at its own start.
            "Opening sentence. [![First image](img.png)](url) continues.",
            "Opening sentence. [![First image](img.png)][ref] continues.",
            // The nested image may be a reference image; its full and
            // collapsed forms are images whether or not the label is defined.
            "Opening sentence. [![First image][img]](url) continues.",
            "Opening sentence. [![First image][]](url) continues.",
            "Opening sentence. [![First image][img]][ref] continues.",
        ] {
            let (head, tail) = text.split_once(". ").expect("case has a boundary");
            assert_eq!(
                split_into_sentences(text, None),
                vec![format!("{head}."), tail.to_string()],
                "input {text:?}"
            );
        }
        // A shortcut reference image nested in a link is an image only when
        // its label is defined, exactly as at the top level.
        let text = "Opening sentence. [![First image]](url) continues.";
        let defined = HashSet::from(["first image".to_string()]);
        assert_eq!(
            split_into_sentences(text, Some(&defined)),
            vec!["Opening sentence.", "[![First image]](url) continues."]
        );
        assert_eq!(
            split_into_sentences(text, Some(&HashSet::new())),
            vec![text.to_string()],
            "an undefined shortcut is bracketed text, and `!` opens no sentence"
        );
        // The nested image's alt text is what has to be capitalized: the same
        // link with a lowercase alt opens no sentence.
        assert_eq!(
            split_into_sentences("Opening sentence. [![first image](img.png)](url) continues.", None),
            vec!["Opening sentence. [![first image](img.png)](url) continues."]
        );
        // A bare `[text]` whose label is defined is a link too, and its text
        // opens the sentence the same way.
        let defined = HashSet::from(["smith 2020".to_string()]);
        assert_eq!(
            split_into_sentences("Claim ends here. [Smith 2020] more text.", Some(&defined)),
            vec!["Claim ends here.", "[Smith 2020] more text."]
        );
        // Controls: a lowercase link text is no sentence start, and a bracket
        // the parse reads as text is not a link opener, so a citation, an
        // undefined shortcut, a footnote label or a link left unterminated
        // starts no sentence however it is capitalized. The definitions are
        // known and empty here, as the rule always supplies them.
        let none_defined = HashSet::new();
        for text in [
            "Opening sentence. [first link](https://example.com) continues.",
            "Opening sentence. [[first note]] continues.",
            "Opening sentence. [[First Note|first note]] continues.",
            "Opening sentence. [[Page continues.",
            "Opening sentence. [[First] stray]] continues.",
            "Opening sentence. ![first alt](img.png) continues.",
            "Opening sentence. [1] is the citation.",
            "Opening sentence. [First](unterminated",
            "Opening sentence. [First][unterminated",
            "Opening sentence. [First] (aside) continues.",
            "Claim ends here. [Smith 2020]",
            "Claim ends here. [Smith 2020] more text.",
            "See the RFC. [RFC] More text.",
            "Claim ends here. [^Note] more text.",
        ] {
            assert_eq!(
                split_into_sentences(text, Some(&none_defined)),
                vec![text.to_string()],
                "input {text:?}"
            );
        }
    }

    #[test]
    fn link_opener_is_read_off_the_parse() {
        // Length of the opener at the start of `text`, or 0 when the parse
        // (with the given definitions) finds no link, image or wikilink there.
        let len = |text: &str, defs: Option<&HashSet<String>>| {
            let chars: Vec<char> = text.chars().collect();
            let char_offsets = char_byte_offsets(&chars);
            let NestedStructure { links, .. } = sentence_structure(text, defs);
            let st = SentenceText {
                text,
                chars: &chars,
                char_offsets: &char_offsets,
                links: &links,
            };
            st.link_end_at(0).map_or(0, |end| link_opener_len(&chars, 0, end))
        };
        let none = HashSet::new();
        assert_eq!(len("[text](url)", Some(&none)), 1);
        assert_eq!(
            len("[text][ref]", Some(&none)),
            1,
            "a full reference is a link whether or not defined"
        );
        assert_eq!(len("[text][]", Some(&none)), 1);
        assert_eq!(len("![alt](img.png)", Some(&none)), 2);
        assert_eq!(len("[[wiki]]", Some(&none)), 2);
        assert_eq!(
            len("[[wiki|shown]]", Some(&none)),
            7,
            "the displayed text starts after the alias pipe"
        );
        assert_eq!(len("![[img.png|100]]", Some(&none)), 11);
        assert_eq!(len("[[wiki|a|b]]", Some(&none)), 7, "the first pipe starts the alias");
        assert_eq!(
            len("[[wiki|shown]] [[a|b]]", Some(&none)),
            7,
            "a pipe past the closing `]]` is not this alias"
        );
        assert_eq!(
            len("[a \\] b](url)", Some(&none)),
            1,
            "an escaped bracket does not close the text"
        );
        assert_eq!(
            len("[![alt](img)](url)", Some(&none)),
            1,
            "the outer opener is skipped first"
        );
        // Text to the parse, so no opener: unterminated links, an unclosed or
        // malformed wikilink, a shortcut nothing defines, and a footnote
        // reference, which the paragraph-level parse has no definition for.
        for text in [
            "[^1]",
            "[text](unterminated",
            "[text][unterminated",
            "[text] (url)",
            "[[wiki",
            "[[wiki]",
            "[[First] stray]]",
            "[Smith 2020]",
            "[Smith 2020] (see also)",
            "[unclosed",
            "!bang",
            "text",
        ] {
            assert_eq!(len(text, Some(&none)), 0, "input {text:?}");
        }
        // The same shortcut is a link once its label is defined, or when the
        // definitions are unknown.
        let smith = HashSet::from(["smith 2020".to_string()]);
        assert_eq!(len("[Smith 2020]", Some(&smith)), 1);
        assert_eq!(len("[Smith 2020]", None), 1);
    }

    #[test]
    fn sentence_per_line_reflow_breaks_before_a_bracket_only_where_the_check_counts() {
        // The check counts a boundary before a link, image or wikilink whose
        // text starts a sentence and none before a lowercase one, a bare
        // citation or a glued link. The reflow has to break at exactly those
        // boundaries: a break the check never counts is a fix that keeps
        // reporting, and a boundary the reflow ignores is a line it never
        // splits. The definitions are known, as the rule always supplies
        // them: `[RFC]` alone is a citation, `[Spec]` a defined shortcut link.
        let defined = HashSet::from(["spec".to_string()]);
        let options = ReflowOptions {
            line_length: 120,
            sentence_per_line: true,
            defined_references: Some(defined.clone()),
            ..Default::default()
        };
        for (text, expected) in [
            (
                "Claim ends here. [Smith](https://example.com) more text. Second sentence.",
                vec![
                    "Claim ends here.",
                    "[Smith](https://example.com) more text.",
                    "Second sentence.",
                ],
            ),
            (
                "Wow! [smith](https://example.com) more text. Second sentence.",
                vec!["Wow!", "[smith](https://example.com) more text.", "Second sentence."],
            ),
            (
                "Claim ends here. [smith](https://example.com) more text. Second sentence.",
                vec![
                    "Claim ends here. [smith](https://example.com) more text.",
                    "Second sentence.",
                ],
            ),
            (
                "Claim ends here. [smith][ref] more text. Second sentence.",
                vec!["Claim ends here. [smith][ref] more text.", "Second sentence."],
            ),
            (
                "Claim ends here. ![alt](img.png) more text. Second sentence.",
                vec!["Claim ends here. ![alt](img.png) more text.", "Second sentence."],
            ),
            (
                "Claim ends here.[Link](https://example.com) more text. Second sentence.",
                vec![
                    "Claim ends here.[Link](https://example.com) more text.",
                    "Second sentence.",
                ],
            ),
            (
                "See the RFC. [RFC] More text. Second sentence.",
                vec!["See the RFC. [RFC] More text.", "Second sentence."],
            ),
            (
                "See the spec. [Spec] More text. Second sentence.",
                vec!["See the spec.", "[Spec] More text.", "Second sentence."],
            ),
            (
                "See the spec. [spec] more text. Second sentence.",
                vec!["See the spec. [spec] more text.", "Second sentence."],
            ),
            (
                "Claim ends here. [[page|Second sentence]] continues. Third sentence.",
                vec![
                    "Claim ends here.",
                    "[[page|Second sentence]] continues.",
                    "Third sentence.",
                ],
            ),
            (
                "Claim ends here. [[Page|second sentence]] continues. Third sentence.",
                vec![
                    "Claim ends here. [[Page|second sentence]] continues.",
                    "Third sentence.",
                ],
            ),
        ] {
            let lines = reflow_line(text, &options);
            assert_eq!(lines, expected, "input {text:?}");
            // The check counts the same number of sentences on the input as
            // the reflow produced lines, and one on each line it produced.
            assert_eq!(
                split_into_sentences(text, Some(&defined)).len(),
                expected.len(),
                "check count for {text:?}"
            );
            for line in &lines {
                assert_eq!(
                    split_into_sentences(line, Some(&defined)).len(),
                    1,
                    "line {line:?} of {text:?}"
                );
            }
        }
    }

    #[test]
    fn sentence_per_line_reflow_holds_atomic_constructs_whole() {
        // The reflow assembles a line one element at a time and re-splits the
        // whole line after each text element, so an atomic element already on
        // the line is exposed to the splitter along with the text after it.
        let options = ReflowOptions {
            line_length: 80,
            sentence_per_line: true,
            ..Default::default()
        };
        let lines = reflow_line(
            "Prefix `code. Still code` and [link. Still link](https://example.com) tail. Next sentence.",
            &options,
        );
        assert_eq!(
            lines,
            vec![
                "Prefix `code. Still code` and [link. Still link](https://example.com) tail.".to_string(),
                "Next sentence.".to_string(),
            ]
        );

        let lines = reflow_line(
            "Prefix ![alt. Alt](img.png) and [target](<https://example.com/First. Second>) tail. Next sentence.",
            &options,
        );
        assert_eq!(
            lines,
            vec![
                "Prefix ![alt. Alt](img.png) and [target](<https://example.com/First. Second>) tail.".to_string(),
                "Next sentence.".to_string(),
            ]
        );

        // Control: a link that carries no boundary of its own leaves the
        // surrounding boundaries exactly where they were.
        let lines = reflow_line("First one. Then [link](url) second. Third one.", &options);
        assert_eq!(
            lines,
            vec![
                "First one.".to_string(),
                "Then [link](url) second.".to_string(),
                "Third one.".to_string(),
            ]
        );
    }

    #[test]
    fn test_is_unordered_list_marker() {
        // Valid unordered list markers
        assert!(is_unordered_list_marker("- item"));
        assert!(is_unordered_list_marker("* item"));
        assert!(is_unordered_list_marker("+ item"));
        assert!(is_unordered_list_marker("-")); // lone marker
        assert!(is_unordered_list_marker("*"));
        assert!(is_unordered_list_marker("+"));

        // Not list markers
        assert!(!is_unordered_list_marker("---")); // horizontal rule
        assert!(!is_unordered_list_marker("***")); // horizontal rule
        assert!(!is_unordered_list_marker("- - -")); // horizontal rule
        assert!(!is_unordered_list_marker("* * *")); // horizontal rule
        assert!(!is_unordered_list_marker("*emphasis*")); // emphasis, not list
        assert!(!is_unordered_list_marker("-word")); // no space after marker
        assert!(!is_unordered_list_marker("")); // empty
        assert!(!is_unordered_list_marker("text")); // plain text
        assert!(!is_unordered_list_marker("# heading")); // heading
    }

    #[test]
    fn test_is_block_boundary() {
        // Block boundaries
        assert!(is_block_boundary("")); // empty line
        assert!(is_block_boundary("# Heading")); // ATX heading
        assert!(is_block_boundary("## Level 2")); // ATX heading
        assert!(is_block_boundary("```rust")); // code fence
        assert!(is_block_boundary("~~~")); // tilde code fence
        assert!(is_block_boundary("> quote")); // blockquote
        assert!(is_block_boundary("| cell |")); // table
        assert!(is_block_boundary("[link]: http://example.com")); // reference def
        assert!(is_block_boundary("---")); // horizontal rule
        assert!(is_block_boundary("***")); // horizontal rule
        assert!(is_block_boundary("- item")); // unordered list
        assert!(is_block_boundary("* item")); // unordered list
        assert!(is_block_boundary("+ item")); // unordered list
        assert!(is_block_boundary("1. item")); // ordered list
        assert!(is_block_boundary("10. item")); // ordered list
        assert!(is_block_boundary(": definition")); // definition list
        assert!(is_block_boundary(":::")); // div marker
        assert!(is_block_boundary("::::: {.callout-note}")); // div marker with attrs

        // NOT block boundaries (paragraph continuation)
        assert!(!is_block_boundary("regular text"));
        assert!(!is_block_boundary("*emphasis*")); // emphasis, not list
        assert!(!is_block_boundary("[link](url)")); // inline link, not reference def
        assert!(!is_block_boundary("some words here"));
    }

    #[test]
    fn test_definition_list_boundary_in_single_line_paragraph() {
        // Verifies that a definition list item after a single-line paragraph
        // is treated as a block boundary, not merged into the paragraph
        let options = ReflowOptions {
            line_length: 80,
            ..Default::default()
        };
        let input = "Term\n: Definition of the term";
        let result = reflow_markdown(input, &options);
        // The definition list marker should remain on its own line
        assert!(
            result.contains(": Definition"),
            "Definition list item should not be merged into previous line. Got: {result:?}"
        );
        let lines: Vec<&str> = result.lines().collect();
        assert_eq!(lines.len(), 2, "Should remain two separate lines. Got: {lines:?}");
        assert_eq!(lines[0], "Term");
        assert_eq!(lines[1], ": Definition of the term");
    }

    #[test]
    fn test_is_paragraph_boundary() {
        // Core block boundary checks are inherited
        assert!(is_paragraph_boundary("# Heading", "# Heading"));
        assert!(is_paragraph_boundary("- item", "- item"));
        assert!(is_paragraph_boundary(":::", ":::"));
        assert!(is_paragraph_boundary(": definition", ": definition"));

        // Indented code blocks (≥4 spaces or tab)
        assert!(is_paragraph_boundary("code", "    code"));
        assert!(is_paragraph_boundary("code", "\tcode"));

        // Table rows via is_potential_table_row
        assert!(is_paragraph_boundary("| a | b |", "| a | b |"));
        assert!(is_paragraph_boundary("a | b", "a | b")); // pipe-delimited without leading pipe

        // Not paragraph boundaries
        assert!(!is_paragraph_boundary("regular text", "regular text"));
        assert!(!is_paragraph_boundary("text", "  text")); // 2-space indent is not code
    }

    #[test]
    fn test_div_marker_boundary_in_reflow_paragraph_at_line() {
        // Verifies that div markers (:::) are treated as paragraph boundaries
        // in reflow_paragraph_at_line, preventing reflow across div boundaries
        let content = "Some paragraph text here.\n\n::: {.callout-note}\nThis is a callout.\n:::\n";
        // Line 3 is the div marker — should not be reflowed
        let result = reflow_paragraph_at_line(content, 3, 80);
        assert!(result.is_none(), "Div marker line should not be reflowed");
    }

    #[test]
    fn starts_block_construct_detects_block_openers() {
        // Bullet list markers: marker char followed by space or end
        for case in ["- item", "-", "* item", "*", "+ item", "+", "-\titem"] {
            assert!(starts_block_construct(case), "bullet: {case:?}");
        }
        // Ordered list markers: only a list numbered 1 with a non-empty first
        // item interrupts a paragraph. Leading zeros keep the number 1.
        for case in ["1. item", "1) item", "01. x", "000000001. x", "1.\titem"] {
            assert!(starts_block_construct(case), "ordered: {case:?}");
        }
        // Blockquote: `>` needs no following space
        for case in ["> quote", ">quote", ">"] {
            assert!(starts_block_construct(case), "blockquote: {case:?}");
        }
        // ATX headings: 1-6 hashes then space or end
        for case in ["# heading", "###### h6", "#", "##"] {
            assert!(starts_block_construct(case), "heading: {case:?}");
        }
        // Code fences: 3+ backticks or tildes
        for case in ["```", "```rust", "````", "~~~", "~~~text"] {
            assert!(starts_block_construct(case), "fence: {case:?}");
        }
        // Setext underlines and thematic breaks
        for case in ["---", "--", "===", "=", "***", "___", "_ _ _", "- - -"] {
            assert!(starts_block_construct(case), "setext/thematic: {case:?}");
        }
        // Footnote and link-reference definitions: hoisting one to line start
        // reclassifies it and can resolve dangling references elsewhere
        for case in [
            "[^1]: text",
            "[^note]:",
            "[ref]: http://example.com",
            "[wat]: url follows",
        ] {
            assert!(starts_block_construct(case), "definition: {case:?}");
        }
        // Block-level HTML tags (rumdl parser's HTML block classification)
        for case in ["<div>content", "</div>", "<p>text", "<table>", "<pre>code", "<h1>x"] {
            assert!(starts_block_construct(case), "html block: {case:?}");
        }
    }

    #[test]
    fn starts_block_construct_allows_ordinary_prose() {
        for case in [
            "",
            "word",
            "-5 degrees",
            "--flag",
            "-item",
            "#hashtag",
            "####### seven hashes is not a heading",
            "1.5 million",
            "1234567890. ten digits is not a list marker",
            "0000000001. ten digits is not a list marker either",
            // A number other than 1 cannot interrupt a paragraph, nor can an
            // empty first item, so neither changes the parse at line start.
            "2. item",
            "7. item",
            "0. item",
            "42) x",
            "123456. item",
            "1.",
            "1)",
            "123456.",
            "123456)",
            "1.item",
            "1:30 pm",
            "*emphasis*",
            "**bold** text",
            "__bold__ text",
            "_emphasis_ text",
            "`code` span",
            "`` double backtick span ``",
            "~~strikethrough~~",
            "=x",
            "== ==",
            "(parenthetical)",
            "[link](url)",
            "[text][ref] more",
            "[bracketed] aside",
            "[a](b) [ref]: first bracket is a link, not a label",
            "[esc\\]: not a close] text",
            "<span>inline</span>",
            "<b>bold</b>",
            "<https://example.com> autolink",
            "<mailto:a@b.com>",
            "<notarealtag>",
        ] {
            assert!(!starts_block_construct(case), "prose: {case:?}");
        }
    }

    #[test]
    fn merge_block_construct_continuations_merges_marker_led_lines() {
        let lines = vec![
            "First sentence?".to_string(),
            "- looks like a list item".to_string(),
            "Second sentence.".to_string(),
        ];
        assert_eq!(
            merge_block_construct_continuations(lines),
            vec![
                "First sentence? - looks like a list item".to_string(),
                "Second sentence.".to_string(),
            ]
        );

        // The first line keeps its position: it replaces the paragraph's
        // original start, where the source already established the context.
        let lines = vec!["- real list content".to_string(), "continuation".to_string()];
        assert_eq!(
            merge_block_construct_continuations(lines.clone()),
            lines,
            "first line must never be merged"
        );

        // Folding cascades: `1.` alone is inert, but absorbing `[ref]:` makes
        // it a list item, so the grown line has to fold back in turn.
        let lines = vec!["prose".to_string(), "1.".to_string(), "[ref]:".to_string()];
        assert_eq!(
            merge_block_construct_continuations(lines),
            vec!["prose 1. [ref]:".to_string()],
            "a merge that creates an opener must fold again"
        );
    }

    #[test]
    fn wrap_never_starts_a_line_with_a_block_marker() {
        let options = ReflowOptions {
            line_length: 25,
            ..Default::default()
        };
        // The dash lands exactly at the wrap point; the wrapper must break one
        // word earlier so the dash stays mid-line.
        let lines = reflow_line(
            "Some words here and then - a dash clause that wraps around the limit.",
            &options,
        );
        assert_eq!(
            lines,
            vec![
                "Some words here and",
                "then - a dash clause that",
                "wraps around the limit."
            ]
        );

        // Every marker category must stay mid-line in wrap mode, whatever the width.
        for input in [
            "Alpha beta gamma delta epsilon - dash clause here to wrap",
            "Alpha beta gamma delta epsilon > quote lookalike here to wrap",
            "Alpha beta gamma delta epsilon # heading lookalike here to wrap",
            "Alpha beta gamma delta epsilon 1. ordered lookalike here to wrap",
            "Alpha beta gamma delta epsilon * star clause here to wrap",
            "Alpha beta gamma delta epsilon + plus clause here to wrap",
        ] {
            for width in 10..40 {
                let options = ReflowOptions {
                    line_length: width,
                    ..Default::default()
                };
                for line in reflow_line(input, &options) {
                    assert!(
                        !starts_block_construct(&line),
                        "width {width}: wrapped line opens a block construct: {line:?} (input {input:?})"
                    );
                }
            }
        }
    }

    #[test]
    fn sentence_per_line_keeps_block_markers_mid_line() {
        let options = ReflowOptions {
            line_length: 80,
            sentence_per_line: true,
            ..Default::default()
        };
        // A sentence "starting" with a dash must stay attached to the previous
        // sentence instead of becoming a list item (issue #728).
        let lines = reflow_line(
            "Google Calendar (Can't we get rid of this dependency? - I don't really see the need)",
            &options,
        );
        assert_eq!(
            lines,
            vec!["Google Calendar (Can't we get rid of this dependency? - I don't really see the need)".to_string()]
        );

        // Same for heading, blockquote, and ordered-list lookalikes.
        let lines = reflow_line("See section 4? # is the marker we use. Fine.", &options);
        assert_eq!(lines, vec!["See section 4? # is the marker we use.", "Fine."]);

        let lines = reflow_line("Is this a problem? > I quote someone here.", &options);
        assert_eq!(lines, vec!["Is this a problem? > I quote someone here."]);

        let lines = reflow_line("Another case! 1. Not a list. More text follows here.", &options);
        for line in &lines {
            assert!(
                !starts_block_construct(line),
                "sentence-per-line output opens a block construct: {line:?}"
            );
        }
    }

    /// Sentence-per-line reflow of `input` under `require-sentence-capital`.
    fn strict_sentence_lines(input: &str, require_sentence_capital: bool) -> Vec<String> {
        let options = ReflowOptions {
            line_length: 80,
            sentence_per_line: true,
            require_sentence_capital,
            ..Default::default()
        };
        reflow_line(input, &options)
    }

    #[test]
    fn strict_mode_lets_a_sentence_open_with_a_number() {
        // `require-sentence-capital` exists to keep `word. lowercase`
        // continuations together; a digit is not a lowercase letter, so a
        // sentence may open with a count, a year, an ordinal or a time.
        for (input, expected) in [
            (
                "The number of items was 5. 2 of them failed.",
                vec!["The number of items was 5.", "2 of them failed."],
            ),
            (
                "Sometimes we have 2. 3 might be here.",
                vec!["Sometimes we have 2.", "3 might be here."],
            ),
            (
                "The number of items was 5. 2nd sentence.",
                vec!["The number of items was 5.", "2nd sentence."],
            ),
            (
                "Released in 2020. 3 of them failed.",
                vec!["Released in 2020.", "3 of them failed."],
            ),
            (
                "First sentence. 2nd sentence.",
                vec!["First sentence.", "2nd sentence."],
            ),
            (
                "We met at 6:00 sharp. 6:00 is early.",
                vec!["We met at 6:00 sharp.", "6:00 is early."],
            ),
            ("Pi is 3.14 roughly. Next.", vec!["Pi is 3.14 roughly.", "Next."]),
            // A `?` inside a quotation follows the same rule as a period, so a
            // digit after the closing quote opens a sentence there too.
            (
                "A \"Is this a test?\" 2020 was memorable.",
                vec!["A \"Is this a test?\"", "2020 was memorable."],
            ),
        ] {
            assert_eq!(strict_sentence_lines(input, true), expected, "input {input:?}");
        }

        // A lowercase continuation still holds the sentence open, and an
        // abbreviation before a number is still an abbreviation.
        for input in [
            "The count was 5. and that was all.",
            "See fig. 3 for details.",
            "See no. 5 in the list.",
            "See ch. 12 and vol. 3 for more.",
            "A \"Is this a test?\" guide to it.",
        ] {
            assert_eq!(
                strict_sentence_lines(input, true),
                vec![input.to_string()],
                "input {input:?}"
            );
        }
    }

    #[test]
    fn sentence_never_opens_with_an_ordered_list_marker() {
        // An inline enumerator keeps its place after the sentence before it,
        // in either mode. Every line the splitter produces ends a sentence,
        // and `2. Do that.` under such a line is a list item to MD032 (in any
        // document that has a list) and to CommonMark for `1.`, so the
        // enumerator is never hoisted to line start; the enumerated text opens
        // the next line instead.
        for (input, require_capital, expected) in [
            (
                "Steps: 1. Do this. 2. Do that.",
                true,
                vec!["Steps: 1.", "Do this. 2.", "Do that."],
            ),
            (
                "First sentence. 1. Do that.",
                true,
                vec!["First sentence. 1.", "Do that."],
            ),
            ("Do this! 2. Do that.", true, vec!["Do this! 2.", "Do that."]),
            ("Do this. 12) Do that.", true, vec!["Do this. 12) Do that."]),
            ("Do this. 2. do that.", true, vec!["Do this. 2. do that."]),
            ("Do this. 2. do that.", false, vec!["Do this. 2.", "do that."]),
            (
                "Twelve. 1234567890. next one here.",
                true,
                vec!["Twelve. 1234567890. next one here."],
            ),
            // A number that is not followed by a marker's `.`/`)` and space
            // opens a sentence as usual.
            ("Do this. 2 more times.", true, vec!["Do this.", "2 more times."]),
            ("How many? 2.", true, vec!["How many?", "2."]),
            // CJK punctuation needs no space before the next sentence, and the
            // marker rule holds after it as well.
            ("第一句。2. Do that.", true, vec!["第一句。2.", "Do that."]),
            ("第一句。 2) 第二句。", true, vec!["第一句。 2) 第二句。"]),
            ("第一句。2 more.", true, vec!["第一句。", "2 more."]),
            ("第一句。第二句。", true, vec!["第一句。", "第二句。"]),
        ] {
            let lines = strict_sentence_lines(input, require_capital);
            assert_eq!(lines, expected, "input {input:?}, require capital {require_capital}");
            for line in &lines {
                let chars: Vec<char> = line.chars().collect();
                assert!(
                    !opens_ordered_list_marker(&chars),
                    "line opens with an ordered-list marker: {line:?} (input {input:?})"
                );
            }
        }
    }

    #[test]
    fn opens_ordered_list_marker_matches_the_marker_shape() {
        let chars = |s: &str| s.chars().collect::<Vec<char>>();
        for text in ["2. x", "1) x", "12. x", "1.\tx", "1234567890. x", "0. x"] {
            assert!(opens_ordered_list_marker(&chars(text)), "{text:?} is a marker");
        }
        for text in ["2.x", "2.", "2)", "2 x", "x. y", "", " 2. x", "2.5 x", "-2. x"] {
            assert!(!opens_ordered_list_marker(&chars(text)), "{text:?} is not a marker");
        }
    }

    #[test]
    fn inline_math_directly_after_display_math_stays_atomic() {
        // The inline-math regex's lookbehind `(?<!\$)` is slice-start-sensitive:
        // a search anchored at the cursor accepts a `$` whose real predecessor
        // is a `$` (the lookbehind sees nothing before the slice), while a
        // cached search anchored earlier sees the `$` and rejects it. After
        // display math consumes `$$a$$`, the cursor sits directly after a `$`;
        // the match cache must re-search there or `$bb cc dd$` degrades to
        // plain text and gets wrapped apart, breaking math rendering.
        let options = ReflowOptions {
            line_length: 8,
            ..Default::default()
        };
        let lines = reflow_line("$$a$$$bb cc dd$ x", &options);
        assert_eq!(lines, vec!["$$a$$$bb cc dd$".to_string(), "x".to_string()]);
    }

    #[test]
    fn test_code_span_parsing() {
        // 1. Single backtick
        let elements = parse_markdown_elements_inner("`code`", false, false, None);
        assert_eq!(elements.len(), 1);
        assert!(matches!(&elements[0], Element::Code { content, marker } if content == "code" && marker == "`"));

        // 2. Double backtick
        let elements = parse_markdown_elements_inner("``code``", false, false, None);
        assert_eq!(elements.len(), 1);
        assert!(matches!(&elements[0], Element::Code { content, marker } if content == "code" && marker == "``"));

        // 3. Double backtick with single backtick inside
        let elements = parse_markdown_elements_inner("``code`inside``", false, false, None);
        assert_eq!(elements.len(), 1);
        assert!(
            matches!(&elements[0], Element::Code { content, marker } if content == "code`inside" && marker == "``")
        );

        // 4. Spaces inside
        let elements = parse_markdown_elements_inner("`` code ``", false, false, None);
        assert_eq!(elements.len(), 1);
        assert!(matches!(&elements[0], Element::Code { content, marker } if content == " code " && marker == "``"));

        // 5. Unclosed backtick (should be parsed as Text)
        let elements = parse_markdown_elements_inner("`unclosed", false, false, None);
        assert_eq!(elements.len(), 1);
        assert!(matches!(&elements[0], Element::Text(s) if s == "`unclosed"));

        // 6. Unclosed backtick followed by a link (the link should be parsed as Link, not Text)
        let elements = parse_markdown_elements_inner("`unclosed [link](url)", false, false, None);
        // We expect: Text("`unclosed "), Link("[link](url)")
        assert_eq!(elements.len(), 2);
        assert!(matches!(&elements[0], Element::Text(s) if s == "`unclosed "));
        assert!(matches!(&elements[1], Element::Link(s) if s == "[link](url)"));
    }

    #[test]
    fn test_reflow_performance_long_input() {
        // Generate a string with many distinct unclosed backtick runs to test worst-case performance.
        // E.g., "` `` ` `` ` ...`"
        let mut text = String::new();
        for i in 1..400 {
            let backticks = "`".repeat(i);
            text.push_str(&backticks);
            text.push(' ');
        }

        let start = std::time::Instant::now();
        let elements = parse_markdown_elements_inner(&text, false, false, None);
        let duration = start.elapsed();

        // Ensure it completes in under 100ms.
        assert!(duration.as_millis() < 100, "Parsing took too long: {duration:?}");
        assert!(!elements.is_empty());
    }

    #[test]
    fn test_reflow_performance_display_math_heavy() {
        // Every consumed `$$a$$` leaves the cursor directly after a `$`. The
        // inline-math slice-start probe must run in place at the cursor; a
        // suffix rescan there makes this input quadratic (~9s in a debug
        // build for these 4000 spans).
        let text = "$$a$$".repeat(4000);

        let start = std::time::Instant::now();
        let elements = parse_markdown_elements_inner(&text, false, false, None);
        let duration = start.elapsed();

        assert!(duration.as_millis() < 100, "Parsing took too long: {duration:?}");
        assert_eq!(elements.len(), 4000);
    }

    #[test]
    fn inline_math_len_at_start_matches_regex_at_slice_start() {
        // Exhaustive parity with INLINE_MATH_REGEX over short `$`-soup
        // strings: the helper must equal "regex match starting at position 0"
        // exactly, since the regex's leading lookbehind is vacuous at a slice
        // start. Any drift silently changes which math spans stay atomic.
        let alphabet = ['$', 'a', ' '];
        let mut inputs: Vec<String> = vec![String::new()];
        let mut frontier: Vec<String> = vec![String::new()];
        for _ in 0..6 {
            let mut longer = Vec::new();
            for prefix in &frontier {
                for ch in alphabet {
                    let mut s = prefix.clone();
                    s.push(ch);
                    longer.push(s);
                }
            }
            inputs.extend(longer.iter().cloned());
            frontier = longer;
        }
        // Multi-byte content must count bytes, not characters.
        inputs.push("$αβ$x".to_string());
        inputs.push("$α$$".to_string());

        for s in &inputs {
            let expected = INLINE_MATH_REGEX
                .find(s)
                .ok()
                .flatten()
                .filter(|m| m.start() == 0)
                .map(|m| m.end());
            assert_eq!(inline_math_len_at_start(s), expected, "input: {s:?}");
        }
    }

    #[test]
    fn inline_math_probe_after_dollar_matches_uncached_parse() {
        // Expected element lists verified against the uncached parser (the
        // parent of the match-cache commit): when a consumed span leaves the
        // cursor directly after a `$`, the at-cursor probe must reproduce
        // exactly what rescanning the suffix used to find - both the hits
        // (the lookbehind is vacuous at the cursor) and the misses.
        let cases = [
            ("$$a$$$b c$ x", r#"[DisplayMath("a"), InlineMath("b c"), Text(" x")]"#),
            (
                "$$a$$$b$ $$a$$$b$",
                r#"[DisplayMath("a"), InlineMath("b"), Text(" "), DisplayMath("a"), InlineMath("b")]"#,
            ),
            // Probe hit whose content is only whitespace.
            (
                "$$a$$$ x $y z$",
                r#"[DisplayMath("a"), InlineMath(" x "), Text("y z$")]"#,
            ),
            // Probe miss: `$$` after the cursor is not inline math.
            ("$$a$$$$ x", r#"[DisplayMath("a"), Text("$$ x")]"#),
            ("$$a$$$$b$$ x", r#"[DisplayMath("a"), DisplayMath("b"), Text(" x")]"#),
            // Probe miss: the trailing lookahead rejects `$c$$`.
            (
                "$a$$b$$c$$d$ tail",
                r#"[Text("$a"), DisplayMath("b"), Text("c$$d$ tail")]"#,
            ),
        ];
        for (input, expected) in cases {
            let elements = parse_markdown_elements_inner(input, false, false, None);
            assert_eq!(format!("{elements:?}"), expected, "input: {input:?}");
        }
    }

    #[test]
    fn test_atomic_spans() {
        // --- Emphasis Spans ---
        let text_emphasis = "hello **word1 word2**";

        let options_disabled = ReflowOptions {
            line_length: 18,
            atomic_spans: true,
            ..Default::default()
        };
        let lines_disabled = reflow_line(text_emphasis, &options_disabled);
        assert_eq!(lines_disabled, vec!["hello", "**word1 word2**"]);

        let options_enabled = ReflowOptions {
            line_length: 18,
            atomic_spans: false,
            ..Default::default()
        };
        let lines_enabled = reflow_line(text_emphasis, &options_enabled);
        assert_eq!(lines_enabled, vec!["hello **word1", "word2**"]);

        // --- Code Spans ---
        let text_code = "hello `word1 word2`";

        let lines_code_disabled = reflow_line(text_code, &options_disabled);
        assert_eq!(lines_code_disabled, vec!["hello", "`word1 word2`"]);

        let lines_code_enabled = reflow_line(text_code, &options_enabled);
        assert_eq!(lines_code_enabled, vec!["hello `word1", "word2`"]);

        // Test multiple backticks with space padding
        let text_code_padding = "hello `` `word1` `word2` ``";
        let lines_padding_enabled = reflow_line(text_code_padding, &options_enabled);
        assert_eq!(lines_padding_enabled, vec![r#"hello `` `word1`"#, r#"`word2` ``"#]);

        // Test atomic span wrapping with attached punctuation (maintainer feedback)
        let text_attached = "**one two**,"; // length 12, bold span is 11

        // With limit 11, the bold span (11) fits, so it should NOT be split even though the total (12) exceeds 11.
        let options_11 = ReflowOptions {
            line_length: 11,
            atomic_spans: true,
            ..Default::default()
        };
        assert_eq!(reflow_line(text_attached, &options_11), vec!["**one two**,"]);

        // With limit 10, the bold span (11) exceeds 10, so it is allowed to be split.
        let options_10 = ReflowOptions {
            line_length: 10,
            atomic_spans: true,
            ..Default::default()
        };
        assert_eq!(reflow_line(text_attached, &options_10), vec!["**one", "two**,"]);
    }

    #[test]
    fn test_emphasis_containing_markers_is_not_split() {
        let options = ReflowOptions {
            line_length: 5,
            atomic_spans: false,
            ..Default::default()
        };
        // Emphasis containing internal markers (e.g. escaped asterisks) should not be split to avoid formatting corruption
        let lines = reflow_line(r#"*foo \*bar*"#, &options);
        assert_eq!(lines, vec![r#"*foo \*bar*"#.to_string()]);
    }

    /// The parsed shape of a markdown fragment, normalized the way wrapping is
    /// allowed to change it and no further: block/inline structure and
    /// code-span contents are compared exactly, while prose whitespace is
    /// collapsed, because a wrap only ever swaps a space for a newline.
    fn semantic_shape(markdown: &str) -> String {
        let mut options = Options::empty();
        options.insert(Options::ENABLE_STRIKETHROUGH);
        let mut out = String::new();
        let push_prose = |out: &mut String, text: &str| {
            for c in text.chars() {
                if c.is_whitespace() {
                    if !out.ends_with(char::is_whitespace) {
                        out.push(' ');
                    }
                } else {
                    out.push(c);
                }
            }
        };
        for event in Parser::new_ext(markdown, options) {
            match event {
                Event::Text(text) => push_prose(&mut out, &text),
                Event::SoftBreak | Event::HardBreak => push_prose(&mut out, " "),
                // Interior whitespace in a code span is literal: compare verbatim.
                Event::Code(code) => out.push_str(&format!("<code>{code}</code>")),
                Event::Start(tag) => out.push_str(&format!("<{tag:?}>")),
                Event::End(tag) => out.push_str(&format!("</{tag:?}>")),
                other => out.push_str(&format!("{other:?}")),
            }
        }
        out.trim().to_string()
    }

    #[test]
    fn test_wrapping_a_span_never_changes_what_it_parses_to() {
        // Breaking a span is only safe if the document still parses the same.
        // Cover both settings and several budgets so the break lands in a
        // different place in each run.
        let corpus = [
            "_This is a very, very, very, very, very long line with some `code` inside._",
            "_alpha beta gamma delta epsilon `a  b` zeta eta theta iota kappa lambda_",
            "**strong text with `code` and more words than fit on one single line**",
            "~~struck text with `code` and more words than fit on one single line~~",
            "_emphasis with **nested strong that is quite long** and trailing words_",
            // Doubly nested spans: the whole content of the outer span is one
            // nested span, so there is no prose outside it to break at.
            "***A doubly nested bold italic span with more words than fit on a line***",
            "___Another doubly nested span with more words than fit on a single line___",
            "**_mixed strong then emphasis with more words than fit on a single line_**",
            "*__mixed emphasis then strong with more words than fit on a single line__*",
            "**~~strong strikethrough with more words than fit on a single line here~~**",
            // A marker that belongs to no well-formed span. Breaking at these
            // spaces would start a line with `* `, making it a list item.
            "**a * b with a stray marker and plenty more words to pass the budget**",
            "_foo `a` bar `b` baz qux quux corge grault garply waldo fred plugh xyzzy_",
            "text before _a long emphasis with `code` inside of it here_ and after",
            "(_a parenthesized long emphasis with `code` inside of it right here_)",
            r#"*foo \*bar baz qux quux corge grault garply waldo fred plugh xyzzy*"#,
            "_tab\tseparated `a\tb` words spread out over quite a long emphasis span_",
            // A link nested in the span: its destination and title are not prose
            // and cannot absorb a line break.
            "_This has [text](<a b c d e f g h i j k>) and trailing words to wrap._",
            "_This has [`code`](<a b c d e f g h i j k>) and trailing words to wrap._",
            r#"_See [x](https://example.com "a long link title here") and `code` too._"#,
            "_A [link with a long label](https://example.com/path) and `code` here._",
            "_An image ![alt text here](<a b c d e f g h>) plus `code` and more text_",
        ];
        for text in corpus {
            let expected = semantic_shape(text);
            for line_length in [20, 30, 40, 80] {
                for atomic_spans in [true, false] {
                    let options = ReflowOptions {
                        line_length,
                        atomic_spans,
                        ..Default::default()
                    };
                    let wrapped = reflow_line(text, &options).join("\n");
                    assert_eq!(
                        semantic_shape(&wrapped),
                        expected,
                        "reflow changed the parse of {text:?} at line_length={line_length} \
                         atomic_spans={atomic_spans}\n  wrapped: {wrapped:?}"
                    );
                }
            }
        }
    }

    #[test]
    fn test_wrapping_a_span_keeps_whole_the_constructs_pulldown_cannot_see() {
        // Wiki links, Hugo shortcodes and math are atomic elements at the top
        // level but are invisible to the CommonMark parser, so `semantic_shape`
        // cannot catch a break inside one. Assert directly that they survive.
        let cases = [
            (
                "_alpha beta gamma [[a wiki link]] delta epsilon zeta eta_",
                "[[a wiki link]]",
            ),
            (
                "_alpha beta gamma {{< foo bar >}} delta epsilon zeta eta_",
                "{{< foo bar >}}",
            ),
            ("_alpha beta gamma $a + b$ delta epsilon zeta eta theta_", "$a + b$"),
            ("_alpha beta gamma $$a + b$$ delta epsilon zeta eta theta_", "$$a + b$$"),
        ];
        for (text, construct) in cases {
            for line_length in [12, 20, 30] {
                for atomic_spans in [true, false] {
                    let options = ReflowOptions {
                        line_length,
                        atomic_spans,
                        ..Default::default()
                    };
                    let wrapped = reflow_line(text, &options).join("\n");
                    assert!(
                        wrapped.contains(construct),
                        "{construct} was broken at line_length={line_length} \
                         atomic_spans={atomic_spans}: {wrapped:?}"
                    );
                }
            }
        }
    }

    #[test]
    fn test_overlong_emphasis_with_nested_code_span_wraps() {
        // An emphasis span longer than the whole line budget must still wrap,
        // even when it contains a nested code span: keeping it atomic would
        // leave a line that can never fit.
        let options = ReflowOptions {
            line_length: 80,
            atomic_spans: true,
            ..Default::default()
        };
        let text = "_This is a very, very, very, very, very, very, very long line that exceeds 80 characters with some `code` inside._";
        let lines = reflow_line(text, &options);
        assert_eq!(
            lines,
            vec![
                "_This is a very, very, very, very, very, very, very long line that exceeds 80",
                "characters with some `code` inside._",
            ]
        );
    }

    #[test]
    fn test_overlong_emphasis_with_nested_strong_wraps() {
        // Same for a nested strong span. The nested span itself stays whole.
        let options = ReflowOptions {
            line_length: 80,
            atomic_spans: true,
            ..Default::default()
        };
        let text = "_This is a very, very, very, very, very, very, very long line that exceeds 80 characters with some **bold** inside._";
        let lines = reflow_line(text, &options);
        assert_eq!(
            lines,
            vec![
                "_This is a very, very, very, very, very, very, very long line that exceeds 80",
                "characters with some **bold** inside._",
            ]
        );
    }

    #[test]
    fn test_overlong_doubly_nested_span_wraps() {
        // The whole content of the outer span is a single nested emphasis span.
        // Holding a nested span whole regardless of length left no break point
        // anywhere inside, so the line could never be wrapped and MD013 reported
        // a violation its own fixer refused to touch.
        let options = ReflowOptions {
            line_length: 80,
            atomic_spans: true,
            ..Default::default()
        };
        let body = "This is a very, very, very, very, very, very, very, very, very, very long line that is emphasised.";
        for (open, close) in [
            ("***", "***"),
            ("___", "___"),
            ("**_", "_**"),
            ("*__", "__*"),
            ("**~~", "~~**"),
        ] {
            let text = format!("{open}{body}{close}");
            assert!(text.len() > options.line_length, "case must start over budget");
            let lines = reflow_line(&text, &options);
            assert!(
                lines.len() > 1,
                "{open}...{close} should wrap but stayed on one line: {lines:?}"
            );
            assert!(
                lines.iter().all(|line| line.len() <= options.line_length),
                "{open}...{close} left a line over the budget: {lines:?}"
            );
            assert_eq!(
                lines.join(" "),
                text,
                "{open}...{close} wrapping must only replace a space with a newline"
            );
        }
    }

    #[test]
    fn test_overlong_span_with_stray_marker_stays_whole() {
        // A `*` that belongs to no well-formed span means the content is not
        // fully modelled. Breaking at these spaces would put `* ` at the start
        // of a line, turning literal text into a list item.
        let options = ReflowOptions {
            line_length: 40,
            atomic_spans: true,
            ..Default::default()
        };
        let text = "**alpha * beta gamma delta epsilon zeta eta theta iota kappa**";
        let lines = reflow_line(text, &options);
        assert_eq!(lines, vec![text], "stray marker must keep the span whole");
    }

    #[test]
    fn test_overlong_span_never_breaks_inside_a_nested_reference_link() {
        // A reference-style link only looks like a link once the document's
        // definitions are in scope, so the span's own parse sees plain text and
        // used to break inside the label. The top level holds these atomic, and
        // an inner span has to agree or `fmt` splits a link in one context and
        // not the other.
        let options = ReflowOptions {
            line_length: 30,
            atomic_spans: true,
            defined_references: Some(HashSet::from([
                "ref".to_string(),
                // A bare `[text]` is a link only when its own label is defined.
                "one two three four five six seven".to_string(),
            ])),
            ..Default::default()
        };
        for (text, link) in [
            (
                "_**alpha [one two three four five six seven][ref] beta gamma delta**_",
                "[one two three four five six seven][ref]",
            ),
            (
                "**alpha [one two three four five six seven][ref] beta gamma delta**",
                "[one two three four five six seven][ref]",
            ),
            (
                "_**alpha ![one two three four five six seven][ref] beta gamma delta**_",
                "![one two three four five six seven][ref]",
            ),
            (
                "_**alpha [one two three four five six seven][] beta gamma delta**_",
                "[one two three four five six seven][]",
            ),
            (
                "_**alpha [one two three four five six seven] beta gamma delta**_",
                "[one two three four five six seven]",
            ),
        ] {
            let lines = reflow_line(text, &options);
            assert!(lines.len() > 1, "over-long span should wrap: {lines:?}");
            assert!(
                lines.iter().any(|line| line.contains(link)),
                "{link} must stay on one line: {lines:?}"
            );
            assert_eq!(lines.join(" "), text, "wrapping must only move line breaks");
        }
    }

    #[test]
    fn test_overlong_span_breaks_inside_an_undefined_shortcut_reference() {
        // A bare `[text]` is only a link when its label is defined. With the
        // definitions in scope and no match, it is literal prose and breaks like
        // any other words, exactly as the top level treats it.
        let options = ReflowOptions {
            line_length: 30,
            atomic_spans: true,
            defined_references: Some(HashSet::new()),
            ..Default::default()
        };
        let text = "_**alpha [one two three four five six seven] beta gamma delta**_";
        let lines = reflow_line(text, &options);
        assert!(lines.len() > 1, "over-long span should wrap: {lines:?}");
        assert!(
            !lines
                .iter()
                .any(|line| line.contains("[one two three four five six seven]")),
            "an undefined shortcut is prose and should break: {lines:?}"
        );
        assert_eq!(lines.join(" "), text, "wrapping must only move line breaks");
    }

    #[test]
    fn test_overlong_span_never_breaks_inside_a_nested_attr_list() {
        // A MkDocs/kramdown attr list carries structural interior whitespace, so
        // splitting it rewrites the attributes. The top level holds it whole; an
        // inner span has to agree. Only when the flavor is enabled.
        let attr = "{.highlight key=\"a b c\"}";
        let text = format!("_**alpha beta gamma delta epsilon zeta{attr} eta theta iota kappa**_");
        let options = ReflowOptions {
            line_length: 20,
            atomic_spans: true,
            attr_lists: true,
            ..Default::default()
        };
        let lines = reflow_line(&text, &options);
        assert!(lines.len() > 1, "over-long span should wrap: {lines:?}");
        assert!(
            lines.iter().any(|line| line.contains(attr)),
            "attr list must stay on one line: {lines:?}"
        );
        assert_eq!(lines.join(" "), text, "wrapping must only move line breaks");

        // With the flavor off, the same braces are literal prose and break like
        // any other words, exactly as the top level treats them.
        let plain = ReflowOptions {
            attr_lists: false,
            ..options
        };
        let lines = reflow_line(&text, &plain);
        assert!(
            !lines.iter().any(|line| line.contains(attr)),
            "without the flavor the braces are prose and should break: {lines:?}"
        );
        assert_eq!(lines.join(" "), text, "wrapping must only move line breaks");
    }

    #[test]
    fn test_overlong_emphasis_never_breaks_inside_nested_code_span() {
        // Interior whitespace in a code span is literal, so a break inside one
        // would rewrite the code. The nested span is a single unbreakable unit
        // and its interior survives byte-for-byte.
        let options = ReflowOptions {
            line_length: 30,
            atomic_spans: true,
            ..Default::default()
        };
        let text = "_alpha beta gamma delta epsilon `a  b` zeta eta theta iota kappa_";
        let lines = reflow_line(text, &options);
        assert!(lines.len() > 1, "over-long emphasis should wrap: {lines:?}");
        assert!(
            lines.iter().any(|line| line.contains("`a  b`")),
            "nested code span must stay whole with its interior spaces: {lines:?}"
        );
        for line in &lines {
            assert_eq!(
                line.matches('`').count() % 2,
                0,
                "no line may contain half a code span: {line:?}"
            );
        }
    }

    #[test]
    fn test_definition_list_marker_does_not_start_line() {
        let options = ReflowOptions {
            line_length: 20,
            ..Default::default()
        };
        // Wrap should not start a line with ": "
        let lines = reflow_line("This is a term and : definition here.", &options);
        for line in &lines {
            assert!(
                !line.trim_start().starts_with(": "),
                "Wrapped line should not start with definition marker: {line}"
            );
        }
    }

    #[test]
    fn test_div_marker_does_not_start_line() {
        let options = ReflowOptions {
            line_length: 20,
            ..Default::default()
        };
        // Wrap should not start a line with ":::"
        let lines = reflow_line("This is some text with ::: class marker.", &options);
        for line in &lines {
            assert!(
                !line.trim_start().starts_with(":::"),
                "Wrapped line should not start with div marker: {line}"
            );
        }
    }
}