markdown-syntax 0.1.2

no_std + alloc Markdown syntax parser and serializer
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
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
//! Markdown source to AST. The entry points are the free [`parse`] function
//! (maximal default dialect) and the [`SyntaxOptions::parse`] /
//! [`SyntaxOptions::parse_strict`] methods. Parsing is tolerant: problems are
//! collected as [`Diagnostic`]s rather than aborting.

use alloc::{borrow::Cow, string::String, vec, vec::Vec};

use crate::{
    ast::*,
    diagnostic::{Diagnostic, DiagnosticCode, DiagnosticSeverity},
    entities::named_character_reference,
    options::{SyntaxConfigError, SyntaxOptions},
    span::Span,
    validate::is_directive_name,
};

/// The result of a tolerant parse: the document plus any diagnostics gathered
/// along the way (empty on a clean parse).
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ParseOutput {
    /// The parsed document tree.
    pub document: Document,
    /// Diagnostics collected during parsing.
    pub diagnostics: Vec<Diagnostic>,
}

/// The error returned by [`SyntaxOptions::parse_strict`].
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ParseStrictError {
    /// The options themselves were contradictory.
    Config(SyntaxConfigError),
    /// An error-severity diagnostic was promoted to a hard failure.
    Diagnostic(Diagnostic),
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct ParsedLinkResource {
    destination: String,
    destination_kind: LinkDestinationKind,
    title: Option<String>,
    title_kind: Option<LinkTitleKind>,
}

const REFERENCE_LABEL_MAX_CHARS: usize = 999;
const WIKILINK_MAX_BYTES: usize = 999;

#[derive(Clone, Copy, Debug)]
struct Line<'a> {
    text: &'a str,
    eol: &'a str,
    start: usize,
    end: usize,
    end_with_eol: usize,
    /// True when this line reached the current container as a *lazy continuation*
    /// — a line with no container marker that nonetheless continues an open
    /// paragraph (CommonMark §5.2 laziness). Block constructs that must not be
    /// started by a lazy line (e.g. a setext underline) consult this flag.
    lazy: bool,
}

#[derive(Clone, Copy, Debug)]
struct ListMarkerInfo<'a> {
    ordered: bool,
    start: Option<u64>,
    delimiter: ListDelimiter,
    indent: usize,
    marker_len: usize,
    content_indent: usize,
    content: &'a str,
}

#[derive(Clone, Copy, Debug)]
struct DescriptionMarker<'a> {
    content_offset: usize,
    content: &'a str,
}

#[derive(Clone, Debug)]
struct DescriptionTerm {
    marker_index: usize,
    term_end: usize,
    blank_after_term: bool,
    source: String,
    source_offset: usize,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum HtmlBlockKind {
    RawTag,
    BlockTag,
    Until(&'static str),
    UntilBlank,
}

/// Parse `input` under the maximal default dialect ([`SyntaxOptions::default`]).
/// Infallible and tolerant; sugar for `SyntaxOptions::default().parse(input)`.
pub fn parse(input: &str) -> ParseOutput {
    SyntaxOptions::default().parse(input)
}

impl SyntaxOptions {
    /// Parse `input` under these options. Infallible and tolerant: a config
    /// conflict (reachable only by hand-building contradictory `Constructs`) is
    /// surfaced as an error diagnostic rather than a hard error. Call
    /// [`SyntaxOptions::validate`] first for fail-fast config checking.
    pub fn parse(&self, input: &str) -> ParseOutput {
        match parse_checked(input, self) {
            Ok(output) => output,
            Err(error) => ParseOutput {
                document: Document::default(),
                diagnostics: vec![Diagnostic::new(
                    DiagnosticSeverity::Error,
                    DiagnosticCode::StrictParse,
                    Span::new(0, input.len()),
                    error.message(),
                )],
            },
        }
    }

    /// Parse `input`, promoting a config conflict or any error-severity
    /// diagnostic to a hard [`ParseStrictError`].
    pub fn parse_strict(&self, input: &str) -> Result<ParseOutput, ParseStrictError> {
        let output = parse_checked(input, self).map_err(ParseStrictError::Config)?;
        if let Some(diagnostic) = output
            .diagnostics
            .iter()
            .find(|diagnostic| diagnostic.severity == DiagnosticSeverity::Error)
        {
            return Err(ParseStrictError::Diagnostic(diagnostic.clone()));
        }
        Ok(output)
    }
}

fn parse_checked(input: &str, options: &SyntaxOptions) -> Result<ParseOutput, SyntaxConfigError> {
    options.validate()?;
    let mut diagnostics = Vec::new();
    let definitions = collect_definitions(input, options);
    let children = parse_blocks(input, 0, true, options, &definitions, &mut diagnostics);

    Ok(ParseOutput {
        document: Document {
            meta: NodeMeta::new(Some(Span::new(0, input.len()))),
            children,
        },
        diagnostics,
    })
}

fn parse_blocks(
    input: &str,
    base_offset: usize,
    allow_frontmatter: bool,
    options: &SyntaxOptions,
    definitions: &[String],
    diagnostics: &mut Vec<Diagnostic>,
) -> Vec<Block> {
    let lines = collect_lines(input, base_offset);
    parse_blocks_from_lines(&lines, allow_frontmatter, options, definitions, diagnostics)
}

fn parse_blocks_from_lines(
    lines: &[Line<'_>],
    allow_frontmatter: bool,
    options: &SyntaxOptions,
    definitions: &[String],
    diagnostics: &mut Vec<Diagnostic>,
) -> Vec<Block> {
    let mut blocks = Vec::new();
    let mut index = 0;

    while index < lines.len() {
        let line = lines[index];
        if line.text.trim().is_empty() {
            index += 1;
            continue;
        }
        let after_definition_unbroken = index > 0
            && !lines[index - 1].text.trim().is_empty()
            && matches!(blocks.last(), Some(Block::Definition(_)));

        if allow_frontmatter && index == 0 {
            if let Some((block, next)) = parse_frontmatter(lines, index, options) {
                blocks.push(block);
                index = next;
                continue;
            }
        }

        if let Some((block, next)) =
            parse_container_directive(lines, index, options, definitions, diagnostics)
        {
            blocks.push(block);
            index = next;
            continue;
        }

        if let Some((block, next)) = parse_math_block(lines, index, options) {
            blocks.push(block);
            index = next;
            continue;
        }

        if let Some((block, next)) = parse_fenced_code(lines, index, options) {
            blocks.push(block);
            index = next;
            continue;
        }

        if let Some((block, next)) =
            parse_block_quote(lines, index, options, definitions, diagnostics)
        {
            blocks.push(block);
            index = next;
            continue;
        }

        if let Some(block) = parse_atx_heading(line, options, definitions) {
            blocks.push(block);
            index += 1;
            continue;
        }

        if let Some(block) = parse_thematic_break(line) {
            blocks.push(block);
            index += 1;
            continue;
        }

        if let Some((block, next)) = parse_list(lines, index, options, definitions, diagnostics) {
            blocks.push(block);
            index = next;
            continue;
        }

        if let Some((block, next)) =
            parse_footnote_definition(lines, index, options, definitions, diagnostics)
        {
            blocks.push(block);
            index = next;
            continue;
        }

        if let Some((block, next)) =
            parse_definition(lines, index, options, after_definition_unbroken)
        {
            blocks.push(block);
            index = next;
            continue;
        }

        if let Some(block) = parse_leaf_directive(line, options, definitions, diagnostics) {
            blocks.push(block);
            index += 1;
            continue;
        }

        if let Some((block, next)) = parse_html_block(lines, index, options) {
            blocks.push(block);
            index = next;
            continue;
        }

        if let Some((block, next)) = parse_mdx_flow(lines, index, options, diagnostics) {
            blocks.push(block);
            index = next;
            continue;
        }

        if !after_definition_unbroken {
            if let Some((block, next)) = parse_indented_code(lines, index, options) {
                blocks.push(block);
                index = next;
                continue;
            }
        }

        if let Some((block, next)) = parse_table(lines, index, options, definitions, diagnostics) {
            blocks.push(block);
            index = next;
            continue;
        }

        if let Some((block, next)) = parse_setext_heading(lines, index, options, definitions) {
            blocks.push(block);
            index = next;
            continue;
        }

        if let Some((block, next)) =
            parse_description_list(lines, index, options, definitions, diagnostics)
        {
            blocks.push(block);
            index = next;
            continue;
        }

        let (block, next) = parse_paragraph(lines, index, options, definitions, diagnostics);
        blocks.push(block);
        index = next;
    }

    blocks
}

fn collect_lines(input: &str, base_offset: usize) -> Vec<Line<'_>> {
    let bytes = input.as_bytes();
    let mut lines = Vec::new();
    let mut start = 0;
    let mut index = 0;

    while index < bytes.len() {
        match bytes[index] {
            b'\n' => {
                let end = index;
                lines.push(Line {
                    text: &input[start..end],
                    eol: &input[index..index + 1],
                    start: base_offset + start,
                    end: base_offset + end,
                    end_with_eol: base_offset + index + 1,
                    lazy: false,
                });
                index += 1;
                start = index;
            }
            b'\r' => {
                let end = index;
                let eol_end = if index + 1 < bytes.len() && bytes[index + 1] == b'\n' {
                    index + 2
                } else {
                    index + 1
                };
                lines.push(Line {
                    text: &input[start..end],
                    eol: &input[index..eol_end],
                    start: base_offset + start,
                    end: base_offset + end,
                    end_with_eol: base_offset + eol_end,
                    lazy: false,
                });
                index = eol_end;
                start = index;
            }
            _ => index += 1,
        }
    }

    if start < bytes.len() || input.is_empty() {
        lines.push(Line {
            text: &input[start..],
            eol: "",
            start: base_offset + start,
            end: base_offset + bytes.len(),
            end_with_eol: base_offset + bytes.len(),
            lazy: false,
        });
    }

    lines
}

fn collect_definitions(input: &str, options: &SyntaxOptions) -> Vec<String> {
    let mut diagnostics = Vec::new();
    let blocks = parse_blocks(input, 0, true, options, &[], &mut diagnostics);
    let mut definitions = Vec::new();
    collect_definition_refs_from_blocks(&blocks, &mut definitions);
    definitions
}

fn collect_definition_refs_from_blocks(blocks: &[Block], definitions: &mut Vec<String>) {
    for block in blocks {
        match block {
            Block::Definition(definition) => {
                if definitions
                    .iter()
                    .all(|identifier| identifier != &definition.identifier)
                {
                    definitions.push(definition.identifier.clone());
                }
            }
            Block::BlockQuote(node) => {
                collect_definition_refs_from_blocks(&node.children, definitions);
            }
            Block::Alert(node) => {
                collect_definition_refs_from_blocks(&node.children, definitions);
            }
            Block::List(node) => {
                for item in &node.children {
                    collect_definition_refs_from_blocks(&item.children, definitions);
                }
            }
            Block::DescriptionList(node) => {
                for item in &node.children {
                    for details in &item.details {
                        collect_definition_refs_from_blocks(&details.children, definitions);
                    }
                }
            }
            Block::FootnoteDefinition(node) => {
                collect_definition_refs_from_blocks(&node.children, definitions);
            }
            Block::ContainerDirective(node) => {
                collect_definition_refs_from_blocks(&node.children, definitions);
            }
            _ => {}
        }
    }
}

fn parse_frontmatter(
    lines: &[Line<'_>],
    index: usize,
    options: &SyntaxOptions,
) -> Option<(Block, usize)> {
    if !options.constructs.frontmatter {
        return None;
    }
    let kind = frontmatter_fence_kind(lines[index].text)?;

    let mut value = String::new();
    let mut cursor = index + 1;
    while cursor < lines.len() {
        if frontmatter_fence_kind(lines[cursor].text) == Some(kind) {
            let span = Span::new(lines[index].start, lines[cursor].end_with_eol);
            return Some((
                Block::Frontmatter(Frontmatter {
                    meta: NodeMeta::new(Some(span)),
                    kind,
                    value,
                }),
                cursor + 1,
            ));
        }
        push_line(&mut value, lines[cursor].text);
        cursor += 1;
    }

    None
}

fn frontmatter_fence_kind(line: &str) -> Option<FrontmatterKind> {
    match line.trim_end_matches([' ', '\t']) {
        "---" => Some(FrontmatterKind::Yaml),
        "+++" => Some(FrontmatterKind::Toml),
        _ => None,
    }
}

fn parse_container_directive(
    lines: &[Line<'_>],
    index: usize,
    options: &SyntaxOptions,
    definitions: &[String],
    diagnostics: &mut Vec<Diagnostic>,
) -> Option<(Block, usize)> {
    if !options.constructs.directive_container {
        return None;
    }
    let trimmed = trim_up_to_three_spaces(lines[index].text)?;
    let Some((fence_len, opener_rest)) = directive_container_opener_prefix(trimmed) else {
        return None;
    };
    let opener_base = lines[index].start + (lines[index].text.len() - trimmed.len()) + fence_len;

    let Some((name, label_source, attributes, _consumed)) = parse_directive_opener(opener_rest)
    else {
        diagnostics.push(Diagnostic::new(
            DiagnosticSeverity::Error,
            DiagnosticCode::InvalidDirectiveName,
            Span::new(lines[index].start, lines[index].end),
            "container directive must have a valid name",
        ));
        return None;
    };
    let label_base = opener_base + name.len() + 1;

    let mut content = String::new();
    let mut cursor = index + 1;
    let mut nested_fences = Vec::new();
    while cursor < lines.len() {
        let line = lines[cursor].text;
        let trimmed = trim_up_to_three_spaces(line);
        if let Some(trimmed) = trimmed {
            if let Some(nested_len) = nested_fences.last().copied() {
                if directive_container_closing_fence(trimmed, nested_len).is_some() {
                    nested_fences.pop();
                    push_line(&mut content, line);
                    cursor += 1;
                    continue;
                }
            } else if directive_container_closing_fence(trimmed, fence_len).is_some() {
                let label = label_source
                    .map(|source| {
                        parse_inlines(source, label_base, options, definitions, diagnostics)
                    })
                    .unwrap_or_default();
                let children = parse_blocks(
                    &content,
                    lines[index + 1].start,
                    false,
                    options,
                    definitions,
                    diagnostics,
                );
                return Some((
                    Block::ContainerDirective(ContainerDirective {
                        meta: NodeMeta::new(Some(Span::new(
                            lines[index].start,
                            lines[cursor].end_with_eol,
                        ))),
                        name,
                        label,
                        attributes,
                        children,
                    }),
                    cursor + 1,
                ));
            }

            if let Some((nested_len, nested_rest)) = directive_container_opener_prefix(trimmed) {
                if parse_directive_opener(nested_rest).is_some() {
                    nested_fences.push(nested_len);
                }
            }
        }

        push_line(&mut content, line);
        cursor += 1;
    }

    diagnostics.push(Diagnostic::new(
        DiagnosticSeverity::Error,
        DiagnosticCode::UnclosedDirectiveContainer,
        Span::new(lines[index].start, lines[index].end),
        "container directive is missing a closing fence",
    ));
    Some((
        Block::ContainerDirective(ContainerDirective {
            meta: NodeMeta::new(Some(Span::new(
                lines[index].start,
                lines.last()?.end_with_eol,
            ))),
            name,
            label: label_source
                .map(|source| parse_inlines(source, label_base, options, definitions, diagnostics))
                .unwrap_or_default(),
            attributes,
            children: parse_blocks(
                &content,
                lines
                    .get(index + 1)
                    .map(|line| line.start)
                    .unwrap_or(lines[index].end),
                false,
                options,
                definitions,
                diagnostics,
            ),
        }),
        lines.len(),
    ))
}

fn directive_container_opener_prefix(input: &str) -> Option<(usize, &str)> {
    let fence_len = input
        .as_bytes()
        .iter()
        .take_while(|byte| **byte == b':')
        .count();
    if fence_len >= 3 {
        Some((fence_len, &input[fence_len..]))
    } else {
        None
    }
}

fn directive_container_closing_fence(input: &str, min_len: usize) -> Option<usize> {
    let fence_len = input
        .as_bytes()
        .iter()
        .take_while(|byte| **byte == b':')
        .count();
    if fence_len >= min_len && input[fence_len..].trim().is_empty() {
        Some(fence_len)
    } else {
        None
    }
}

fn parse_math_block(
    lines: &[Line<'_>],
    index: usize,
    options: &SyntaxOptions,
) -> Option<(Block, usize)> {
    if !options.constructs.math_block {
        return None;
    }
    // A math-flow opener is the fenced-code analogue: a `>=2` dollar run after
    // 0–3 columns of indent, optionally followed by an "info"/meta string that
    // must NOT contain another `$` (`$$ $$` is inline math, not a flow open).
    // The opening indent is stripped (up to its own width) from each content
    // line, exactly like a fenced code block.
    let opener = trim_up_to_three_spaces(lines[index].text)?;
    let fence_length = math_block_fence_length(opener)?;
    let opening_indent = leading_indent_columns(lines[index].text);

    let mut value = String::new();
    let mut content_lines = 0usize;
    let mut cursor = index + 1;
    while cursor < lines.len() {
        if let Some(close_line) = trim_up_to_three_spaces(lines[cursor].text) {
            if math_block_fence_closes(close_line, fence_length) {
                return Some((
                    Block::MathBlock(MathBlock {
                        meta: NodeMeta::new(Some(Span::new(
                            lines[index].start,
                            lines[cursor].end_with_eol,
                        ))),
                        value,
                    }),
                    cursor + 1,
                ));
            }
        }
        if content_lines > 0 {
            // The previous content line's `eol` usually separates lines. This
            // fallback only covers synthetic child input that lacks an EOL despite
            // yielding another line.
            ensure_line_separator(&mut value);
        }
        let stripped = strip_leading_indent_columns(lines[cursor].text, opening_indent);
        value.push_str(&stripped);
        value.push_str(lines[cursor].eol);
        content_lines += 1;
        cursor += 1;
    }

    // EOF closes the block (an unclosed opener runs to end of document); an
    // immediate EOF after the opener yields an empty math block.
    Some((
        Block::MathBlock(MathBlock {
            meta: NodeMeta::new(Some(Span::new(
                lines[index].start,
                lines.last()?.end_with_eol,
            ))),
            value,
        }),
        lines.len(),
    ))
}

/// Length of the leading `$` run if `input` (already indent-stripped) is a valid
/// math-flow opener: `>=2` dollars, then an info string with no further `$`.
fn math_block_fence_length(input: &str) -> Option<usize> {
    let length = input
        .as_bytes()
        .iter()
        .take_while(|byte| **byte == b'$')
        .count();
    if length < 2 || input[length..].contains('$') {
        return None;
    }
    Some(length)
}

/// A math-flow closing line (already indent-stripped) is a run of `>=length`
/// dollars and nothing else (trailing whitespace aside).
fn math_block_fence_closes(input: &str, length: usize) -> bool {
    let count = input
        .as_bytes()
        .iter()
        .take_while(|byte| **byte == b'$')
        .count();
    count >= length && input[count..].trim().is_empty()
}

fn parse_fenced_code(
    lines: &[Line<'_>],
    index: usize,
    options: &SyntaxOptions,
) -> Option<(Block, usize)> {
    let line = fence_line(lines[index].text, options)?;
    let (marker, length) = fence_start(line)?;
    // CommonMark: up to N columns of indentation (N = the opening fence's
    // indent, 0–3) are removed from each content line.
    let opening_indent = leading_indent_columns(lines[index].text);
    let info = line[length..].trim();
    if marker == FenceMarker::Backtick && info.contains('`') {
        return None;
    }
    let info = if info.is_empty() {
        None
    } else {
        Some(unescape_string(info))
    };

    let mut value = String::new();
    // Join content lines with `\n` while preserving a leading blank line: a
    // fenced block can open with a blank content line, and `push_line`'s
    // empty-output proxy cannot tell zero lines from one empty line, so it would
    // drop that leading blank. Track the count explicitly (as parse_math_block).
    let mut content_lines = 0usize;
    let mut cursor = index + 1;
    while cursor < lines.len() {
        if let Some(close_line) = fence_line(lines[cursor].text, options) {
            if fence_close(close_line, marker, length) {
                return Some((
                    Block::CodeBlock(CodeBlock {
                        meta: NodeMeta::new(Some(Span::new(
                            lines[index].start,
                            lines[cursor].end_with_eol,
                        ))),
                        kind: CodeBlockKind::Fenced { marker, length },
                        info,
                        value,
                    }),
                    cursor + 1,
                ));
            }
        }
        if content_lines > 0 {
            // The previous content line's `eol` usually separates lines. This
            // fallback only covers synthetic child input that lacks an EOL despite
            // yielding another line.
            ensure_line_separator(&mut value);
        }
        let stripped = strip_leading_indent_columns(lines[cursor].text, opening_indent);
        value.push_str(&stripped);
        value.push_str(lines[cursor].eol);
        content_lines += 1;
        cursor += 1;
    }
    Some((
        Block::CodeBlock(CodeBlock {
            meta: NodeMeta::new(Some(Span::new(
                lines[index].start,
                lines.last()?.end_with_eol,
            ))),
            kind: CodeBlockKind::Fenced { marker, length },
            info,
            value,
        }),
        lines.len(),
    ))
}

fn fence_line<'a>(line: &'a str, options: &SyntaxOptions) -> Option<&'a str> {
    if options.constructs.indented_code {
        trim_up_to_three_spaces(line)
    } else {
        Some(trim_ascii_start(line))
    }
}

fn container_closed_after_unclosed_fence(
    lines: &[Line<'_>],
    cursor: usize,
    last_content_index: usize,
    content: &str,
    options: &SyntaxOptions,
) -> bool {
    !lines[last_content_index].eol.is_empty()
        && (cursor >= lines.len() || lines[cursor].text.trim().is_empty())
        && content_has_unclosed_fenced_code(content, options)
}

fn content_has_unclosed_fenced_code(content: &str, options: &SyntaxOptions) -> bool {
    let lines = collect_lines(content, 0);
    let mut open_fence = None;
    for line in lines {
        let Some(trimmed) = fence_line(line.text, options) else {
            continue;
        };
        if let Some((marker, length, has_nonblank_content)) = open_fence {
            if fence_close(trimmed, marker, length) {
                open_fence = None;
            } else {
                open_fence = Some((
                    marker,
                    length,
                    has_nonblank_content || !trimmed.trim().is_empty(),
                ));
            }
            continue;
        }
        let Some((marker, length)) = fence_start(trimmed) else {
            continue;
        };
        let info = trimmed[length..].trim();
        if marker != FenceMarker::Backtick || !info.contains('`') {
            open_fence = Some((marker, length, false));
        }
    }
    open_fence.is_some_and(|(_, _, has_nonblank_content)| !has_nonblank_content)
}

/// Recursively determines whether the innermost block reachable through this
/// (already marker-stripped) block-quote content line is an OPEN paragraph —
/// the only block kind that a following lazy continuation line may extend.
///
/// Nested quote markers are stripped one level at a time so that, e.g.,
/// `> > a` reports that the deepest content `a` is an open paragraph (this is
/// what lets a lazy line continue a paragraph buried inside several quotes).
/// Indented code, blank lines, HTML blocks, and every other block start are
/// reported as NOT-an-open-paragraph.
fn block_quote_content_paragraph_open(content: &str, options: &SyntaxOptions) -> bool {
    let Some(trimmed) = trim_up_to_three_spaces(content) else {
        // >= 4 columns of indentation: indented code, never a paragraph.
        return false;
    };
    if trimmed.is_empty() {
        return false;
    }
    if let Some(rest) = trimmed.strip_prefix('>') {
        let rest = rest.strip_prefix(' ').unwrap_or(rest);
        return block_quote_content_paragraph_open(rest, options);
    }
    if let Some(marker) = list_marker_info(trimmed) {
        let first_content = list_marker_first_content(trimmed, marker);
        return block_quote_content_paragraph_open(&first_content, options);
    }
    !lazy_line_starts_block(trimmed, options)
}

/// Whether a line starts a block for the purpose of LAZY-continuation
/// suppression. Identical to [`likely_block_start`] except that *every* HTML
/// block start — including the type-7 "complete tag" form that cannot interrupt
/// a paragraph with a marker present — blocks lazy continuation. A bare `<a>`
/// after `> a` must close the quote, not be absorbed as paragraph text.
fn lazy_line_starts_block(input: &str, options: &SyntaxOptions) -> bool {
    likely_block_start(input, options)
        || (options.constructs.html_block && line_starts_html_block(input))
        // A lazy line that almost opens a fenced code block — any fence-char
        // run after up to three spaces of indent — ends the paragraph instead
        // of continuing it (GH-19): `> x\n``\n` closes the quote rather than
        // joining `` ` `` onto the paragraph.
        || trim_up_to_three_spaces(input).is_some_and(|t| t.starts_with('`') || t.starts_with('~'))
}

fn parse_block_quote(
    lines: &[Line<'_>],
    index: usize,
    options: &SyntaxOptions,
    definitions: &[String],
    diagnostics: &mut Vec<Diagnostic>,
) -> Option<(Block, usize)> {
    if !trim_up_to_three_spaces(lines[index].text)?.starts_with('>') {
        return None;
    }

    let mut content = String::new();
    // Lazy provenance per collected content line, parallel to the `\n`-joined
    // `content`. Re-split (`collect_lines`) lines map 1:1 to these flags, so the
    // child parser can suppress lazy-only constructs (e.g. setext underlines).
    let mut lazy_flags: Vec<bool> = Vec::new();
    let mut cursor = index;
    let mut paragraph_open = false;
    let mut in_table = false;
    let mut last_content_line: Option<String> = None;
    let mut content_base_offset = None;
    while cursor < lines.len() {
        let raw = lines[cursor].text;
        let trimmed_opt = trim_up_to_three_spaces(raw);
        let marked = trimmed_opt.is_some_and(|trimmed| trimmed.starts_with('>'));
        let quote_rest_owned: String;
        if let Some(trimmed) = trimmed_opt {
            if trimmed.is_empty() {
                break;
            }
        }
        let (line, line_start) = if marked {
            let trimmed = trimmed_opt.expect("marked implies a trimmed line");
            let trimmed_start = lines[cursor].start + (raw.len() - trimmed.len());
            let mut rest_start = 1;
            let mut rest = &trimmed[rest_start..];
            if rest.starts_with(' ') {
                rest_start += 1;
                rest = &rest[1..];
            } else if rest.starts_with('\t') {
                let marker_end_column = leading_indent_columns(raw) + 1;
                match strip_leading_indent_columns_from(rest, 1, marker_end_column) {
                    Cow::Borrowed(stripped) => rest = stripped,
                    Cow::Owned(stripped) => {
                        quote_rest_owned = stripped;
                        rest = &quote_rest_owned;
                    }
                }
            }
            (rest, trimmed_start + rest_start)
        } else if in_table {
            // An open GFM table absorbs unmarked rows (lazy table body); a
            // non-row unmarked line ends the quote.
            break;
        } else if paragraph_open && !lazy_line_starts_block(raw, options) {
            // Lazy paragraph continuation: a marker-less line that continues an
            // open paragraph (possibly nested). The RAW line is used verbatim —
            // its indentation (even >= 4 columns) is paragraph text, not code.
            (raw, lines[cursor].start)
        } else {
            break;
        };

        let mut escaped_lazy = String::new();
        let line = if !marked
            && last_content_line.as_deref().is_some_and(|previous| {
                table_can_start_source(
                    previous,
                    line,
                    options.constructs.indented_code,
                    options.constructs.spoiler,
                )
            }) {
            escaped_lazy.push_str(line);
            if let Some(offset) = escaped_lazy.find('-') {
                escaped_lazy.insert(offset, '\\');
            }
            &escaped_lazy
        } else {
            line
        };

        let starts_table = last_content_line.as_deref().is_some_and(|previous| {
            table_can_start_source(
                previous,
                line,
                options.constructs.indented_code,
                options.constructs.spoiler,
            )
        });
        if marked && starts_table {
            paragraph_open = false;
            in_table = true;
        } else if marked && in_table && block_quote_table_body_row(line, options) {
            paragraph_open = false;
        } else {
            in_table = false;
            // Track the innermost open paragraph across nested quote markers so a
            // following lazy line can reach a paragraph buried in nested quotes.
            paragraph_open = block_quote_content_paragraph_open(line, options);
        }
        last_content_line = Some(line.into());
        if content_base_offset.is_none() {
            content_base_offset = Some(line_start);
        }
        push_line(&mut content, line);
        lazy_flags.push(!marked);
        cursor += 1;
    }

    let span = Span::new(lines[index].start, lines[cursor - 1].end_with_eol);
    let child_base_offset = content_base_offset.unwrap_or(lines[index].start);
    if !lines[cursor - 1].eol.is_empty() && !ends_with_line_ending(&content) {
        content.push_str(lines[cursor - 1].eol);
    }
    if container_closed_after_unclosed_fence(lines, cursor, cursor - 1, &content, options) {
        content.push('\n');
    }
    if let Some(alert) = parse_alert_from_block_quote(
        &content,
        child_base_offset,
        span,
        options,
        definitions,
        diagnostics,
    ) {
        return Some((alert, cursor));
    }

    let mut child_lines = collect_lines(&content, child_base_offset);
    for (child, &lazy) in child_lines.iter_mut().zip(lazy_flags.iter()) {
        child.lazy = lazy;
    }
    let children = parse_blocks_from_lines(&child_lines, false, options, definitions, diagnostics);
    Some((
        Block::BlockQuote(BlockQuote {
            meta: NodeMeta::new(Some(span)),
            children,
        }),
        cursor,
    ))
}

fn parse_alert_from_block_quote(
    content: &str,
    base_offset: usize,
    span: Span,
    options: &SyntaxOptions,
    definitions: &[String],
    diagnostics: &mut Vec<Diagnostic>,
) -> Option<Block> {
    if !options.constructs.gfm_alert {
        return None;
    }
    let (first_line, rest) = content.split_once('\n').unwrap_or((content, ""));
    let (kind, title) = parse_alert_marker(first_line)?;
    let rest_base_offset = base_offset + first_line.len() + usize::from(!rest.is_empty());
    let children = if rest.is_empty() {
        Vec::new()
    } else {
        parse_blocks(
            rest,
            rest_base_offset,
            false,
            options,
            definitions,
            diagnostics,
        )
    };
    Some(Block::Alert(Alert {
        meta: NodeMeta::new(Some(span)),
        kind,
        title,
        children,
    }))
}

fn parse_alert_marker(line: &str) -> Option<(AlertKind, Option<String>)> {
    let close = line.find(']')?;
    let marker = line.get(0..close + 1)?;
    if !marker.starts_with("[!") {
        return None;
    }
    let kind = match &marker[2..close].to_ascii_lowercase()[..] {
        "note" => AlertKind::Note,
        "tip" => AlertKind::Tip,
        "important" => AlertKind::Important,
        "warning" => AlertKind::Warning,
        "caution" => AlertKind::Caution,
        _ => return None,
    };
    let title = line[close + 1..].trim();
    Some((
        kind,
        if title.is_empty() {
            None
        } else {
            Some(title.into())
        },
    ))
}

fn block_quote_table_body_row(line: &str, options: &SyntaxOptions) -> bool {
    table_indent_line(line, options.constructs.indented_code).is_some_and(|row| {
        !row.trim().is_empty() && contains_unescaped_pipe(row, options.constructs.spoiler)
    })
}

fn parse_list(
    lines: &[Line<'_>],
    index: usize,
    options: &SyntaxOptions,
    definitions: &[String],
    diagnostics: &mut Vec<Diagnostic>,
) -> Option<(Block, usize)> {
    let first_marker = list_marker_info(lines[index].text)?;
    let mut items = Vec::new();
    let mut cursor = index;
    let mut tight = true;

    while cursor < lines.len() {
        // A thematic break (`* * *`, `---`, …) outranks a list marker at the same
        // position: it ends the list rather than opening a nested item. Test it
        // before accepting the line as a marker (precedence belongs at the call
        // site, not inside `list_marker_info`).
        if parse_thematic_break(lines[cursor]).is_some() {
            break;
        }
        let Some(marker) = list_marker_info(lines[cursor].text) else {
            break;
        };
        if !same_list_marker(first_marker, marker) {
            break;
        }

        let item_start = cursor;
        let mut item_end = cursor;
        let mut item_tight = true;
        // Byte offsets within `content` at which an item-internal blank line
        // sits. After the item's children are parsed, a blank loosens the item
        // only when it falls in the GAP between two consecutive top-level
        // children (a direct separator); a blank absorbed inside a nested
        // container's span does not (per-list tightness).
        let mut item_blank_offsets: Vec<usize> = Vec::new();
        let mut content = String::new();
        // Lazy provenance per collected content line (parallel to the `\n`-joined
        // `content`, mapped 1:1 by the re-split `collect_lines`). A line is lazy
        // when it reached the item only as a paragraph continuation while
        // dedented below the item's content start: it is paragraph text and must
        // not begin a new block (e.g. `- d\n    - e` keeps `- e` as the lazy tail
        // of `d`'s paragraph, not a sublist — CommonMark "too few spaces").
        let mut lazy_flags: Vec<bool> = Vec::new();
        let mut open_fence = None;
        let first_content = list_marker_first_content(lines[cursor].text, marker);
        let mut last_content_line: Option<String> = Some(first_content.as_ref().into());
        let mut paragraph_open = list_item_paragraph_stays_open(None, &first_content, options);
        // CommonMark §5.2: a list item can begin with at most one blank line.
        // When the marker has no content the item starts blank, and the first
        // following blank line ends it — later indented content cannot join
        // (`-\n\n  foo` → empty item + separate paragraph).
        let mut item_started_blank = first_content.trim().is_empty();
        push_line(&mut content, &first_content);
        lazy_flags.push(false);
        update_list_item_fence(&first_content, &mut open_fence);
        cursor += 1;

        while cursor < lines.len() {
            if lines[cursor].text.trim().is_empty() {
                // Blank/whitespace lines inside an open fenced code block are
                // verbatim code content, not item-ending blanks: keep them.
                if open_fence.is_some() {
                    let stripped = strip_list_continuation(
                        lines[cursor].text,
                        marker.content_indent,
                        first_marker.indent,
                    );
                    push_line(&mut content, &stripped);
                    lazy_flags.push(false);
                    update_list_item_fence(&stripped, &mut open_fence);
                    item_end = cursor;
                    cursor += 1;
                    continue;
                }
                let next = next_nonblank_line(lines, cursor + 1);
                if item_started_blank
                    || next >= lines.len()
                    || sibling_list_marker_at_line(
                        lines[next].text,
                        first_marker,
                        marker.content_indent,
                    )
                    || leading_indent_columns(lines[next].text) < marker.content_indent
                {
                    if next < lines.len()
                        && sibling_list_marker_at_line(
                            lines[next].text,
                            first_marker,
                            marker.content_indent,
                        )
                    {
                        item_tight = false;
                    }
                    cursor = next;
                    break;
                }
                // A blank between item content is recorded; whether it actually
                // loosens THIS list is decided structurally after the item's
                // children are parsed (a blank buried in a nested sublist must
                // not loosen the outer list — CommonMark requires the item to
                // *directly* contain the blank-separated blocks). Track the blank
                // line's offset within the collected content so the structural
                // check can tell a direct-child separator from a nested one.
                item_blank_offsets.push(content.len() + usize::from(!content.is_empty()));
                paragraph_open = false;
                push_line(&mut content, "");
                lazy_flags.push(false);
                item_end = cursor;
                cursor += 1;
                continue;
            }

            item_started_blank = false;

            if sibling_list_marker_at_line(lines[cursor].text, first_marker, marker.content_indent)
            {
                break;
            }

            // A list marker of a different type/delimiter is a block boundary
            // (CommonMark §5.3: changing the marker starts a new list). It is not
            // a same-list sibling, so it would otherwise be absorbed as lazy
            // paragraph text — break the item instead so a new list can start.
            if leading_indent_columns(lines[cursor].text) < marker.content_indent
                && !same_list_marker_line(lines[cursor].text, first_marker)
                && list_marker_info(lines[cursor].text).is_some()
            {
                break;
            }

            if leading_indent_columns(lines[cursor].text) < marker.content_indent {
                if likely_block_start(lines[cursor].text, options) || !paragraph_open {
                    break;
                }
            }

            // A line dedented below the item's content start only stays in the
            // item as a lazy paragraph continuation (it reached here because a
            // paragraph was open). Mark it lazy so the re-parse keeps it as
            // paragraph text rather than letting a stripped `- e`/`> q`/`# h`
            // begin a fresh block inside the item.
            let lazy = paragraph_open
                && leading_indent_columns(lines[cursor].text) < marker.content_indent;
            let stripped = strip_list_continuation(
                lines[cursor].text,
                marker.content_indent,
                first_marker.indent,
            );
            let starts_table = last_content_line.as_deref().is_some_and(|previous| {
                table_can_start_source(
                    previous,
                    &stripped,
                    options.constructs.indented_code,
                    options.constructs.spoiler,
                )
            });
            paragraph_open = if starts_table {
                false
            } else {
                list_item_paragraph_stays_open(Some(paragraph_open), &stripped, options)
            };
            push_line(&mut content, &stripped);
            lazy_flags.push(lazy);
            update_list_item_fence(&stripped, &mut open_fence);
            last_content_line = Some(stripped.into_owned());
            item_end = cursor;
            cursor += 1;
        }

        let child_base = lines[item_start].start + marker.content_indent;
        if !lines[item_end].eol.is_empty() && !ends_with_line_ending(&content) {
            content.push_str(lines[item_end].eol);
        }
        if container_closed_after_unclosed_fence(lines, cursor, item_end, &content, options) {
            content.push('\n');
        }
        let mut child_lines = collect_lines(&content, child_base);
        for (child, &lazy) in child_lines.iter_mut().zip(lazy_flags.iter()) {
            child.lazy = lazy;
        }
        let mut children =
            parse_blocks_from_lines(&child_lines, false, options, definitions, diagnostics);
        let checked = if options.constructs.gfm_task_list_item {
            take_task_marker_from_children(&mut children)
        } else {
            None
        };

        if item_tight
            && blank_separates_top_level_blocks(&item_blank_offsets, &children, child_base)
        {
            item_tight = false;
        }
        tight = tight && item_tight;
        items.push(ListItem {
            meta: NodeMeta::new(Some(Span::new(
                lines[item_start].start,
                lines[item_end].end_with_eol,
            ))),
            checked,
            children,
        });
    }

    Some((
        Block::List(List {
            meta: NodeMeta::new(Some(Span::new(
                lines[index].start,
                lines[cursor - 1].end_with_eol,
            ))),
            ordered: first_marker.ordered,
            start: first_marker.start,
            delimiter: first_marker.delimiter,
            tight,
            children: items,
        }),
        cursor,
    ))
}

/// Whether an item-internal blank line directly separates two of the item's own
/// top-level block children — which loosens the list. A blank loosens the item
/// when some top-level child STARTS after the blank: that child was split off
/// from the preceding content by the blank. A blank with no top-level child
/// starting after it was either trailing or absorbed into a nested container
/// (e.g. a sublist), so it does not loosen the outer list — CommonMark only
/// counts blank lines between blocks the item *directly* contains, and per-list
/// tightness keeps a sublist's internal blank from propagating outward.
///
/// Blank offsets and child spans share the `child_base` content origin (both
/// were produced from the same stripped item content), so the comparison is in
/// one coordinate space.
fn blank_separates_top_level_blocks(
    blank_offsets: &[usize],
    children: &[Block],
    child_base: usize,
) -> bool {
    if blank_offsets.is_empty() || children.len() < 2 {
        return false;
    }
    let Some(&first_blank) = blank_offsets.iter().min() else {
        return false;
    };
    children.iter().any(|child| {
        block_span(child).is_some_and(|span| span.start.saturating_sub(child_base) > first_blank)
    })
}

fn block_span(block: &Block) -> Option<Span> {
    let meta = match block {
        Block::Paragraph(node) => &node.meta,
        Block::Heading(node) => &node.meta,
        Block::ThematicBreak(node) => &node.meta,
        Block::BlockQuote(node) => &node.meta,
        Block::Alert(node) => &node.meta,
        Block::List(node) => &node.meta,
        Block::DescriptionList(node) => &node.meta,
        Block::CodeBlock(node) => &node.meta,
        Block::HtmlBlock(node) => &node.meta,
        Block::Definition(node) => &node.meta,
        Block::FootnoteDefinition(node) => &node.meta,
        Block::Table(node) => &node.meta,
        Block::MathBlock(node) => &node.meta,
        Block::Frontmatter(node) => &node.meta,
        Block::MdxEsm(node) => &node.meta,
        Block::MdxExpression(node) => &node.meta,
        Block::MdxJsx(node) => &node.meta,
        Block::LeafDirective(node) => &node.meta,
        Block::ContainerDirective(node) => &node.meta,
    };
    meta.span
}

fn list_item_paragraph_stays_open(
    previous_open: Option<bool>,
    line: &str,
    options: &SyntaxOptions,
) -> bool {
    if line.trim().is_empty() {
        return false;
    }
    if previous_open == Some(false) {
        return false;
    }
    block_quote_content_paragraph_open(line, options)
}

fn parse_description_list(
    lines: &[Line<'_>],
    index: usize,
    options: &SyntaxOptions,
    definitions: &[String],
    diagnostics: &mut Vec<Diagnostic>,
) -> Option<(Block, usize)> {
    if !options.constructs.description_list || !is_description_term_line(lines[index].text, options)
    {
        return None;
    }

    let mut cursor = index;
    let mut items = Vec::new();
    let mut tight = true;
    let mut list_end = lines[index].end_with_eol;

    while cursor < lines.len() {
        if !is_description_term_line(lines[cursor].text, options) {
            break;
        }
        let Some(term) = description_term(lines, cursor, options) else {
            break;
        };
        let term_line = lines[cursor];
        let mut details = Vec::new();
        let item_start = term_line.start;
        let mut item_end = lines[term.term_end].end_with_eol;
        tight = tight && !term.blank_after_term;
        cursor = term.marker_index;

        loop {
            let Some(marker) = description_marker(lines[cursor].text) else {
                break;
            };
            let (detail, next, detail_tight) = parse_description_details(
                lines,
                cursor,
                marker,
                options,
                definitions,
                diagnostics,
            )?;
            tight = tight && detail_tight;
            item_end = detail
                .meta
                .span
                .map(|span| span.end)
                .unwrap_or(lines[cursor].end_with_eol);
            details.push(detail);
            cursor = next;

            let next_nonblank = next_nonblank_line(lines, cursor);
            if next_nonblank < lines.len()
                && description_marker(lines[next_nonblank].text).is_some()
            {
                if next_nonblank != cursor {
                    tight = false;
                }
                cursor = next_nonblank;
                continue;
            }
            break;
        }

        if details.is_empty() {
            return None;
        }
        list_end = item_end;
        items.push(DescriptionItem {
            meta: NodeMeta::new(Some(Span::new(item_start, item_end))),
            term: parse_inlines(
                &term.source,
                term.source_offset,
                options,
                definitions,
                diagnostics,
            ),
            details,
        });

        let next_item = next_nonblank_line(lines, cursor);
        if next_item >= lines.len() {
            cursor = next_item;
            break;
        }
        if description_term(lines, next_item, options).is_some() {
            if next_item != cursor {
                tight = false;
            }
            cursor = next_item;
            continue;
        }
        cursor = next_item;
        break;
    }

    (!items.is_empty()).then_some((
        Block::DescriptionList(DescriptionList {
            meta: NodeMeta::new(Some(Span::new(lines[index].start, list_end))),
            tight,
            children: items,
        }),
        cursor,
    ))
}

fn parse_description_details(
    lines: &[Line<'_>],
    index: usize,
    marker: DescriptionMarker<'_>,
    options: &SyntaxOptions,
    definitions: &[String],
    diagnostics: &mut Vec<Diagnostic>,
) -> Option<(DescriptionDetails, usize, bool)> {
    let mut content = String::new();
    push_line(&mut content, marker.content);
    let mut cursor = index + 1;
    let mut end = lines[index].end_with_eol;
    let mut tight = true;
    let mut paragraph_open = paragraph_stays_open(marker.content, options);

    while cursor < lines.len() {
        if lines[cursor].text.trim().is_empty() {
            let next = next_nonblank_line(lines, cursor + 1);
            // A blank that merely separates this definition from a following
            // `:`/`~` marker (another definition of the SAME term) is
            // content-separating, so it loosens the list. A blank that ends the
            // item — because the next non-blank line begins a new TERM, or the
            // document ends — is just an item boundary and must NOT loosen the
            // list (such blank-separated term groups stay tight).
            if next >= lines.len() || description_term(lines, next, options).is_some() {
                cursor = next;
                break;
            }
            if description_marker(lines[next].text).is_some() {
                tight = false;
                cursor = next;
                break;
            }
            if strip_indent_continuation(lines[next].text).is_none() {
                break;
            }
            push_line(&mut content, "");
            paragraph_open = false;
            tight = false;
            end = lines[cursor].end_with_eol;
            cursor += 1;
            continue;
        }

        if description_marker(lines[cursor].text).is_some()
            || description_term(lines, cursor, options).is_some()
        {
            break;
        }

        let continuation = if let Some(continuation) = strip_indent_continuation(lines[cursor].text)
        {
            continuation
        } else if paragraph_open && !likely_block_start(lines[cursor].text, options) {
            trim_ascii_start(lines[cursor].text)
        } else {
            break;
        };
        paragraph_open = paragraph_stays_open(continuation, options);
        push_line(&mut content, continuation);
        end = lines[cursor].end_with_eol;
        cursor += 1;
    }

    if content.trim().is_empty() {
        return None;
    }

    Some((
        DescriptionDetails {
            meta: NodeMeta::new(Some(Span::new(lines[index].start, end))),
            children: parse_blocks(
                &content,
                lines[index].start + marker.content_offset,
                false,
                options,
                definitions,
                diagnostics,
            ),
        },
        cursor,
        tight,
    ))
}

fn description_term(
    lines: &[Line<'_>],
    term_index: usize,
    options: &SyntaxOptions,
) -> Option<DescriptionTerm> {
    if term_index >= lines.len() || !is_description_term_line(lines[term_index].text, options) {
        return None;
    }
    let mut source = String::new();
    let mut term_end = term_index;
    let mut cursor = term_index;
    while cursor < lines.len() && is_description_term_line(lines[cursor].text, options) {
        if !source.is_empty() {
            source.push('\n');
        }
        source.push_str(trim_ascii_start(lines[cursor].text).trim_end());
        term_end = cursor;
        cursor += 1;
    }

    let mut marker_index = cursor;
    let mut blank_after_term = false;
    while marker_index < lines.len() && lines[marker_index].text.trim().is_empty() {
        blank_after_term = true;
        marker_index += 1;
    }
    (marker_index < lines.len() && description_marker(lines[marker_index].text).is_some()).then(
        || DescriptionTerm {
            marker_index,
            term_end,
            blank_after_term,
            source,
            source_offset: lines[term_index].start + leading_trim_bytes(lines[term_index].text),
        },
    )
}

fn is_description_term_line(line: &str, options: &SyntaxOptions) -> bool {
    leading_indent_columns(line) <= 3
        && !line.trim().is_empty()
        && description_marker(line).is_none()
        && !likely_block_start(line, options)
}

fn description_marker(line: &str) -> Option<DescriptionMarker<'_>> {
    let (columns, bytes) = leading_indent(line);
    if columns > 2 || !matches!(line.as_bytes().get(bytes), Some(b':' | b'~')) {
        return None;
    }
    if line
        .as_bytes()
        .get(bytes + 1)
        .is_some_and(|byte| !matches!(*byte, b' ' | b'\t'))
    {
        return None;
    }
    let mut content_offset = bytes + 1;
    while line
        .as_bytes()
        .get(content_offset)
        .is_some_and(|byte| matches!(*byte, b' ' | b'\t'))
    {
        content_offset += 1;
    }
    Some(DescriptionMarker {
        content_offset,
        content: &line[content_offset..],
    })
}

/// A paragraph inside an indent-continuation container (footnote/description
/// detail) keeps absorbing the next line as long as it is non-blank and does
/// not itself begin a new block.
fn paragraph_stays_open(line: &str, options: &SyntaxOptions) -> bool {
    !line.trim().is_empty() && !likely_block_start(line, options)
}

/// Strips one level of indent-continuation (four spaces or a tab) from a line.
fn strip_indent_continuation(input: &str) -> Option<&str> {
    input
        .strip_prefix("    ")
        .or_else(|| input.strip_prefix('\t'))
}

fn parse_atx_heading(
    line: Line<'_>,
    options: &SyntaxOptions,
    definitions: &[String],
) -> Option<Block> {
    let text = trim_up_to_three_spaces(line.text)?;
    let depth = text
        .as_bytes()
        .iter()
        .take_while(|byte| **byte == b'#')
        .count();
    if depth == 0 || depth > 6 {
        return None;
    }
    if text
        .as_bytes()
        .get(depth)
        .is_some_and(|byte| !matches!(*byte, b' ' | b'\t'))
        && text.len() != depth
    {
        return None;
    }
    let after_opening = &text[depth..];
    let content_start_in_text = depth + leading_trim_bytes(after_opening);
    let content = trim_closing_hashes(after_opening.trim_start());
    let content_start = line.start + (line.text.len() - text.len()) + content_start_in_text;
    Some(Block::Heading(Heading {
        meta: NodeMeta::new(Some(Span::new(line.start, line.end))),
        depth: depth as u8,
        kind: HeadingKind::Atx,
        children: parse_inlines(
            content,
            content_start,
            options,
            definitions,
            &mut Vec::new(),
        ),
    }))
}

fn parse_thematic_break(line: Line<'_>) -> Option<Block> {
    let text = trim_up_to_three_spaces(line.text)?.trim();
    let mut marker = None;
    let mut count = 0;
    for char in text.chars() {
        if char == ' ' || char == '\t' {
            continue;
        }
        let current = match char {
            '-' => ThematicBreakMarker::Dash,
            '*' => ThematicBreakMarker::Asterisk,
            '_' => ThematicBreakMarker::Underscore,
            _ => return None,
        };
        if marker.is_some_and(|marker| marker != current) {
            return None;
        }
        marker = Some(current);
        count += 1;
    }
    if count >= 3 {
        Some(Block::ThematicBreak(ThematicBreak {
            meta: NodeMeta::new(Some(Span::new(line.start, line.end))),
            marker: marker?,
        }))
    } else {
        None
    }
}

fn parse_definition(
    lines: &[Line<'_>],
    index: usize,
    options: &SyntaxOptions,
    allow_subsequent_indent: bool,
) -> Option<(Block, usize)> {
    let line = lines[index];
    let text = trim_definition_start(line.text, allow_subsequent_indent)?;
    if !text.starts_with('[') {
        return None;
    }

    // A reference-definition label may span several lines (CommonMark §4.7): the
    // `]:` closing the label can appear on a later line. Accumulate continuation
    // lines until the label closes, stopping at a blank line or end of input (a
    // blank line cannot occur inside a label). The first line's <=3-space indent
    // is already stripped by `trim_up_to_three_spaces`; continuation lines are
    // appended verbatim, and `normalize_label` collapses the interior newlines and
    // surrounding whitespace when the label is matched.
    let mut accumulated = String::from(text);
    let mut label_end_line = index;
    let close = loop {
        if let Some(close) = find_reference_label_end(&accumulated, 0) {
            if accumulated.as_bytes().get(close + 1) == Some(&b':') {
                break close;
            }
            // A closed label not followed by `:` is not a definition.
            return None;
        }
        let next = label_end_line + 1;
        if next >= lines.len() || lines[next].text.trim().is_empty() {
            return None;
        }
        // The unclosed label behaves like an open paragraph: a continuation line
        // that itself begins a block construct (a setext underline, or a GFM table
        // header/delimiter pair) interrupts it, so the definition fails and the
        // lines are re-parsed as blocks (CommonMark/GFM prefer setext headings,
        // thematic breaks, fenced code, and tables over a label that has not yet
        // closed — e.g. `[\na\n=\n]: b` or `[\na\n:-\n]: b`).
        if likely_block_start(lines[next].text, options)
            || setext_underline_depth(lines[next].text).is_some()
            || table_can_start(lines, next, options)
        {
            return None;
        }
        accumulated.push('\n');
        accumulated.push_str(lines[next].text);
        label_end_line = next;
    };
    let label = String::from(&accumulated[1..close]);
    if normalize_label(&label).is_empty() {
        return None;
    }
    let label = label.as_str();
    let mut source = String::from(&accumulated[close + 2..]);
    let mut cursor = label_end_line;
    let mut best_without_title = None;

    loop {
        if let Some(resource) = parse_definition_destination_title(&source) {
            if resource.title.is_some() {
                return Some((
                    Block::Definition(Definition {
                        meta: NodeMeta::new(Some(Span::new(
                            line.start,
                            lines[cursor].end_with_eol,
                        ))),
                        label: label.into(),
                        identifier: normalize_label(label),
                        destination: resource.destination,
                        destination_kind: resource.destination_kind,
                        title: resource.title,
                        title_kind: resource.title_kind,
                    }),
                    cursor + 1,
                ));
            }

            best_without_title = Some((resource, cursor + 1));
            let next = cursor + 1;
            if next >= lines.len()
                || lines[next].text.trim().is_empty()
                || !line_can_start_definition_title(lines[next].text)
            {
                break;
            }
        }

        let next = cursor + 1;
        if next >= lines.len() || lines[next].text.trim().is_empty() {
            break;
        }
        // A continuation line that itself begins a block-level construct (or a
        // setext underline) cannot be swallowed into the definition's pending,
        // not-yet-closed title: such a line interrupts the would-be paragraph, so
        // the definition fails and the lines are re-parsed as blocks (e.g.
        // `[a]: b '` then `***` is a paragraph + thematic break, not a title).
        if likely_block_start(lines[next].text, options)
            || setext_underline_depth(lines[next].text).is_some()
        {
            break;
        }
        source.push('\n');
        source.push_str(lines[next].text);
        cursor = next;
    }

    let (resource, next) = best_without_title?;
    let end = lines[next - 1].end_with_eol;
    Some((
        Block::Definition(Definition {
            meta: NodeMeta::new(Some(Span::new(line.start, end))),
            label: label.into(),
            identifier: normalize_label(label),
            destination: resource.destination,
            destination_kind: resource.destination_kind,
            title: resource.title,
            title_kind: resource.title_kind,
        }),
        next,
    ))
}

fn trim_definition_start(input: &str, allow_subsequent_indent: bool) -> Option<&str> {
    if let Some(trimmed) = trim_up_to_three_spaces(input) {
        return Some(trimmed);
    }
    if allow_subsequent_indent {
        let (columns, bytes) = leading_indent(input);
        if columns == 4 {
            return Some(&input[bytes..]);
        }
    }
    None
}

fn parse_footnote_definition(
    lines: &[Line<'_>],
    index: usize,
    options: &SyntaxOptions,
    definitions: &[String],
    diagnostics: &mut Vec<Diagnostic>,
) -> Option<(Block, usize)> {
    if !options.constructs.footnote_definition {
        return None;
    }
    let line = lines[index];
    let text = line.text.trim();
    if !text.starts_with("[^") {
        return None;
    }
    let close = find_footnote_definition_label_end(text)?;
    let label = &text[2..close];
    if !is_footnote_label(label) {
        return None;
    }
    let rest = text[close + 2..].trim();
    let mut content = String::new();
    push_line(&mut content, rest);
    let mut cursor = index + 1;
    let mut end = line.end_with_eol;
    let mut paragraph_open = paragraph_stays_open(rest, options);

    while cursor < lines.len() {
        if lines[cursor].text.trim().is_empty() {
            let next = next_nonblank_line(lines, cursor + 1);
            if next >= lines.len() || !is_footnote_continuation(lines[next].text) {
                break;
            }
            push_line(&mut content, "");
            paragraph_open = false;
            end = lines[cursor].end_with_eol;
            cursor += 1;
            continue;
        }

        let continuation = if let Some(continuation) = strip_indent_continuation(lines[cursor].text)
        {
            continuation
        } else if paragraph_open && !likely_block_start(lines[cursor].text, options) {
            trim_ascii_start(lines[cursor].text)
        } else {
            break;
        };
        paragraph_open = paragraph_stays_open(continuation, options);
        push_line(&mut content, continuation);
        end = lines[cursor].end_with_eol;
        cursor += 1;
    }

    Some((
        Block::FootnoteDefinition(FootnoteDefinition {
            meta: NodeMeta::new(Some(Span::new(line.start, end))),
            label: label.into(),
            identifier: normalize_label(label),
            children: parse_blocks(
                &content,
                line.end.saturating_sub(rest.len()),
                false,
                options,
                definitions,
                diagnostics,
            ),
        }),
        cursor,
    ))
}

fn is_footnote_continuation(input: &str) -> bool {
    strip_indent_continuation(input).is_some()
}

fn parse_leaf_directive(
    line: Line<'_>,
    options: &SyntaxOptions,
    definitions: &[String],
    diagnostics: &mut Vec<Diagnostic>,
) -> Option<Block> {
    if !options.constructs.directive_leaf {
        return None;
    }
    let trimmed = line.text.trim_start();
    if trimmed.starts_with(":::") || !trimmed.starts_with("::") {
        return None;
    }
    let opener_base = line.start + (line.text.len() - trimmed.len()) + 2;
    let Some((name, label_source, attributes, _)) = parse_directive_opener(&trimmed[2..]) else {
        diagnostics.push(Diagnostic::new(
            DiagnosticSeverity::Error,
            DiagnosticCode::InvalidDirectiveName,
            Span::new(line.start, line.end),
            "leaf directive must have a valid name",
        ));
        return None;
    };
    let label = label_source
        .map(|source| {
            parse_inlines(
                source,
                opener_base + name.len() + 1,
                options,
                definitions,
                diagnostics,
            )
        })
        .unwrap_or_default();
    Some(Block::LeafDirective(LeafDirective {
        meta: NodeMeta::new(Some(Span::new(line.start, line.end))),
        name,
        label,
        attributes,
    }))
}

fn parse_html_block(
    lines: &[Line<'_>],
    index: usize,
    options: &SyntaxOptions,
) -> Option<(Block, usize)> {
    if !options.constructs.html_block {
        return None;
    }

    let trimmed = trim_up_to_three_spaces(lines[index].text)?;
    let kind = html_block_start(trimmed)?;
    let mut value = String::new();
    let mut cursor = index;
    match kind {
        HtmlBlockKind::RawTag => {
            // CommonMark §4.6 type-1: the block ends on a line containing ANY of
            // `</script>`, `</pre>`, `</style>`, `</textarea>` (case-insensitive),
            // regardless of which opened it.
            while cursor < lines.len() {
                push_line(&mut value, lines[cursor].text);
                if ["script", "pre", "style", "textarea"]
                    .iter()
                    .any(|tag| line_contains_raw_closing_tag(lines[cursor].text, tag))
                {
                    cursor += 1;
                    break;
                }
                cursor += 1;
            }
        }
        HtmlBlockKind::BlockTag => {
            while cursor < lines.len() && !lines[cursor].text.trim().is_empty() {
                push_line(&mut value, lines[cursor].text);
                cursor += 1;
            }
        }
        HtmlBlockKind::Until(end) => {
            while cursor < lines.len() {
                push_line(&mut value, lines[cursor].text);
                if lines[cursor].text.contains(end) {
                    cursor += 1;
                    break;
                }
                cursor += 1;
            }
        }
        HtmlBlockKind::UntilBlank => {
            while cursor < lines.len() && !lines[cursor].text.trim().is_empty() {
                push_line(&mut value, lines[cursor].text);
                cursor += 1;
            }
        }
    }
    Some((
        Block::HtmlBlock(HtmlBlock {
            meta: NodeMeta::new(Some(Span::new(
                lines[index].start,
                lines[cursor - 1].end_with_eol,
            ))),
            value,
        }),
        cursor,
    ))
}

fn html_block_start(input: &str) -> Option<HtmlBlockKind> {
    let trimmed = input.trim_end();
    if !trimmed.starts_with('<') {
        return None;
    }

    if raw_html_tag_start(trimmed) {
        return Some(HtmlBlockKind::RawTag);
    }
    if trimmed.starts_with("<!--") {
        return Some(HtmlBlockKind::Until("-->"));
    }
    if trimmed.starts_with("<?") {
        return Some(HtmlBlockKind::Until("?>"));
    }
    if is_declaration_start(trimmed) {
        return Some(HtmlBlockKind::Until(">"));
    }
    if trimmed.starts_with("<![CDATA[") {
        return Some(HtmlBlockKind::Until("]]>"));
    }

    if html_block_tag_start(trimmed) {
        return Some(HtmlBlockKind::BlockTag);
    }

    let Some((end, _tag_name)) = parse_html_tag(trimmed, 0) else {
        return None;
    };
    let rest = trimmed[end..].trim();
    if rest.is_empty() {
        Some(HtmlBlockKind::UntilBlank)
    } else {
        None
    }
}

pub(crate) fn line_starts_html_block(input: &str) -> bool {
    trim_up_to_three_spaces(input)
        .and_then(html_block_start)
        .is_some()
}

fn raw_html_tag_start(input: &str) -> bool {
    for tag in ["script", "pre", "style", "textarea"] {
        if html_raw_open_tag_prefix(input, tag) {
            return true;
        }
    }
    false
}

fn html_raw_open_tag_prefix(input: &str, tag: &str) -> bool {
    let Some(rest) = input.strip_prefix('<') else {
        return false;
    };
    if rest.starts_with('/') || rest.len() < tag.len() {
        return false;
    }
    let rest_bytes = rest.as_bytes();
    let tag_bytes = tag.as_bytes();
    if !rest_bytes
        .get(..tag_bytes.len())
        .is_some_and(|name| name.eq_ignore_ascii_case(tag_bytes))
    {
        return false;
    }
    match rest_bytes.get(tag.len()) {
        None => true,
        Some(b' ' | b'\t' | b'\n' | b'\r' | b'>') => true,
        Some(b'/') => {
            rest_bytes.get(tag.len() + 1) == Some(&b'>') && rest_bytes.get(tag.len() + 2).is_none()
        }
        _ => false,
    }
}

fn line_contains_raw_closing_tag(input: &str, tag: &str) -> bool {
    let bytes = input.as_bytes();
    let tag_bytes = tag.as_bytes();
    let mut cursor = 0;

    while cursor + 2 + tag_bytes.len() <= bytes.len() {
        let tag_start = cursor + 2;
        let tag_end = tag_start + tag_bytes.len();
        if bytes.get(cursor) == Some(&b'<')
            && bytes.get(cursor + 1) == Some(&b'/')
            && bytes
                .get(tag_start..tag_end)
                .is_some_and(|name| name.eq_ignore_ascii_case(tag_bytes))
        {
            match bytes.get(tag_end) {
                Some(b'>') => return true,
                Some(byte) if byte.is_ascii_whitespace() => {
                    let mut after_space = tag_end;
                    while bytes
                        .get(after_space)
                        .is_some_and(|byte| byte.is_ascii_whitespace())
                    {
                        after_space += 1;
                    }
                    if bytes.get(after_space) == Some(&b'>') {
                        return true;
                    }
                }
                _ => {}
            }
        }
        cursor += 1;
    }

    false
}

fn html_block_tag_start(input: &str) -> bool {
    let bytes = input.as_bytes();
    if bytes.first() != Some(&b'<') {
        return false;
    }

    let mut cursor = 1;
    if bytes.get(cursor) == Some(&b'/') {
        cursor += 1;
    }

    let name_start = cursor;
    if !bytes
        .get(cursor)
        .is_some_and(|byte| byte.is_ascii_alphabetic())
    {
        return false;
    }
    cursor += 1;
    while bytes.get(cursor).is_some_and(|byte| html_name_byte(*byte)) {
        cursor += 1;
    }

    let name = &input[name_start..cursor];
    if !html_block_tag(name) {
        return false;
    }

    match bytes.get(cursor) {
        None | Some(b' ' | b'\t' | b'\n' | b'\r' | b'>') => true,
        Some(b'/') if bytes.get(cursor + 1) == Some(&b'>') => true,
        _ => false,
    }
}

fn html_block_tag(tag: &str) -> bool {
    matches!(
        tag.to_ascii_lowercase().as_str(),
        "address"
            | "article"
            | "aside"
            | "base"
            | "basefont"
            | "blockquote"
            | "body"
            | "caption"
            | "center"
            | "col"
            | "colgroup"
            | "dd"
            | "details"
            | "dialog"
            | "dir"
            | "div"
            | "dl"
            | "dt"
            | "fieldset"
            | "figcaption"
            | "figure"
            | "footer"
            | "form"
            | "frame"
            | "frameset"
            | "h1"
            | "h2"
            | "h3"
            | "h4"
            | "h5"
            | "h6"
            | "head"
            | "header"
            | "hr"
            | "html"
            | "iframe"
            | "legend"
            | "li"
            | "link"
            | "main"
            | "menu"
            | "menuitem"
            | "nav"
            | "noframes"
            | "ol"
            | "optgroup"
            | "option"
            | "p"
            | "param"
            | "search"
            | "section"
            | "summary"
            | "table"
            | "tbody"
            | "td"
            | "tfoot"
            | "th"
            | "thead"
            | "title"
            | "tr"
            | "track"
            | "ul"
    )
}

fn is_declaration_start(input: &str) -> bool {
    input
        .as_bytes()
        .get(2)
        .is_some_and(|byte| input.starts_with("<!") && byte.is_ascii_alphabetic())
}

fn parse_mdx_flow(
    lines: &[Line<'_>],
    index: usize,
    options: &SyntaxOptions,
    diagnostics: &mut Vec<Diagnostic>,
) -> Option<(Block, usize)> {
    if options.constructs.mdx_esm {
        if let Some((block, next)) = parse_mdx_esm_flow(lines, index, diagnostics) {
            return Some((block, next));
        }
    }

    let line = lines[index];
    let trimmed = line.text.trim_start();
    if options.constructs.mdx_expression_block && trimmed.starts_with('{') {
        let open_byte = line.text.len() - trimmed.len();
        if let Some((close_line, close_byte)) = find_mdx_expression_close(lines, index, open_byte) {
            return Some((
                Block::MdxExpression(MdxExpression {
                    meta: NodeMeta::new(Some(Span::new(line.start, lines[close_line].end))),
                    value: collect_mdx_expression_value(
                        lines, index, open_byte, close_line, close_byte,
                    ),
                }),
                close_line + 1,
            ));
        }
        diagnostics.push(Diagnostic::new(
            DiagnosticSeverity::Error,
            DiagnosticCode::InvalidMdx,
            Span::new(line.start + open_byte, lines.last()?.end_with_eol),
            "MDX expression block is missing a closing brace",
        ));
    }
    if options.constructs.mdx_jsx_block && trimmed.starts_with('<') {
        if let Some(close_line) = find_mdx_jsx_close(lines, index) {
            return Some((
                Block::MdxJsx(MdxJsx {
                    meta: NodeMeta::new(Some(Span::new(line.start, lines[close_line].end))),
                    value: collect_line_range(lines, index, close_line),
                }),
                close_line + 1,
            ));
        }
        let start_byte = line.text.len() - trimmed.len();
        if let Some(root) = mdx_jsx_tag_start(line.text, start_byte) {
            if !root.closing {
                if let Some((_tag_end_line, _tag_end_byte, self_closing)) =
                    find_mdx_jsx_tag_end(lines, index, start_byte)
                {
                    if !self_closing {
                        diagnostics.push(Diagnostic::new(
                            DiagnosticSeverity::Error,
                            DiagnosticCode::InvalidMdx,
                            Span::new(line.start + start_byte, lines.last()?.end_with_eol),
                            "MDX JSX block is missing a closing tag",
                        ));
                    }
                }
            }
        }
    }
    None
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
struct MdxEsmState {
    brace_depth: usize,
    bracket_depth: usize,
    paren_depth: usize,
    block_comment: bool,
    quote: Option<u8>,
    escaped: bool,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum MdxBraceState {
    Normal,
    SingleQuoted,
    DoubleQuoted,
    Template,
    LineComment,
    BlockComment,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum MdxJsxTag<'a> {
    Fragment,
    Named(&'a str),
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct MdxJsxTagStart<'a> {
    tag: MdxJsxTag<'a>,
    closing: bool,
}

fn parse_mdx_esm_flow(
    lines: &[Line<'_>],
    index: usize,
    diagnostics: &mut Vec<Diagnostic>,
) -> Option<(Block, usize)> {
    if !is_mdx_esm_start(lines[index].text) {
        return None;
    }

    let mut value = String::new();
    let mut state = MdxEsmState::default();
    let mut cursor = index;
    while cursor < lines.len() {
        let line = lines[cursor].text;
        if cursor > index && !is_mdx_esm_continuation(line, &state) {
            break;
        }
        if cursor > index {
            value.push('\n');
        }
        value.push_str(line);
        update_mdx_esm_state(line, &mut state);
        cursor += 1;
    }
    if cursor >= lines.len() && state_has_open_mdx_esm_construct(&state) {
        diagnostics.push(Diagnostic::new(
            DiagnosticSeverity::Error,
            DiagnosticCode::InvalidMdx,
            Span::new(lines[index].start, lines[cursor - 1].end_with_eol),
            "MDX ESM block is missing a closing delimiter",
        ));
    }

    Some((
        Block::MdxEsm(MdxEsm {
            meta: NodeMeta::new(Some(Span::new(lines[index].start, lines[cursor - 1].end))),
            value,
        }),
        cursor,
    ))
}

fn is_mdx_esm_start(line: &str) -> bool {
    line.starts_with("import ") || line.starts_with("export ")
}

fn is_mdx_esm_continuation(line: &str, state: &MdxEsmState) -> bool {
    if state_has_open_mdx_esm_construct(state) {
        return true;
    }
    let trimmed = line.trim_start();
    if trimmed.is_empty() {
        return false;
    }
    is_mdx_esm_start(line) || trimmed.starts_with("//") || trimmed.starts_with("/*")
}

fn state_has_open_mdx_esm_construct(state: &MdxEsmState) -> bool {
    state.brace_depth > 0
        || state.bracket_depth > 0
        || state.paren_depth > 0
        || state.block_comment
        || state.quote == Some(b'`')
}

fn update_mdx_esm_state(line: &str, state: &mut MdxEsmState) {
    let bytes = line.as_bytes();
    let mut index = 0;
    while index < bytes.len() {
        let byte = bytes[index];
        if state.block_comment {
            if byte == b'*' && bytes.get(index + 1) == Some(&b'/') {
                state.block_comment = false;
                index += 1;
            }
            index += 1;
            continue;
        }

        if let Some(delimiter) = state.quote {
            if state.escaped {
                state.escaped = false;
            } else if byte == b'\\' {
                state.escaped = true;
            } else if byte == delimiter {
                state.quote = None;
            }
            index += 1;
            continue;
        }

        match byte {
            b'\'' | b'"' | b'`' => state.quote = Some(byte),
            b'/' if bytes.get(index + 1) == Some(&b'/') => break,
            b'/' if bytes.get(index + 1) == Some(&b'*') => {
                state.block_comment = true;
                index += 1;
            }
            b'{' => state.brace_depth += 1,
            b'}' => state.brace_depth = state.brace_depth.saturating_sub(1),
            b'[' => state.bracket_depth += 1,
            b']' => state.bracket_depth = state.bracket_depth.saturating_sub(1),
            b'(' => state.paren_depth += 1,
            b')' => state.paren_depth = state.paren_depth.saturating_sub(1),
            _ => {}
        }
        index += 1;
    }
}

fn find_mdx_expression_close(
    lines: &[Line<'_>],
    index: usize,
    open_byte: usize,
) -> Option<(usize, usize)> {
    let mut depth = 0usize;
    let mut state = MdxBraceState::Normal;
    let mut escaped = false;
    let mut cursor = index;

    while cursor < lines.len() {
        let bytes = lines[cursor].text.as_bytes();
        let mut byte_index = if cursor == index { open_byte } else { 0 };
        while byte_index < bytes.len() {
            let byte = bytes[byte_index];
            match state {
                MdxBraceState::Normal => match byte {
                    b'\'' => state = MdxBraceState::SingleQuoted,
                    b'"' => state = MdxBraceState::DoubleQuoted,
                    b'`' => state = MdxBraceState::Template,
                    b'/' if bytes.get(byte_index + 1) == Some(&b'/') => {
                        state = MdxBraceState::LineComment;
                        break;
                    }
                    b'/' if bytes.get(byte_index + 1) == Some(&b'*') => {
                        state = MdxBraceState::BlockComment;
                        byte_index += 1;
                    }
                    b'{' => depth += 1,
                    b'}' => {
                        depth = depth.checked_sub(1)?;
                        if depth == 0 {
                            return lines[cursor].text[byte_index + 1..]
                                .trim()
                                .is_empty()
                                .then_some((cursor, byte_index));
                        }
                    }
                    _ => {}
                },
                MdxBraceState::SingleQuoted => {
                    update_mdx_quote_state(byte, b'\'', &mut state, &mut escaped);
                }
                MdxBraceState::DoubleQuoted => {
                    update_mdx_quote_state(byte, b'"', &mut state, &mut escaped);
                }
                MdxBraceState::Template => {
                    update_mdx_quote_state(byte, b'`', &mut state, &mut escaped);
                }
                MdxBraceState::LineComment => break,
                MdxBraceState::BlockComment => {
                    if byte == b'*' && bytes.get(byte_index + 1) == Some(&b'/') {
                        state = MdxBraceState::Normal;
                        byte_index += 1;
                    }
                }
            }
            byte_index += 1;
        }
        if state == MdxBraceState::LineComment {
            state = MdxBraceState::Normal;
        }
        cursor += 1;
    }

    None
}

fn update_mdx_quote_state(byte: u8, delimiter: u8, state: &mut MdxBraceState, escaped: &mut bool) {
    if *escaped {
        *escaped = false;
        return;
    }
    if byte == b'\\' {
        *escaped = true;
        return;
    }
    if byte == delimiter {
        *state = MdxBraceState::Normal;
    }
}

fn find_mdx_expression_inline_close(input: &str, open_byte: usize) -> Option<usize> {
    let bytes = input.as_bytes();
    if bytes.get(open_byte) != Some(&b'{') {
        return None;
    }

    let mut depth = 0usize;
    let mut state = MdxBraceState::Normal;
    let mut escaped = false;
    let mut cursor = open_byte;
    while cursor < bytes.len() {
        let byte = bytes[cursor];
        match state {
            MdxBraceState::Normal => match byte {
                b'\'' => state = MdxBraceState::SingleQuoted,
                b'"' => state = MdxBraceState::DoubleQuoted,
                b'`' => state = MdxBraceState::Template,
                b'/' if bytes.get(cursor + 1) == Some(&b'/') => {
                    state = MdxBraceState::LineComment;
                    cursor += 1;
                }
                b'/' if bytes.get(cursor + 1) == Some(&b'*') => {
                    state = MdxBraceState::BlockComment;
                    cursor += 1;
                }
                b'{' => depth += 1,
                b'}' => {
                    depth = depth.checked_sub(1)?;
                    if depth == 0 {
                        return Some(cursor);
                    }
                }
                _ => {}
            },
            MdxBraceState::SingleQuoted => {
                update_mdx_quote_state(byte, b'\'', &mut state, &mut escaped);
            }
            MdxBraceState::DoubleQuoted => {
                update_mdx_quote_state(byte, b'"', &mut state, &mut escaped);
            }
            MdxBraceState::Template => {
                update_mdx_quote_state(byte, b'`', &mut state, &mut escaped);
            }
            MdxBraceState::LineComment => {
                if byte == b'\n' {
                    state = MdxBraceState::Normal;
                }
            }
            MdxBraceState::BlockComment => {
                if byte == b'*' && bytes.get(cursor + 1) == Some(&b'/') {
                    state = MdxBraceState::Normal;
                    cursor += 1;
                }
            }
        }
        cursor += 1;
    }
    None
}

fn collect_mdx_expression_value(
    lines: &[Line<'_>],
    start_line: usize,
    open_byte: usize,
    close_line: usize,
    close_byte: usize,
) -> String {
    let mut value = String::new();
    let mut cursor = start_line;
    while cursor <= close_line {
        if cursor > start_line {
            value.push('\n');
        }
        let line = lines[cursor].text;
        let segment = if cursor == start_line && cursor == close_line {
            &line[open_byte + 1..close_byte]
        } else if cursor == start_line {
            &line[open_byte + 1..]
        } else if cursor == close_line {
            &line[..close_byte]
        } else {
            line
        };
        value.push_str(segment);
        cursor += 1;
    }
    value
}

fn find_mdx_jsx_close<'a>(lines: &'a [Line<'a>], index: usize) -> Option<usize> {
    let line = lines[index];
    let trimmed = line.text.trim_start();
    let start_byte = line.text.len() - trimmed.len();
    let root = mdx_jsx_tag_start(line.text, start_byte)?;
    if root.closing {
        return None;
    }

    let (mut cursor_line, mut cursor_byte, self_closing) =
        find_mdx_jsx_tag_end(lines, index, start_byte)?;
    if self_closing {
        return Some(cursor_line);
    }

    let mut depth = 1usize;
    cursor_byte += 1;
    'scan: while cursor_line < lines.len() {
        let line = lines[cursor_line].text;
        while cursor_byte < line.len() {
            let Some(relative_start) = line[cursor_byte..].find('<') else {
                break;
            };
            let tag_start_byte = cursor_byte + relative_start;
            let Some(candidate) = mdx_jsx_tag_start(line, tag_start_byte) else {
                cursor_byte = tag_start_byte + 1;
                continue;
            };
            let Some((tag_end_line, tag_end_byte, candidate_self_closing)) =
                find_mdx_jsx_tag_end(lines, cursor_line, tag_start_byte)
            else {
                return None;
            };

            if mdx_jsx_tag_matches(root.tag, candidate.tag) {
                if candidate.closing {
                    depth = depth.saturating_sub(1);
                    if depth == 0 {
                        return Some(tag_end_line);
                    }
                } else if !candidate_self_closing {
                    depth += 1;
                }
            }

            cursor_byte = tag_end_byte + 1;
            if tag_end_line != cursor_line {
                cursor_line = tag_end_line;
                continue 'scan;
            }
        }
        cursor_line += 1;
        cursor_byte = 0;
    }
    None
}

fn parse_mdx_jsx_inline(input: &str, index: usize) -> Option<(usize, String)> {
    let root = mdx_jsx_tag_start(input, index)?;
    if root.closing {
        return None;
    }

    let (mut cursor, self_closing) = find_mdx_jsx_tag_end_in_text(input, index)?;
    if self_closing {
        let end = cursor + 1;
        return Some((end, input[index..end].into()));
    }

    let mut depth = 1usize;
    cursor += 1;
    while cursor < input.len() {
        let Some(relative_start) = input[cursor..].find('<') else {
            return None;
        };
        let tag_start_byte = cursor + relative_start;
        let Some(candidate) = mdx_jsx_tag_start(input, tag_start_byte) else {
            cursor = tag_start_byte + 1;
            continue;
        };
        let Some((tag_end, candidate_self_closing)) =
            find_mdx_jsx_tag_end_in_text(input, tag_start_byte)
        else {
            return None;
        };

        if mdx_jsx_tag_matches(root.tag, candidate.tag) {
            if candidate.closing {
                depth = depth.saturating_sub(1);
                if depth == 0 {
                    let end = tag_end + 1;
                    return Some((end, input[index..end].into()));
                }
            } else if !candidate_self_closing {
                depth += 1;
            }
        }
        cursor = tag_end + 1;
    }
    None
}

fn mdx_jsx_tag_start(input: &str, start: usize) -> Option<MdxJsxTagStart<'_>> {
    let bytes = input.as_bytes();
    if bytes.get(start) != Some(&b'<') {
        return None;
    }

    match bytes.get(start + 1) {
        Some(b'>') => {
            return Some(MdxJsxTagStart {
                tag: MdxJsxTag::Fragment,
                closing: false,
            });
        }
        Some(b'/') if bytes.get(start + 2) == Some(&b'>') => {
            return Some(MdxJsxTagStart {
                tag: MdxJsxTag::Fragment,
                closing: true,
            });
        }
        Some(b'!' | b'?') | None => return None,
        _ => {}
    }

    let closing = bytes.get(start + 1) == Some(&b'/');
    let name_start = start + if closing { 2 } else { 1 };
    if !bytes
        .get(name_start)
        .is_some_and(|byte| is_mdx_jsx_name_start_byte(*byte))
    {
        return None;
    }

    let mut name_end = name_start + 1;
    while bytes
        .get(name_end)
        .is_some_and(|byte| is_mdx_jsx_name_byte(*byte))
    {
        name_end += 1;
    }
    if name_end == name_start {
        return None;
    }
    if bytes
        .get(name_end)
        .is_some_and(|byte| !is_mdx_jsx_name_delimiter(*byte))
    {
        return None;
    }
    Some(MdxJsxTagStart {
        tag: MdxJsxTag::Named(&input[name_start..name_end]),
        closing,
    })
}

fn mdx_jsx_tag_matches(left: MdxJsxTag<'_>, right: MdxJsxTag<'_>) -> bool {
    match (left, right) {
        (MdxJsxTag::Fragment, MdxJsxTag::Fragment) => true,
        (MdxJsxTag::Named(left), MdxJsxTag::Named(right)) => left == right,
        _ => false,
    }
}

fn find_mdx_jsx_tag_end(
    lines: &[Line<'_>],
    start_line: usize,
    start_byte: usize,
) -> Option<(usize, usize, bool)> {
    let mut line_index = start_line;
    let mut byte_index = start_byte + 1;
    let mut quote = None;
    let mut escaped = false;
    let mut expression_depth = 0usize;
    let mut expression_state = MdxBraceState::Normal;
    let mut expression_escaped = false;

    while line_index < lines.len() {
        let bytes = lines[line_index].text.as_bytes();
        while byte_index < bytes.len() {
            let byte = bytes[byte_index];
            if expression_depth > 0 {
                if update_mdx_jsx_expression_state(
                    byte,
                    bytes.get(byte_index + 1).copied(),
                    &mut expression_depth,
                    &mut expression_state,
                    &mut expression_escaped,
                ) {
                    byte_index += 1;
                }
                byte_index += 1;
                continue;
            }

            if let Some(delimiter) = quote {
                if escaped {
                    escaped = false;
                } else if byte == b'\\' {
                    escaped = true;
                } else if byte == delimiter {
                    quote = None;
                }
                byte_index += 1;
                continue;
            }

            match byte {
                b'\'' | b'"' => quote = Some(byte),
                b'{' => {
                    expression_depth = 1;
                    expression_state = MdxBraceState::Normal;
                    expression_escaped = false;
                }
                b'>' if expression_depth == 0 => {
                    let self_closing =
                        previous_nonspace_before(lines, line_index, byte_index) == Some(b'/');
                    return Some((line_index, byte_index, self_closing));
                }
                _ => {}
            }
            byte_index += 1;
        }
        if expression_state == MdxBraceState::LineComment {
            expression_state = MdxBraceState::Normal;
        }
        line_index += 1;
        byte_index = 0;
    }
    None
}

fn previous_nonspace_before(
    lines: &[Line<'_>],
    line_index: usize,
    byte_index: usize,
) -> Option<u8> {
    let mut cursor_line = line_index;
    let mut cursor_byte = byte_index;

    loop {
        if let Some(byte) = lines[cursor_line].text.as_bytes()[..cursor_byte]
            .iter()
            .rev()
            .copied()
            .find(|byte| !byte.is_ascii_whitespace())
        {
            return Some(byte);
        }
        if cursor_line == 0 {
            return None;
        }
        cursor_line -= 1;
        cursor_byte = lines[cursor_line].text.len();
    }
}

fn find_mdx_jsx_tag_end_in_text(input: &str, start_byte: usize) -> Option<(usize, bool)> {
    let bytes = input.as_bytes();
    let mut byte_index = start_byte + 1;
    let mut quote = None;
    let mut escaped = false;
    let mut expression_depth = 0usize;
    let mut expression_state = MdxBraceState::Normal;
    let mut expression_escaped = false;

    while byte_index < bytes.len() {
        let byte = bytes[byte_index];
        if expression_depth > 0 {
            if update_mdx_jsx_expression_state(
                byte,
                bytes.get(byte_index + 1).copied(),
                &mut expression_depth,
                &mut expression_state,
                &mut expression_escaped,
            ) {
                byte_index += 1;
            }
            byte_index += 1;
            continue;
        }

        if let Some(delimiter) = quote {
            if escaped {
                escaped = false;
            } else if byte == b'\\' {
                escaped = true;
            } else if byte == delimiter {
                quote = None;
            }
            byte_index += 1;
            continue;
        }

        match byte {
            b'\'' | b'"' => quote = Some(byte),
            b'{' => {
                expression_depth = 1;
                expression_state = MdxBraceState::Normal;
                expression_escaped = false;
            }
            b'>' if expression_depth == 0 => {
                let self_closing = previous_nonspace_before_text(input, byte_index) == Some(b'/');
                return Some((byte_index, self_closing));
            }
            _ => {}
        }
        byte_index += 1;
    }
    None
}

fn previous_nonspace_before_text(input: &str, byte_index: usize) -> Option<u8> {
    input.as_bytes()[..byte_index]
        .iter()
        .rev()
        .copied()
        .find(|byte| !byte.is_ascii_whitespace())
}

fn update_mdx_jsx_expression_state(
    byte: u8,
    next: Option<u8>,
    depth: &mut usize,
    state: &mut MdxBraceState,
    escaped: &mut bool,
) -> bool {
    match *state {
        MdxBraceState::Normal => match byte {
            b'\'' => *state = MdxBraceState::SingleQuoted,
            b'"' => *state = MdxBraceState::DoubleQuoted,
            b'`' => *state = MdxBraceState::Template,
            b'/' if next == Some(b'/') => {
                *state = MdxBraceState::LineComment;
                return true;
            }
            b'/' if next == Some(b'*') => {
                *state = MdxBraceState::BlockComment;
                return true;
            }
            b'{' => *depth += 1,
            b'}' => {
                *depth = (*depth).saturating_sub(1);
                if *depth == 0 {
                    *state = MdxBraceState::Normal;
                    *escaped = false;
                }
            }
            _ => {}
        },
        MdxBraceState::SingleQuoted => {
            update_mdx_quote_state(byte, b'\'', state, escaped);
        }
        MdxBraceState::DoubleQuoted => {
            update_mdx_quote_state(byte, b'"', state, escaped);
        }
        MdxBraceState::Template => {
            update_mdx_quote_state(byte, b'`', state, escaped);
        }
        MdxBraceState::LineComment => {
            if byte == b'\n' {
                *state = MdxBraceState::Normal;
            }
        }
        MdxBraceState::BlockComment => {
            if byte == b'*' && next == Some(b'/') {
                *state = MdxBraceState::Normal;
                return true;
            }
        }
    }
    false
}

fn is_mdx_jsx_name_start_byte(byte: u8) -> bool {
    byte.is_ascii_alphabetic() || matches!(byte, b'_' | b'$')
}

fn is_mdx_jsx_name_byte(byte: u8) -> bool {
    byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b':' | b'_' | b'-' | b'$')
}

fn is_mdx_jsx_name_delimiter(byte: u8) -> bool {
    byte.is_ascii_whitespace() || matches!(byte, b'/' | b'>' | b'{' | b'}')
}

fn collect_line_range(lines: &[Line<'_>], start: usize, end: usize) -> String {
    let mut value = String::new();
    let mut cursor = start;
    while cursor <= end {
        if cursor > start {
            value.push('\n');
        }
        value.push_str(lines[cursor].text);
        cursor += 1;
    }
    value
}

fn parse_indented_code(
    lines: &[Line<'_>],
    index: usize,
    options: &SyntaxOptions,
) -> Option<(Block, usize)> {
    if !options.constructs.indented_code || strip_indented_code_prefix(lines[index].text).is_none()
    {
        return None;
    }
    let mut value = String::new();
    let mut cursor = index;
    // Track the last line that carried real content: leading and trailing blank
    // lines are not part of an indented code block, only interior ones are.
    let mut content_end = index;
    let mut content_end_len = 0usize;
    while cursor < lines.len() {
        if let Some(text) = strip_indented_code_prefix(lines[cursor].text) {
            ensure_line_separator(&mut value);
            value.push_str(text);
            value.push_str(lines[cursor].eol);
            if !text.trim().is_empty() {
                content_end = cursor;
                content_end_len = value.len();
            }
            cursor += 1;
            continue;
        }

        if !lines[cursor].text.trim().is_empty() {
            break;
        }
        ensure_line_separator(&mut value);
        value.push_str(lines[cursor].eol);
        cursor += 1;
    }
    // Drop trailing blank lines accumulated past the last real content line.
    value.truncate(content_end_len);
    Some((
        Block::CodeBlock(CodeBlock {
            meta: NodeMeta::new(Some(Span::new(
                lines[index].start,
                lines[content_end].end_with_eol,
            ))),
            kind: CodeBlockKind::Indented,
            info: None,
            value,
        }),
        cursor,
    ))
}

fn strip_indented_code_prefix(input: &str) -> Option<&str> {
    let mut column = 0usize;
    for (index, byte) in input.as_bytes().iter().enumerate() {
        match *byte {
            b' ' => {
                column += 1;
                if column == 4 {
                    return Some(&input[index + 1..]);
                }
            }
            b'\t' => {
                column += 4 - (column % 4);
                if column >= 4 {
                    return Some(&input[index + 1..]);
                }
            }
            _ => return None,
        }
    }
    None
}

fn parse_table(
    lines: &[Line<'_>],
    index: usize,
    options: &SyntaxOptions,
    definitions: &[String],
    diagnostics: &mut Vec<Diagnostic>,
) -> Option<(Block, usize)> {
    if !options.constructs.gfm_table || index + 1 >= lines.len() {
        return None;
    }
    let delimiter = table_indent_line(lines[index + 1].text, options.constructs.indented_code)?;
    if list_marker_info(delimiter).is_some() {
        return None;
    }
    if !table_has_separator(lines[index].text, delimiter, options.constructs.spoiler) {
        return None;
    }
    let alignments = parse_table_delimiter(delimiter, options.constructs.spoiler)?;
    let headers = split_table_row(lines[index].text, options.constructs.spoiler);
    if headers.len() != alignments.len() {
        return None;
    }

    let mut rows = Vec::new();
    rows.push(TableRow {
        meta: NodeMeta::new(Some(Span::new(lines[index].start, lines[index].end))),
        cells: headers
            .iter()
            .map(|cell| TableCell {
                meta: NodeMeta::default(),
                children: parse_inlines(
                    cell.trim(),
                    lines[index].start,
                    options,
                    definitions,
                    diagnostics,
                ),
            })
            .collect(),
    });

    let mut cursor = index + 2;
    while cursor < lines.len() {
        let Some(row) = table_indent_line(lines[cursor].text, options.constructs.indented_code)
        else {
            break;
        };
        // Once a table is open, every non-blank line that isn't a real block
        // start is a body row (GFM); pipeless lines (incl. setext underlines)
        // become a single padded cell.
        if row.trim().is_empty() || table_body_line_ends_table(lines[cursor].text, options) {
            break;
        }
        let cells = split_table_row(row, options.constructs.spoiler);
        rows.push(TableRow {
            meta: NodeMeta::new(Some(Span::new(lines[cursor].start, lines[cursor].end))),
            cells: alignments
                .iter()
                .enumerate()
                .map(|(cell_index, _)| {
                    let value = cells.get(cell_index).map(String::as_str).unwrap_or("");
                    TableCell {
                        meta: NodeMeta::default(),
                        children: parse_inlines(
                            value.trim(),
                            lines[cursor].start,
                            options,
                            definitions,
                            diagnostics,
                        ),
                    }
                })
                .collect(),
        });
        cursor += 1;
    }

    Some((
        Block::Table(Table {
            meta: NodeMeta::new(Some(Span::new(
                lines[index].start,
                lines[cursor - 1].end_with_eol,
            ))),
            alignments,
            rows,
        }),
        cursor,
    ))
}

fn parse_setext_heading(
    lines: &[Line<'_>],
    index: usize,
    options: &SyntaxOptions,
    definitions: &[String],
) -> Option<(Block, usize)> {
    if index + 1 >= lines.len() || lines[index].text.trim().is_empty() {
        return None;
    }

    // A setext heading is a (possibly multi-line) paragraph followed by an
    // underline. Scan over paragraph-continuation lines to find the underline,
    // stopping if a continuation line is itself a block start (which would
    // interrupt the paragraph before any underline could apply).
    let mut underline_index = index + 1;
    loop {
        // A setext underline that arrived as a LAZY block-quote continuation is
        // paragraph text, not an underline: `> a\n===` is `<p>a\n===</p>`, while
        // a MARKED `> a\n> ---` stays an H2 (its `---` is not lazy). The lazy
        // flag distinguishes the two; a lazy underline keeps scanning as
        // ordinary paragraph-continuation text.
        let underline_depth = if lines[underline_index].lazy {
            None
        } else {
            setext_underline_depth(lines[underline_index].text)
        };
        if let Some(depth) = underline_depth {
            let mut value = String::new();
            for line in &lines[index..underline_index] {
                // Trim leading indentation only: a fully `.trim()`ed content line
                // would discard the trailing spaces that form a hard line break.
                push_line(&mut value, trim_ascii_start(line.text));
            }
            return Some((
                Block::Heading(Heading {
                    meta: NodeMeta::new(Some(Span::new(
                        lines[index].start,
                        lines[underline_index].end,
                    ))),
                    depth,
                    kind: HeadingKind::Setext,
                    children: parse_inlines(
                        &value,
                        lines[index].start,
                        options,
                        definitions,
                        &mut Vec::new(),
                    ),
                }),
                underline_index + 1,
            ));
        }

        // Not an underline: it must be a valid paragraph-continuation line for
        // the run to remain a setext heading.
        let line = lines[underline_index].text;
        if line.trim().is_empty()
            || table_can_start(lines, underline_index, options)
            || likely_block_start(line, options)
        {
            return None;
        }
        underline_index += 1;
        if underline_index >= lines.len() {
            return None;
        }
    }
}

fn setext_underline_depth(input: &str) -> Option<u8> {
    let underline = trim_up_to_three_spaces(input)?.trim();
    match underline {
        text if !text.is_empty() && text.chars().all(|char| char == '=') => Some(1),
        text if !text.is_empty() && text.chars().all(|char| char == '-') => Some(2),
        _ => None,
    }
}

fn parse_paragraph(
    lines: &[Line<'_>],
    index: usize,
    options: &SyntaxOptions,
    definitions: &[String],
    diagnostics: &mut Vec<Diagnostic>,
) -> (Block, usize) {
    let mut value = String::new();
    let start = lines[index].start;
    let mut cursor = index;
    while cursor < lines.len() {
        if lines[cursor].text.trim().is_empty() {
            break;
        }
        // A lazy continuation line is paragraph text by construction (it reached
        // this paragraph as the dedented tail of an enclosing container), so it
        // cannot itself start a new block — skip the block-boundary checks.
        if cursor > index && !lines[cursor].lazy {
            if table_can_start(lines, cursor, options) {
                break;
            }
            if likely_block_start(lines[cursor].text, options) {
                break;
            }
        }
        if !value.is_empty() {
            value.push('\n');
        }
        value.push_str(trim_ascii_start(lines[cursor].text));
        cursor += 1;
    }

    let end = lines[cursor - 1].end;
    (
        Block::Paragraph(Paragraph {
            meta: NodeMeta::new(Some(Span::new(start, end))),
            children: parse_inlines(&value, start, options, definitions, diagnostics),
        }),
        cursor,
    )
}

/// A `*` or `_` delimiter run recorded during the inline scan for later
/// resolution by the CommonMark delimiter-stack algorithm (`process_emphasis`).
#[derive(Clone, Copy)]
struct DelimMarker {
    /// Index of the placeholder text node in the flat node list. The text node
    /// holds the as-yet-unmatched delimiter characters; matching trims it from
    /// the appropriate side and matched characters are removed entirely.
    node_index: usize,
    marker: u8,
    /// Remaining unmatched delimiter characters in this run.
    length: usize,
    can_open: bool,
    can_close: bool,
    /// Absolute byte offset of the run's first remaining delimiter character.
    span_start: usize,
    /// `true` once this run is consumed (fully matched) or demoted to plain text.
    inactive: bool,
}

/// Records a `*`/`_`/`~` delimiter run as a literal text node plus a stack
/// entry.
///
/// Flanking is computed on the whole run (CommonMark treats left/right-flanking
/// as a property of the run, not of an individual delimiter), so the same
/// `can_open`/`can_close` helpers that the older ad-hoc scanner used are reused
/// here unchanged — including the `_` intraword punctuation rules.
///
/// `strikethrough` enables the GFM cross-marker bonus: when strikethrough is an
/// active construct, a `*`/`_` run immediately adjacent to a `~` counts as
/// openable/closeable even though `~` is a punctuation character (this is what
/// makes `a*~b~*c` emphasize). The bonus is never granted to a `~` run itself —
/// tilde gets plain CommonMark flanking.
fn record_emphasis_delimiter(
    nodes: &mut Vec<Inline>,
    delimiters: &mut Vec<DelimMarker>,
    input: &str,
    index: usize,
    base_offset: usize,
    marker: u8,
    strikethrough: bool,
) {
    let length = delimiter_byte_run_len(input, index, marker);
    let (mut can_open, mut can_close) = if marker == b'_' {
        (
            can_open_underscore(input, index, length),
            can_close_underscore(input, index, length),
        )
    } else {
        (
            can_open_delimited(input, index, length),
            can_close_delimited(input, index, length),
        )
    };

    // GFM: a `*`/`_` run touching a `~` strikethrough marker may open/close even
    // when ordinary flanking refuses it (the `~` would otherwise be a blocking
    // punctuation neighbour). Tilde itself never receives this bonus.
    if strikethrough && marker != b'~' {
        let before = input[..index].chars().next_back();
        let after = input[index + length..].chars().next();
        if after == Some('~') {
            can_open = true;
        }
        if before == Some('~') {
            can_close = true;
        }
    }

    let value = String::from(marker as char).repeat(length);

    let node_index = nodes.len();
    nodes.push(Inline::Text(Text {
        meta: NodeMeta::new(Some(Span::new(
            base_offset + index,
            base_offset + index + length,
        ))),
        value,
    }));

    delimiters.push(DelimMarker {
        node_index,
        marker,
        length,
        can_open,
        can_close,
        span_start: base_offset + index,
        inactive: false,
    });
}

/// Resolves recorded `*`/`_` delimiter runs into `Emphasis`/`Strong` nodes using
/// the CommonMark delimiter-stack algorithm, leaving unmatched runs as text.
fn process_emphasis(mut nodes: Vec<Inline>, mut delimiters: Vec<DelimMarker>) -> Vec<Inline> {
    if delimiters.is_empty() {
        return nodes;
    }

    // `openers_bottom` records, per (marker, opener-can-also-close, length % 3),
    // the lowest opener index a closer is allowed to reach. Closers below this
    // bound for their key have already been proven to have no compatible opener.
    // Three markers (`*`, `_`, `~`) × both-flag × length%3.
    let mut openers_bottom: [Option<usize>; 18] = [None; 18];
    let mut closer_idx = 0;

    while closer_idx < delimiters.len() {
        let closer = delimiters[closer_idx];
        if closer.inactive || !closer.can_close {
            closer_idx += 1;
            continue;
        }

        let key = openers_bottom_key(&closer);
        let bottom = openers_bottom[key];

        // Walk back to the nearest compatible opener above the recorded bound.
        let mut opener_idx = None;
        let mut search = closer_idx;
        while search > 0 {
            search -= 1;
            if let Some(bottom) = bottom {
                if search < bottom {
                    break;
                }
            }
            let candidate = delimiters[search];
            if candidate.inactive || candidate.marker != closer.marker || !candidate.can_open {
                continue;
            }
            if emphasis_delimiters_match(&candidate, &closer) {
                opener_idx = Some(search);
                break;
            }
        }

        let Some(opener_idx) = opener_idx else {
            // No opener found: remember how far we searched so future closers of
            // the same key skip the same dead range. A closer that cannot also
            // open is removed so it is never revisited.
            openers_bottom[key] = Some(closer_idx);
            if !closer.can_open {
                delimiters[closer_idx].inactive = true;
            }
            closer_idx += 1;
            continue;
        };

        let (used, wrap) = if closer.marker == b'~' {
            // Strikethrough consumes the whole (equal-length) run on each side at
            // once; the marker width selects the `Delete` flavour.
            let length = delimiters[closer_idx].length;
            let marker = if length >= 2 {
                DeleteMarker::DoubleTilde
            } else {
                DeleteMarker::SingleTilde
            };
            (length, EmphasisWrap::Delete(marker))
        } else {
            let strong = delimiters[opener_idx].length >= 2 && delimiters[closer_idx].length >= 2;
            let used = if strong { 2 } else { 1 };
            let wrap = if strong {
                EmphasisWrap::Strong
            } else {
                EmphasisWrap::Emphasis
            };
            (used, wrap)
        };

        apply_emphasis(
            &mut nodes,
            &mut delimiters,
            opener_idx,
            closer_idx,
            used,
            wrap,
        );

        // Drop delimiters strictly between the opener and closer: they could not
        // match outward across this newly closed span.
        let mut inner = opener_idx + 1;
        while inner < closer_idx {
            delimiters[inner].inactive = true;
            inner += 1;
        }

        if delimiters[opener_idx].length == 0 {
            delimiters[opener_idx].inactive = true;
        }
        if delimiters[closer_idx].length == 0 {
            delimiters[closer_idx].inactive = true;
            closer_idx += 1;
        }
        // When the closer still has delimiters left it stays the active closer so
        // the leftover can match an earlier opener (e.g. `***foo*` keeps `**`).
    }

    // Adjacent text nodes can appear where unmatched delimiter runs ended up
    // beside literal text (`**foo*bar*` -> `**foo` + emphasis). CommonMark
    // coalesces them as the final step; do the same for the spans we created.
    merge_adjacent_text(&mut nodes);
    nodes
}

/// Merges consecutive `Text` nodes in a list, recursing into the `Emphasis`/
/// `Strong` nodes produced at this level. Other containers were already
/// finalized by their own `parse_inlines` pass and are left untouched.
fn merge_adjacent_text(nodes: &mut Vec<Inline>) {
    let mut write = 0;
    for read in 0..nodes.len() {
        if read != write {
            nodes.swap(read, write);
        }
        if write > 0 {
            let (head, tail) = nodes.split_at_mut(write);
            if let (Inline::Text(previous), Inline::Text(current)) =
                (&mut head[write - 1], &tail[0])
            {
                previous.value.push_str(&current.value);
                if let (Some(previous_span), Some(current_span)) =
                    (previous.meta.span.as_mut(), current.meta.span)
                {
                    previous_span.end = current_span.end;
                }
                continue;
            }
        }
        write += 1;
    }
    nodes.truncate(write);

    for node in nodes.iter_mut() {
        match node {
            Inline::Emphasis(emphasis) => merge_adjacent_text(&mut emphasis.children),
            Inline::Strong(strong) => merge_adjacent_text(&mut strong.children),
            Inline::Delete(delete) => merge_adjacent_text(&mut delete.children),
            _ => {}
        }
    }
}

/// Index into `openers_bottom` for a closer's (marker, both-flags, length%3) key.
fn openers_bottom_key(closer: &DelimMarker) -> usize {
    let marker = match closer.marker {
        b'_' => 1,
        b'~' => 2,
        _ => 0,
    };
    let both = usize::from(closer.can_open && closer.can_close);
    let modulo = closer.length % 3;
    ((marker * 2) + both) * 3 + modulo
}

/// CommonMark opener/closer compatibility, including the rule of three.
fn emphasis_delimiters_match(opener: &DelimMarker, closer: &DelimMarker) -> bool {
    // GFM strikethrough: opener and closer runs must be the same length (a `~`
    // never pairs with `~~`). The rule of three does not apply to `~`.
    if opener.marker == b'~' {
        return opener.length == closer.length;
    }

    // Rule of three: if either delimiter can both open and close, the sum of the
    // two run lengths must not be a multiple of three, unless both lengths are
    // themselves multiples of three.
    let opener_both = opener.can_open && opener.can_close;
    let closer_both = closer.can_open && closer.can_close;
    if opener_both || closer_both {
        let sum = opener.length + closer.length;
        if sum % 3 == 0 && !(opener.length % 3 == 0 && closer.length % 3 == 0) {
            return false;
        }
    }
    true
}

/// The node a matched delimiter pair collapses into.
#[derive(Clone, Copy)]
enum EmphasisWrap {
    Emphasis,
    Strong,
    Delete(DeleteMarker),
}

/// Wraps the nodes between two delimiter runs into an `Emphasis`/`Strong`/
/// `Delete` node, consuming `used` characters from each side and keeping every
/// other delimiter's `node_index` consistent with the rewritten node list.
fn apply_emphasis(
    nodes: &mut Vec<Inline>,
    delimiters: &mut [DelimMarker],
    opener_idx: usize,
    closer_idx: usize,
    used: usize,
    wrap: EmphasisWrap,
) {
    let opener_node = delimiters[opener_idx].node_index;
    let closer_node = delimiters[closer_idx].node_index;

    // Trim the consumed characters from the opener's text node (right side) and
    // the closer's text node (left side), updating their recorded lengths/spans.
    trim_delimiter_text_tail(&mut nodes[opener_node], used);
    delimiters[opener_idx].length -= used;
    delimiters[opener_idx].span_start += used;

    trim_delimiter_text_head(&mut nodes[closer_node], used);
    delimiters[closer_idx].length -= used;

    // Span covers the consumed opener delimiters through the consumed closer
    // delimiters. The exact value is informational; structure is what matters.
    let span_start = delimiters[opener_idx].span_start - used;
    let span_end = delimiters[closer_idx].span_start + delimiters[closer_idx].length + used;

    // The wrapped children are the nodes strictly between the opener and closer
    // text nodes.
    let children_start = opener_node + 1;
    let children_end = closer_node; // exclusive
    let children: Vec<Inline> = nodes.drain(children_start..children_end).collect();
    let removed = children.len();

    let meta = NodeMeta::new(Some(Span::new(span_start, span_end)));
    let wrapped = match wrap {
        EmphasisWrap::Strong => Inline::Strong(Strong { meta, children }),
        EmphasisWrap::Emphasis => Inline::Emphasis(Emphasis { meta, children }),
        EmphasisWrap::Delete(marker) => Inline::Delete(Delete {
            meta,
            marker,
            children,
        }),
    };
    nodes.insert(children_start, wrapped);

    // Indices at or past the (old) closer node shift by `1 - removed`: the drain
    // removed `removed` nodes then the insert added one. Apply this using the
    // original `children_end` threshold before any further mutation.
    reindex_delimiters(delimiters, children_end, 1 - removed as isize);

    // Drop any placeholder text node that has been fully consumed so leftover
    // delimiters never survive as literal text. Remove the closer first because
    // it sits at the higher index and removal shifts everything after it.
    if delimiters[closer_idx].length == 0 {
        let pos = delimiters[closer_idx].node_index;
        nodes.remove(pos);
        reindex_delimiters(delimiters, pos, -1);
    }
    if delimiters[opener_idx].length == 0 {
        let pos = delimiters[opener_idx].node_index;
        nodes.remove(pos);
        reindex_delimiters(delimiters, pos, -1);
    }
}

/// Adjusts `node_index` for every delimiter at or after `from` by `delta`.
fn reindex_delimiters(delimiters: &mut [DelimMarker], from: usize, delta: isize) {
    if delta == 0 {
        return;
    }
    for delimiter in delimiters.iter_mut() {
        if delimiter.node_index >= from {
            delimiter.node_index = (delimiter.node_index as isize + delta) as usize;
        }
    }
}

/// Removes `count` trailing delimiter characters from a placeholder text node.
fn trim_delimiter_text_tail(node: &mut Inline, count: usize) {
    if let Inline::Text(text) = node {
        let new_len = text.value.len().saturating_sub(count);
        text.value.truncate(new_len);
        if let Some(span) = text.meta.span.as_mut() {
            span.end = span.end.saturating_sub(count);
        }
    }
}

/// Removes `count` leading delimiter characters from a placeholder text node.
fn trim_delimiter_text_head(node: &mut Inline, count: usize) {
    if let Inline::Text(text) = node {
        let count = count.min(text.value.len());
        text.value.drain(..count);
        if let Some(span) = text.meta.span.as_mut() {
            span.start += count;
        }
    }
}

fn parse_inlines(
    input: &str,
    base_offset: usize,
    options: &SyntaxOptions,
    definitions: &[String],
    diagnostics: &mut Vec<Diagnostic>,
) -> Vec<Inline> {
    parse_inlines_with_context(
        input,
        base_offset,
        options,
        definitions,
        diagnostics,
        InlineContext::default(),
    )
}

#[derive(Clone, Copy)]
struct InlineContext {
    allow_links: bool,
}

impl Default for InlineContext {
    fn default() -> Self {
        Self { allow_links: true }
    }
}

fn parse_inlines_with_context(
    input: &str,
    base_offset: usize,
    options: &SyntaxOptions,
    definitions: &[String],
    diagnostics: &mut Vec<Diagnostic>,
    context: InlineContext,
) -> Vec<Inline> {
    let bytes = input.as_bytes();
    let mut nodes = Vec::new();
    let mut text_start = 0;
    let mut text = String::new();
    let mut index = 0;
    // Core `*`/`_` emphasis is resolved with a CommonMark delimiter stack after
    // the scan completes. During the scan we emit each candidate delimiter run as
    // a literal text node and record its position here so `process_emphasis` can
    // rewrite the flat node list into Emphasis/Strong (or leave it as text).
    let mut delimiters: Vec<DelimMarker> = Vec::new();

    while index < bytes.len() {
        if bytes[index] == b'\\' {
            if let Some((next_index, char)) = next_char(input, index + 1) {
                if char.is_ascii_punctuation() {
                    if options.parse.preserve_character_escapes {
                        flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                        nodes.push(Inline::Escape(Escape {
                            meta: NodeMeta::new(Some(Span::new(
                                base_offset + index,
                                base_offset + next_index,
                            ))),
                            value: char,
                        }));
                        index = next_index;
                        text_start = index;
                        continue;
                    }
                    if text.is_empty() {
                        text_start = base_offset + index;
                    }
                    if gfm_link_label_preserves_url_dot_escape(&text, char, options, context) {
                        text.push('\\');
                    }
                    text.push(char);
                    index = next_index;
                    continue;
                }
            }
        }

        if bytes[index] == b'&' {
            if let Some((end, value)) = parse_character_reference(input, index) {
                if options.parse.preserve_character_references {
                    flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                    nodes.push(Inline::CharacterReference(CharacterReference {
                        meta: NodeMeta::new(Some(Span::new(
                            base_offset + index,
                            base_offset + end,
                        ))),
                        reference: input[index..end].into(),
                        value,
                    }));
                    index = end;
                    text_start = index;
                    continue;
                }
                if text.is_empty() {
                    text_start = base_offset + index;
                }
                text.push_str(&value);
                index = end;
                continue;
            }
        }

        if bytes[index] == b'\n' {
            if text.ends_with('\\') {
                text.pop();
                flush_text(
                    &mut nodes,
                    &mut text,
                    text_start,
                    base_offset + index.saturating_sub(1),
                );
                nodes.push(Inline::LineBreak(LineBreak {
                    meta: NodeMeta::new(Some(Span::new(
                        base_offset + index.saturating_sub(1),
                        base_offset + index + 1,
                    ))),
                    kind: LineBreakKind::Backslash,
                }));
                index += 1;
                text_start = index;
                continue;
            }
            let trailing_spaces = trailing_space_count(&text);
            if is_hard_break_suffix(&text, trailing_spaces) {
                text.truncate(text.len() - trailing_spaces);
                flush_text(
                    &mut nodes,
                    &mut text,
                    text_start,
                    base_offset + index.saturating_sub(trailing_spaces),
                );
                nodes.push(Inline::LineBreak(LineBreak {
                    meta: NodeMeta::new(Some(Span::new(
                        base_offset + index.saturating_sub(trailing_spaces),
                        base_offset + index + 1,
                    ))),
                    kind: LineBreakKind::Spaces,
                }));
                index += 1;
                text_start = index;
                continue;
            }
            if trailing_spaces > 0 {
                text.truncate(text.len() - trailing_spaces);
            }
            flush_text(&mut nodes, &mut text, text_start, base_offset + index);
            nodes.push(Inline::SoftBreak(SoftBreak {
                meta: NodeMeta::new(Some(Span::new(
                    base_offset + index,
                    base_offset + index + 1,
                ))),
            }));
            index += 1;
            text_start = index;
            continue;
        }

        if bytes[index] == b'`' {
            if let Some((end, code_span)) = parse_code_span(input, index) {
                flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                nodes.push(Inline::Code(CodeInline {
                    meta: NodeMeta::new(Some(Span::new(base_offset + index, base_offset + end))),
                    value: code_span.value,
                    raw: code_span.raw,
                    fence_length: code_span.fence_length,
                }));
                index = end;
                text_start = index;
                continue;
            } else {
                // No matching-length close for this opening backtick run:
                // CommonMark renders the whole run as literal text. Consume the
                // entire run here so the loop does not advance one byte and retry
                // a shorter sub-run that could spuriously match a shorter close
                // (```foo`` stayed a phantom 2-backtick code span).
                let run = bytes[index..]
                    .iter()
                    .take_while(|byte| **byte == b'`')
                    .count();
                if text.is_empty() {
                    text_start = base_offset + index;
                }
                for _ in 0..run {
                    text.push('`');
                }
                index += run;
                continue;
            }
        }

        if options.constructs.spoiler
            && bytes.get(index) == Some(&b'|')
            && bytes.get(index + 1) == Some(&b'|')
            && bytes.get(index + 2) != Some(&b'|')
        {
            if let Some(end) = find_spoiler_close(input, index + 2) {
                flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                let inner = &input[index + 2..end];
                nodes.push(Inline::Spoiler(Spoiler {
                    meta: NodeMeta::new(Some(Span::new(
                        base_offset + index,
                        base_offset + end + 2,
                    ))),
                    children: parse_inlines_with_context(
                        inner,
                        base_offset + index + 2,
                        options,
                        definitions,
                        diagnostics,
                        context,
                    ),
                }));
                index = end + 2;
                text_start = index;
                continue;
            }
        }

        if bytes[index] == b'*' && delimiter_byte_run_start(input, index, b'*') == index {
            let run_len = delimiter_byte_run_len(input, index, b'*');
            flush_text(&mut nodes, &mut text, text_start, base_offset + index);
            record_emphasis_delimiter(
                &mut nodes,
                &mut delimiters,
                input,
                index,
                base_offset,
                b'*',
                options.constructs.gfm_strikethrough,
            );
            index += run_len;
            text_start = index;
            continue;
        }

        if options.constructs.underline
            && bytes.get(index) == Some(&b'_')
            && bytes.get(index + 1) == Some(&b'_')
            && bytes.get(index + 2) == Some(&b'_')
            && can_open_underscore(input, index, 1)
        {
            if let Some(end) = find_closing_delimiter(input, index + 3, "___", true) {
                flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                let inner = &input[index + 3..end];
                let underline = Inline::Underline(Underline {
                    meta: NodeMeta::new(Some(Span::new(
                        base_offset + index + 1,
                        base_offset + end + 2,
                    ))),
                    children: parse_inlines_with_context(
                        inner,
                        base_offset + index + 3,
                        options,
                        definitions,
                        diagnostics,
                        context,
                    ),
                });
                nodes.push(Inline::Emphasis(Emphasis {
                    meta: NodeMeta::new(Some(Span::new(
                        base_offset + index,
                        base_offset + end + 3,
                    ))),
                    children: vec![underline],
                }));
                index = end + 3;
                text_start = index;
                continue;
            }
        }

        if options.constructs.underline
            && bytes.get(index) == Some(&b'_')
            && bytes.get(index + 1) == Some(&b'_')
            && can_open_underscore(input, index, 2)
        {
            if let Some(end) = find_closing_delimiter(input, index + 2, "__", true) {
                flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                let inner = &input[index + 2..end];
                nodes.push(Inline::Underline(Underline {
                    meta: NodeMeta::new(Some(Span::new(
                        base_offset + index,
                        base_offset + end + 2,
                    ))),
                    children: parse_inlines_with_context(
                        inner,
                        base_offset + index + 2,
                        options,
                        definitions,
                        diagnostics,
                        context,
                    ),
                }));
                index = end + 2;
                text_start = index;
                continue;
            }
        }

        // Core `_` emphasis/strong is resolved by the delimiter stack, just like
        // `*`. The `___`/`__` underline-extension branches above run first and
        // `continue` when they consume the run, so reaching this point means the
        // run is plain emphasis material (underline disabled, or no underline
        // close was found).
        if bytes[index] == b'_' && delimiter_byte_run_start(input, index, b'_') == index {
            // A leading `_` can begin a GFM email local part (`_a@b.c`); try the
            // literal autolink before recording the `_` as an emphasis
            // delimiter, otherwise the `_` would be consumed and the email would
            // wrongly start one char later (where its left boundary fails).
            if (options.constructs.gfm_autolink_literal || options.constructs.relaxed_autolinks)
                && context.allow_links
            {
                if let Some((end, destination)) = parse_literal_autolink(
                    input,
                    index,
                    options.constructs.gfm_autolink_literal,
                    options.constructs.relaxed_autolinks,
                ) {
                    flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                    nodes.push(Inline::Autolink(Autolink {
                        meta: NodeMeta::new(Some(Span::new(
                            base_offset + index,
                            base_offset + end,
                        ))),
                        destination,
                        kind: AutolinkKind::GfmLiteral {
                            original: input[index..end].into(),
                        },
                    }));
                    index = end;
                    text_start = index;
                    continue;
                }
            }
            let run_len = delimiter_byte_run_len(input, index, b'_');
            flush_text(&mut nodes, &mut text, text_start, base_offset + index);
            record_emphasis_delimiter(
                &mut nodes,
                &mut delimiters,
                input,
                index,
                base_offset,
                b'_',
                options.constructs.gfm_strikethrough,
            );
            index += run_len;
            text_start = index;
            continue;
        }

        if options.constructs.insert
            && bytes.get(index) == Some(&b'+')
            && bytes.get(index + 1) == Some(&b'+')
            && bytes.get(index + 2) != Some(&b'+')
            && can_open_delimited(input, index, 2)
        {
            if let Some(end) = find_closing_delimiter(input, index + 2, "++", false) {
                flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                let inner = &input[index + 2..end];
                nodes.push(Inline::Insert(Insert {
                    meta: NodeMeta::new(Some(Span::new(
                        base_offset + index,
                        base_offset + end + 2,
                    ))),
                    children: parse_inlines_with_context(
                        inner,
                        base_offset + index + 2,
                        options,
                        definitions,
                        diagnostics,
                        context,
                    ),
                }));
                index = end + 2;
                text_start = index;
                continue;
            }
        }

        if options.constructs.highlight
            && bytes.get(index) == Some(&b'=')
            && bytes.get(index + 1) == Some(&b'=')
            && bytes.get(index + 2) != Some(&b'=')
            && can_open_delimited(input, index, 2)
        {
            if let Some(end) = find_closing_delimiter(input, index + 2, "==", false) {
                flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                let inner = &input[index + 2..end];
                nodes.push(Inline::Mark(Mark {
                    meta: NodeMeta::new(Some(Span::new(
                        base_offset + index,
                        base_offset + end + 2,
                    ))),
                    children: parse_inlines_with_context(
                        inner,
                        base_offset + index + 2,
                        options,
                        definitions,
                        diagnostics,
                        context,
                    ),
                }));
                index = end + 2;
                text_start = index;
                continue;
            }
        }

        if options.constructs.subscript
            && starts_exact_byte_run(input, index, b'~', 1)
            && !single_tilde_delete_takes_precedence(options, input, index)
        {
            if let Some(end) = find_simple_inline_close(input, index + 1, b'~') {
                flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                let inner = &input[index + 1..end];
                nodes.push(Inline::Subscript(Subscript {
                    meta: NodeMeta::new(Some(Span::new(
                        base_offset + index,
                        base_offset + end + 1,
                    ))),
                    children: parse_inlines_with_context(
                        inner,
                        base_offset + index + 1,
                        options,
                        definitions,
                        diagnostics,
                        context,
                    ),
                }));
                index = end + 1;
                text_start = index;
                continue;
            }
        }

        if options.constructs.inline_footnote
            && options.constructs.footnote_reference
            && bytes.get(index) == Some(&b'^')
            && bytes.get(index + 1) == Some(&b'[')
        {
            if let Some(close) = find_inline_footnote_end(input, index + 2) {
                let inner = &input[index + 2..close];
                if !inner.trim().is_empty() {
                    flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                    nodes.push(Inline::InlineFootnote(InlineFootnote {
                        meta: NodeMeta::new(Some(Span::new(
                            base_offset + index,
                            base_offset + close + 1,
                        ))),
                        children: parse_inlines_with_context(
                            inner,
                            base_offset + index + 2,
                            options,
                            definitions,
                            diagnostics,
                            context,
                        ),
                    }));
                    index = close + 1;
                    text_start = index;
                    continue;
                }
            }
        }

        if options.constructs.superscript
            && bytes.get(index) == Some(&b'^')
            && !(options.constructs.inline_footnote && bytes.get(index + 1) == Some(&b'['))
        {
            if let Some(end) = find_simple_inline_close(input, index + 1, b'^') {
                flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                let inner = &input[index + 1..end];
                nodes.push(Inline::Superscript(Superscript {
                    meta: NodeMeta::new(Some(Span::new(
                        base_offset + index,
                        base_offset + end + 1,
                    ))),
                    children: parse_inlines_with_context(
                        inner,
                        base_offset + index + 1,
                        options,
                        definitions,
                        diagnostics,
                        context,
                    ),
                }));
                index = end + 1;
                text_start = index;
                continue;
            }
        }

        // GFM strikethrough joins the shared CommonMark delimiter stack: a `~`
        // run is recorded as a candidate run (just like `*`/`_`) and paired into
        // `Delete` by `process_emphasis`, rather than scanned greedily here. Only
        // runs of length 1 (single-tilde mode) or 2 can ever form strikethrough;
        // runs of 3+ never do, so they fall through to literal text. The
        // subscript branch above already claimed single `~` runs it owns.
        if options.constructs.gfm_strikethrough
            && bytes[index] == b'~'
            && delimiter_byte_run_start(input, index, b'~') == index
        {
            let run_len = delimiter_byte_run_len(input, index, b'~');
            let recordable =
                run_len == 2 || (run_len == 1 && options.parse.single_tilde_strikethrough);
            if recordable {
                flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                record_emphasis_delimiter(
                    &mut nodes,
                    &mut delimiters,
                    input,
                    index,
                    base_offset,
                    b'~',
                    true,
                );
                index += run_len;
                text_start = index;
                continue;
            }
        }

        if bytes[index] == b'!' && index + 1 < bytes.len() && bytes[index + 1] == b'[' {
            if let Some((end, image)) =
                parse_image(input, index, base_offset, options, definitions, diagnostics)
            {
                flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                nodes.push(image);
                index = end;
                text_start = index;
                continue;
            }
        }

        if bytes[index] == b'[' {
            if let Some((end, wikilink)) = parse_wikilink(input, index, base_offset, options) {
                flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                nodes.push(wikilink);
                index = end;
                text_start = index;
                continue;
            }
            if let Some((end, link)) = parse_link(
                input,
                index,
                base_offset,
                options,
                definitions,
                diagnostics,
                context,
            ) {
                flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                nodes.push(link);
                index = end;
                text_start = index;
                continue;
            }
            if options.constructs.footnote_reference
                && bytes.get(index) == Some(&b'[')
                && bytes.get(index + 1) == Some(&b'^')
            {
                if let Some(close) = find_footnote_reference_label_end(input, index + 2) {
                    let label = &input[index + 2..close];
                    if is_footnote_label(label) {
                        flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                        nodes.push(Inline::FootnoteReference(FootnoteReference {
                            meta: NodeMeta::new(Some(Span::new(
                                base_offset + index,
                                base_offset + close + 1,
                            ))),
                            label: label.into(),
                            identifier: normalize_label(label),
                        }));
                        index = close + 1;
                        text_start = index;
                        continue;
                    }
                }
            }
        }

        if bytes[index] == b'$' && options.constructs.math_inline {
            if let Some((end, value, kind)) = parse_math_inline(input, index) {
                flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                nodes.push(Inline::Math(MathInline {
                    meta: NodeMeta::new(Some(Span::new(base_offset + index, base_offset + end))),
                    value,
                    kind,
                }));
                index = end;
                text_start = index;
                continue;
            }
            // A dollar run that opens but finds no exact-length close is emitted
            // as literal text in one piece (like a code-span). Skipping the
            // whole run prevents re-opening with a shorter marker inside it, so
            // `$$$foo$$` stays literal rather than matching `$$foo$$`. A lone
            // `$` before a backtick (the code-math form) is a run of 1, so this
            // still advances correctly when that form fails.
            let run = bytes[index..]
                .iter()
                .take_while(|byte| **byte == b'$')
                .count();
            if run > 1 {
                if text.is_empty() {
                    text_start = base_offset + index;
                }
                text.push_str(&input[index..index + run]);
                index += run;
                continue;
            }
        }

        // GFM bare autolinks must not fire inside an existing link's text
        // (no links in links) — `context.allow_links` is false in label scans.
        if (options.constructs.gfm_autolink_literal || options.constructs.relaxed_autolinks)
            && context.allow_links
        {
            if let Some((end, destination)) = parse_literal_autolink(
                input,
                index,
                options.constructs.gfm_autolink_literal,
                options.constructs.relaxed_autolinks,
            ) {
                flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                nodes.push(Inline::Autolink(Autolink {
                    meta: NodeMeta::new(Some(Span::new(base_offset + index, base_offset + end))),
                    destination,
                    kind: AutolinkKind::GfmLiteral {
                        original: input[index..end].into(),
                    },
                }));
                index = end;
                text_start = index;
                continue;
            }
        }

        if bytes[index] == b'<' {
            if let Some(end) = parse_autolink_end(input, index) {
                let raw = &input[index..end];
                if is_autolink(raw) {
                    flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                    if context.allow_links {
                        nodes.push(Inline::Autolink(Autolink {
                            meta: NodeMeta::new(Some(Span::new(
                                base_offset + index,
                                base_offset + end,
                            ))),
                            destination: raw[1..raw.len() - 1].into(),
                            kind: AutolinkKind::Angle,
                        }));
                    } else {
                        nodes.push(Inline::Text(Text {
                            meta: NodeMeta::new(Some(Span::new(
                                base_offset + index,
                                base_offset + end,
                            ))),
                            value: raw[1..raw.len() - 1].into(),
                        }));
                    }
                    index = end;
                    text_start = index;
                    continue;
                }
            }
            if options.constructs.mdx_jsx_inline {
                if let Some((end, raw)) = parse_mdx_jsx_inline(input, index) {
                    flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                    nodes.push(Inline::MdxJsx(MdxJsxInline {
                        meta: NodeMeta::new(Some(Span::new(
                            base_offset + index,
                            base_offset + end,
                        ))),
                        value: raw,
                    }));
                    index = end;
                    text_start = index;
                    continue;
                }
            }
            if let Some((end, raw)) = parse_html_inline(input, index) {
                if options.constructs.html_inline {
                    flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                    nodes.push(Inline::Html(HtmlInline {
                        meta: NodeMeta::new(Some(Span::new(
                            base_offset + index,
                            base_offset + end,
                        ))),
                        value: raw,
                    }));
                    index = end;
                    text_start = index;
                    continue;
                }
            }
        }

        if bytes[index] == b'{' && options.constructs.mdx_expression_inline {
            if let Some(end) = find_mdx_expression_inline_close(input, index) {
                flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                nodes.push(Inline::MdxExpression(MdxExpressionInline {
                    meta: NodeMeta::new(Some(Span::new(
                        base_offset + index,
                        base_offset + end + 1,
                    ))),
                    value: input[index + 1..end].into(),
                }));
                index = end + 1;
                text_start = index;
                continue;
            } else {
                diagnostics.push(Diagnostic::new(
                    DiagnosticSeverity::Error,
                    DiagnosticCode::InvalidMdx,
                    Span::new(base_offset + index, base_offset + input.len()),
                    "MDX expression is missing a closing brace",
                ));
            }
        }

        if bytes[index] == b':' && options.constructs.shortcode {
            if let Some((end, name)) = parse_shortcode(input, index) {
                flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                nodes.push(Inline::Shortcode(Shortcode {
                    meta: NodeMeta::new(Some(Span::new(base_offset + index, base_offset + end))),
                    name,
                }));
                index = end;
                text_start = index;
                continue;
            }
        }

        if bytes[index] == b':' && options.constructs.directive_text {
            if let Some((end, directive)) =
                parse_text_directive(input, index, base_offset, options, definitions, diagnostics)
            {
                flush_text(&mut nodes, &mut text, text_start, base_offset + index);
                nodes.push(directive);
                index = end;
                text_start = index;
                continue;
            }
        }

        let (next_index, char) = next_char(input, index).expect("valid UTF-8 byte index");
        if text.is_empty() {
            text_start = base_offset + index;
        }
        text.push(if char == '\0' { '\u{FFFD}' } else { char });
        index = next_index;
    }

    flush_text(&mut nodes, &mut text, text_start, base_offset + input.len());
    process_emphasis(nodes, delimiters)
}

fn parse_shortcode(input: &str, index: usize) -> Option<(usize, String)> {
    if input[index..].starts_with("::") {
        return None;
    }

    let mut cursor = index + 1;
    while let Some((next, char)) = next_char(input, cursor) {
        if char == ':' {
            if cursor == index + 1 {
                return None;
            }
            return Some((next, input[index + 1..cursor].into()));
        }
        if !(char.is_ascii_alphanumeric() || matches!(char, '_' | '-' | '+')) {
            return None;
        }
        cursor = next;
    }
    None
}

fn parse_wikilink(
    input: &str,
    index: usize,
    base_offset: usize,
    options: &SyntaxOptions,
) -> Option<(usize, Inline)> {
    let configured_order = if options.constructs.wikilink_title_after_pipe {
        WikiLinkLabelOrder::AfterPipe
    } else if options.constructs.wikilink_title_before_pipe {
        WikiLinkLabelOrder::BeforePipe
    } else {
        return None;
    };
    if input.as_bytes().get(index) != Some(&b'[') || input.as_bytes().get(index + 1) != Some(&b'[')
    {
        return None;
    }

    let close = find_wikilink_close(input, index + 2)?;
    let source = &input[index + 2..close];
    if source.is_empty() || source.len() > WIKILINK_MAX_BYTES {
        return None;
    }

    let (target_source, label_source, label_order) =
        if let Some(separator) = find_wikilink_separator(source) {
            match configured_order {
                WikiLinkLabelOrder::AfterPipe => (
                    &source[..separator],
                    &source[separator + 1..],
                    WikiLinkLabelOrder::AfterPipe,
                ),
                WikiLinkLabelOrder::BeforePipe => (
                    &source[separator + 1..],
                    &source[..separator],
                    WikiLinkLabelOrder::BeforePipe,
                ),
            }
        } else {
            (source, source, configured_order)
        };

    let target = unescape_string(target_source);
    if target.is_empty() {
        return None;
    }
    let label = unescape_string(label_source);
    let end = close + 2;
    Some((
        end,
        Inline::WikiLink(WikiLink {
            meta: NodeMeta::new(Some(Span::new(base_offset + index, base_offset + end))),
            target,
            label,
            label_order,
        }),
    ))
}

fn find_wikilink_close(input: &str, start: usize) -> Option<usize> {
    let bytes = input.as_bytes();
    let mut cursor = start;
    while cursor < input.len() {
        match bytes[cursor] {
            b'\\' => {
                cursor += 1;
                if cursor < input.len() {
                    cursor = next_char(input, cursor)?.0;
                }
            }
            b'\n' | b'\r' => return None,
            b']' if bytes.get(cursor + 1) == Some(&b']') => return Some(cursor),
            _ => cursor = next_char(input, cursor)?.0,
        }
    }
    None
}

fn find_wikilink_separator(input: &str) -> Option<usize> {
    let bytes = input.as_bytes();
    let mut cursor = 0;
    while cursor < input.len() {
        match bytes[cursor] {
            b'\\' => {
                cursor += 1;
                if cursor < input.len() {
                    cursor = next_char(input, cursor)?.0;
                }
            }
            b'|' => return Some(cursor),
            _ => cursor = next_char(input, cursor)?.0,
        }
    }
    None
}

fn trailing_space_count(input: &str) -> usize {
    input
        .as_bytes()
        .iter()
        .rev()
        .take_while(|byte| matches!(**byte, b' ' | b'\t'))
        .count()
}

fn is_hard_break_suffix(input: &str, trailing: usize) -> bool {
    // A hard line break is two or more spaces immediately before the newline
    // with no intervening tab; a tab anywhere in the trailing whitespace run
    // demotes it to a soft break.
    let bytes = input.as_bytes();
    trailing >= 2
        && bytes[bytes.len() - trailing..]
            .iter()
            .all(|byte| *byte == b' ')
}

fn parse_image(
    input: &str,
    index: usize,
    base_offset: usize,
    options: &SyntaxOptions,
    definitions: &[String],
    diagnostics: &mut Vec<Diagnostic>,
) -> Option<(usize, Inline)> {
    let label_start = index + 2;
    let label_end = find_link_label_end(input, index + 1)?;
    let alt_source = &input[label_start..label_end];
    let after_label = label_end + 1;
    if input.as_bytes().get(after_label) == Some(&b'(') {
        let (close, resource) = parse_link_resource(input, after_label)?;
        return Some((
            close,
            Inline::Image(Image {
                meta: NodeMeta::new(Some(Span::new(base_offset + index, base_offset + close))),
                destination: resource.destination,
                destination_kind: resource.destination_kind,
                title: resource.title,
                title_kind: resource.title_kind,
                alt: parse_inlines(
                    alt_source,
                    base_offset + label_start,
                    options,
                    definitions,
                    diagnostics,
                ),
            }),
        ));
    }
    if input.as_bytes().get(after_label) == Some(&b'[') {
        let close = find_reference_label_end(input, after_label)?;
        let label = &input[after_label + 1..close];
        let identifier = if label.is_empty() { alt_source } else { label };
        if definition_exists(definitions, identifier) {
            return Some((
                close + 1,
                Inline::ImageReference(ImageReference {
                    meta: NodeMeta::new(Some(Span::new(
                        base_offset + index,
                        base_offset + close + 1,
                    ))),
                    identifier: normalize_label(identifier),
                    label: identifier.into(),
                    kind: if label.is_empty() {
                        ReferenceKind::Collapsed
                    } else {
                        ReferenceKind::Full
                    },
                    alt: parse_inlines(
                        alt_source,
                        base_offset + label_start,
                        options,
                        definitions,
                        diagnostics,
                    ),
                }),
            ));
        }
        // A present `[...]` second label that resolves to no definition is not a
        // reference and does not fall back to a shortcut (mirrors parse_link).
        return None;
    }
    // Shortcut image reference `![foo]` (no following `(`/`[`) where `foo` is a
    // defined label — mirrors parse_link's shortcut branch.
    if definition_exists(definitions, alt_source) {
        return Some((
            after_label,
            Inline::ImageReference(ImageReference {
                meta: NodeMeta::new(Some(Span::new(
                    base_offset + index,
                    base_offset + after_label,
                ))),
                identifier: normalize_label(alt_source),
                label: alt_source.into(),
                kind: ReferenceKind::Shortcut,
                alt: parse_inlines(
                    alt_source,
                    base_offset + label_start,
                    options,
                    definitions,
                    diagnostics,
                ),
            }),
        ));
    }
    None
}

fn parse_link(
    input: &str,
    index: usize,
    base_offset: usize,
    options: &SyntaxOptions,
    definitions: &[String],
    diagnostics: &mut Vec<Diagnostic>,
    context: InlineContext,
) -> Option<(usize, Inline)> {
    if !context.allow_links {
        return None;
    }
    let label_end = find_link_label_end(input, index)?;
    let label_source = &input[index + 1..label_end];
    if label_contains_link(label_source, base_offset + index + 1, options, definitions) {
        return None;
    }
    let after_label = label_end + 1;
    if input.as_bytes().get(after_label) == Some(&b'(') {
        // A present-but-invalid `(...)` resource is not an inline link, but
        // CommonMark still resolves `[label]` as a shortcut reference and leaves
        // the invalid `(...)` as literal text (links 568) — so fall through to
        // the reference branches below instead of bailing out of parse_link.
        if let Some((close, resource)) = parse_link_resource(input, after_label) {
            return Some((
                close,
                Inline::Link(Link {
                    meta: NodeMeta::new(Some(Span::new(base_offset + index, base_offset + close))),
                    destination: resource.destination,
                    destination_kind: resource.destination_kind,
                    title: resource.title,
                    title_kind: resource.title_kind,
                    children: parse_inlines_with_context(
                        label_source,
                        base_offset + index + 1,
                        options,
                        definitions,
                        diagnostics,
                        InlineContext { allow_links: false },
                    ),
                }),
            ));
        }
    }
    if input.as_bytes().get(after_label) == Some(&b'[') {
        let close = find_reference_label_end(input, after_label)?;
        let label = &input[after_label + 1..close];
        let identifier = if label.is_empty() {
            label_source
        } else {
            label
        };
        if definition_exists(definitions, identifier) {
            return Some((
                close + 1,
                Inline::LinkReference(LinkReference {
                    meta: NodeMeta::new(Some(Span::new(
                        base_offset + index,
                        base_offset + close + 1,
                    ))),
                    identifier: normalize_label(identifier),
                    label: identifier.into(),
                    kind: if label.is_empty() {
                        ReferenceKind::Collapsed
                    } else {
                        ReferenceKind::Full
                    },
                    children: parse_inlines_with_context(
                        label_source,
                        base_offset + index + 1,
                        options,
                        definitions,
                        diagnostics,
                        InlineContext { allow_links: false },
                    ),
                }),
            ));
        }
        // A present `[...]` second label that resolves to no definition is NOT a
        // link, and CommonMark does not fall back to treating the first label as
        // a shortcut (`[x][ ]`, `[x][undef]` stay literal). Only a truly absent
        // `[...]` reaches the shortcut path below.
        return None;
    }
    if definition_exists(definitions, label_source) {
        return Some((
            after_label,
            Inline::LinkReference(LinkReference {
                meta: NodeMeta::new(Some(Span::new(
                    base_offset + index,
                    base_offset + after_label,
                ))),
                identifier: normalize_label(label_source),
                label: label_source.into(),
                kind: ReferenceKind::Shortcut,
                children: parse_inlines_with_context(
                    label_source,
                    base_offset + index + 1,
                    options,
                    definitions,
                    diagnostics,
                    InlineContext { allow_links: false },
                ),
            }),
        ));
    }
    None
}

fn find_reference_label_end(input: &str, open: usize) -> Option<usize> {
    // A reference/definition link label does not nest: it ends at the first
    // unescaped `]`, and an unescaped interior `[` disqualifies it.
    if input.as_bytes().get(open) != Some(&b'[') {
        return None;
    }

    let mut cursor = open + 1;
    while cursor < input.len() {
        let (next, char) = next_char(input, cursor)?;
        match char {
            '\\' => {
                cursor = next_char(input, next)
                    .map(|(after_escape, _)| after_escape)
                    .unwrap_or(next);
                continue;
            }
            '[' => return None,
            ']' => {
                return reference_label_is_within_limit(&input[open + 1..cursor]).then_some(cursor);
            }
            _ => {}
        }
        cursor = next;
    }
    None
}

fn label_contains_link(
    label_source: &str,
    base_offset: usize,
    options: &SyntaxOptions,
    definitions: &[String],
) -> bool {
    let mut diagnostics = Vec::new();
    let inlines = parse_inlines_with_context(
        label_source,
        base_offset,
        options,
        definitions,
        &mut diagnostics,
        InlineContext::default(),
    );
    contains_link_inline(&inlines)
}

fn contains_link_inline(inlines: &[Inline]) -> bool {
    inlines.iter().any(|inline| match inline {
        Inline::Link(_) | Inline::LinkReference(_) => true,
        Inline::Emphasis(node) => contains_link_inline(&node.children),
        Inline::Strong(node) => contains_link_inline(&node.children),
        Inline::Delete(node) => contains_link_inline(&node.children),
        Inline::TextDirective(node) => contains_link_inline(&node.label),
        _ => false,
    })
}

fn find_link_label_end(input: &str, open: usize) -> Option<usize> {
    if input.as_bytes().get(open) != Some(&b'[') {
        return None;
    }

    let mut depth = 1usize;
    let mut cursor = open + 1;
    while cursor < input.len() {
        let (next, char) = next_char(input, cursor)?;
        match char {
            '\\' => {
                cursor = next_char(input, next)
                    .map(|(after_escape, _)| after_escape)
                    .unwrap_or(next);
                continue;
            }
            '`' => {
                if let Some((end, _)) = parse_code_span(input, cursor) {
                    cursor = end;
                    continue;
                }
            }
            '<' => {
                if let Some(end) = parse_autolink_end(input, cursor) {
                    let raw = &input[cursor..end];
                    if is_autolink(raw) {
                        cursor = end;
                        continue;
                    }
                }
                if let Some((end, _)) = parse_html_inline(input, cursor) {
                    cursor = end;
                    continue;
                }
            }
            '[' => depth += 1,
            ']' => {
                depth = depth.checked_sub(1)?;
                if depth == 0 {
                    return Some(cursor);
                }
            }
            _ => {}
        }
        cursor = next;
    }
    None
}

fn parse_text_directive(
    input: &str,
    index: usize,
    base_offset: usize,
    options: &SyntaxOptions,
    definitions: &[String],
    diagnostics: &mut Vec<Diagnostic>,
) -> Option<(usize, Inline)> {
    if input[index..].starts_with("::") {
        return None;
    }
    if index > 0 {
        let previous = input[..index].chars().next_back()?;
        if !previous.is_whitespace() && !matches!(previous, '(' | '[' | '{') {
            return None;
        }
    }
    let opener_source = &input[index + 1..];
    let (name, label_source, attributes, consumed) = match parse_directive_opener(opener_source) {
        Some(opener) => opener,
        None => {
            if directive_opener_looks_malformed(opener_source) {
                diagnostics.push(Diagnostic::new(
                    DiagnosticSeverity::Error,
                    DiagnosticCode::InvalidDirectiveName,
                    Span::new(base_offset + index, base_offset + input.len()),
                    "text directive opener is malformed",
                ));
            }
            return None;
        }
    };
    let label = label_source
        .map(|source| {
            parse_inlines(
                source,
                base_offset + index + 1 + name.len() + 1,
                options,
                definitions,
                diagnostics,
            )
        })
        .unwrap_or_default();
    Some((
        index + 1 + consumed,
        Inline::TextDirective(TextDirective {
            meta: NodeMeta::new(Some(Span::new(
                base_offset + index,
                base_offset + index + 1 + consumed,
            ))),
            name,
            label,
            attributes,
        }),
    ))
}

fn parse_directive_opener(
    input: &str,
) -> Option<(String, Option<&str>, Vec<DirectiveAttribute>, usize)> {
    let mut index = 0;
    while let Some((next, char)) = next_char(input, index) {
        if char.is_ascii_alphanumeric() || char == '_' || char == '-' {
            index = next;
        } else {
            break;
        }
    }
    let name = &input[..index];
    if !is_directive_name(name) {
        return None;
    }

    let mut label = None;
    let mut attributes = Vec::new();
    let mut consumed = index;
    if input.as_bytes().get(consumed) == Some(&b'[') {
        let close = find_link_label_end(input, consumed)?;
        label = Some(&input[consumed + 1..close]);
        consumed = close + 1;
    }
    if input.as_bytes().get(consumed) == Some(&b'{') {
        let close = find_directive_attributes_close(input, consumed)?;
        attributes = parse_attributes(&input[consumed + 1..close]);
        consumed = close + 1;
    }

    Some((name.into(), label, attributes, consumed))
}

fn directive_opener_looks_malformed(input: &str) -> bool {
    let mut index = 0;
    while let Some((next, char)) = next_char(input, index) {
        if char.is_ascii_alphanumeric() || char == '_' || char == '-' {
            index = next;
        } else {
            break;
        }
    }
    index > 0
        && is_directive_name(&input[..index])
        && matches!(input.as_bytes().get(index), Some(b'[' | b'{'))
}

fn find_directive_attributes_close(input: &str, open: usize) -> Option<usize> {
    if input.as_bytes().get(open) != Some(&b'{') {
        return None;
    }

    let bytes = input.as_bytes();
    let mut cursor = open + 1;
    let mut quote = None;
    let mut escaped = false;
    while cursor < input.len() {
        let byte = bytes[cursor];
        if escaped {
            escaped = false;
            cursor += 1;
            continue;
        }
        if byte == b'\\' {
            escaped = true;
            cursor += 1;
            continue;
        }
        if let Some(delimiter) = quote {
            if byte == delimiter {
                quote = None;
            }
            cursor += 1;
            continue;
        }
        match byte {
            b'"' | b'\'' => quote = Some(byte),
            b'}' => return Some(cursor),
            _ => {}
        }
        cursor += 1;
    }
    None
}

fn parse_attributes(input: &str) -> Vec<DirectiveAttribute> {
    let mut attributes = Vec::new();
    let mut cursor = 0;
    while cursor < input.len() {
        cursor = skip_spaces(input, cursor);
        if cursor >= input.len() {
            break;
        }

        if input.as_bytes().get(cursor) == Some(&b'#') {
            let (id, next) = parse_attribute_token(input, cursor + 1);
            if !id.is_empty() {
                attributes.push(DirectiveAttribute {
                    name: "id".into(),
                    value: Some(id.into()),
                });
            }
            cursor = next;
            continue;
        }

        if input.as_bytes().get(cursor) == Some(&b'.') {
            let (class, next) = parse_attribute_token(input, cursor + 1);
            if !class.is_empty() {
                attributes.push(DirectiveAttribute {
                    name: "class".into(),
                    value: Some(class.into()),
                });
            }
            cursor = next;
            continue;
        }

        let (name, next) = parse_attribute_name(input, cursor);
        if name.is_empty() {
            break;
        }
        cursor = skip_spaces(input, next);
        if input.as_bytes().get(cursor) == Some(&b'=') {
            cursor = skip_spaces(input, cursor + 1);
            if let Some((value, next)) = parse_attribute_value(input, cursor) {
                attributes.push(DirectiveAttribute {
                    name: name.into(),
                    value: Some(value),
                });
                cursor = next;
            } else {
                attributes.push(DirectiveAttribute {
                    name: name.into(),
                    value: Some(String::new()),
                });
            }
        } else {
            attributes.push(DirectiveAttribute {
                name: name.into(),
                value: None,
            });
        }
    }
    attributes
}

fn parse_attribute_token(input: &str, index: usize) -> (&str, usize) {
    let mut cursor = index;
    while let Some((next, char)) = next_char(input, cursor) {
        if char.is_whitespace() {
            break;
        }
        cursor = next;
    }
    (&input[index..cursor], cursor)
}

fn parse_attribute_name(input: &str, index: usize) -> (&str, usize) {
    let mut cursor = index;
    while let Some((next, char)) = next_char(input, cursor) {
        if char.is_whitespace() || char == '=' {
            break;
        }
        cursor = next;
    }
    (&input[index..cursor], cursor)
}

fn parse_attribute_value(input: &str, index: usize) -> Option<(String, usize)> {
    let quote = input.as_bytes().get(index).copied();
    if matches!(quote, Some(b'"' | b'\'')) {
        let quote = quote?;
        let mut cursor = index + 1;
        while cursor < input.len() {
            let (next, char) = next_char(input, cursor)?;
            if char as u8 == quote && !is_escaped_at(input, cursor) {
                return Some((unescape_ascii_punctuation(&input[index + 1..cursor]), next));
            }
            cursor = next;
        }
        return None;
    }

    let (value, next) = parse_attribute_token(input, index);
    Some((
        unescape_selected(value, |char| matches!(char, '\\' | '&')),
        next,
    ))
}

struct CodeSpanSource {
    value: String,
    raw: String,
    fence_length: usize,
}

fn parse_code_span(input: &str, index: usize) -> Option<(usize, CodeSpanSource)> {
    let len = input[index..]
        .as_bytes()
        .iter()
        .take_while(|byte| **byte == b'`')
        .count();
    let search_start = index + len;
    let close = find_code_span_close(input, search_start, len)?;
    let raw = &input[search_start..close];
    Some((
        close + len,
        CodeSpanSource {
            value: normalize_code_span(raw),
            raw: raw.into(),
            fence_length: len,
        },
    ))
}

fn find_code_span_close(input: &str, start: usize, marker_len: usize) -> Option<usize> {
    let bytes = input.as_bytes();
    let mut cursor = start;
    while cursor < bytes.len() {
        if bytes[cursor] != b'`' {
            cursor = next_char(input, cursor)
                .map(|(next, _)| next)
                .unwrap_or(bytes.len());
            continue;
        }
        let run_len = bytes[cursor..]
            .iter()
            .take_while(|byte| **byte == b'`')
            .count();
        if run_len == marker_len {
            return Some(cursor);
        }
        cursor += run_len;
    }
    None
}

fn normalize_code_span(input: &str) -> String {
    let mut normalized = String::new();
    let mut cursor = 0;
    while cursor < input.len() {
        let (next, char) = next_char(input, cursor).expect("valid UTF-8 byte index");
        if char == '\r' {
            if input.as_bytes().get(next) == Some(&b'\n') {
                cursor = next + 1;
            } else {
                cursor = next;
            }
            normalized.push(' ');
            continue;
        }
        if char == '\n' {
            normalized.push(' ');
            cursor = next;
            continue;
        }
        normalized.push(char);
        cursor = next;
    }

    if normalized.starts_with(' ')
        && normalized.ends_with(' ')
        && normalized.chars().any(|char| char != ' ')
    {
        normalized[1..normalized.len() - 1].into()
    } else {
        normalized
    }
}

fn can_open_delimited(input: &str, index: usize, marker_len: usize) -> bool {
    delimiter_flanking(input, index, marker_len).left
}

fn can_close_delimited(input: &str, index: usize, marker_len: usize) -> bool {
    delimiter_flanking(input, index, marker_len).right
}

fn find_closing_delimiter(
    input: &str,
    start: usize,
    marker: &str,
    underscore: bool,
) -> Option<usize> {
    let marker_len = marker.len();
    let mut cursor = start;
    let mut nested = 0usize;
    while cursor <= input.len() {
        let candidate = input[cursor..].find(marker).map(|offset| cursor + offset)?;
        if is_escaped_at(input, candidate) {
            cursor = candidate + marker_len;
            continue;
        }
        if delimiter_candidate_precedes_link_close(input, start, candidate, marker_len) {
            cursor = candidate + marker_len;
            continue;
        }
        if marker_len == 1 && nested == 0 && starts_longer_delimiter_run(input, candidate, marker) {
            cursor = candidate + delimiter_run_len(input, candidate, marker);
            continue;
        }

        let can_open = if underscore {
            can_open_underscore(input, candidate, marker_len)
        } else {
            can_open_delimited(input, candidate, marker_len)
        };
        let can_close = if underscore {
            can_close_underscore(input, candidate, marker_len)
        } else {
            can_close_delimited(input, candidate, marker_len)
        };

        if can_close {
            if nested == 0 {
                return Some(candidate);
            }
            nested -= 1;
            cursor = candidate + marker_len;
            continue;
        }
        if can_open {
            nested += 1;
        }
        cursor = candidate + marker_len;
    }
    None
}

fn find_single_tilde_delete_close(input: &str, start: usize) -> Option<usize> {
    let mut cursor = start;
    while cursor < input.len() {
        let Some(candidate) = input[cursor..].find('~').map(|index| cursor + index) else {
            break;
        };
        if !is_escaped_at(input, candidate) && single_tilde_can_close_delete(input, candidate) {
            return Some(candidate);
        }
        cursor = candidate + 1;
    }
    None
}

fn single_tilde_can_open_delete(input: &str, index: usize) -> bool {
    starts_exact_byte_run(input, index, b'~', 1)
        && can_open_delimited(input, index, 1)
        && !tilde_is_alphanumeric_interior(input, index)
}

fn single_tilde_can_close_delete(input: &str, index: usize) -> bool {
    starts_exact_byte_run(input, index, b'~', 1)
        && can_close_delimited(input, index, 1)
        && !tilde_is_alphanumeric_interior(input, index)
}

fn single_tilde_delete_takes_precedence(
    options: &SyntaxOptions,
    input: &str,
    index: usize,
) -> bool {
    options.constructs.gfm_strikethrough
        && options.parse.single_tilde_strikethrough
        && single_tilde_can_open_delete(input, index)
        && find_single_tilde_delete_close(input, index + 1).is_some()
}

fn tilde_is_alphanumeric_interior(input: &str, index: usize) -> bool {
    let previous = input[..index].chars().next_back();
    let next = input[index + 1..].chars().next();
    previous.is_some_and(|char| char.is_alphanumeric())
        && next.is_some_and(|char| char.is_alphanumeric())
}

fn starts_exact_byte_run(input: &str, index: usize, marker: u8, len: usize) -> bool {
    input.as_bytes().get(index) == Some(&marker)
        && delimiter_byte_run_start(input, index, marker) == index
        && delimiter_byte_run_len(input, index, marker) == len
}

fn delimiter_byte_run_start(input: &str, index: usize, marker: u8) -> usize {
    let bytes = input.as_bytes();
    let mut start = index;
    while start > 0 && bytes[start - 1] == marker && !is_escaped_at(input, start - 1) {
        start -= 1;
    }
    start
}

fn delimiter_byte_run_len(input: &str, index: usize, marker: u8) -> usize {
    let bytes = input.as_bytes();
    let mut cursor = index;
    while bytes.get(cursor) == Some(&marker) {
        cursor += 1;
    }
    cursor - index
}

fn find_simple_inline_close(input: &str, start: usize, marker: u8) -> Option<usize> {
    let bytes = input.as_bytes();
    let mut cursor = start;
    while cursor < input.len() {
        match bytes[cursor] {
            b'\\' => {
                cursor += 1;
                if cursor < input.len() {
                    cursor = next_char(input, cursor)?.0;
                }
            }
            b'\n' | b'\r' => return None,
            byte if byte == marker => return (cursor > start).then_some(cursor),
            _ => cursor = next_char(input, cursor)?.0,
        }
    }
    None
}

fn find_spoiler_close(input: &str, start: usize) -> Option<usize> {
    let bytes = input.as_bytes();
    let mut cursor = start;
    while cursor + 1 < input.len() {
        match bytes[cursor] {
            b'\\' => {
                cursor += 1;
                if cursor < input.len() {
                    cursor = next_char(input, cursor)?.0;
                }
            }
            b'\n' | b'\r' => return None,
            b'|' if bytes.get(cursor + 1) == Some(&b'|')
                && cursor > start
                && bytes.get(cursor.wrapping_sub(1)) != Some(&b'|') =>
            {
                return Some(cursor);
            }
            _ => cursor = next_char(input, cursor)?.0,
        }
    }
    None
}

fn starts_longer_delimiter_run(input: &str, index: usize, marker: &str) -> bool {
    input[index..].starts_with(marker)
        && !input[..index].ends_with(marker)
        && input[index + marker.len()..].starts_with(marker)
}

fn delimiter_run_len(input: &str, index: usize, marker: &str) -> usize {
    let mut cursor = index;
    while input[cursor..].starts_with(marker) {
        cursor += marker.len();
    }
    cursor - index
}

fn delimiter_candidate_precedes_link_close(
    input: &str,
    start: usize,
    candidate: usize,
    marker_len: usize,
) -> bool {
    let bytes = input.as_bytes();
    if bytes.get(candidate + marker_len) != Some(&b']') {
        return false;
    }
    if !matches!(bytes.get(candidate + marker_len + 1), Some(b'(' | b'[')) {
        return false;
    }

    let mut depth = 0usize;
    let mut cursor = start;
    while cursor < candidate {
        let Some((next, char)) = next_char(input, cursor) else {
            break;
        };
        match char {
            '\\' => {
                cursor = next_char(input, next)
                    .map(|(after_escape, _)| after_escape)
                    .unwrap_or(next);
                continue;
            }
            '`' => {
                if let Some((end, _)) = parse_code_span(input, cursor) {
                    cursor = end;
                    continue;
                }
            }
            '[' => depth += 1,
            ']' => depth = depth.saturating_sub(1),
            _ => {}
        }
        cursor = next;
    }
    depth > 0
}

fn can_open_underscore(input: &str, index: usize, marker_len: usize) -> bool {
    let flanking = delimiter_flanking(input, index, marker_len);
    flanking.left
        && (!flanking.right || flanking.previous.is_some_and(|c| c.is_ascii_punctuation()))
}

fn can_close_underscore(input: &str, index: usize, marker_len: usize) -> bool {
    let flanking = delimiter_flanking(input, index, marker_len);
    flanking.right && (!flanking.left || flanking.next.is_some_and(|c| c.is_ascii_punctuation()))
}

#[derive(Clone, Copy)]
struct DelimiterFlanking {
    left: bool,
    right: bool,
    previous: Option<char>,
    next: Option<char>,
}

fn delimiter_flanking(input: &str, index: usize, marker_len: usize) -> DelimiterFlanking {
    let previous = input[..index].chars().next_back();
    let next = input[index + marker_len..].chars().next();

    let previous_whitespace = previous.is_none_or(char::is_whitespace);
    let next_whitespace = next.is_none_or(char::is_whitespace);
    let previous_punctuation = previous.is_some_and(is_flanking_punctuation);
    let next_punctuation = next.is_some_and(is_flanking_punctuation);

    let left = next.is_some()
        && !next_whitespace
        && !(next_punctuation && !previous_whitespace && !previous_punctuation);
    let right = previous.is_some()
        && !previous_whitespace
        && !(previous_punctuation && !next_whitespace && !next_punctuation);

    DelimiterFlanking {
        left,
        right,
        previous,
        next,
    }
}

/// Dollar-fenced inline math, GitHub Flavored Markdown dialect.
///
/// A `$` is a flanking delimiter resolved at scan time (math is not pushed onto
/// the emphasis delimiter stack). An opening run of one or two `$` (runs of
/// three or more never form math) scans forward for a matching closing run:
///
/// * single `$`: cannot open if the next char is ASCII whitespace; the closing
///   `$` cannot be preceded by ASCII whitespace nor followed by an ASCII digit;
///   a `\$` inside is skipped (the backslash is kept verbatim, never a
///   delimiter); the close must be a run of exactly one `$`.
/// * double `$$`: no flanking and no digit guard; closes on the next run of two
///   `$`; content is kept verbatim and may span newlines (this is still an
///   inline display span — `$$` flow blocks are handled by `parse_math_block`).
///
/// The closing run is matched greedily (the nearest valid close wins), which is
/// equivalent to emphasis-style "nearest preceding open" because a failed open
/// emits a literal `$`/`$$` and the scan resumes after it. Content for the
/// single-`$` form is normalized like a code span (line endings → spaces, one
/// edge-space strip); the `$$` display form is verbatim. The `` $`…`$ `` code
/// form takes precedence.
fn parse_math_inline(input: &str, index: usize) -> Option<(usize, String, MathInlineKind)> {
    if let Some((end, value)) = parse_math_code_inline(input, index) {
        return Some((end, value, MathInlineKind::Code));
    }

    let bytes = input.as_bytes();
    let open_dollars = bytes[index..]
        .iter()
        .take_while(|byte| **byte == b'$')
        .count();
    // The maximum math fence length is 2 dollars: a run of three or more never
    // opens math.
    if open_dollars == 0 || open_dollars > 2 {
        return None;
    }

    let content_start = index + open_dollars;
    let close = scan_to_closing_dollar(input, content_start, open_dollars)?;
    let content_end = close - open_dollars;
    // The span requires `endpos - startpos >= fence_length * 2 + 1`, i.e. at
    // least one content byte between the open and close fences.
    if content_end <= content_start {
        return None;
    }

    let raw = &input[content_start..content_end];
    let value = if open_dollars == 1 {
        normalize_math_text(raw)
    } else {
        raw.into()
    };
    let dollars = u8::try_from(open_dollars).unwrap_or(u8::MAX);
    Some((close, value, MathInlineKind::Dollar { dollars }))
}

/// Scans for the closing dollar run. `start` is the first content byte
/// (just past the opening run); returns the byte offset just past a matching
/// closing run of exactly `open_dollars` `$`.
fn scan_to_closing_dollar(input: &str, start: usize, open_dollars: usize) -> Option<usize> {
    let bytes = input.as_bytes();
    // A space immediately after a single opening `$` forbids the open.
    if open_dollars == 1 && bytes.get(start).is_some_and(|byte| is_math_space(*byte)) {
        return None;
    }

    let mut cursor = start;
    loop {
        while cursor < bytes.len() && bytes[cursor] != b'$' {
            cursor += 1;
        }
        if cursor >= bytes.len() {
            return None;
        }
        // `cursor` now points at the first `$` of a potential closing run; the
        // char just before it gates the single-`$` flanking and escape rules.
        let prev = bytes[cursor - 1];
        if open_dollars == 1 && is_math_space(prev) {
            return None;
        }
        if open_dollars == 1 && prev == b'\\' {
            // An escaped `\$` is content, not a delimiter: skip this one `$` and
            // keep scanning (the backslash stays in the content verbatim).
            cursor += 1;
            continue;
        }
        let run = bytes[cursor..]
            .iter()
            .take(open_dollars)
            .take_while(|byte| **byte == b'$')
            .count();
        // The single-`$` close cannot be followed by an ASCII digit.
        if open_dollars == 1 && bytes.get(cursor + run).is_some_and(u8::is_ascii_digit) {
            return None;
        }
        if run == open_dollars {
            return Some(cursor + run);
        }
        cursor += run;
    }
}

/// Math whitespace: ASCII tab, line feed, carriage return, and space.
fn is_math_space(byte: u8) -> bool {
    matches!(byte, b'\t' | b'\n' | b'\r' | b' ')
}

/// Applies the code-span content rules to dollar-fenced math: line endings
/// become single spaces, then if the content begins AND ends with U+0020 and is
/// not entirely spaces, one space is stripped from each edge.
fn normalize_math_text(input: &str) -> String {
    let mut normalized = String::new();
    let mut cursor = 0;
    while cursor < input.len() {
        let (next, char) = next_char(input, cursor).expect("valid UTF-8 byte index");
        if char == '\r' {
            if input.as_bytes().get(next) == Some(&b'\n') {
                cursor = next + 1;
            } else {
                cursor = next;
            }
            normalized.push(' ');
            continue;
        }
        if char == '\n' {
            normalized.push(' ');
            cursor = next;
            continue;
        }
        normalized.push(char);
        cursor = next;
    }

    if normalized.starts_with(' ')
        && normalized.ends_with(' ')
        && normalized.chars().any(|char| char != ' ')
    {
        normalized[1..normalized.len() - 1].into()
    } else {
        normalized
    }
}

fn parse_math_code_inline(input: &str, index: usize) -> Option<(usize, String)> {
    if !input[index..].starts_with("$`") {
        return None;
    }

    let search_start = index + 2;
    let close = input[search_start..]
        .find("`$")
        .map(|offset| search_start + offset)?;
    if close == search_start {
        return None;
    }

    Some((close + 2, input[search_start..close].into()))
}

fn parse_link_resource(input: &str, open: usize) -> Option<(usize, ParsedLinkResource)> {
    let bytes = input.as_bytes();
    if bytes.get(open) != Some(&b'(') {
        return None;
    }
    let (mut cursor, initial_space) = skip_link_resource_space_with_info(input, open + 1)?;
    if bytes.get(cursor) == Some(&b')') {
        return Some((
            cursor + 1,
            ParsedLinkResource {
                destination: String::new(),
                destination_kind: LinkDestinationKind::Omitted,
                title: None,
                title_kind: None,
            },
        ));
    }
    if initial_space && matches!(bytes.get(cursor), Some(b'"' | b'\'' | b'(')) {
        let (title, title_kind, next) = parse_link_title(input, cursor)?;
        cursor = skip_link_resource_space(input, next)?;
        if bytes.get(cursor) == Some(&b')') {
            return Some((
                cursor + 1,
                ParsedLinkResource {
                    destination: String::new(),
                    destination_kind: LinkDestinationKind::Omitted,
                    title: Some(title),
                    title_kind: Some(title_kind),
                },
            ));
        }
        return None;
    }
    let (destination, destination_kind, next) = parse_link_destination(input, cursor)?;
    let (after_destination, had_space) = skip_link_resource_space_with_info(input, next)?;
    cursor = after_destination;
    if bytes.get(cursor) == Some(&b')') {
        return Some((
            cursor + 1,
            ParsedLinkResource {
                destination,
                destination_kind,
                title: None,
                title_kind: None,
            },
        ));
    }
    if !had_space {
        return None;
    }

    let (title, title_kind, next) = parse_link_title(input, cursor)?;
    cursor = skip_link_resource_space(input, next)?;
    if bytes.get(cursor) == Some(&b')') {
        Some((
            cursor + 1,
            ParsedLinkResource {
                destination,
                destination_kind,
                title: Some(title),
                title_kind: Some(title_kind),
            },
        ))
    } else {
        None
    }
}

fn parse_link_destination(
    input: &str,
    index: usize,
) -> Option<(String, LinkDestinationKind, usize)> {
    if input.as_bytes().get(index) == Some(&b'<') {
        let mut cursor = index + 1;
        while cursor < input.len() {
            let (next, char) = next_char(input, cursor)?;
            if char == '>' && !is_escaped_at(input, cursor) {
                return Some((
                    unescape_ascii_punctuation(&input[index + 1..cursor]),
                    LinkDestinationKind::Angle,
                    next,
                ));
            }
            if (char == '<' && !is_escaped_at(input, cursor)) || char == '\n' || char == '\r' {
                return None;
            }
            cursor = next;
        }
        return None;
    }

    let mut cursor = index;
    let mut depth = 0usize;
    while cursor < input.len() {
        let (next, char) = next_char(input, cursor)?;
        // A bare destination terminates on ASCII space or an ASCII control
        // character; Unicode whitespace (e.g. U+00A0) is ordinary. A backslash
        // before a space is NOT an escape (only ASCII punctuation is escapable),
        // so `\ ` still terminates the destination → `[a](\ b)` is not a link.
        if (char == ' ' || char.is_ascii_control()) && depth == 0 {
            break;
        }
        if char == '(' && !is_escaped_at(input, cursor) {
            depth += 1;
            // CommonMark caps balanced parens in a bare destination at depth 32.
            if depth > 32 {
                return None;
            }
        } else if char == ')' && !is_escaped_at(input, cursor) {
            if depth == 0 {
                break;
            }
            depth -= 1;
        }
        cursor = next;
    }

    if cursor == index || depth > 0 {
        None
    } else {
        Some((
            unescape_ascii_punctuation(&input[index..cursor]),
            LinkDestinationKind::Bare,
            cursor,
        ))
    }
}

fn parse_link_title(input: &str, index: usize) -> Option<(String, LinkTitleKind, usize)> {
    let opener = input.as_bytes().get(index).copied()?;
    let (closer, title_kind) = match opener {
        b'"' => ('"', LinkTitleKind::DoubleQuote),
        b'\'' => ('\'', LinkTitleKind::SingleQuote),
        b'(' => (')', LinkTitleKind::Paren),
        _ => return None,
    };
    let mut cursor = index + 1;
    while cursor < input.len() {
        let (next, char) = next_char(input, cursor)?;
        if char == closer && !is_escaped_at(input, cursor) {
            if contains_blank_line(&input[index + 1..cursor]) {
                return None;
            }
            return Some((
                unescape_ascii_punctuation(&input[index + 1..cursor]),
                title_kind,
                next,
            ));
        }
        if opener == b'(' && char == '(' && !is_escaped_at(input, cursor) {
            return None;
        }
        cursor = next;
    }
    None
}

fn contains_blank_line(input: &str) -> bool {
    if !input.bytes().any(|byte| matches!(byte, b'\n' | b'\r')) {
        return false;
    }
    // A title that merely begins or ends with an EOL is allowed; only an INTERIOR
    // blank line (a blank line bounded by content on both sides) is rejected. The
    // empty first/last line entries that a leading/trailing newline produces are
    // boundary artifacts, not blank lines in the title content.
    let lines = collect_lines(input, 0);
    let interior = lines.len().saturating_sub(1);
    lines
        .iter()
        .take(interior)
        .skip(1)
        .any(|line| line.text.trim().is_empty())
}

fn skip_link_resource_space(input: &str, index: usize) -> Option<usize> {
    skip_link_resource_space_with_info(input, index).map(|(index, _)| index)
}

fn skip_link_resource_space_with_info(input: &str, mut index: usize) -> Option<(usize, bool)> {
    let mut line_breaks = 0usize;
    let mut had_space = false;
    while input
        .as_bytes()
        .get(index)
        .is_some_and(|byte| matches!(*byte, b' ' | b'\t' | b'\n' | b'\r'))
    {
        had_space = true;
        match input.as_bytes()[index] {
            b'\n' => {
                line_breaks += 1;
                if line_breaks > 1 {
                    return None;
                }
                index += 1;
            }
            b'\r' => {
                line_breaks += 1;
                if line_breaks > 1 {
                    return None;
                }
                if input.as_bytes().get(index + 1) == Some(&b'\n') {
                    index += 2;
                } else {
                    index += 1;
                }
            }
            _ => index += 1,
        }
    }
    Some((index, had_space))
}

pub(crate) fn parse_character_reference(input: &str, index: usize) -> Option<(usize, String)> {
    let rest = input.get(index..)?;
    if let Some(rest) = rest
        .strip_prefix("&#x")
        .or_else(|| rest.strip_prefix("&#X"))
    {
        let digits = rest.find(';')?;
        if digits == 0 || digits > 6 || !rest[..digits].bytes().all(|byte| byte.is_ascii_hexdigit())
        {
            return None;
        }
        let value = u32::from_str_radix(&rest[..digits], 16).ok()?;
        return Some((
            index + 3 + digits + 1,
            character_reference_value(value).into(),
        ));
    }
    if let Some(rest) = rest.strip_prefix("&#") {
        let digits = rest.find(';')?;
        if digits == 0 || digits > 7 || !rest[..digits].bytes().all(|byte| byte.is_ascii_digit()) {
            return None;
        }
        let value = rest[..digits].parse::<u32>().ok()?;
        return Some((
            index + 2 + digits + 1,
            character_reference_value(value).into(),
        ));
    }

    let name_end = rest.find(';')?;
    if name_end == 0 || name_end > 32 {
        return None;
    }
    let name = &rest[1..name_end];
    named_character_reference(name).map(|value| (index + name_end + 1, value.into()))
}

/// Decode a numeric character reference codepoint to its scalar value.
///
/// This follows the CommonMark reference behavior: `U+0000`, the UTF-16
/// surrogate range, and codepoints beyond the Unicode scalar range decode to
/// `U+FFFD`; every other codepoint decodes to itself.
///
/// Two deliberate non-behaviors:
/// - We do NOT apply the HTML5 Windows-1252 remapping of C1 bytes; `&#128;`
///   decodes to `U+0080`, not the Euro sign. The CommonMark reference does not
///   perform that remapping.
/// - We do NOT extend replacement to the C0/C1 controls, DEL, or the Unicode
///   noncharacters the way some HTML-oriented decoders do. Keeping those as
///   their literal scalar is what makes the serializer's `&#xNN;` escaping of
///   control characters round-trip through a re-parse. The roundtrip corpus
///   only pins `{0 -> FFFD, 9 -> tab, 10 -> line feed, surrogate -> FFFD,
///   out-of-range -> FFFD}`, all of which this matches.
pub(crate) fn character_reference_value(value: u32) -> char {
    if value == 0 {
        '\u{FFFD}'
    } else {
        char::from_u32(value).unwrap_or('\u{FFFD}')
    }
}

pub(crate) fn is_escaped_at(input: &str, index: usize) -> bool {
    let bytes = input.as_bytes();
    let mut cursor = index;
    let mut count = 0;
    while cursor > 0 && bytes[cursor - 1] == b'\\' {
        count += 1;
        cursor -= 1;
    }
    count % 2 == 1
}

fn parse_definition_destination_title(input: &str) -> Option<ParsedLinkResource> {
    let (mut cursor, _) = skip_link_resource_space_with_info(input, 0)?;
    let (destination, destination_kind, next) = parse_link_destination(input, cursor)?;
    cursor = next;

    let (next, had_space) = skip_link_resource_space_with_info(input, cursor)?;
    cursor = next;
    if cursor >= input.len() {
        return Some(ParsedLinkResource {
            destination,
            destination_kind,
            title: None,
            title_kind: None,
        });
    }
    if !had_space {
        return None;
    }

    let (title, title_kind, next) = parse_link_title(input, cursor)?;
    let after_title = skip_link_resource_space(input, next)?;
    (after_title == input.len()).then_some(ParsedLinkResource {
        destination,
        destination_kind,
        title: Some(title),
        title_kind: Some(title_kind),
    })
}

fn line_can_start_definition_title(input: &str) -> bool {
    let trimmed = input.trim_start();
    matches!(trimmed.as_bytes().first(), Some(b'"' | b'\'' | b'('))
}

fn unescape_ascii_punctuation(input: &str) -> String {
    // Only ASCII punctuation is escapable (`\ ` keeps its backslash).
    unescape_selected(input, |char| char.is_ascii_punctuation())
}

fn unescape_string(input: &str) -> String {
    unescape_selected(input, |char| char.is_ascii_punctuation() || char == '&')
}

fn unescape_selected(input: &str, should_unescape: impl Fn(char) -> bool) -> String {
    let mut output = String::new();
    let mut cursor = 0;
    while cursor < input.len() {
        if input.as_bytes().get(cursor) == Some(&b'&') {
            if let Some((end, value)) = parse_character_reference(input, cursor) {
                output.push_str(&value);
                cursor = end;
                continue;
            }
        }
        let (next, char) = next_char(input, cursor).expect("valid UTF-8 byte index");
        if char == '\\' {
            if let Some((after_escape, escaped)) = next_char(input, next) {
                if should_unescape(escaped) {
                    output.push(escaped);
                } else {
                    output.push(char);
                    output.push(escaped);
                }
                cursor = after_escape;
            } else {
                output.push(char);
                cursor = next;
            }
        } else {
            output.push(if char == '\0' { '\u{FFFD}' } else { char });
            cursor = next;
        }
    }
    output
}

fn push_line(output: &mut String, line: &str) {
    if !output.is_empty() {
        output.push('\n');
    }
    output.push_str(line);
}

fn ensure_line_separator(output: &mut String) {
    if !output.is_empty() && !ends_with_line_ending(output) {
        output.push('\n');
    }
}

fn ends_with_line_ending(input: &str) -> bool {
    input.ends_with('\n') || input.ends_with('\r')
}

fn flush_text(nodes: &mut Vec<Inline>, text: &mut String, text_start: usize, end: usize) {
    if !text.is_empty() {
        nodes.push(Inline::Text(Text {
            meta: NodeMeta::new(Some(Span::new(text_start, end))),
            value: core::mem::take(text),
        }));
    }
}

fn gfm_link_label_preserves_url_dot_escape(
    text: &str,
    escaped: char,
    options: &SyntaxOptions,
    context: InlineContext,
) -> bool {
    escaped == '.'
        && !context.allow_links
        && options.constructs.gfm_autolink_literal
        && (text.starts_with("www.") || text.starts_with("http://") || text.starts_with("https://"))
}

fn next_char(input: &str, index: usize) -> Option<(usize, char)> {
    let char = input[index..].chars().next()?;
    Some((index + char.len_utf8(), char))
}

/// A CommonMark "Unicode punctuation character" for emphasis/strong flanking:
/// ASCII punctuation plus the non-ASCII Unicode `P*`/`S*` categories. Only the
/// flanking classification needs the Unicode set; escape/label logic stays
/// ASCII-only via `char::is_ascii_punctuation`.
fn is_flanking_punctuation(value: char) -> bool {
    value.is_ascii_punctuation() || crate::unicode_punctuation::is_unicode_punctuation(value)
}

/// Fold a reference label to its matching identifier. Per CommonMark, two
/// labels match when their RAW source (no backslash unescape, no entity decode)
/// agrees after collapsing internal whitespace to a single space, trimming, and
/// Unicode case-folding (`to_uppercase()` then `to_lowercase()`). So `[foo\!]`
/// does NOT match `[foo!]`, and `[&copy;]` does NOT match `[©]`.
///
/// The serializer's `normalize_reference_label` delegates here so the
/// Shortcut/Collapsed omission oracle stays in lockstep with this matcher.
pub(crate) fn normalize_label(label: &str) -> String {
    label
        // Unicode full casefold maps capital sharp S (ẞ, U+1E9E) to "ss"; Rust's
        // `to_uppercase` leaves it unchanged (it is already uppercase), so without
        // this `[ẞ]` would not match a `[SS]: …` definition (links 540). This is
        // the only char where `to_uppercase().to_lowercase()` diverges from the
        // full casefold that matters for label matching.
        .replace('ẞ', "ss")
        .split_whitespace()
        .collect::<Vec<_>>()
        .join(" ")
        .to_uppercase()
        .to_lowercase()
}

fn definition_exists(definitions: &[String], label: &str) -> bool {
    if label.is_empty() || !reference_label_is_within_limit(label) {
        return false;
    }

    let identifier = normalize_label(label);
    definitions
        .iter()
        .any(|definition| definition == &identifier)
}

fn reference_label_is_within_limit(label: &str) -> bool {
    label.chars().take(REFERENCE_LABEL_MAX_CHARS + 1).count() <= REFERENCE_LABEL_MAX_CHARS
}

fn trim_up_to_three_spaces(input: &str) -> Option<&str> {
    let (columns, bytes) = leading_indent(input);
    if columns <= 3 {
        Some(&input[bytes..])
    } else {
        None
    }
}

fn fence_start(input: &str) -> Option<(FenceMarker, usize)> {
    let marker = match input.as_bytes().first()? {
        b'`' => FenceMarker::Backtick,
        b'~' => FenceMarker::Tilde,
        _ => return None,
    };
    let byte = match marker {
        FenceMarker::Backtick => b'`',
        FenceMarker::Tilde => b'~',
    };
    let length = input
        .as_bytes()
        .iter()
        .take_while(|item| **item == byte)
        .count();
    if length >= 3 {
        Some((marker, length))
    } else {
        None
    }
}

fn fence_close(input: &str, marker: FenceMarker, length: usize) -> bool {
    let byte = match marker {
        FenceMarker::Backtick => b'`',
        FenceMarker::Tilde => b'~',
    };
    let count = input
        .as_bytes()
        .iter()
        .take_while(|item| **item == byte)
        .count();
    count >= length && input[count..].trim().is_empty()
}

fn trim_closing_hashes(input: &str) -> &str {
    let input = input.trim_end();
    let hash_start = input.trim_end_matches('#').len();
    if hash_start == input.len() {
        return input;
    }
    if hash_start == 0 {
        return "";
    }

    let before = &input[..hash_start];
    if before.ends_with(' ') || before.ends_with('\t') {
        before.trim_end()
    } else {
        input
    }
}

fn list_marker_info(input: &str) -> Option<ListMarkerInfo<'_>> {
    let trimmed = trim_up_to_three_spaces(input)?;
    let indent = input.len() - trimmed.len();
    let bytes = trimmed.as_bytes();
    match bytes.first()? {
        b'-' | b'*' | b'+' if is_list_padding_byte(bytes.get(1).copied()) => {
            let delimiter = match bytes[0] {
                b'-' => ListDelimiter::Dash,
                b'*' => ListDelimiter::Asterisk,
                _ => ListDelimiter::Plus,
            };
            let (content_offset, content_indent) = list_content_offset(trimmed, 1, indent);
            Some(ListMarkerInfo {
                ordered: false,
                start: None,
                delimiter,
                indent,
                marker_len: 1,
                content_indent,
                content: &trimmed[content_offset..],
            })
        }
        byte if byte.is_ascii_digit() => {
            let mut end = 0;
            while bytes.get(end).is_some_and(|byte| byte.is_ascii_digit()) {
                end += 1;
            }
            if end > 9 {
                return None;
            }
            let delimiter = match bytes.get(end)? {
                b'.' => ListDelimiter::Period,
                b')' => ListDelimiter::Paren,
                _ => return None,
            };
            if !is_list_padding_byte(bytes.get(end + 1).copied()) {
                return None;
            }
            let start = trimmed[..end].parse().ok()?;
            let marker_len = end + 1;
            let (content_offset, content_indent) = list_content_offset(trimmed, marker_len, indent);
            Some(ListMarkerInfo {
                ordered: true,
                start: Some(start),
                delimiter,
                indent,
                marker_len,
                content_indent,
                content: &trimmed[content_offset..],
            })
        }
        _ => None,
    }
}

fn list_content_offset(input: &str, marker_len: usize, indent: usize) -> (usize, usize) {
    let bytes = input.as_bytes();
    if bytes.get(marker_len).is_none() {
        return (marker_len, indent + marker_len + 1);
    }
    let mut cursor = marker_len;
    let mut column = indent + marker_len;
    let marker_end_column = column;
    while let Some(byte) = bytes.get(cursor) {
        match *byte {
            b' ' => column += 1,
            b'\t' => column += 4 - (column % 4),
            _ => break,
        }
        cursor += 1;
    }
    // The line is the marker followed only by whitespace: an empty item whose
    // first line is blank. CommonMark §5.2 fixes its content indent at marker
    // width + 1 regardless of how many trailing spaces follow, so content on the
    // next line indented one column past the marker joins the item.
    if cursor >= bytes.len() {
        return (cursor, marker_end_column + 1);
    }
    let padding_columns = column.saturating_sub(marker_end_column);
    if padding_columns > 0 && padding_columns <= 4 {
        (cursor, column)
    } else {
        (marker_len + 1, marker_end_column + 1)
    }
}

fn list_marker_first_content<'a>(input: &'a str, marker: ListMarkerInfo<'a>) -> Cow<'a, str> {
    let Some(trimmed) = trim_up_to_three_spaces(input) else {
        return Cow::Borrowed(marker.content);
    };
    let after_marker = &trimmed[marker.marker_len..];
    if after_marker.starts_with('\t') {
        strip_leading_indent_columns_from(after_marker, 1, marker.indent + marker.marker_len)
    } else {
        Cow::Borrowed(marker.content)
    }
}

fn is_list_padding_byte(byte: Option<u8>) -> bool {
    matches!(byte, None | Some(b' ' | b'\t'))
}

fn same_list_marker(left: ListMarkerInfo<'_>, right: ListMarkerInfo<'_>) -> bool {
    // CommonMark §5.3: list items belong to the same list when they share a
    // bullet character or ordered delimiter. Indentation does not enter into
    // it — `- foo\n - bar\n  - baz` is one four-item bullet list, not three.
    left.ordered == right.ordered && left.delimiter == right.delimiter
}

/// Whether `input` begins a *sibling* item of the current list item.
///
/// A same-delimiter marker is a sibling only when it is not indented far enough
/// to nest inside the current item — i.e. its indent is less than the item's
/// `content_indent`. A marker indented at or beyond the content start belongs to
/// a sublist within the item and is consumed as item content instead.
fn sibling_list_marker_at_line(
    input: &str,
    first_marker: ListMarkerInfo<'_>,
    content_indent: usize,
) -> bool {
    list_marker_info(input).is_some_and(|candidate| {
        same_list_marker(first_marker, candidate) && candidate.indent < content_indent
    })
}

/// Whether `input` begins a list marker belonging to the same list as
/// `first_marker` (same ordered/unordered kind and delimiter). Used to tell a
/// marker that merely continues the current list apart from one that, by
/// changing the marker type, starts a new list (CommonMark §5.3).
fn same_list_marker_line(input: &str, first_marker: ListMarkerInfo<'_>) -> bool {
    list_marker_info(input).is_some_and(|candidate| same_list_marker(first_marker, candidate))
}

fn next_nonblank_line(lines: &[Line<'_>], mut index: usize) -> usize {
    while index < lines.len() && lines[index].text.trim().is_empty() {
        index += 1;
    }
    index
}

fn leading_indent(input: &str) -> (usize, usize) {
    let mut column = 0usize;
    let mut bytes = 0usize;
    for byte in input.as_bytes() {
        match *byte {
            b' ' => column += 1,
            b'\t' => column += 4 - (column % 4),
            _ => break,
        }
        bytes += 1;
    }
    (column, bytes)
}

fn leading_indent_columns(input: &str) -> usize {
    leading_indent(input).0
}

/// Removes up to `max_columns` columns of leading whitespace, stopping at the
/// first non-space/tab byte (tabs advance to the next 4-column tab stop). A tab
/// that straddles the column budget is PARTIALLY consumed: the columns beyond the
/// budget are re-emitted as spaces (CommonMark tab-expansion of indentation), so
/// the result may be an owned `String`. Whitespace already at/over the budget
/// (and any literal tab whose start sits at the budget) is returned verbatim.
fn strip_leading_indent_columns(input: &str, max_columns: usize) -> Cow<'_, str> {
    strip_leading_indent_columns_from(input, max_columns, 0)
}

fn strip_leading_indent_columns_from(
    input: &str,
    max_columns: usize,
    start_column: usize,
) -> Cow<'_, str> {
    let mut column = start_column;
    let target_column = start_column + max_columns;
    for (index, byte) in input.as_bytes().iter().enumerate() {
        let next = match *byte {
            b' ' => column + 1,
            b'\t' => column + (4 - (column % 4)),
            _ => return Cow::Borrowed(&input[index..]),
        };
        if next > target_column {
            // A tab whose expansion crosses the budget (its start still inside the
            // budget) is split: the over-budget columns survive as spaces.
            if *byte == b'\t' && column < target_column {
                let residual = next - target_column;
                let mut owned = String::with_capacity(residual + input.len() - (index + 1));
                for _ in 0..residual {
                    owned.push(' ');
                }
                let mut rest_column = next;
                let mut rest_index = index + 1;
                while let Some(rest_byte) = input.as_bytes().get(rest_index) {
                    match *rest_byte {
                        b' ' => {
                            owned.push(' ');
                            rest_column += 1;
                            rest_index += 1;
                        }
                        b'\t' => {
                            let width = 4 - (rest_column % 4);
                            for _ in 0..width {
                                owned.push(' ');
                            }
                            rest_column += width;
                            rest_index += 1;
                        }
                        _ => break,
                    }
                }
                owned.push_str(&input[rest_index..]);
                return Cow::Owned(owned);
            }
            return Cow::Borrowed(&input[index..]);
        }
        column = next;
    }
    Cow::Borrowed("")
}

fn strip_list_continuation(input: &str, content_indent: usize, list_indent: usize) -> Cow<'_, str> {
    let (indent_columns, indent_bytes) = leading_indent(input);
    if indent_columns >= content_indent {
        // Remove exactly `content_indent` columns. A tab straddling that budget
        // is split: the columns past the budget survive as spaces (CommonMark
        // tab expansion of list-item indentation), so a `\t`-only line inside a
        // 2-column item keeps the residual two spaces instead of vanishing.
        strip_leading_indent_columns(input, content_indent)
    } else if indent_columns > list_indent {
        Cow::Borrowed(&input[indent_bytes..])
    } else {
        Cow::Borrowed(trim_ascii_start(input))
    }
}

fn take_task_marker_from_children(children: &mut [Block]) -> Option<bool> {
    let Some(Block::Paragraph(paragraph)) = children.first_mut() else {
        return None;
    };
    take_task_marker_from_inlines(&mut paragraph.children)
}

fn take_task_marker_from_inlines(inlines: &mut Vec<Inline>) -> Option<bool> {
    let Some(Inline::Text(text)) = inlines.first() else {
        return None;
    };
    let first = text.value.clone();

    if let Some((checked, consumed)) = task_marker_inline_prefix(&first) {
        if !first[consumed..].is_empty() || inlines_have_content_after(inlines, 1) {
            remove_text_prefix(inlines, consumed);
            return Some(checked);
        }
    }

    if let Some(checked) = task_marker_at_text_end(&first) {
        if inlines
            .get(1)
            .is_some_and(|inline| matches!(inline, Inline::SoftBreak(_)))
            && inlines_have_content_after(inlines, 2)
        {
            inlines.remove(1);
            inlines.remove(0);
            return Some(checked);
        }
    }

    if task_marker_split_open(&first)
        && inlines
            .get(1)
            .is_some_and(|inline| matches!(inline, Inline::SoftBreak(_)))
    {
        let Some(Inline::Text(next)) = inlines.get(2) else {
            return None;
        };
        if let Some((checked, consumed)) = task_marker_split_close_prefix(&next.value) {
            if !next.value[consumed..].is_empty() || inlines_have_content_after(inlines, 3) {
                inlines.remove(1);
                inlines.remove(0);
                remove_text_prefix(inlines, consumed);
                return Some(checked);
            }
        }
    }

    None
}

fn task_marker_inline_prefix(input: &str) -> Option<(bool, usize)> {
    let start = leading_trim_bytes(input);
    let rest = &input[start..];
    let checked = task_marker_checked(rest)?;
    let after_marker = start + 3;
    match input.as_bytes().get(after_marker) {
        Some(b' ' | b'\t') => Some((checked, after_marker + 1)),
        _ => None,
    }
}

fn task_marker_at_text_end(input: &str) -> Option<bool> {
    let start = leading_trim_bytes(input);
    let rest = &input[start..];
    let checked = task_marker_checked(rest)?;
    if rest.len() == 3 {
        Some(checked)
    } else {
        None
    }
}

fn task_marker_split_open(input: &str) -> bool {
    let start = leading_trim_bytes(input);
    input[start..] == *"["
}

fn task_marker_split_close_prefix(input: &str) -> Option<(bool, usize)> {
    match input.as_bytes().get(..2)? {
        b"] " => Some((false, 2)),
        b"]\t" => Some((false, 2)),
        b"x]" | b"X]" if matches!(input.as_bytes().get(2), Some(b' ' | b'\t')) => Some((true, 3)),
        _ => None,
    }
}

fn task_marker_checked(input: &str) -> Option<bool> {
    if input.starts_with("[ ]") {
        Some(false)
    } else if input.starts_with("[x]") || input.starts_with("[X]") {
        Some(true)
    } else {
        None
    }
}

fn remove_text_prefix(inlines: &mut Vec<Inline>, consumed: usize) {
    if let Some(Inline::Text(text)) = inlines.first_mut() {
        text.value = text.value[consumed..].into();
        if text.value.is_empty() {
            inlines.remove(0);
        }
    }
}

fn inlines_have_content_after(inlines: &[Inline], start: usize) -> bool {
    inlines.iter().skip(start).any(|inline| match inline {
        Inline::Text(text) => !text.value.is_empty(),
        Inline::SoftBreak(_) | Inline::LineBreak(_) => false,
        _ => true,
    })
}

fn update_list_item_fence(line: &str, open_fence: &mut Option<(FenceMarker, usize)>) {
    let Some(trimmed) = trim_up_to_three_spaces(line) else {
        return;
    };
    if let Some((marker, length)) = *open_fence {
        if fence_close(trimmed, marker, length) {
            *open_fence = None;
        }
        return;
    }
    if let Some((marker, length)) = fence_start(trimmed) {
        *open_fence = Some((marker, length));
    }
}

fn trim_ascii_start(input: &str) -> &str {
    input.trim_start_matches(|char| matches!(char, ' ' | '\t'))
}

fn leading_trim_bytes(input: &str) -> usize {
    input.len() - trim_ascii_start(input).len()
}

fn parse_table_delimiter(input: &str, spoiler: bool) -> Option<Vec<TableAlignment>> {
    let cells = split_table_row(input, spoiler);
    if cells.is_empty() {
        return None;
    }
    let mut alignments = Vec::new();
    for cell in cells {
        alignments.push(table_delimiter_alignment(cell.trim())?);
    }
    Some(alignments)
}

// A delimiter cell is `:?` `-`+ `:?` once trimmed: colons only at the
// boundaries, the dashes contiguous, no interior space or colon.
fn table_delimiter_alignment(cell: &str) -> Option<TableAlignment> {
    let bytes = cell.as_bytes();
    let mut cursor = 0;
    let left = bytes.first() == Some(&b':');
    if left {
        cursor += 1;
    }
    let dash_start = cursor;
    while bytes.get(cursor) == Some(&b'-') {
        cursor += 1;
    }
    if cursor == dash_start {
        return None;
    }
    let right = bytes.get(cursor) == Some(&b':');
    if right {
        cursor += 1;
    }
    if cursor != bytes.len() {
        return None;
    }
    Some(match (left, right) {
        (true, true) => TableAlignment::Center,
        (true, false) => TableAlignment::Left,
        (false, true) => TableAlignment::Right,
        (false, false) => TableAlignment::None,
    })
}

/// Normalizes a table line's leading indentation: when indented code is enabled
/// a four-space indent would start a code block, so up to three leading spaces
/// are trimmed and four or more disqualifies the line.
fn table_indent_line(input: &str, indented_code: bool) -> Option<&str> {
    if indented_code {
        trim_up_to_three_spaces(input)
    } else {
        Some(input)
    }
}

// True if a backtick run of `length` at `start` has a matching-length closing
// run later in `input`. The table row scanner still treats unescaped pipes as
// cell boundaries; this state only prevents extension syntax such as spoilers
// from being recognized inside a code span.
fn backtick_run_has_close(input: &str, start: usize, length: usize) -> bool {
    let bytes = input.as_bytes();
    let mut i = start + length;
    while i < input.len() {
        if bytes[i] == b'`' {
            let run = input[i..]
                .as_bytes()
                .iter()
                .take_while(|byte| **byte == b'`')
                .count();
            if run == length {
                return true;
            }
            i += run;
        } else {
            i += 1;
        }
    }
    false
}

fn table_backslash_pipe_run(input: &str, cursor: usize) -> Option<(usize, bool)> {
    let bytes = input.as_bytes();
    if bytes.get(cursor) != Some(&b'\\') {
        return None;
    }
    let mut pipe = cursor;
    while bytes.get(pipe) == Some(&b'\\') {
        pipe += 1;
    }
    (bytes.get(pipe) == Some(&b'|')).then_some((pipe, (pipe - cursor) % 2 == 1))
}

fn split_table_row(input: &str, spoiler: bool) -> Vec<String> {
    let trimmed = input.trim();
    let mut cells = Vec::new();
    let mut cell = String::new();
    let mut cursor = 0;
    let mut code_fence = None;
    let mut spoiler_open = false;
    // Byte offset just past the most recent genuine cell-delimiter pipe. When the
    // scan ends with only whitespace after it, that pipe was a trailing border and
    // the empty leftover cell is dropped (rather than blindly trusting that the
    // line ends with `|`, which mis-fires on a spoiler-close `||` or a code-span
    // pipe — see tbl-4).
    let mut trailing_delimiter_end = None;

    while cursor < trimmed.len() {
        let (next, char) = next_char(trimmed, cursor).expect("valid UTF-8 byte index");
        // GitHub/cmark-gfm treats an odd backslash run before `|` as a literal
        // cell-content pipe, but an even run leaves the pipe as a delimiter. Keep
        // the original run before an even delimiter so the inline parser resolves
        // the visible backslashes correctly.
        if char == '\\' {
            if let Some((pipe, escaped)) = table_backslash_pipe_run(trimmed, cursor) {
                if escaped {
                    for _ in 0..pipe - cursor - 1 {
                        cell.push('\\');
                    }
                    cell.push('|');
                    cursor = pipe + 1;
                } else {
                    for _ in 0..pipe - cursor {
                        cell.push('\\');
                    }
                    cursor = pipe;
                }
                continue;
            }
        }
        // Backticks are never escapable, so a preceding backslash does not block a
        // code-span boundary (a `\` directly before a closing backtick is content,
        // not an escape — see tbl-3).
        if char == '`' {
            let length = trimmed[cursor..]
                .as_bytes()
                .iter()
                .take_while(|byte| **byte == b'`')
                .count();
            if code_fence == Some(length) {
                code_fence = None;
            } else if code_fence.is_none() && backtick_run_has_close(trimmed, cursor, length) {
                code_fence = Some(length);
            }
            cell.push_str(&trimmed[cursor..cursor + length]);
            cursor += length;
            continue;
        }

        if spoiler
            && char == '|'
            && trimmed.as_bytes().get(cursor + 1) == Some(&b'|')
            && code_fence.is_some()
        {
            cell.push_str("||");
            cursor += 2;
            continue;
        }

        if spoiler
            && char == '|'
            && trimmed.as_bytes().get(cursor + 1) == Some(&b'|')
            && code_fence.is_none()
            && !is_escaped_at(trimmed, cursor)
        {
            let closes_spoiler =
                spoiler_open && trimmed.as_bytes().get(cursor.wrapping_sub(1)) != Some(&b'|');
            let opens_spoiler = !spoiler_open
                && trimmed.as_bytes().get(cursor + 2) != Some(&b'|')
                && find_spoiler_close(trimmed, cursor + 2).is_some();
            if closes_spoiler || opens_spoiler {
                spoiler_open = opens_spoiler;
                cell.push_str("||");
                cursor += 2;
                continue;
            }
        }

        if char == '|' && !spoiler_open && !is_escaped_at(trimmed, cursor) {
            cells.push(core::mem::take(&mut cell));
            // A delimiter ends the cell; spoiler state never spans a cell boundary.
            spoiler_open = false;
            trailing_delimiter_end = Some(next);
        } else {
            cell.push(char);
        }
        cursor = next;
    }
    cells.push(cell);

    if trimmed.starts_with('|') {
        cells.remove(0);
    }
    // Drop the empty cell created by a trailing border pipe: the last genuine
    // delimiter must sit at the very end (only whitespace after it).
    if let Some(end) = trailing_delimiter_end {
        if trimmed[end..].trim().is_empty() {
            cells.pop();
        }
    }
    cells
}

fn table_can_start(lines: &[Line<'_>], index: usize, options: &SyntaxOptions) -> bool {
    if !options.constructs.gfm_table || index + 1 >= lines.len() {
        return false;
    }
    table_can_start_source(
        lines[index].text,
        lines[index + 1].text,
        options.constructs.indented_code,
        options.constructs.spoiler,
    )
}

pub(crate) fn gfm_table_can_start_source(header: &str, delimiter: &str) -> bool {
    table_can_start_source(header, delimiter, true, false)
}

fn table_can_start_source(
    header: &str,
    delimiter: &str,
    indented_code: bool,
    spoiler: bool,
) -> bool {
    let Some(delimiter) = table_indent_line(delimiter, indented_code) else {
        return false;
    };
    if list_marker_info(delimiter).is_some() {
        return false;
    }
    if !table_has_separator(header, delimiter, spoiler) {
        return false;
    }
    let Some(alignments) = parse_table_delimiter(delimiter, spoiler) else {
        return false;
    };
    split_table_row(header, spoiler).len() == alignments.len()
}

fn table_has_separator(header: &str, delimiter: &str, spoiler: bool) -> bool {
    // GFM makes leading/trailing pipes optional, so `parse_table_delimiter` plus
    // the header/alignment column-count check usually suffice. The one exception
    // is a single resolved column with no disambiguating syntax: `a\n-\nb` has
    // matching one-column shapes yet no pipe and no alignment colon, so it is a
    // loose paragraph/setext, not a table. A single column still forms a table
    // when a pipe appears in the header/delimiter or the delimiter carries an
    // explicit alignment colon (`a\n-:`, `a\n:-:`, …).
    let Some(alignments) = parse_table_delimiter(delimiter, spoiler) else {
        return true;
    };
    if alignments.len() == 1 {
        return contains_unescaped_pipe(header, spoiler)
            || contains_unescaped_pipe(delimiter, spoiler)
            || delimiter.contains(':');
    }
    true
}

// Still used by `block_quote_table_body_row` to detect a table row appearing as
// a block-quote continuation line (which DOES require a pipe).
fn contains_unescaped_pipe(input: &str, spoiler: bool) -> bool {
    let mut cursor = 0;
    let mut code_fence = None;
    let mut spoiler_open = false;
    while cursor < input.len() {
        let (next, char) = next_char(input, cursor).expect("valid UTF-8 byte index");
        if char == '\\' {
            if let Some((pipe, escaped)) = table_backslash_pipe_run(input, cursor) {
                cursor = if escaped { pipe + 1 } else { pipe };
                continue;
            }
        }
        // Backticks are never escapable; a preceding backslash is code-span content.
        if char == '`' {
            let length = input[cursor..]
                .as_bytes()
                .iter()
                .take_while(|byte| **byte == b'`')
                .count();
            if code_fence == Some(length) {
                code_fence = None;
            } else if code_fence.is_none() {
                code_fence = Some(length);
            }
            cursor += length;
            continue;
        }
        if spoiler
            && char == '|'
            && input.as_bytes().get(cursor + 1) == Some(&b'|')
            && code_fence.is_some()
        {
            cursor += 2;
            continue;
        }
        if spoiler
            && char == '|'
            && input.as_bytes().get(cursor + 1) == Some(&b'|')
            && code_fence.is_none()
            && !is_escaped_at(input, cursor)
        {
            let closes_spoiler =
                spoiler_open && input.as_bytes().get(cursor.wrapping_sub(1)) != Some(&b'|');
            let opens_spoiler = !spoiler_open
                && input.as_bytes().get(cursor + 2) != Some(&b'|')
                && find_spoiler_close(input, cursor + 2).is_some();
            if closes_spoiler || opens_spoiler {
                spoiler_open = opens_spoiler;
                cursor += 2;
                continue;
            }
        }
        if char == '|' && !spoiler_open && !is_escaped_at(input, cursor) {
            return true;
        }
        cursor = next;
    }
    false
}

fn likely_block_start(input: &str, options: &SyntaxOptions) -> bool {
    // Block-structure markers (ATX, fences, thematic breaks, list markers, math
    // fences, directives, …) only begin a block when indented at most 3 columns.
    // At >=4 columns the line is indented code, which never interrupts a
    // paragraph, so no marker test should fire.
    let Some(trimmed) = trim_up_to_three_spaces(input) else {
        return false;
    };
    trimmed.starts_with('#')
        || trimmed.starts_with('>')
        || trimmed.starts_with("```")
        || trimmed.starts_with("~~~")
        || list_marker_can_interrupt_paragraph(input)
        || parse_thematic_break(Line {
            text: input,
            eol: "",
            start: 0,
            end: input.len(),
            end_with_eol: input.len(),
            lazy: false,
        })
        .is_some()
        || (options.constructs.html_block && line_starts_interrupting_html_block(input))
        || (options.constructs.math_block && math_block_fence_length(trimmed).is_some())
        || (options.constructs.directive_container && trimmed.starts_with(":::"))
        || (options.constructs.directive_leaf && trimmed.starts_with("::"))
        || (options.constructs.footnote_definition && line_starts_footnote_definition(trimmed))
}

// A GFM footnote definition `[^label]:` is a block boundary: it interrupts a
// paragraph and ends a prior footnote's lazy continuation.
fn line_starts_footnote_definition(trimmed: &str) -> bool {
    trimmed.starts_with("[^")
        && find_footnote_definition_label_end(trimmed)
            .is_some_and(|close| is_footnote_label(&trimmed[2..close]))
}

fn list_marker_can_interrupt_paragraph(input: &str) -> bool {
    list_marker_info(input).is_some_and(|marker| {
        // An empty list item never interrupts a paragraph (CommonMark §5.3):
        // `foo\n*` is a single paragraph, not a paragraph plus an empty list.
        !marker.content.trim().is_empty() && (!marker.ordered || marker.start == Some(1))
    })
}

// GFM table-body termination is stricter than paragraph interruption: an open
// table also ends on a list marker with EMPTY content (`-`, `*`, `1.`), which
// `likely_block_start` deliberately ignores for paragraphs. Used only by the
// table body loop; `likely_block_start` itself is left untouched.
fn table_body_line_ends_table(line: &str, options: &SyntaxOptions) -> bool {
    likely_block_start(line, options)
        || list_marker_info(line).is_some()
        || (options.constructs.html_block && line_starts_html_block(line))
}

fn line_starts_interrupting_html_block(input: &str) -> bool {
    match trim_up_to_three_spaces(input).and_then(html_block_start) {
        Some(HtmlBlockKind::UntilBlank) | None => false,
        Some(_) => true,
    }
}

fn parse_autolink_end(input: &str, index: usize) -> Option<usize> {
    input[index..].find('>').map(|end| index + end + 1)
}

fn parse_html_inline(input: &str, index: usize) -> Option<(usize, String)> {
    let rest = &input[index..];
    if rest.starts_with("<!--") {
        let end = rest.find("-->")? + 3;
        return Some((index + end, rest[..end].into()));
    }
    if rest.starts_with("<?") {
        let end = rest.find("?>")? + 2;
        return Some((index + end, rest[..end].into()));
    }
    if rest.starts_with("<![CDATA[") {
        let end = rest.find("]]>")? + 3;
        return Some((index + end, rest[..end].into()));
    }
    if is_declaration_start(rest) {
        let end = rest.find('>')? + 1;
        return Some((index + end, rest[..end].into()));
    }

    let (end, _) = parse_html_tag(input, index)?;
    Some((end, input[index..end].into()))
}

fn parse_html_tag(input: &str, index: usize) -> Option<(usize, &str)> {
    let bytes = input.as_bytes();
    if bytes.get(index) != Some(&b'<') {
        return None;
    }

    let closing = bytes.get(index + 1) == Some(&b'/');
    let name_start = index + if closing { 2 } else { 1 };
    let first = *bytes.get(name_start)?;
    if !first.is_ascii_alphabetic() {
        return None;
    }

    let mut cursor = name_start + 1;
    while bytes.get(cursor).is_some_and(|byte| html_name_byte(*byte)) {
        cursor += 1;
    }
    let name = &input[name_start..cursor];

    if closing {
        cursor = skip_spaces(input, cursor);
        if bytes.get(cursor) == Some(&b'>') {
            return Some((cursor + 1, name));
        }
        return None;
    }

    let mut needs_space = false;
    loop {
        let before_spaces = cursor;
        cursor = skip_spaces(input, cursor);
        let had_space = cursor > before_spaces;
        match bytes.get(cursor) {
            Some(b'>') => return Some((cursor + 1, name)),
            Some(b'/') if bytes.get(cursor + 1) == Some(&b'>') => return Some((cursor + 2, name)),
            Some(byte) if had_space && html_attribute_name_start(*byte) => {
                cursor += 1;
                while bytes
                    .get(cursor)
                    .is_some_and(|byte| html_attribute_name_byte(*byte))
                {
                    cursor += 1;
                }
                let after_name = cursor;
                let after_spaces = skip_spaces(input, cursor);
                if bytes.get(after_spaces) == Some(&b'=') {
                    cursor = skip_spaces(input, after_spaces + 1);
                    cursor = parse_html_attribute_value(input, cursor)?;
                } else {
                    cursor = after_name;
                }
                needs_space = true;
            }
            Some(_) if needs_space => return None,
            _ => return None,
        }
    }
}

fn parse_html_attribute_value(input: &str, index: usize) -> Option<usize> {
    let bytes = input.as_bytes();
    match bytes.get(index)? {
        b'"' | b'\'' => {
            let quote = bytes[index];
            let mut cursor = index + 1;
            while cursor < bytes.len() {
                if bytes[cursor] == quote {
                    return Some(cursor + 1);
                }
                cursor += 1;
            }
            None
        }
        b'=' | b'<' | b'>' | b'`' => None,
        _ => {
            let mut cursor = index;
            while bytes.get(cursor).is_some_and(|byte| {
                !byte.is_ascii_whitespace()
                    && !matches!(*byte, b'"' | b'\'' | b'=' | b'<' | b'>' | b'`')
            }) {
                cursor += 1;
            }
            if cursor == index {
                None
            } else {
                Some(cursor)
            }
        }
    }
}

fn html_name_byte(byte: u8) -> bool {
    byte.is_ascii_alphanumeric() || byte == b'-'
}

fn html_attribute_name_start(byte: u8) -> bool {
    byte.is_ascii_alphabetic() || byte == b'_' || byte == b':'
}

fn html_attribute_name_byte(byte: u8) -> bool {
    byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b':' | b'.' | b'-')
}

fn skip_spaces(input: &str, mut index: usize) -> usize {
    while input
        .as_bytes()
        .get(index)
        .is_some_and(|byte| matches!(*byte, b' ' | b'\t' | b'\n' | b'\r'))
    {
        index += 1;
    }
    index
}

fn is_autolink(input: &str) -> bool {
    let inner = &input[1..input.len() - 1];
    is_uri_autolink(inner) || is_email_autolink(inner)
}

fn is_uri_autolink(input: &str) -> bool {
    let Some(colon) = input.find(':') else {
        return false;
    };
    let scheme = &input[..colon];
    if scheme.len() < 2 || scheme.len() > 32 {
        return false;
    }
    let mut bytes = scheme.bytes();
    if !bytes.next().is_some_and(|byte| byte.is_ascii_alphabetic()) {
        return false;
    }
    if !bytes.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'+' | b'.' | b'-')) {
        return false;
    }
    input[colon + 1..]
        .chars()
        .all(|char| !matches!(char, '<' | '>') && !char.is_control() && !char.is_whitespace())
}

fn is_email_autolink(input: &str) -> bool {
    if input.chars().any(char::is_whitespace) {
        return false;
    }
    let Some(at) = input.find('@') else {
        return false;
    };
    if at == 0 || at + 1 >= input.len() {
        return false;
    }
    // Angle-bracket `<email>` autolinks use the strict CommonMark domain
    // grammar but, unlike the GFM bare form, allow a single (dotless) label.
    is_email_local_part(&input[..at]) && is_email_domain(&input[at + 1..], 1)
}

// GFM literal-autolink dispatch. Tries, in order: `http(s)://` URLs, `www.`
// URLs, extended-protocol (`mailto:`/`xmpp:`) emails, and bare emails. Each
// branch enforces cmark-gfm's per-scheme preceding-character guard and its
// domain/host rules; the trailing trim is shared (`autolink_delim`). The
// returned destination is the synthesized href (a `http://`/`mailto:` prefix
// may be prepended); the caller keeps `input[index..end]` as the visible
// original.
fn parse_literal_autolink(
    input: &str,
    index: usize,
    gfm: bool,
    relaxed: bool,
) -> Option<(usize, String)> {
    let rest = &input[index..];

    if gfm {
        // `http://` / `https://` URLs. cmark requires the char before the scheme
        // to be non-alphanumeric (so `mmmhttp://…` does not link from `mmmh`).
        if let Some(scheme_len) = rest
            .starts_with("http://")
            .then_some(7)
            .or_else(|| rest.starts_with("https://").then_some(8))
        {
            if !literal_scheme_prefix_ok(input, index) {
                return None;
            }
            let host = &input[index + scheme_len..];
            // A non-empty domain or bracketed IPv6 host is additionally
            // required, so `http://`, `http://#`, `http://$` are not links.
            if !http_literal_host_ok(host) {
                if relaxed {
                    // Let cmark-gfm's relaxed `scheme://` pass decide cases
                    // such as a bare `http://` followed by whitespace.
                } else {
                    return None;
                }
            } else {
                // The URL extent is scanned from the very start (after `://`) and the
                // trailing trim runs over the whole URL. Relaxed mode balances
                // brackets/braces so `[abc]`/`{abc}`/IPv6 hosts stay in the URL.
                let end = autolink_url_end(input, index + scheme_len, index + scheme_len, relaxed);
                if end <= index + scheme_len {
                    return None;
                }
                if literal_autolink_suppressed_by_link_label(input, index, end, relaxed, gfm) {
                    return None;
                }
                return Some((end, input[index..end].into()));
            }
        }

        // `www.` URLs (synthesize a `http://` href). cmark allows the preceding
        // char to be one of `*_~(` or whitespace (or start of input).
        if rest
            .as_bytes()
            .get(..4)
            .is_some_and(|prefix| prefix.eq_ignore_ascii_case(b"www."))
        {
            if !literal_www_prefix_ok(input, index) {
                return None;
            }
            check_domain(rest, false)?;
            let end = autolink_url_end(input, index, index, relaxed);
            if end <= index || (!relaxed && end <= index + 3 && !literal_starts_line(input, index))
            {
                return None;
            }
            if literal_autolink_suppressed_by_link_label(input, index, end, relaxed, gfm) {
                return None;
            }
            let mut destination = String::from("http://");
            destination.push_str(&input[index..end]);
            return Some((end, destination));
        }

        if let Some(email) = parse_literal_email(input, index) {
            return Some(email);
        }
    }

    if relaxed {
        // cmark-gfm "relaxed" URL autolinks: a bare `scheme://…` for any scheme
        // (`smb://`, `irc://`, `rdar://`, `we://`, `nex://[…]`, …) or a
        // scheme-less leading `://…` (`://-`). Requires the same non-alphanumeric
        // preceding char as the http literal and at least one non-whitespace
        // char after `://`; no host/domain validation (cmark-gfm is permissive
        // here — `smb:///path` and `://-` both linkify). The extent is balanced.
        if literal_scheme_prefix_ok(input, index) {
            if let Some(after_slashes) = relaxed_scheme_after_slashes(rest) {
                let body_start = index + after_slashes;
                let next = input[body_start..].chars().next();
                if next.is_none_or(|char| char.is_whitespace()) && after_slashes == 3 {
                    return None;
                }
                let end = autolink_url_end(input, body_start, body_start, true);
                if end > index {
                    if literal_autolink_suppressed_by_link_label(input, index, end, relaxed, gfm) {
                        return None;
                    }
                    return Some((end, input[index..end].into()));
                }
            }
        }
    }

    None
}

// Returns the byte offset (within `rest`) just past a relaxed `scheme://` (any
// ASCII-alpha-then-`[alnum+. -]` scheme) or scheme-less `://` prefix, if `rest`
// starts with one. No scheme length cap — cmark-gfm's relaxed autolink is
// permissive. Returns `None` for a bare `scheme:` without `//` (that is the
// email/angle-autolink path's job).
fn relaxed_scheme_after_slashes(rest: &str) -> Option<usize> {
    let bytes = rest.as_bytes();
    if bytes.starts_with(b"://") {
        return Some(3);
    }
    let first = bytes.first()?;
    if !first.is_ascii_alphabetic() {
        return None;
    }
    let mut i = 1;
    while i < bytes.len() {
        match bytes[i] {
            b':' => break,
            byte if byte.is_ascii_alphanumeric() || matches!(byte, b'+' | b'.' | b'-') => i += 1,
            _ => return None,
        }
    }
    if bytes.get(i..i + 3) == Some(b"://") {
        Some(i + 3)
    } else {
        None
    }
}

// The char immediately before a `http(s)://` literal must be non-alphabetic.
// An escaped `<` (`\<http://…`) is just literal text before the URL, so the
// literal still forms (the `<` is not treated as an angle-autolink opener).
fn literal_scheme_prefix_ok(input: &str, index: usize) -> bool {
    if index == 0 {
        return true;
    }
    let Some(previous) = input[..index].chars().next_back() else {
        return true;
    };
    !previous.is_ascii_alphabetic()
}

// The char before a `www.` literal must be one of cmark-gfm's accepted ASCII
// delimiters or ordinary Markdown layout whitespace. Unicode whitespace is not
// a start delimiter for this branch.
fn literal_www_prefix_ok(input: &str, index: usize) -> bool {
    if index == 0 {
        return true;
    }
    let Some(previous) = input[..index].chars().next_back() else {
        return true;
    };
    if matches!(previous, '*' | '_' | '~' | '(' | '[' | ']') {
        return true;
    }
    matches!(previous, ' ' | '\t' | '\n' | '\r')
}

fn literal_starts_line(input: &str, index: usize) -> bool {
    index == 0
        || input
            .as_bytes()
            .get(index - 1)
            .is_some_and(|byte| matches!(byte, b'\n' | b'\r'))
}

fn literal_autolink_suppressed_by_link_label(
    input: &str,
    index: usize,
    end: usize,
    relaxed: bool,
    gfm_autolink_literal: bool,
) -> bool {
    if !has_unclosed_link_label_opener(input, index) {
        return false;
    }
    if input[end..].starts_with("](") && !link_resource_tail_has_close(input, end + 2) {
        return true;
    }
    !relaxed && !gfm_autolink_literal && input.as_bytes().get(end).is_some_and(|byte| *byte == b']')
}

fn has_unclosed_link_label_opener(input: &str, index: usize) -> bool {
    let line_start = input[..index]
        .rfind(['\n', '\r'])
        .map_or(0, |offset| offset + 1);
    let mut depth = 0usize;
    let mut cursor = line_start;
    while cursor < index {
        let Some((next, char)) = next_char(input, cursor) else {
            break;
        };
        match char {
            '\\' => {
                cursor = next_char(input, next)
                    .map(|(after_escape, _)| after_escape)
                    .unwrap_or(next);
                continue;
            }
            '[' => depth += 1,
            ']' => {
                depth = depth.saturating_sub(1);
            }
            _ => {}
        }
        cursor = next;
    }
    depth > 0
}

fn link_resource_tail_has_close(input: &str, start: usize) -> bool {
    let mut cursor = start;
    while cursor < input.len() {
        let Some((next, char)) = next_char(input, cursor) else {
            break;
        };
        match char {
            '\\' => {
                cursor = next_char(input, next)
                    .map(|(after_escape, _)| after_escape)
                    .unwrap_or(next);
                continue;
            }
            '\n' | '\r' => return false,
            ')' => return true,
            _ => {}
        }
        cursor = next;
    }
    false
}

fn http_literal_host_ok(host: &str) -> bool {
    if host.starts_with('[') {
        return bracketed_ipv6_host_end(host).is_some();
    }
    match host.chars().next() {
        Some(char) if char.is_ascii() && char.is_ascii_alphanumeric() => {
            check_domain(host, true).is_some()
        }
        Some(char) if !char.is_ascii() && is_valid_hostchar(char) => {
            check_domain(host, true).is_some()
        }
        _ => false,
    }
}

fn bracketed_ipv6_host_end(host: &str) -> Option<usize> {
    let close = host.find(']')?;
    (close > 1).then_some(close + 1)
}

// Port of cmark-gfm `is_valid_hostchar`: a host char is valid when it is not a
// Unicode space and not a Unicode punctuation character.
fn is_valid_hostchar(char: char) -> bool {
    !char.is_whitespace() && !crate::unicode_punctuation::is_unicode_punctuation(char)
}

// Port of cmark-gfm `check_domain`. Scans the leading host of `data` (up to the
// first non-host char) and returns its byte length, or `None` when invalid.
// Rejects a `_` in either of the last two `.`-separated host segments (unless
// the host has >10 segments — a DoS guard). When `allow_short` is false a dot
// is required (the `www.` rule). The URL extent past the host is determined by
// `autolink_url_end`, so the precise length here only gates validity.
//
// cmark walks bytes with `is_valid_hostchar` decoding each char; this walks
// chars directly (UTF-8 safe) over the host prefix, which yields the same
// dot/underscore-segment verdict. A `\` escapes the following char.
fn check_domain(data: &str, allow_short: bool) -> Option<usize> {
    let mut np = 0usize;
    let mut uscore1 = 0usize;
    let mut uscore2 = 0usize;
    let mut host_len = 0usize;

    let mut chars = data.char_indices().peekable();
    while let Some((offset, char)) = chars.next() {
        // cmark's accounting loop runs `for (i = 1; i < size - 1; i++)`: it
        // never inspects the first char (offset 0) nor the final char of the
        // chunk. We replicate that — a trailing `_` (e.g. `http://a_`) is not
        // counted, so the link still forms.
        let account = offset != 0 && chars.peek().is_some();
        match char {
            '\\' => {
                // Escape: consume the next char as a literal host char.
                host_len = offset + char.len_utf8();
                if let Some((next_off, next)) = chars.next() {
                    host_len = next_off + next.len_utf8();
                }
            }
            '_' if account => {
                uscore2 += 1;
                host_len = offset + char.len_utf8();
            }
            '.' if account => {
                uscore1 = uscore2;
                uscore2 = 0;
                np += 1;
                host_len = offset + char.len_utf8();
            }
            '_' | '.' | '-' => {
                host_len = offset + char.len_utf8();
            }
            _ => {
                if !is_valid_hostchar(char) {
                    break;
                }
                host_len = offset + char.len_utf8();
            }
        }
    }

    if (uscore1 > 0 || uscore2 > 0) && np <= 10 {
        return None;
    }

    if allow_short || np > 0 {
        Some(host_len)
    } else {
        None
    }
}

// Forward scan from `start` for the URL extent: every char up to whitespace,
// `<`, or `]` ends the URL. CommonMark allows `>` and `[` inside (the renderer
// percent-encodes them); a `]` is additionally treated as a hard URL boundary
// (autolink-3), so a `]` ends the scan and is never part of the link.
// `trim_from` is where the trailing trim may reach (the URL start).
fn autolink_url_end(input: &str, start: usize, trim_from: usize, balanced: bool) -> usize {
    let bytes = input.as_bytes();
    let mut end = start;
    // Relaxed (cmark-gfm) URL extents balance `[`/`]` and `{`/`}` so an IPv6
    // host `nex://[fe80…]/z` and a balanced `[abc]`/`{abc}` run stay inside the
    // URL while an unbalanced trailing `]`/`}` ends it. Strict (GFM literal)
    // extents stop at the first `]` (no balancing) — the two oracle shapes
    // differ on purpose (`autolink_brackets_unbalanced` keeps both `]`;
    // `autolink_relaxed_links_brackets_balanced` keeps one).
    let mut bracket_depth = 0i32;
    let mut curly_depth = 0i32;
    let mut strict_has_open_bracket = false;
    let mut strict_inside_backticks = false;
    for (offset, char) in input[start..].char_indices() {
        if char.is_whitespace() || char == '<' || is_autolink_terminating_control(char) {
            break;
        }
        if balanced {
            match char {
                '[' => bracket_depth += 1,
                ']' => {
                    if bracket_depth > 0 {
                        bracket_depth -= 1;
                    } else {
                        break;
                    }
                }
                '{' => curly_depth += 1,
                '}' => {
                    if curly_depth > 0 {
                        curly_depth -= 1;
                    } else {
                        break;
                    }
                }
                _ => {}
            }
        } else {
            match char {
                '[' => strict_has_open_bracket = true,
                '`' => strict_inside_backticks = !strict_inside_backticks,
                ']' if !strict_has_open_bracket && !strict_inside_backticks => break,
                _ => {}
            }
        }
        // Round-trip guard: when a literal autolink ends (a trailing entity
        // run, punctuation trim, unbalanced `)`, or the `]`/`<` hard boundary),
        // the text that follows often begins with a char the serializer escapes
        // with a backslash (`\&`, `\[`, `\]`, `\<`, `\>`, `\*`, `\_`, …). The
        // URL scan must stop at such a `\<punct>` so the escape is not re-merged
        // into the destination. A `\` before `.` (or any non-punctuation) is a
        // genuine literal backslash inside the URL (e.g. `www.x.com/a\.`), which
        // the serializer never produces, so it stays part of the URL.
        if char == '\\' {
            if let Some(&next) = bytes.get(start + offset + 1) {
                let next_is_escapable_punct = next.is_ascii_punctuation() && next != b'.';
                if next_is_escapable_punct {
                    break;
                }
            }
        }
        end = start + offset + char.len_utf8();
    }
    autolink_delim(input, trim_from, end)
}

fn is_autolink_terminating_control(char: char) -> bool {
    matches!(char, '\u{2066}'..='\u{2069}')
}

// Port of cmark-gfm `autolink_delim`: trim trailing delimiters from the end of
// the URL. A trailing `) ? ! . , : * _ ~ ' "` is trimmed; `)` only when there
// are more `)` than `(` in the link; a trailing `&…;` entity run is excluded
// whole; a lone trailing `;` is trimmed.
fn autolink_delim(input: &str, start: usize, mut end: usize) -> usize {
    let bytes = input.as_bytes();
    let mut opening = 0usize;
    let mut closing = 0usize;
    for &byte in &bytes[start..end] {
        match byte {
            b'(' => opening += 1,
            b')' => closing += 1,
            _ => {}
        }
    }

    while end > start {
        match bytes[end - 1] {
            b')' => {
                if closing <= opening {
                    break;
                }
                closing -= 1;
                end -= 1;
            }
            b'?' | b'!' | b'.' | b',' | b':' | b'*' | b'_' | b'~' | b'\'' | b'"' => {
                end -= 1;
            }
            b';' => {
                // A trailing hex numeric character reference `&#x…;` is excluded
                // whole. This is the round-trip dual of the serializer, which
                // encodes a text char that would otherwise merge into the URL as
                // a hex entity; no autolink-oracle URL ends in `&#x…;`, so this
                // is conformance-safe (decimal `&#…;` is left intact to match
                // the oracle, which keeps `www.a&#35` in the URL).
                if let Some(amp) = trailing_hex_entity_run_start(bytes, start, end) {
                    end = amp;
                } else {
                    // Walk back over alphanumerics; if they reach a `&`, exclude
                    // the whole `&…;` entity run, otherwise trim just the `;`.
                    let mut new_end = end - 1;
                    while new_end > start && bytes[new_end - 1].is_ascii_alphanumeric() {
                        new_end -= 1;
                    }
                    if new_end > start && new_end < end - 1 && bytes[new_end - 1] == b'&' {
                        end = new_end - 1;
                    } else {
                        end -= 1;
                    }
                }
            }
            _ => break,
        }
    }
    end
}

// When the URL ends with a hex numeric character reference `&#x[hex]+;`, returns
// the offset of its leading `&`; otherwise `None`. Used only by `autolink_delim`
// to trim the serializer's round-trip boundary marker (the serializer encodes a
// would-merge text char as `&#xNN;`). Decimal `&#…;` is intentionally NOT
// matched so the oracle's `www.a&#35` URLs stay intact.
fn trailing_hex_entity_run_start(bytes: &[u8], start: usize, end: usize) -> Option<usize> {
    if end <= start || bytes[end - 1] != b';' {
        return None;
    }
    let mut cursor = end - 1;
    while cursor > start && bytes[cursor - 1].is_ascii_hexdigit() {
        cursor -= 1;
    }
    // Require at least one hex digit, then `&#x` (case-insensitive `x`).
    if cursor == end - 1 || cursor < start + 3 {
        return None;
    }
    let x = bytes[cursor - 1];
    if (x == b'x' || x == b'X') && bytes[cursor - 2] == b'#' && bytes[cursor - 3] == b'&' {
        Some(cursor - 3)
    } else {
        None
    }
}

// GFM bare-email literal (and the extended `mailto:`/`xmpp:` protocol forms).
// `index` must be the link start: cmark anchors the email at the left edge
// found by rewinding from `@` over `[A-Za-z0-9._+-]` (or a `mailto:`/`xmpp:`
// scheme), so this only succeeds when the char before `index` is not part of
// that left extent.
fn parse_literal_email(input: &str, index: usize) -> Option<(usize, String)> {
    let rest = &input[index..];
    let at = rest.find('@')?;
    if at == 0 {
        return None;
    }
    let local = &rest[..at];

    // Determine whether this `@` is preceded by an extended protocol scheme
    // (`mailto:` / `xmpp:`), which both relaxes the href synthesis and (xmpp)
    // allows `/` in the domain.
    let (auto_mailto, is_xmpp) = classify_email_local(local);

    // Left-boundary guard (autolink-1): the char before `index` must not be a
    // local-part continuation char, otherwise the true link starts earlier and
    // this position is interior. After a recognized scheme, the scheme's own
    // preceding-char rule is what matters.
    if !email_left_boundary_ok(input, index, auto_mailto) {
        return None;
    }

    if !email_local_is_valid(local, auto_mailto) {
        return None;
    }

    let domain_start = index + at + 1;
    let domain_end = literal_email_domain_end(input, domain_start, is_xmpp)?;
    let trimmed = autolink_delim(input, domain_start, domain_end);
    if trimmed <= domain_start {
        return None;
    }

    let domain = &input[domain_start..trimmed];
    if !is_gfm_email_domain(domain, is_xmpp) {
        return None;
    }

    let mut destination = String::new();
    if auto_mailto {
        destination.push_str("mailto:");
    }
    destination.push_str(&input[index..trimmed]);
    Some((trimmed, destination))
}

// Classify the local part for the extended-protocol forms. Returns
// `(auto_mailto, is_xmpp)`: `mailto:user` → (false, false); `xmpp:user` →
// (false, true); a bare local part → (true, false). The scheme match is
// case-insensitive.
fn classify_email_local(local: &str) -> (bool, bool) {
    if let Some(rest) = strip_ci_prefix(local, "mailto:") {
        if !rest.is_empty() {
            return (false, false);
        }
    }
    if let Some(rest) = strip_ci_prefix(local, "xmpp:") {
        if !rest.is_empty() {
            return (false, true);
        }
    }
    (true, false)
}

fn strip_ci_prefix<'a>(input: &'a str, prefix: &str) -> Option<&'a str> {
    let bytes = input.as_bytes();
    let plen = prefix.len();
    if bytes.len() >= plen && bytes[..plen].eq_ignore_ascii_case(prefix.as_bytes()) {
        Some(&input[plen..])
    } else {
        None
    }
}

// The left-boundary check for an email literal. The link is anchored at its
// true left edge: the preceding char must not be an ASCII alphanumeric (which
// would extend the local part leftward). For the bare form, a preceding `/` is
// also rejected (`/a@b.c` is not linked), while the extended
// `mailto:`/`xmpp:` form permits `/` before the scheme (so
// `…/mailto:beedrill@…` links).
fn email_left_boundary_ok(input: &str, index: usize, auto_mailto: bool) -> bool {
    if index == 0 {
        return true;
    }
    let Some(previous) = input[..index].chars().next_back() else {
        return true;
    };
    if previous.is_ascii_alphanumeric() {
        if auto_mailto
            && input[index..].starts_with('+')
            && prefix_ends_with_gfm_email(input, index)
        {
            return true;
        }
        return false;
    }
    if auto_mailto && previous == '/' {
        return false;
    }
    true
}

fn prefix_ends_with_gfm_email(input: &str, end: usize) -> bool {
    let start = input[..end]
        .rfind(char::is_whitespace)
        .map_or(0, |offset| offset + 1);
    let candidate = &input[start..end];
    let Some(at) = candidate.rfind('@') else {
        return false;
    };
    email_local_is_valid(&candidate[..at], true) && is_gfm_email_domain(&candidate[at + 1..], false)
}

// Validate the email local part. For the bare form, every char must be a GFM
// email atext byte (`[A-Za-z0-9.+_-]` plus the dot-separated structure). For
// the extended-protocol forms, the part after the scheme is validated.
fn email_local_is_valid(local: &str, auto_mailto: bool) -> bool {
    let body = if auto_mailto {
        local
    } else if let Some(rest) = strip_ci_prefix(local, "mailto:") {
        rest
    } else if let Some(rest) = strip_ci_prefix(local, "xmpp:") {
        rest
    } else {
        local
    };
    !body.is_empty() && body.bytes().all(is_gfm_email_local_byte)
}

// GFM email local-part charset (autolink-1): a narrower set than RFC atext,
// matching cmark's rewind class `[A-Za-z0-9.+_-]`.
fn is_gfm_email_local_byte(byte: u8) -> bool {
    byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'+' | b'_' | b'-')
}

fn is_email_local_part(input: &str) -> bool {
    !input.is_empty()
        && input
            .split('.')
            .all(|segment| !segment.is_empty() && segment.bytes().all(is_email_atext))
}

fn is_email_atext(byte: u8) -> bool {
    byte.is_ascii_alphanumeric()
        || matches!(
            byte,
            b'!' | b'#'
                | b'$'
                | b'%'
                | b'&'
                | b'\''
                | b'*'
                | b'+'
                | b'/'
                | b'='
                | b'?'
                | b'^'
                | b'_'
                | b'`'
                | b'{'
                | b'|'
                | b'}'
                | b'~'
                | b'-'
        )
}

// Port of cmark-gfm's email-domain scan (`postprocess_text`). Scans forward
// from `index` over the email domain, accepting alphanumerics, `-`, `_`, and
// `.`; for the `xmpp:` form a `/` is also accepted (path). A dot only counts
// toward the "at least one dot" requirement when it is followed by an
// alphanumeric. The scanned span must be >= 1 byte, contain at least one such
// dot, and end in an alphabetic char or a dot. Returns the domain end offset
// (before trailing trim), or `None` when invalid.
fn literal_email_domain_end(input: &str, index: usize, is_xmpp: bool) -> Option<usize> {
    let bytes = input.as_bytes();
    let mut end = index;
    let mut np = 0usize;
    while end < bytes.len() {
        let byte = bytes[end];
        if byte.is_ascii_alphanumeric() {
            end += 1;
        } else if byte == b'.' && end + 1 < bytes.len() && bytes[end + 1].is_ascii_alphanumeric() {
            np += 1;
            end += 1;
        } else if byte == b'-' || byte == b'_' || (byte == b'/' && is_xmpp) {
            // `-`/`_` always continue the domain; `/` continues only the xmpp
            // path form.
            end += 1;
        } else {
            break;
        }
    }
    if end <= index {
        return None;
    }
    let len = end - index;
    let last = bytes[end - 1];
    if len < 1 || np == 0 || !(last.is_ascii_alphabetic() || last == b'.') {
        return None;
    }
    Some(end)
}

// Final structural validation of the trimmed email domain. The cmark scan
// already enforced the dot/last-char rules; this re-checks them after the
// shared trailing trim removed any delimiters, and rejects a domain ending in
// `-`/`_` (autolink-7: a hyphen in the final label disqualifies the link).
fn is_gfm_email_domain(input: &str, is_xmpp: bool) -> bool {
    if input.is_empty() {
        return false;
    }
    // A `/` path is only legal in the `xmpp:` form; split it off for the host
    // structural checks.
    let host = if is_xmpp {
        input.split('/').next().unwrap_or(input)
    } else {
        input
    };
    if !host.contains('.') {
        return false;
    }
    let last = host.as_bytes()[host.len() - 1];
    // The final label must not end in `-` or `_`, and the trailing label may
    // not be all ASCII digits.
    if matches!(last, b'-' | b'_') {
        return false;
    }
    host.split('.').all(|label| {
        !label.is_empty()
            && label
                .bytes()
                .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_'))
    })
}

fn is_email_domain(input: &str, min_labels: usize) -> bool {
    let mut label_count = 0usize;
    for label in input.split('.') {
        label_count += 1;
        let bytes = label.as_bytes();
        if bytes.is_empty()
            || bytes.len() > 63
            || !bytes
                .first()
                .is_some_and(|byte| byte.is_ascii_alphanumeric())
            || !bytes
                .last()
                .is_some_and(|byte| byte.is_ascii_alphanumeric())
            || !bytes
                .iter()
                .all(|byte| byte.is_ascii_alphanumeric() || *byte == b'-')
        {
            return false;
        }
    }
    label_count >= min_labels
}

fn is_footnote_label(label: &str) -> bool {
    !label.is_empty()
        && reference_label_is_within_limit(label)
        && !label.chars().any(char::is_whitespace)
}

fn find_footnote_definition_label_end(input: &str) -> Option<usize> {
    let close = find_footnote_reference_label_end(input, 2)?;
    if input.as_bytes().get(close + 1) == Some(&b':') {
        Some(close)
    } else {
        None
    }
}

fn find_footnote_reference_label_end(input: &str, mut cursor: usize) -> Option<usize> {
    while cursor < input.len() {
        let (next, char) = next_char(input, cursor)?;
        if char == ']' && !is_escaped_at(input, cursor) {
            return Some(cursor);
        }
        cursor = next;
    }
    None
}

fn find_inline_footnote_end(input: &str, mut cursor: usize) -> Option<usize> {
    let mut depth = 0usize;
    while cursor < input.len() {
        let (next, char) = next_char(input, cursor)?;
        if !is_escaped_at(input, cursor) {
            match char {
                '[' => depth += 1,
                ']' if depth == 0 => return Some(cursor),
                ']' => depth = depth.saturating_sub(1),
                _ => {}
            }
        }
        cursor = next;
    }
    None
}