panache-parser 0.10.0

Lossless CST parser and syntax wrappers for Pandoc markdown, Quarto, and RMarkdown
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
//! Inline IR for both CommonMark and Pandoc dialects.
//!
//! The inline parsing pipeline runs in three passes over an intermediate
//! representation (IR):
//!
//! 1. **Scan** ([`build_ir`]): walk the source bytes once, producing a flat
//!    [`Vec<IrEvent>`]. Opaque higher-precedence constructs (escapes, code
//!    spans, autolinks, raw HTML, plus Pandoc math / native spans / inline
//!    footnotes / footnote references / citations / bracketed spans) are
//!    skipped past as a single [`IrEvent::Construct`] event whose source
//!    range is preserved for losslessness. Delimiter runs (`*`/`_`),
//!    bracket markers (`[`, `![`, `]`), soft line breaks, and plain text
//!    spans become distinct events.
//!
//! 2. **Process brackets** ([`process_brackets`]) — CommonMark §6.3: the
//!    bracket-stack algorithm walks `]` markers left-to-right. For each
//!    `]`, the algorithm finds the nearest active opener and tries to
//!    resolve the pair as a link or image: inline `[text](dest)`, full
//!    reference `[text][label]`, collapsed `[text][]`, or shortcut
//!    `[text]`. Under CommonMark, reference forms are validated against
//!    the document refdef map and a successful match deactivates all
//!    earlier active openers (§6.3 "links may not contain other links").
//!    Under Pandoc, reference forms resolve shape-only (any non-empty
//!    label) and the deactivation pass is skipped; outer-wins nested-link
//!    semantics are enforced by the emission walk's `suppress_inner_links`
//!    flag instead.
//!
//! 3. **Process emphasis** ([`process_emphasis_in_range`]): the classic
//!    delimiter-stack algorithm runs over the [`IrEvent::DelimRun`]
//!    events, pairing openers with closers and recording matches on the
//!    runs. Runs first scoped per resolved bracket pair (innermost
//!    first), then a top-level pass over the residual events. Each match
//!    consumes 1 or 2 inner-edge bytes from each side; leftover bytes
//!    fall through to literal text. Dialect gates (Pandoc flanking rules,
//!    mod-3 rejection, asymmetric (1,2)/(2,1) rejection, opener-count >= 4
//!    rejection, triple-emph nesting flip, cascade-then-rerun) branch on
//!    the `dialect` parameter.
//!
//! The emission walk in [`super::core::parse_inline_range_impl`] consumes
//! three byte-keyed plans built by [`build_full_plans`]: an
//! [`EmphasisPlan`] for delim-run dispositions, a [`BracketPlan`] for
//! resolved link/image bracket pairs, and a [`ConstructPlan`] for
//! standalone Pandoc constructs (inline footnotes, native spans, footnote
//! references, citations, bracketed spans). Matched delim runs become
//! `EMPHASIS` / `STRONG` nodes; matched bracket pairs become `LINK` /
//! `IMAGE` nodes via the dispatcher's `try_parse_*` recognizers (called
//! to *parse* a matched range, not to *resolve* it). Unmatched delims and
//! brackets fall through to plain text.

use crate::options::ParserOptions;
use crate::parser::inlines::refdef_map::{RefdefMap, normalize_label};
use std::collections::{BTreeMap, HashSet};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EmphasisKind {
    Emph,
    Strong,
}

/// Disposition of a single delimiter byte after emphasis resolution.
#[derive(Debug, Clone, Copy)]
pub enum DelimChar {
    /// Start of an opening marker. The marker spans `len` bytes from this
    /// position; the matching closer starts at `partner` and spans
    /// `partner_len` bytes.
    Open {
        len: u8,
        partner: usize,
        partner_len: u8,
        kind: EmphasisKind,
    },
    /// Start of a closing marker. The matching opener starts at `partner`.
    /// Emission jumps past close markers via the matching `Open` entry, so
    /// this variant is only consulted defensively.
    Close,
    /// Unmatched delimiter byte; emit as literal text.
    Literal,
}

/// Byte-keyed disposition map for `*` / `_` delimiter chars produced by
/// the IR's emphasis pass and consumed by the inline emission walk.
#[derive(Debug, Default, Clone)]
pub struct EmphasisPlan {
    by_pos: BTreeMap<usize, DelimChar>,
}

impl EmphasisPlan {
    pub fn lookup(&self, pos: usize) -> Option<DelimChar> {
        self.by_pos.get(&pos).copied()
    }

    pub fn is_empty(&self) -> bool {
        self.by_pos.is_empty()
    }

    /// Construct an `EmphasisPlan` from a byte-keyed disposition map.
    pub fn from_dispositions(by_pos: BTreeMap<usize, DelimChar>) -> Self {
        Self { by_pos }
    }
}

use super::bracketed_spans::try_parse_bracketed_span;
use super::citations::{try_parse_bare_citation, try_parse_bracketed_citation};
use super::code_spans::try_parse_code_span;
use super::escapes::{EscapeType, try_parse_escape};
use super::inline_footnotes::{try_parse_footnote_reference, try_parse_inline_footnote};
use super::inline_html::try_parse_inline_html;
use super::links::{
    LinkScanContext, try_parse_autolink, try_parse_inline_image, try_parse_inline_link,
    try_parse_reference_image, try_parse_reference_link,
};
use super::math::{
    try_parse_display_math, try_parse_double_backslash_display_math,
    try_parse_double_backslash_inline_math, try_parse_gfm_inline_math, try_parse_inline_math,
    try_parse_single_backslash_display_math, try_parse_single_backslash_inline_math,
};
use super::native_spans::try_parse_native_span;

/// One event in the inline IR.
///
/// Events partition the source byte range covered by the IR exactly: their
/// `range()` values are contiguous and non-overlapping, so concatenating
/// them reproduces the original input. This is the losslessness invariant
/// the emission pass relies on.
#[derive(Debug, Clone)]
pub enum IrEvent {
    /// Plain text byte span. Emitted as a single `TEXT` token, possibly
    /// merged with adjacent literal-disposition delim/bracket bytes.
    Text { start: usize, end: usize },

    /// An opaque higher-precedence construct (escape, code span, autolink,
    /// raw HTML). The emission pass re-parses these from the source byte
    /// range using the existing per-construct emitters; we don't store a
    /// pre-built `GreenNode` because `rowan::GreenNodeBuilder` doesn't
    /// support inserting subtrees directly. The byte range is what makes
    /// emission well-defined — the construct kind is recovered by the
    /// emitter dispatching on the leading byte.
    Construct {
        start: usize,
        end: usize,
        kind: ConstructKind,
    },

    /// A `*` or `_` delimiter run. The `matches` vec is filled in by
    /// [`process_emphasis`]; before that pass it is empty.
    DelimRun {
        ch: u8,
        start: usize,
        end: usize,
        can_open: bool,
        can_close: bool,
        /// Matched fragments produced by `process_emphasis`. Each entry
        /// is one `(byte_offset_within_run, len, partner_event_idx,
        /// partner_byte_offset, kind, is_opener)` tuple. Empty until the
        /// pass runs; possibly multiple entries when a single run matches
        /// at multiple positions (e.g. a 4-run that closes 2+2 pairs).
        matches: Vec<DelimMatch>,
    },

    /// `[` or `![` bracket marker. Resolved by [`process_brackets`].
    OpenBracket {
        start: usize,
        /// `start + 1` for `[`, `start + 2` for `![`.
        end: usize,
        is_image: bool,
        /// True until a later resolution rule deactivates this opener.
        active: bool,
        /// Filled in when the matching `CloseBracket` resolves the pair
        /// to a link / image.
        resolution: Option<BracketResolution>,
        /// Pandoc-only: extents of an unresolved bracket-shape pattern
        /// (full reference / collapsed / shortcut whose label doesn't
        /// match a refdef). Mutually exclusive with `resolution:
        /// Some(...)`. When `Some`, emission wraps `[start, end)` in
        /// an `UNRESOLVED_REFERENCE` node so downstream tools can
        /// attach behavior to the bracket-shape pattern. Always
        /// `None` under `Dialect::CommonMark`.
        unresolved_ref: Option<UnresolvedRefShape>,
    },

    /// `]` bracket marker. Resolved by [`process_brackets`].
    CloseBracket {
        pos: usize,
        /// True if this `]` was paired with an opener and the pair was
        /// turned into a link / image.
        matched: bool,
    },

    /// A soft line break (a `\n` or `\r\n` ending a paragraph-internal
    /// line). Includes the line-ending bytes verbatim.
    SoftBreak { start: usize, end: usize },

    /// A hard line break (`  \n` / `\\\n` / `   \n` etc.). Includes any
    /// trailing-space bytes plus the line ending.
    HardBreak { start: usize, end: usize },
}

impl IrEvent {
    /// The source byte range this event covers.
    pub fn range(&self) -> (usize, usize) {
        match self {
            IrEvent::Text { start, end }
            | IrEvent::Construct { start, end, .. }
            | IrEvent::DelimRun { start, end, .. }
            | IrEvent::OpenBracket { start, end, .. }
            | IrEvent::SoftBreak { start, end }
            | IrEvent::HardBreak { start, end } => (*start, *end),
            IrEvent::CloseBracket { pos, .. } => (*pos, *pos + 1),
        }
    }
}

/// Categorical tag for a [`IrEvent::Construct`] event so emission knows
/// which parser to call to rebuild the CST subtree.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConstructKind {
    /// `\X` literal-character escape (CommonMark §2.4).
    Escape,
    /// `` `code` `` span (§6.1).
    CodeSpan,
    /// `<scheme://...>` or `<email@host>` (§6.5).
    Autolink,
    /// `<tag ...>` and friends (§6.6).
    InlineHtml,
    /// Pandoc opaque construct that doesn't have a dedicated kind yet
    /// (currently: math spans). Pre-recognised in `build_ir` under
    /// `Dialect::Pandoc` solely so the emphasis pass treats the entire
    /// construct as opaque and delim runs inside don't cross its
    /// boundary. Emission re-parses the construct via the dispatcher's
    /// existing `try_parse_*` chain.
    PandocOpaque,
    /// Pandoc inline footnote `^[note text]`. Recognised in `build_ir`
    /// under `Dialect::Pandoc` and consumed by the emission walk via
    /// the IR's `ConstructPlan`. The dispatcher's legacy `^[` branch
    /// is gated to CommonMark dialect only.
    InlineFootnote,
    /// Pandoc native span `<span ...>...</span>`. Recognised in
    /// `build_ir` under `Dialect::Pandoc` and consumed by the emission
    /// walk via the IR's `ConstructPlan`. The dispatcher's legacy
    /// `<span>` branch is gated to CommonMark dialect only.
    NativeSpan,
    /// Pandoc footnote reference `[^id]`. Recognised in `build_ir`
    /// under `Dialect::Pandoc` and consumed by the emission walk via
    /// the IR's `ConstructPlan`. The dispatcher's legacy `[^id]`
    /// branch is gated to CommonMark dialect only.
    FootnoteReference,
    /// Pandoc bracketed citation `[@key]`, `[see @key, p. 1]`,
    /// `[@a; @b]`. Recognised in `build_ir` under `Dialect::Pandoc`
    /// and consumed by the emission walk via the IR's `ConstructPlan`.
    /// The dispatcher's legacy `[@cite]` branch is gated to CommonMark
    /// dialect only.
    BracketedCitation,
    /// Pandoc bare citation `@key` or `-@key` (author-in-text /
    /// suppress-author). Recognised in `build_ir` under
    /// `Dialect::Pandoc` and consumed by the emission walk via the
    /// IR's `ConstructPlan`. The dispatcher's legacy `@` and `-@`
    /// branches are gated to CommonMark dialect only.
    BareCitation,
    /// Pandoc bracketed span `[content]{attrs}`. Recognised in
    /// `build_ir` under `Dialect::Pandoc` and consumed by the emission
    /// walk via the IR's `ConstructPlan`. The dispatcher's legacy
    /// `[text]{attrs}` branch is gated to CommonMark dialect only.
    BracketedSpan,
}

/// One matched fragment within a [`IrEvent::DelimRun`].
#[derive(Debug, Clone, Copy)]
pub struct DelimMatch {
    /// Byte offset of this fragment relative to the run's `start`.
    pub offset_in_run: u8,
    /// Number of bytes in this fragment (1 or 2).
    pub len: u8,
    /// Whether this fragment is the opener (`true`) or closer of the pair.
    pub is_opener: bool,
    /// IR event index of the partner run.
    pub partner_event: u32,
    /// Byte offset within the partner run of the partner fragment.
    pub partner_offset: u8,
    /// Emphasis kind (Emph for `len == 1`, Strong for `len == 2`).
    pub kind: EmphasisKind,
}

/// Pandoc-only: extents of an unresolved bracket-shape reference
/// pattern. Recorded on `IrEvent::OpenBracket.unresolved_ref` when the
/// no-resolution fall-through fires under `Dialect::Pandoc`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct UnresolvedRefShape {
    /// IR event index of the matching `CloseBracket`. Used by the
    /// scoped-emphasis pass to treat the wrapper as a tree boundary.
    pub close_event: u32,
    /// One past the end of the inner text (the byte position of the
    /// outer `]`). Combined with the opener's `end` field, this is the
    /// inner text range that goes through normal inline parsing.
    pub text_end: usize,
    /// One past the end of the full bracket-shape pattern. For
    /// shortcut form `[text]`: `close_pos + 1`. For collapsed
    /// `[text][]`: `close_pos + 3`. For full `[text][label]`: the byte
    /// after the closing `]` of `[label]`.
    pub end: usize,
}

/// Successful bracket resolution: the `[`...`]` pair is a link or image.
#[derive(Debug, Clone)]
pub struct BracketResolution {
    /// IR event index of the matching `CloseBracket`.
    pub close_event: u32,
    /// Source range of the link text (between `[`/`![` and `]`).
    pub text_start: usize,
    pub text_end: usize,
    /// Source range of the link suffix (`(...)`, `[label]`, `[]`, or
    /// empty for shortcut). When `kind == ShortcutReference`,
    /// `suffix_start == suffix_end == close_pos + 1`.
    pub suffix_start: usize,
    pub suffix_end: usize,
    pub kind: LinkKind,
}

/// What kind of link/image we resolved a bracket pair to.
#[derive(Debug, Clone)]
pub enum LinkKind {
    /// `[text](dest)` or `[text](dest "title")`.
    Inline { dest: String, title: Option<String> },
    /// `[text][label]` — explicit reference.
    FullReference { label: String },
    /// `[text][]` — collapsed reference. Label is the link text.
    CollapsedReference,
    /// `[text]` — shortcut reference. Label is the link text.
    ShortcutReference,
}

// ============================================================================
// Pass 1: Scan
// ============================================================================

/// Scan `text[start..end]` once, producing a flat IR of events.
///
/// The scan is forward-only and never backtracks: each iteration either
/// consumes a known construct (escape, code span, autolink, raw HTML),
/// records a delim run / bracket marker / line break, or steps past a
/// single UTF-8 boundary as plain text. Adjacent text bytes are coalesced
/// into a single [`IrEvent::Text`] event by the run-flush step.
pub fn build_ir(text: &str, start: usize, end: usize, config: &ParserOptions) -> Vec<IrEvent> {
    let mut events = Vec::new();
    build_ir_into(text, start, end, config, &mut events);
    events
}

/// Like [`build_ir`] but writes into a caller-provided `Vec<IrEvent>`,
/// clearing it first. Used by [`build_full_plans`] to amortise the
/// per-call allocation through a thread-local scratch pool.
pub(super) fn build_ir_into(
    text: &str,
    start: usize,
    end: usize,
    config: &ParserOptions,
    events: &mut Vec<IrEvent>,
) {
    events.clear();
    let bytes = text.as_bytes();
    let exts = &config.extensions;
    let is_commonmark = config.dialect == crate::options::Dialect::CommonMark;

    let mut pos = start;
    let mut text_run_start = start;
    // Pandoc-only: extent of the current bracket-shape link/image's
    // opaque range. While `pos < pandoc_bracket_extent`, autolinks /
    // raw HTML / native spans are NOT recognised — pandoc-native
    // treats `[link text]` as opaque to those constructs (CommonMark
    // spec example #526 / #538). The lookahead at `[`/`![` sets this
    // when a bracket-shape forms a valid link/image; once `pos`
    // passes the extent, normal scanning resumes. CommonMark
    // dialect's link-text-vs-autolink ordering is handled by the
    // dispatcher's `try_parse_inline_link` rejecting outer matches
    // when the link text contains a valid autolink (a different
    // mechanism, see `LinkScanContext.skip_autolinks`).
    let mut pandoc_bracket_extent: usize = 0;

    // Pre-computed byte mask: `mask[b]` is `true` iff byte `b` could
    // start any IR-recognised construct under the current dialect /
    // extensions. Used to bulk-skip plain bytes between structural
    // bytes — the per-byte branch chain below only runs at positions
    // where a construct is actually possible. Non-ASCII bytes
    // (>= 0x80) are never structural and are skipped together with
    // ASCII plain text.
    let mask = build_ir_byte_mask(config);

    macro_rules! flush_text {
        () => {
            if pos > text_run_start {
                events.push(IrEvent::Text {
                    start: text_run_start,
                    end: pos,
                });
            }
        };
    }

    while pos < end {
        // Fast-skip plain bytes. `text_run_start` is preserved across
        // the skip so the next structural-event flush picks them up.
        while pos < end && !mask[bytes[pos] as usize] {
            pos += 1;
        }
        if pos >= end {
            break;
        }
        let b = bytes[pos];

        // Pandoc-only: at `[` or `![`, look ahead to see if this
        // bracket-shape forms a valid link/image. If so, suppress
        // autolink / raw HTML / native span recognition until `pos`
        // passes the bracket-shape's end. Skipped if we're already
        // inside an enclosing bracket-shape's opaque range.
        if !is_commonmark
            && pos >= pandoc_bracket_extent
            && (b == b'[' || (b == b'!' && pos + 1 < end && bytes[pos + 1] == b'['))
            && let Some(len) = try_pandoc_bracket_link_extent(text, pos, end, config)
        {
            pandoc_bracket_extent = pos + len;
        }
        let in_pandoc_bracket = !is_commonmark && pos < pandoc_bracket_extent;

        // Backslash escape (§2.4) — including `\\\n` hard line break.
        if b == b'\\'
            && let Some((len, _ch, escape_type)) = try_parse_escape(&text[pos..])
            && pos + len <= end
        {
            let enabled = match escape_type {
                EscapeType::Literal => is_commonmark || exts.all_symbols_escapable,
                EscapeType::HardLineBreak => exts.escaped_line_breaks,
                EscapeType::NonbreakingSpace => exts.all_symbols_escapable,
            };
            if enabled {
                flush_text!();
                let kind = match escape_type {
                    EscapeType::HardLineBreak => {
                        events.push(IrEvent::HardBreak {
                            start: pos,
                            end: pos + len,
                        });
                        pos += len;
                        text_run_start = pos;
                        continue;
                    }
                    EscapeType::Literal | EscapeType::NonbreakingSpace => ConstructKind::Escape,
                };
                events.push(IrEvent::Construct {
                    start: pos,
                    end: pos + len,
                    kind,
                });
                pos += len;
                text_run_start = pos;
                continue;
            }
        }

        // Code span (§6.1) — opaque to emphasis and brackets.
        if b == b'`'
            && let Some((len, _, _, _)) = try_parse_code_span(&text[pos..])
            && pos + len <= end
        {
            flush_text!();
            events.push(IrEvent::Construct {
                start: pos,
                end: pos + len,
                kind: ConstructKind::CodeSpan,
            });
            pos += len;
            text_run_start = pos;
            continue;
        }

        // Pandoc-only: math spans are opaque to emphasis. The legacy
        // `parse_until_closer_with_nested_*` skip-list includes inline
        // math; without recognising it here, delim runs inside `$math$`
        // would be picked up by the emphasis pass and break losslessness
        // (the dispatcher's math parser would later re-claim the bytes,
        // duplicating content).
        if !is_commonmark && let Some(len) = try_pandoc_math_opaque(text, pos, end, config) {
            flush_text!();
            events.push(IrEvent::Construct {
                start: pos,
                end: pos + len,
                kind: ConstructKind::PandocOpaque,
            });
            pos += len;
            text_run_start = pos;
            continue;
        }

        // Pandoc-only: native span `<span ...>...</span>`. Must come
        // before the generic autolink/raw-html branches so the open tag
        // doesn't get claimed as inline HTML. Span content is opaque to
        // the emphasis pass; emission consumes the event via the IR's
        // `ConstructPlan`. Suppressed inside Pandoc bracket-shape
        // link/image text.
        if !is_commonmark
            && !in_pandoc_bracket
            && b == b'<'
            && exts.native_spans
            && let Some((len, _, _)) = try_parse_native_span(&text[pos..])
            && pos + len <= end
        {
            flush_text!();
            events.push(IrEvent::Construct {
                start: pos,
                end: pos + len,
                kind: ConstructKind::NativeSpan,
            });
            pos += len;
            text_run_start = pos;
            continue;
        }

        // Autolink (§6.5) before raw HTML — autolinks are the more
        // specific shape inside `<...>`. Both are suppressed inside
        // Pandoc bracket-shape link/image text (pandoc-native treats
        // link text as opaque to autolinks and raw HTML).
        if b == b'<' && !in_pandoc_bracket {
            if exts.autolinks
                && let Some((len, _)) = try_parse_autolink(&text[pos..], is_commonmark)
                && pos + len <= end
            {
                flush_text!();
                events.push(IrEvent::Construct {
                    start: pos,
                    end: pos + len,
                    kind: ConstructKind::Autolink,
                });
                pos += len;
                text_run_start = pos;
                continue;
            }
            if exts.raw_html
                && let Some(len) = try_parse_inline_html(&text[pos..], config.dialect)
                && pos + len <= end
            {
                flush_text!();
                events.push(IrEvent::Construct {
                    start: pos,
                    end: pos + len,
                    kind: ConstructKind::InlineHtml,
                });
                pos += len;
                text_run_start = pos;
                continue;
            }
        }

        // Pandoc-only: inline footnote `^[note]`. Recognized at scan
        // time so the emphasis pass treats it as opaque (delim runs
        // inside the footnote can't pair with delim runs outside).
        if !is_commonmark
            && b == b'^'
            && exts.inline_footnotes
            && let Some((len, _)) = try_parse_inline_footnote(&text[pos..])
            && pos + len <= end
        {
            flush_text!();
            events.push(IrEvent::Construct {
                start: pos,
                end: pos + len,
                kind: ConstructKind::InlineFootnote,
            });
            pos += len;
            text_run_start = pos;
            continue;
        }

        // Pandoc-only: footnote reference `[^id]`. Recognised at scan
        // time so the emphasis pass treats it as opaque (delim runs
        // inside the label can't pair with delim runs outside) and the
        // emission walk dispatches it directly via the IR's
        // `ConstructPlan`. Must come before the generic bracket-opaque
        // scan so the dedicated kind wins.
        if !is_commonmark
            && b == b'['
            && pos + 1 < end
            && bytes[pos + 1] == b'^'
            && exts.footnotes
            && let Some((len, _)) = try_parse_footnote_reference(&text[pos..])
            && pos + len <= end
        {
            flush_text!();
            events.push(IrEvent::Construct {
                start: pos,
                end: pos + len,
                kind: ConstructKind::FootnoteReference,
            });
            pos += len;
            text_run_start = pos;
            continue;
        }

        // Pandoc-only: bracketed citation `[@cite]`. Recognised at
        // scan time so the emphasis pass treats it as opaque (delim
        // runs inside the citation can't pair with delim runs outside)
        // and the emission walk dispatches it directly via the IR's
        // `ConstructPlan`. Must come before the generic bracket-opaque
        // scan so the dedicated kind wins.
        if !is_commonmark
            && b == b'['
            && exts.citations
            && let Some((len, _)) = try_parse_bracketed_citation(&text[pos..])
            && pos + len <= end
        {
            flush_text!();
            events.push(IrEvent::Construct {
                start: pos,
                end: pos + len,
                kind: ConstructKind::BracketedCitation,
            });
            pos += len;
            text_run_start = pos;
            continue;
        }

        // Pandoc-only: bare citation `@key` or `-@key`. Recognised at
        // scan time so the emission walk dispatches it directly via
        // the IR's `ConstructPlan`. Bare citations don't contain
        // emphasis-eligible content, so opacity is moot here — IR
        // participation is only for dispatch consolidation.
        if !is_commonmark
            && (b == b'@' || (b == b'-' && pos + 1 < end && bytes[pos + 1] == b'@'))
            && (exts.citations || exts.quarto_crossrefs)
            && let Some((len, _, _)) = try_parse_bare_citation(&text[pos..])
            && pos + len <= end
        {
            flush_text!();
            events.push(IrEvent::Construct {
                start: pos,
                end: pos + len,
                kind: ConstructKind::BareCitation,
            });
            pos += len;
            text_run_start = pos;
            continue;
        }

        // Pandoc-only: bracketed span `[content]{attrs}`. Recognised
        // at scan time so the emphasis pass treats it as opaque (delim
        // runs inside the span content can't pair with delim runs
        // outside) and the emission walk dispatches it directly via
        // the IR's `ConstructPlan`. Must come before the generic
        // bracket-opaque scan so the dedicated kind wins.
        // `try_parse_bracketed_span` requires `]` to be immediately
        // followed by `{`, so this never shadows inline links
        // (`[text](url)`) or reference links (`[label][refdef]`) —
        // those don't have the `{attrs}` suffix.
        if !is_commonmark
            && b == b'['
            && exts.bracketed_spans
            && let Some((len, _, _)) = try_parse_bracketed_span(&text[pos..])
            && pos + len <= end
        {
            flush_text!();
            events.push(IrEvent::Construct {
                start: pos,
                end: pos + len,
                kind: ConstructKind::BracketedSpan,
            });
            pos += len;
            text_run_start = pos;
            continue;
        }

        // `![` opens an image bracket. Recognised whenever any
        // image-producing extension is on — `inline_images` for the
        // `![alt](url)` form, or `reference_links` for the
        // `![alt][label]` reference-image form (e.g. MultiMarkdown
        // disables `inline_images` but uses reference images).
        if b == b'!'
            && pos + 1 < end
            && bytes[pos + 1] == b'['
            && (exts.inline_images || exts.reference_links)
        {
            flush_text!();
            events.push(IrEvent::OpenBracket {
                start: pos,
                end: pos + 2,
                is_image: true,
                active: true,
                resolution: None,
                unresolved_ref: None,
            });
            pos += 2;
            text_run_start = pos;
            continue;
        }

        // `[` opens a link bracket. Recognised whenever any
        // link-producing extension is on — `inline_links` for
        // `[text](url)`, or `reference_links` for `[text][label]` /
        // `[text]` shortcut form.
        if b == b'[' && (exts.inline_links || exts.reference_links) {
            flush_text!();
            events.push(IrEvent::OpenBracket {
                start: pos,
                end: pos + 1,
                is_image: false,
                active: true,
                resolution: None,
                unresolved_ref: None,
            });
            pos += 1;
            text_run_start = pos;
            continue;
        }

        // `]` closes a link/image bracket.
        if b == b']' {
            flush_text!();
            events.push(IrEvent::CloseBracket {
                pos,
                matched: false,
            });
            pos += 1;
            text_run_start = pos;
            continue;
        }

        // `*` or `_` delimiter run.
        if b == b'*' || b == b'_' {
            flush_text!();
            let mut run_end = pos;
            while run_end < end && bytes[run_end] == b {
                run_end += 1;
            }
            let count = run_end - pos;
            let (can_open, can_close) = compute_flanking(text, pos, count, b, config.dialect);
            events.push(IrEvent::DelimRun {
                ch: b,
                start: pos,
                end: run_end,
                can_open,
                can_close,
                matches: Vec::new(),
            });
            pos = run_end;
            text_run_start = pos;
            continue;
        }

        // Hard line break: 2+ trailing spaces before newline. We detect
        // this when we're sitting on a `\n` (or `\r\n`) and the preceding
        // bytes within the current text run are spaces.
        if b == b'\n' || (b == b'\r' && pos + 1 < end && bytes[pos + 1] == b'\n') {
            // Count trailing spaces in the text accumulated so far.
            let nl_len = if b == b'\r' { 2 } else { 1 };
            let mut trailing_spaces = 0;
            let mut s = pos;
            while s > text_run_start && bytes[s - 1] == b' ' {
                trailing_spaces += 1;
                s -= 1;
            }
            if trailing_spaces >= 2 {
                // Flush text *before* the trailing spaces.
                if s > text_run_start {
                    events.push(IrEvent::Text {
                        start: text_run_start,
                        end: s,
                    });
                }
                events.push(IrEvent::HardBreak {
                    start: s,
                    end: pos + nl_len,
                });
                pos += nl_len;
                text_run_start = pos;
                continue;
            }

            // Soft line break: flush preceding text, emit the line ending
            // as its own event so the emitter can render `NEWLINE` tokens
            // verbatim.
            flush_text!();
            events.push(IrEvent::SoftBreak {
                start: pos,
                end: pos + nl_len,
            });
            pos += nl_len;
            text_run_start = pos;
            continue;
        }

        // Plain byte — advance one UTF-8 char.
        let ch_len = text[pos..]
            .chars()
            .next()
            .map_or(1, std::primitive::char::len_utf8);
        pos += ch_len.max(1);
    }

    flush_text!();
}

/// Build a 256-entry mask: `mask[b]` is `true` iff byte `b` could start
/// any IR-recognised construct under the current dialect / extensions.
///
/// This is the build-IR-specific superset of "is this byte interesting".
/// Plain bytes between structural bytes are bulk-skipped via this mask
/// in the [`build_ir`] hot loop; missing a byte here is a correctness
/// bug (we'd skip past a real construct), but having extras only costs
/// us a wasted branch round-trip.
fn build_ir_byte_mask(config: &ParserOptions) -> [bool; 256] {
    let mut mask = [false; 256];
    let exts = &config.extensions;
    let is_commonmark = config.dialect == crate::options::Dialect::CommonMark;

    // Always structural for IR scanning:
    //   `\n` / `\r` — soft / hard breaks
    //   `\\`        — escape, hard line break, backslash math
    //   `` ` ``     — code span (IR construct)
    //   `*` / `_`   — emphasis delim runs (IR core)
    mask[b'\n' as usize] = true;
    mask[b'\r' as usize] = true;
    mask[b'\\' as usize] = true;
    mask[b'`' as usize] = true;
    mask[b'*' as usize] = true;
    mask[b'_' as usize] = true;

    // Brackets: scanned whenever any bracket-shaped construct is
    // reachable. `]` is structural unconditionally if `[` is — the IR
    // emits a CloseBracket event regardless of which opener variant
    // matches. `!` is gated on image-producing extensions; the leading
    // `!` of `![alt]` is the only image entry point.
    if exts.inline_links
        || exts.reference_links
        || exts.inline_images
        || exts.bracketed_spans
        || exts.footnotes
        || exts.citations
    {
        mask[b'[' as usize] = true;
        mask[b']' as usize] = true;
    }
    if exts.inline_images || exts.reference_links {
        mask[b'!' as usize] = true;
    }

    // `<` covers autolinks, raw HTML, and Pandoc native spans.
    if exts.autolinks || exts.raw_html || (!is_commonmark && exts.native_spans) {
        mask[b'<' as usize] = true;
    }

    // `^` covers Pandoc inline footnotes (`^[...]` recognised in IR
    // under Pandoc dialect). CM dialect inline footnotes go through
    // the dispatcher, not the IR.
    if !is_commonmark && exts.inline_footnotes {
        mask[b'^' as usize] = true;
    }

    // `@` covers Pandoc bare citation `@key` and `[@cite]`. The leading
    // `[` of `[@cite]` is already in the mask via the bracket gate;
    // gating `@` here also covers the bare-citation form.
    if !is_commonmark && (exts.citations || exts.quarto_crossrefs) {
        mask[b'@' as usize] = true;
        // `-` only matters as the first byte of `-@cite`. Tracking it
        // here avoids missing the suppress-author bare citation form.
        mask[b'-' as usize] = true;
    }

    // `$` covers Pandoc dollar / GFM math. CM doesn't recognise math
    // in `build_ir`.
    if !is_commonmark
        && (exts.tex_math_dollars
            || exts.tex_math_gfm
            || exts.tex_math_single_backslash
            || exts.tex_math_double_backslash)
    {
        mask[b'$' as usize] = true;
    }

    mask
}

// ============================================================================
// Flanking (CommonMark §6.2)
// ============================================================================

fn compute_flanking(
    text: &str,
    pos: usize,
    count: usize,
    ch: u8,
    dialect: crate::options::Dialect,
) -> (bool, bool) {
    if dialect == crate::options::Dialect::Pandoc {
        // Pandoc-markdown's recursive-descent emphasis parser does NOT
        // apply CommonMark §6.2 flanking rules. Instead it gates on:
        //   - opener: must not be followed by whitespace (Pandoc
        //     `try_parse_emphasis` line 247 in legacy core.rs).
        //   - closer: no flanking gate at all (Pandoc-markdown's
        //     `ender` parser only counts characters; see Markdown.hs
        //     in pandoc/src/Text/Pandoc/Readers/Markdown.hs).
        //   - underscore intraword hard rule: `_` adjacent to an
        //     alphanumeric on either side cannot open / close
        //     (Pandoc's `intraword_underscores` extension default).
        let prev_char = (pos > 0).then(|| text[..pos].chars().last()).flatten();
        let next_char = text.get(pos + count..).and_then(|s| s.chars().next());
        let followed_by_ws = next_char.is_none_or(|c| c.is_whitespace());

        let mut can_open = !followed_by_ws;
        // Pandoc-markdown's `ender` (in pandoc/Readers/Markdown.hs)
        // has no flanking restriction on closers — just a count match.
        // Set can_close unconditionally; the per-pair match logic in
        // `process_emphasis_in_range_filtered` constrains pairing via
        // the equal-count rule.
        let mut can_close = true;

        if ch == b'_' {
            let prev_is_alnum = prev_char.is_some_and(|c| c.is_alphanumeric());
            let next_is_alnum = next_char.is_some_and(|c| c.is_alphanumeric());
            if prev_is_alnum {
                can_open = false;
            }
            if next_is_alnum {
                can_close = false;
            }
        }

        return (can_open, can_close);
    }

    // CommonMark §6.2 flanking.
    let lf = is_left_flanking(text, pos, count);
    let rf = is_right_flanking(text, pos, count);
    if ch == b'*' {
        (lf, rf)
    } else {
        let prev_char = (pos > 0).then(|| text[..pos].chars().last()).flatten();
        let next_char = text.get(pos + count..).and_then(|s| s.chars().next());
        let preceded_by_punct = prev_char.is_some_and(is_unicode_punct_or_symbol);
        let followed_by_punct = next_char.is_some_and(is_unicode_punct_or_symbol);
        let can_open = lf && (!rf || preceded_by_punct);
        let can_close = rf && (!lf || followed_by_punct);
        (can_open, can_close)
    }
}

/// Pandoc-only: identify a math span starting at `pos` and return its
/// byte length. Tries `$math$` and `$$display$$` (gated on
/// `tex_math_dollars`), GFM `$math$` (gated on `tex_math_gfm`), and the
/// `\(math\)` / `\[math\]` / `\\(math\\)` / `\\[math\\]` backslash
/// forms (gated on `tex_math_single_backslash` / `_double_backslash`).
/// Math content is opaque to emphasis: `$a * b$` must not produce an
/// emphasis closer at the inner `*`.
fn try_pandoc_math_opaque(
    text: &str,
    pos: usize,
    end: usize,
    config: &ParserOptions,
) -> Option<usize> {
    let bytes = text.as_bytes();
    let exts = &config.extensions;
    let b = bytes[pos];

    if exts.tex_math_dollars && b == b'$' {
        if let Some((len, _)) = try_parse_display_math(&text[pos..])
            && pos + len <= end
        {
            return Some(len);
        }
        if let Some((len, _)) = try_parse_inline_math(&text[pos..])
            && pos + len <= end
        {
            return Some(len);
        }
    }
    if exts.tex_math_gfm
        && b == b'$'
        && let Some((len, _)) = try_parse_gfm_inline_math(&text[pos..])
        && pos + len <= end
    {
        return Some(len);
    }
    if exts.tex_math_double_backslash && b == b'\\' {
        if let Some((len, _)) = try_parse_double_backslash_display_math(&text[pos..])
            && pos + len <= end
        {
            return Some(len);
        }
        if let Some((len, _)) = try_parse_double_backslash_inline_math(&text[pos..])
            && pos + len <= end
        {
            return Some(len);
        }
    }
    if exts.tex_math_single_backslash && b == b'\\' {
        if let Some((len, _)) = try_parse_single_backslash_display_math(&text[pos..])
            && pos + len <= end
        {
            return Some(len);
        }
        if let Some((len, _)) = try_parse_single_backslash_inline_math(&text[pos..])
            && pos + len <= end
        {
            return Some(len);
        }
    }
    None
}

/// Pandoc-only: identify a bracket-shaped opaque construct starting at
/// `pos` and return its byte length. Tries the dispatcher's precedence
/// order:
///   1. `![alt](dest)` inline image
///   2. `![alt][ref]` / `![alt]` reference image (shape-only opacity)
///   3. `[^id]` footnote reference
///   4. `[text](dest)` inline link
///   5. `[text][ref]` / `[text]` reference link (shape-only opacity)
///   6. `[@cite]` bracketed citation
///   7. `[text]{attrs}` bracketed span
///
/// Returns `None` if the bytes at `pos` don't open any recognised Pandoc
/// bracket-shaped construct. In that case the scanner falls through to
/// the generic `OpenBracket`/`CloseBracket` emission and the dispatcher
/// emits the bracket bytes as literal text (or as plain emphasis if the
/// pattern matches an opener).
/// Lookahead helper: at a `[` or `![` byte under Pandoc dialect, return
/// the total byte length of the bracket-shape link/image if it forms a
/// valid one, else `None`. Used by `build_ir` to suppress autolink /
/// raw HTML / native span recognition inside Pandoc link text —
/// pandoc-native treats link text as opaque to those constructs
/// (CommonMark spec example #526 / #538 differs). Mirrors the
/// dispatcher's `try_parse_*` precedence so the lookahead, the IR's
/// `process_brackets` resolution, and the dispatcher's emission agree
/// on the bracket-shape's byte boundaries.
fn try_pandoc_bracket_link_extent(
    text: &str,
    pos: usize,
    end: usize,
    config: &ParserOptions,
) -> Option<usize> {
    let bytes = text.as_bytes();
    let exts = &config.extensions;
    let ctx = LinkScanContext::from_options(config);
    let allow_shortcut = exts.shortcut_reference_links;

    // `![...]` images.
    if bytes[pos] == b'!' {
        if pos + 1 >= end || bytes[pos + 1] != b'[' {
            return None;
        }
        if exts.inline_images
            && let Some((len, _, _, _)) = try_parse_inline_image(&text[pos..], ctx)
            && pos + len <= end
        {
            return Some(len);
        }
        if exts.reference_links
            && let Some((len, _, _, _)) = try_parse_reference_image(&text[pos..], allow_shortcut)
            && pos + len <= end
        {
            return Some(len);
        }
        return None;
    }

    // `[...]` openers — try in dispatcher order. Footnote refs
    // (`[^id]`), bracketed citations (`[@cite]`), and bracketed spans
    // (`[text]{attrs}`) are recognised by their own dedicated branches
    // in `build_ir` and don't need this lookahead.
    if exts.inline_links
        && let Some((len, _, _, _)) = try_parse_inline_link(&text[pos..], false, ctx)
        && pos + len <= end
    {
        return Some(len);
    }
    if exts.reference_links
        && let Some((len, _, _, _)) =
            try_parse_reference_link(&text[pos..], allow_shortcut, exts.inline_links, ctx)
        && pos + len <= end
    {
        return Some(len);
    }

    None
}

fn is_unicode_punct_or_symbol(c: char) -> bool {
    if c.is_ascii() {
        c.is_ascii_punctuation()
    } else {
        !c.is_alphanumeric() && !c.is_whitespace()
    }
}

fn is_left_flanking(text: &str, run_start: usize, run_len: usize) -> bool {
    let after = run_start + run_len;
    let next_char = text.get(after..).and_then(|s| s.chars().next());
    let prev_char = (run_start > 0)
        .then(|| text[..run_start].chars().last())
        .flatten();

    let followed_by_ws = next_char.is_none_or(|c| c.is_whitespace());
    if followed_by_ws {
        return false;
    }
    let followed_by_punct = next_char.is_some_and(is_unicode_punct_or_symbol);
    if !followed_by_punct {
        return true;
    }
    prev_char.is_none_or(|c| c.is_whitespace() || is_unicode_punct_or_symbol(c))
}

fn is_right_flanking(text: &str, run_start: usize, run_len: usize) -> bool {
    let after = run_start + run_len;
    let next_char = text.get(after..).and_then(|s| s.chars().next());
    let prev_char = (run_start > 0)
        .then(|| text[..run_start].chars().last())
        .flatten();

    let preceded_by_ws = prev_char.is_none_or(|c| c.is_whitespace());
    if preceded_by_ws {
        return false;
    }
    let preceded_by_punct = prev_char.is_some_and(is_unicode_punct_or_symbol);
    if !preceded_by_punct {
        return true;
    }
    next_char.is_none_or(|c| c.is_whitespace() || is_unicode_punct_or_symbol(c))
}

// ============================================================================
// Pass 2: Process emphasis (CommonMark §6.2)
// ============================================================================

/// Run the CommonMark §6.3 `process_emphasis` algorithm over the IR's
/// delim runs. Mutates the IR in place: matched runs gain entries in their
/// `matches` vec, unmatched bytes stay implicit (the emission pass treats
/// any byte not covered by a match as literal text).
///
/// The algorithm tracks a per-bucket `openers_bottom` exclusive lower
/// bound to keep walk-back bounded; consume rules and the §6.2 mod-3
/// rejection match the reference implementation.
pub fn process_emphasis(events: &mut [IrEvent], dialect: crate::options::Dialect) {
    process_emphasis_in_range(events, 0, events.len(), dialect);
}

/// Range-scoped variant of [`process_emphasis`].
///
/// Only delim runs whose IR event index lies in `[lo, hi)` are considered.
/// Used by [`build_full_plans`] to run emphasis pairing inside each
/// resolved bracket pair *before* the global top-level pass, so emphasis
/// can never form across a link's bracket boundary (CommonMark §6.3
/// requires bracket resolution to happen first when at a `]`, with
/// emphasis processed on the link's inner range).
///
/// The function additionally skips delim runs that already carry a
/// recorded match in their `matches` vec — this lets the second
/// (top-level) pass reuse the same algorithm without re-pairing bytes
/// already consumed by inner-range passes.
pub fn process_emphasis_in_range(
    events: &mut [IrEvent],
    lo: usize,
    hi: usize,
    dialect: crate::options::Dialect,
) {
    process_emphasis_in_range_filtered(events, lo, hi, None, dialect);
}

/// Internal variant of [`process_emphasis_in_range`] with an optional
/// exclusion bitmap. Event indices for which `excluded[i] == true` are
/// treated as if their delim run were already fully consumed — used by
/// [`build_full_plans`] to keep the top-level emphasis pass from pairing
/// across a resolved bracket pair's boundary (the inner delim runs of
/// such a pair belong to the link's inner range and were already paired
/// by the scoped pass).
fn process_emphasis_in_range_filtered(
    events: &mut [IrEvent],
    lo: usize,
    hi: usize,
    excluded: Option<&[bool]>,
    dialect: crate::options::Dialect,
) {
    let is_commonmark = dialect == crate::options::Dialect::CommonMark;
    if is_commonmark {
        run_emphasis_pass(events, lo, hi, excluded, dialect, &[], false);
        return;
    }
    // Pandoc dialect: cascade-then-rerun. Run the standard pass, then
    // invalidate Emph/Strong pairs whose inner range contains an
    // unmatched same-char run with both can_open && can_close (Pandoc's
    // recursive descent would have failed those outer pairs because the
    // inner content has a stray, ambiguous delimiter the recursive
    // parser cannot pair). The invalidated pairs go into a "rejected
    // list" that the next iteration of the standard pass consults to
    // pick a different opener for the same closer (or reject the
    // closer altogether). Iterate to a fixed point.
    //
    // The rerun (iter 2+) runs in `strict` mode: a candidate pair is
    // rejected if its inner range contains an unmatched same-char run
    // with count > pair.count. This mirrors pandoc-markdown's
    // recursive-descent semantics where, e.g. inside a failed outer
    // `**...**` Strong, the inner `one c` parser's `option2`
    // (`string [c,c] >> two c mempty`) greedily consumes a stray `**`
    // and prevents subsequent `*` runs from pairing as Emph. Without
    // this gate, `**foo *bar** baz*` would produce Emph[bar** baz]
    // after the outer Strong invalidation, but pandoc treats it as
    // all-literal because the inner `**` blocks the Emph match.
    let mut rejected: Vec<(usize, usize)> = Vec::new();
    let max_iters = events.len().saturating_add(2);
    let mut iter = 0;
    loop {
        let strict = iter > 0;
        run_emphasis_pass(events, lo, hi, excluded, dialect, &rejected, strict);
        let invalidations = pandoc_cascade_invalidate(events, excluded);
        if invalidations.is_empty() {
            break;
        }
        rejected.extend(invalidations);
        iter += 1;
        if iter >= max_iters {
            break;
        }
    }
    // Recovery for `***A **B** C***` patterns: synthesise the inner
    // Strong match the standard delim-stack algorithm can't reach.
    pandoc_inner_strong_recovery(events);
}

/// One pass of the CommonMark §6.2 emphasis pairing algorithm over the
/// IR's [`DelimRun`](IrEvent::DelimRun) events in `[lo, hi)`. Pandoc
/// dialect gates apply when `dialect == Dialect::Pandoc`. The
/// `rejected_pairs` list (Pandoc only) excludes specific
/// (opener_event_idx, closer_event_idx) pairs from matching — used by
/// the cascade-then-rerun loop to prevent invalidated pairs from
/// re-forming on the next iteration.
fn run_emphasis_pass(
    events: &mut [IrEvent],
    lo: usize,
    hi: usize,
    excluded: Option<&[bool]>,
    dialect: crate::options::Dialect,
    rejected_pairs: &[(usize, usize)],
    strict_pandoc: bool,
) {
    let is_commonmark = dialect == crate::options::Dialect::CommonMark;
    let hi = hi.min(events.len());
    if lo >= hi {
        return;
    }
    // Indices of DelimRun events within [lo, hi), in order, that have
    // not already been fully consumed by an earlier scoped pass and that
    // are not in the optional exclusion bitmap.
    let mut delim_idxs: Vec<usize> = events[lo..hi]
        .iter()
        .enumerate()
        .filter_map(|(i, e)| {
            let abs = lo + i;
            match e {
                IrEvent::DelimRun { matches, .. }
                    if matches.is_empty()
                        && excluded.is_none_or(|ex| ex.get(abs).copied() != Some(true)) =>
                {
                    Some(abs)
                }
                _ => None,
            }
        })
        .collect();
    if delim_idxs.is_empty() {
        return;
    }

    // Working state: count (remaining unmatched chars) and source_start
    // (first remaining char) per delim run. Indexed by position in
    // `delim_idxs`.
    let mut count: Vec<usize> = Vec::with_capacity(delim_idxs.len());
    let mut source_start: Vec<usize> = Vec::with_capacity(delim_idxs.len());
    let mut removed: Vec<bool> = vec![false; delim_idxs.len()];

    for &ev_idx in &delim_idxs {
        if let IrEvent::DelimRun { start, end, .. } = &events[ev_idx] {
            count.push(end - start);
            source_start.push(*start);
        }
    }

    // openers_bottom[ch_idx][len%3][can_open] → exclusive lower bound
    // (an index into `delim_idxs`, or None meaning "no bottom yet").
    let mut openers_bottom: [[[Option<usize>; 2]; 3]; 2] = [[[None; 2]; 3]; 2];

    // First active index, scanning forward.
    let first_active =
        |removed: &[bool]| -> Option<usize> { (0..removed.len()).find(|&i| !removed[i]) };
    let next_active = |removed: &[bool], from: usize| -> Option<usize> {
        (from + 1..removed.len()).find(|&i| !removed[i])
    };
    let prev_active =
        |removed: &[bool], from: usize| -> Option<usize> { (0..from).rev().find(|&i| !removed[i]) };

    let min_closer_count = 1usize;
    let mut closer_local = first_active(&removed);
    while let Some(c) = closer_local {
        let ev_c_idx = delim_idxs[c];
        let (ch_c, can_open_c, can_close_c) = match &events[ev_c_idx] {
            IrEvent::DelimRun {
                ch,
                can_open,
                can_close,
                ..
            } => (*ch, *can_open, *can_close),
            _ => unreachable!(),
        };
        if !can_close_c || removed[c] || count[c] < min_closer_count {
            closer_local = next_active(&removed, c);
            continue;
        }

        let ch_idx = if ch_c == b'*' { 0 } else { 1 };
        let closer_mod = count[c] % 3;
        let closer_open_bucket = can_open_c as usize;
        let bottom = openers_bottom[ch_idx][closer_mod][closer_open_bucket];

        // Walk back to find a compatible opener.
        let mut found_opener: Option<usize> = None;
        let mut walk = prev_active(&removed, c);
        while let Some(o) = walk {
            if Some(o) == bottom {
                break;
            }
            let ev_o_idx = delim_idxs[o];
            let (ch_o, can_open_o, can_close_o) = match &events[ev_o_idx] {
                IrEvent::DelimRun {
                    ch,
                    can_open,
                    can_close,
                    ..
                } => (*ch, *can_open, *can_close),
                _ => unreachable!(),
            };
            if !removed[o] && ch_o == ch_c && can_open_o {
                let oc_sum = count[o] + count[c];
                let opener_both = can_open_o && can_close_o;
                let closer_both = can_open_c && can_close_c;
                let mod3_reject = is_commonmark
                    && (opener_both || closer_both)
                    && oc_sum.is_multiple_of(3)
                    && !(count[o].is_multiple_of(3) && count[c].is_multiple_of(3));
                // Pandoc-markdown rejects emph/strong pairs whose counts
                // disagree in the exactly-(1,2) / (2,1) shape:
                //   - `**foo*` (2,1): `try_parse_two` looks only for a
                //     `**` closer; the lone `*` doesn't satisfy that.
                //   - `*foo**` (1,2): `try_parse_one` encountering `**`
                //     tries `try_parse_two`; absence of an inner `**`
                //     closer cascades the outer parse to fail.
                // Other count combinations DO match (verified against
                // `pandoc -f markdown`):
                //   - (1,3) / (3,1) → emph match, opposite-side
                //     leftover `**` literal.
                //   - (2,3) / (3,2) → strong match, single `*` literal.
                //   - (3,3) → STRONG(EM(...)) nested.
                //   - (1..3, 4+) → match (Pandoc's ender walks the
                //     closer run for a valid position; algorithm
                //     consumes leftmost via leftover-as-literal).
                // Opener count >= 4 is rejected (Pandoc's
                // `try_parse_emphasis` has no count-4+ dispatch).
                let pandoc_reject = !is_commonmark
                    && ((count[o] == 1 && count[c] == 2)
                        || (count[o] == 2 && count[c] == 1)
                        || count[o] >= 4);
                let pair_rejected = !is_commonmark && {
                    let oe = delim_idxs[o];
                    let ce = delim_idxs[c];
                    rejected_pairs.iter().any(|&(ro, rc)| ro == oe && rc == ce)
                };
                // Pandoc strict-rerun gate (iter 2+ only): block a
                // candidate pair if any unmatched same-char run between
                // its opener and closer has remaining count strictly
                // greater than the consume rule for this pair.
                // Mirrors pandoc-markdown's recursive descent where
                // `one c`'s `option2` (`string [c,c] >> two c`) would
                // greedily consume a stray higher-count run, blocking
                // the outer `one c` from finding its `ender c 1` —
                // e.g. `**foo *bar** baz*` after the outer Strong
                // invalidates: a naïve rerun pairs ev1 (`*`) ↔ ev3
                // (`*`) as Emph (consume=1), but pandoc treats the
                // `**` between as having "consumed" any further
                // matching, leaving everything literal.
                let strict_block = strict_pandoc && {
                    let tentative_consume = if !is_commonmark && count[o] >= 3 && count[c] >= 3 {
                        1
                    } else if count[o] >= 2 && count[c] >= 2 {
                        2
                    } else {
                        1
                    };
                    let lo_evt = delim_idxs[o] + 1;
                    let hi_evt = delim_idxs[c];
                    (lo_evt..hi_evt).any(|k| match &events[k] {
                        IrEvent::DelimRun {
                            ch: ch_k,
                            start,
                            end,
                            matches,
                            ..
                        } => {
                            *ch_k == ch_c && {
                                let total = end - start;
                                let consumed: usize = matches.iter().map(|m| m.len as usize).sum();
                                total.saturating_sub(consumed) > tentative_consume
                            }
                        }
                        _ => false,
                    })
                };
                if !mod3_reject && !pandoc_reject && !pair_rejected && !strict_block {
                    found_opener = Some(o);
                    break;
                }
            }
            if o == 0 {
                break;
            }
            walk = prev_active(&removed, o);
        }

        if let Some(o) = found_opener {
            // Consume rule:
            //   CommonMark — consume 2 (Strong) when both sides have
            //     >= 2 chars, else 1 (Emph). For `***x***` (3,3) this
            //     produces EM(STRONG(...)) because the first match
            //     consumes 2 from each side (Strong outermost).
            //   Pandoc — when both sides have >= 3, consume 1 first
            //     (Emph innermost) leaving 2 + 2 to pair as Strong on
            //     the second pass. This produces STRONG(EM(...)) for
            //     `***x***`, matching Pandoc-markdown's recursive
            //     `try_parse_three` algorithm.
            let consume = if !is_commonmark && count[o] >= 3 && count[c] >= 3 {
                1
            } else if count[o] >= 2 && count[c] >= 2 {
                2
            } else {
                1
            };
            let kind = if consume == 2 {
                EmphasisKind::Strong
            } else {
                EmphasisKind::Emph
            };

            // Opener consumes inner-edge (rightmost) chars.
            let opener_match_offset =
                source_start[o] + count[o] - consume - source_start_event(&events[delim_idxs[o]]);
            // Closer consumes inner-edge (leftmost) chars.
            let closer_match_offset = source_start[c] - source_start_event(&events[delim_idxs[c]]);

            // Record match on opener.
            if let IrEvent::DelimRun { matches, .. } = &mut events[delim_idxs[o]] {
                matches.push(DelimMatch {
                    offset_in_run: opener_match_offset as u8,
                    len: consume as u8,
                    is_opener: true,
                    partner_event: delim_idxs[c] as u32,
                    partner_offset: closer_match_offset as u8,
                    kind,
                });
            }
            // Record match on closer.
            if let IrEvent::DelimRun { matches, .. } = &mut events[delim_idxs[c]] {
                matches.push(DelimMatch {
                    offset_in_run: closer_match_offset as u8,
                    len: consume as u8,
                    is_opener: false,
                    partner_event: delim_idxs[o] as u32,
                    partner_offset: opener_match_offset as u8,
                    kind,
                });
            }

            count[o] -= consume;
            source_start[c] += consume;
            count[c] -= consume;

            // Remove all openers strictly between o and c.
            let mut between = next_active(&removed, o);
            while let Some(idx) = between {
                if idx == c {
                    break;
                }
                removed[idx] = true;
                between = next_active(&removed, idx);
            }

            if count[o] == 0 {
                removed[o] = true;
            }
            if count[c] == 0 {
                removed[c] = true;
                closer_local = next_active(&removed, c);
            }
            // Else re-process the same closer with reduced count.
        } else {
            openers_bottom[ch_idx][closer_mod][closer_open_bucket] = prev_active(&removed, c);
            if !can_open_c {
                removed[c] = true;
            }
            closer_local = next_active(&removed, c);
        }
    }

    // No further mutation needed: matches are recorded; remaining bytes
    // stay implicit literal. Pandoc cascade is invoked by the caller
    // (`process_emphasis_in_range_filtered`) once per pass so it can
    // accumulate invalidations into a rejected-pairs list and re-run.
    let _ = (&mut delim_idxs, &mut openers_bottom, min_closer_count);
}

/// Pandoc-only post-processing pass over [`process_emphasis_in_range_filtered`]
/// matches: invalidate any matched delim pair that contains an unmatched
/// same-character run between its opener and closer. Returns the list
/// of (opener_event_idx, closer_event_idx) pairs that were invalidated
/// in this call, so the caller can seed a rejected-pairs list and
/// re-run the standard pass — this lets Pandoc re-pair the inner runs
/// that the invalidated outer match would have stolen via
/// between-removal (e.g. `*foo **bar* baz**` → after the outer
/// `ev0..ev2` Emph is invalidated, `ev1..ev3` matches as Strong on the
/// next iteration).
fn pandoc_cascade_invalidate(
    events: &mut [IrEvent],
    excluded: Option<&[bool]>,
) -> Vec<(usize, usize)> {
    let mut invalidated_pairs: Vec<(usize, usize)> = Vec::new();
    // Early-exit: if there are no `DelimRun` events at all, the cascade
    // pass is a no-op. Avoids allocating the two scratch vecs below for
    // every range with no `*`/`_` runs (which is the common case for
    // ranges that contain only standalone constructs / brackets).
    if !events.iter().any(|e| matches!(e, IrEvent::DelimRun { .. })) {
        return invalidated_pairs;
    }
    let is_excluded = |k: usize| excluded.is_some_and(|ex| ex.get(k).copied() == Some(true));
    // Reuse two scratch vecs across the inner loop iterations instead
    // of `.collect()` each time. These are tiny per-paragraph
    // allocations but the function is called for every Pandoc inline
    // emphasis pass and shows up in malloc traffic.
    let mut total: Vec<usize> = Vec::with_capacity(events.len());
    let mut consumed: Vec<usize> = Vec::with_capacity(events.len());
    loop {
        total.clear();
        consumed.clear();
        // Compute total bytes (run length) and consumed bytes (sum of
        // match lens) per DelimRun event index.
        total.extend(events.iter().map(|e| match e {
            IrEvent::DelimRun { start, end, .. } => end - start,
            _ => 0,
        }));
        consumed.extend(events.iter().map(|e| match e {
            IrEvent::DelimRun { matches, .. } => matches.iter().map(|m| m.len as usize).sum(),
            _ => 0,
        }));

        // Find a pair to invalidate. We invalidate one and restart so
        // the cascade can re-evaluate dependent pairs.
        let mut to_invalidate: Option<(usize, u8)> = None;
        'outer: for opener_idx in 0..events.len() {
            let IrEvent::DelimRun {
                ch: ch_o, matches, ..
            } = &events[opener_idx]
            else {
                continue;
            };
            for (mi, m) in matches.iter().enumerate() {
                if !m.is_opener {
                    continue;
                }
                let closer_idx = m.partner_event as usize;
                if closer_idx <= opener_idx || closer_idx >= events.len() {
                    continue;
                }
                // Scan events strictly between opener and closer for any
                // DelimRun with the same `ch`, unmatched bytes, AND
                // both `can_open` and `can_close` (i.e., the run could
                // have participated in pairing on both sides). A
                // can_open-only or can_close-only run is a one-sided
                // fragment (e.g. an isolated `*` after a backslash
                // escape) that the Pandoc recursive-descent path would
                // never have tried as a nested-strong opener — those
                // shouldn't cascade-invalidate the surrounding pair.
                for k in (opener_idx + 1)..closer_idx {
                    if is_excluded(k) {
                        continue;
                    }
                    if let IrEvent::DelimRun {
                        ch: ch_k,
                        can_open: co_k,
                        can_close: cc_k,
                        ..
                    } = &events[k]
                        && *ch_k == *ch_o
                        && consumed[k] < total[k]
                        && *co_k
                        && *cc_k
                    {
                        to_invalidate = Some((opener_idx, mi as u8));
                        break 'outer;
                    }
                }
            }
        }

        let Some((opener_idx, mi)) = to_invalidate else {
            break;
        };

        // Look up the partner event/offset before mutating.
        let (closer_idx, opener_offset) = match &events[opener_idx] {
            IrEvent::DelimRun { matches, .. } => {
                let m = matches[mi as usize];
                (m.partner_event as usize, m.offset_in_run)
            }
            _ => break,
        };

        // Remove the opener match.
        if let IrEvent::DelimRun { matches, .. } = &mut events[opener_idx] {
            matches.remove(mi as usize);
        }
        // Remove the corresponding closer match (closer's match has
        // is_opener=false and partner_offset == opener's offset_in_run).
        if let IrEvent::DelimRun { matches, .. } = &mut events[closer_idx] {
            matches.retain(|m| m.is_opener || m.partner_offset != opener_offset);
        }
        invalidated_pairs.push((opener_idx, closer_idx));
    }
    invalidated_pairs
}

/// Pandoc-only post-pass: recover the inner Strong match in
/// `***A **B** C***` patterns where the IR's standard pass produced
/// `Emph[Strong[A], "B**...** C"]` (matching the outer triple as
/// Strong+Emph but losing the inner `**...**`-as-Strong-of-`C` pair).
///
/// Pandoc's recursive descent here goes
/// `three c → ender c 2 → one c → option2 → two c`, producing
/// `Emph[Strong[A], "B", Strong[C]]` — two Strong nodes inside an outer
/// Emph. The standard delim-stack algorithm can't reach this pairing
/// because between-removal during the outer Emph match removes the
/// inner closer-side `**` (e.g. `bar**`) from the candidate pool.
///
/// This recovery scans Emph matches whose opener and closer originally
/// had count >= 3, and whose closer has unmatched bytes >= 2 after the
/// standard pass; for each, we look for an unmatched same-char
/// between-run with count >= 2 and `can_close = true` (the would-be
/// inner-Strong opener) and synthesise a Strong match that consumes
/// the leftmost 2 bytes of the closer (where the existing Emph match
/// shifts to the rightmost 1 byte). The byte-position rewrite lets
/// the CST emission produce well-nested `Emph[..., Strong[...]]` —
/// outer Emph close at the rightmost outer-triple byte, inner Strong
/// close at the leftmost two.
fn pandoc_inner_strong_recovery(events: &mut [IrEvent]) {
    let n = events.len();
    // (between_idx, opener_idx, closer_idx, len)
    let mut to_apply: Vec<(usize, usize, usize, u8)> = Vec::new();

    for opener_idx in 0..n {
        let (open_total, open_matches_clone, ch_o) = match &events[opener_idx] {
            IrEvent::DelimRun {
                start,
                end,
                matches,
                ch,
                ..
            } => (*end - *start, matches.clone(), *ch),
            _ => continue,
        };
        if open_total < 3 {
            continue;
        }

        for m in open_matches_clone.iter() {
            if !m.is_opener || m.kind != EmphasisKind::Emph {
                continue;
            }
            let closer_idx = m.partner_event as usize;
            if closer_idx <= opener_idx || closer_idx >= n {
                continue;
            }

            let (close_total, close_consumed) = match &events[closer_idx] {
                IrEvent::DelimRun {
                    start,
                    end,
                    matches,
                    ..
                } => {
                    let total = end - start;
                    let consumed: usize = matches.iter().map(|m| m.len as usize).sum();
                    (total, consumed)
                }
                _ => continue,
            };
            if close_total < 3 {
                continue;
            }
            let leftover = close_total.saturating_sub(close_consumed);
            if leftover < 2 {
                continue;
            }

            // Walk backward from closer-1 looking for the rightmost
            // unmatched same-char run with count >= 2 and
            // can_close=true.
            for k in ((opener_idx + 1)..closer_idx).rev() {
                if let IrEvent::DelimRun {
                    ch,
                    start,
                    end,
                    matches,
                    can_close,
                    ..
                } = &events[k]
                {
                    if *ch != ch_o || !*can_close {
                        continue;
                    }
                    let total = end - start;
                    let consumed: usize = matches.iter().map(|m| m.len as usize).sum();
                    let remaining = total.saturating_sub(consumed);
                    if remaining < 2 {
                        continue;
                    }
                    to_apply.push((k, opener_idx, closer_idx, 2));
                    break;
                }
            }
        }
    }

    for (between_idx, opener_idx, closer_idx, len) in to_apply {
        // Find the existing Emph match on the closer side.
        let (closer_emph_match_idx, closer_emph_offset) = {
            let mut found: Option<(usize, u8)> = None;
            if let IrEvent::DelimRun { matches, .. } = &events[closer_idx] {
                for (mi, m) in matches.iter().enumerate() {
                    if !m.is_opener
                        && m.partner_event as usize == opener_idx
                        && m.kind == EmphasisKind::Emph
                    {
                        found = Some((mi, m.offset_in_run));
                        break;
                    }
                }
            }
            match found {
                Some(x) => x,
                None => continue,
            }
        };

        // Find the corresponding Emph match on the opener side.
        let opener_emph_match_idx = {
            let mut found: Option<usize> = None;
            if let IrEvent::DelimRun { matches, .. } = &events[opener_idx] {
                for (mi, m) in matches.iter().enumerate() {
                    if m.is_opener
                        && m.partner_event as usize == closer_idx
                        && m.kind == EmphasisKind::Emph
                    {
                        found = Some(mi);
                        break;
                    }
                }
            }
            match found {
                Some(x) => x,
                None => continue,
            }
        };

        // Shift the Emph closer's offset to the right of the new
        // Strong closer's bytes (Strong takes leftmost `len` bytes,
        // Emph takes the next byte).
        let new_closer_emph_offset = closer_emph_offset + len;

        // Update closer's Emph offset_in_run.
        if let IrEvent::DelimRun { matches, .. } = &mut events[closer_idx] {
            matches[closer_emph_match_idx].offset_in_run = new_closer_emph_offset;
        }
        // Update opener's Emph partner_offset to point at the shifted
        // Emph closer position.
        if let IrEvent::DelimRun { matches, .. } = &mut events[opener_idx] {
            matches[opener_emph_match_idx].partner_offset = new_closer_emph_offset;
        }

        // Add Strong opener match on the between-run.
        if let IrEvent::DelimRun { matches, .. } = &mut events[between_idx] {
            matches.push(DelimMatch {
                offset_in_run: 0,
                len,
                is_opener: true,
                partner_event: closer_idx as u32,
                partner_offset: closer_emph_offset,
                kind: EmphasisKind::Strong,
            });
        }
        // Add Strong closer match on the closer (at the original
        // pre-shift Emph-closer position; the bytes that were the
        // single Emph closer now become the leftmost 2 bytes of the
        // Strong closer).
        if let IrEvent::DelimRun { matches, .. } = &mut events[closer_idx] {
            matches.push(DelimMatch {
                offset_in_run: closer_emph_offset,
                len,
                is_opener: false,
                partner_event: between_idx as u32,
                partner_offset: 0,
                kind: EmphasisKind::Strong,
            });
        }
    }
}

fn source_start_event(event: &IrEvent) -> usize {
    match event {
        IrEvent::DelimRun { start, .. } => *start,
        _ => unreachable!("source_start_event called on non-DelimRun"),
    }
}

// ============================================================================
// Pass 3: Process brackets (CommonMark §6.3)
// ============================================================================

/// Resolve `[`/`![`/`]` markers into link/image nodes per CommonMark §6.3
/// (with Pandoc-aware variations under `Dialect::Pandoc`).
///
/// Walks the IR forward looking for `]` markers. For each one, finds the
/// nearest active matching `[`/`![` and tries to resolve the bracket pair
/// as a link or image. Resolution is tried in spec order:
///
/// 1. Inline link / image: `[text](dest)` or `[text](dest "title")`.
/// 2. Full reference: `[text][label]`, where `label` is in `refdefs`.
/// 3. Collapsed reference: `[text][]`, where `text` (normalised) is in
///    `refdefs`.
/// 4. Shortcut reference: `[text]` not followed by `(` or `[`, where
///    `text` (normalised) is in `refdefs`.
///
/// On a match, the opener gets a `BracketResolution` and the closer is
/// flagged `matched`. Under `Dialect::CommonMark`, all earlier active link
/// openers are deactivated to implement the §6.3 "links may not contain
/// other links" rule (image brackets do not deactivate earlier link
/// openers — only links do). Under `Dialect::Pandoc`, the deactivate-pass
/// is skipped: pandoc-native is outer-wins for nested links (the inner
/// `[inner](u2)` of `[link [inner](u2)](u1)` is literal text inside the
/// outer link), and the dispatcher enforces this via a `suppress_inner_links`
/// flag during LINK-text recursion. So under Pandoc the IR can leave both
/// outer and inner resolved and trust the dispatcher to suppress inner
/// LINK emission.
///
/// On a miss the bracket pair stays opaque-as-literal and the closer is
/// dropped from the bracket stack so the next `]` can re-pair.
///
/// Reference-form resolution consults the refdef map under both
/// dialects (CommonMark §6.3 and Pandoc-markdown agree on the
/// document-scoped lookup rule). Under Pandoc, when a bracket-shape
/// pattern (`[text][label]`, `[text][]`, `[text]`) doesn't resolve to
/// a refdef, the opener is tagged with `unresolved_ref = Some(...)`
/// and the closer's `matched` is set to `true` so that
/// [`build_bracket_plan`] emits a [`BracketDispo::UnresolvedReference`]
/// keyed at the opener. Emission then wraps `[start, end)` in an
/// `UNRESOLVED_REFERENCE` node — distinct from `LINK` — so downstream
/// tools (linter, LSP) can attach behavior to the bracket-shape
/// pattern without the parser having to lie about resolution.
///
/// Under CommonMark, no `unresolved_ref` is recorded; the
/// no-resolution fall-through behaves as today (opener deactivated,
/// brackets emit as literal text).
pub fn process_brackets(
    events: &mut [IrEvent],
    text: &str,
    refdefs: Option<&RefdefMap>,
    dialect: crate::options::Dialect,
) {
    let empty: HashSet<String> = HashSet::new();
    let labels: &HashSet<String> = match refdefs {
        Some(map) => map.as_ref(),
        None => &empty,
    };
    let is_commonmark = dialect == crate::options::Dialect::CommonMark;
    // Refdef-aware label resolution under both dialects.
    let label_resolves =
        |key_norm: &str| -> bool { !key_norm.is_empty() && labels.contains(key_norm) };

    // Walk forward through events, treating it as a linear scan for `]`.
    let mut i = 0;
    while i < events.len() {
        let close_pos = match &events[i] {
            IrEvent::CloseBracket { pos, .. } => *pos,
            _ => {
                i += 1;
                continue;
            }
        };

        // Find the nearest active OpenBracket before `i`.
        let mut o = match find_active_opener(events, i) {
            Some(o) => o,
            None => {
                i += 1;
                continue;
            }
        };

        let (open_end, is_image) = match &events[o] {
            IrEvent::OpenBracket { end, is_image, .. } => (*end, *is_image),
            _ => unreachable!(),
        };
        let text_start = open_end;
        let text_end = close_pos;
        let after_close = close_pos + 1;

        // 1. Inline link / image.
        if let Some((suffix_end, dest, title)) = try_inline_suffix(text, after_close) {
            // §6.3 link-in-link rule (CommonMark): if this is a *link*
            // (not an image), and any earlier active link opener exists,
            // deactivate them. We also deactivate openers strictly before
            // `o` here because matching means the inner link wins; the
            // spec applies this *after* matching. Pandoc skips this —
            // outer-wins is enforced by the dispatcher's
            // `suppress_inner_links` flag during LINK-text recursion.
            if !is_image && is_commonmark {
                deactivate_earlier_link_openers(events, o);
            }
            commit_resolution(
                events,
                o,
                i,
                text_start,
                text_end,
                after_close,
                suffix_end,
                LinkKind::Inline { dest, title },
            );
            // Remove the opener from the bracket stack: it has been
            // matched (active=false will fall out automatically since
            // resolution is Some).
            mark_opener_resolved(events, o);
            i += 1;
            continue;
        }

        // 2. Full reference link: `[text][label]`.
        let full_ref_suffix = try_full_reference_suffix(text, after_close);
        if let Some((suffix_end, label_raw)) = &full_ref_suffix {
            let label_norm = normalize_label(label_raw);
            if label_resolves(&label_norm) {
                if !is_image && is_commonmark {
                    deactivate_earlier_link_openers(events, o);
                }
                commit_resolution(
                    events,
                    o,
                    i,
                    text_start,
                    text_end,
                    after_close,
                    *suffix_end,
                    LinkKind::FullReference {
                        label: label_raw.clone(),
                    },
                );
                mark_opener_resolved(events, o);
                i += 1;
                continue;
            }
            // Bracketed but unresolved label: §6.3 says we still treat
            // `[text][label]` as not-a-link, but the brackets get
            // consumed as literal text AND the shortcut form is
            // suppressed (since the `]` is followed by a link label).
        }

        // 3. Collapsed `[]`.
        let link_text = &text[text_start..text_end];
        let link_text_norm = normalize_label(link_text);
        let is_collapsed = is_collapsed_marker(text, after_close);
        let collapsed_suffix_end = after_close + 2;

        if is_collapsed && label_resolves(&link_text_norm) {
            if !is_image && is_commonmark {
                deactivate_earlier_link_openers(events, o);
            }
            commit_resolution(
                events,
                o,
                i,
                text_start,
                text_end,
                after_close,
                collapsed_suffix_end,
                LinkKind::CollapsedReference,
            );
            mark_opener_resolved(events, o);
            i += 1;
            continue;
        }
        // `[text][]` with text not in refdefs — falls through to
        // literal text; shortcut is suppressed (followed by `[]`).

        // 4. Shortcut form: `[text]` not followed by `[]` or `[label]`.
        // Per CommonMark §6.3: "A shortcut reference link consists of a
        // link label that matches a link reference definition elsewhere
        // in the document and is not followed by [] or a link label."
        // The full-ref / collapsed shape attempts above suppress the
        // shortcut even when their labels don't resolve — the bracket
        // bytes still get consumed as literal text.
        let shortcut_suppressed = full_ref_suffix.is_some() || is_collapsed;
        if !shortcut_suppressed && label_resolves(&link_text_norm) {
            if !is_image && is_commonmark {
                deactivate_earlier_link_openers(events, o);
            }
            commit_resolution(
                events,
                o,
                i,
                text_start,
                text_end,
                after_close,
                after_close,
                LinkKind::ShortcutReference,
            );
            mark_opener_resolved(events, o);
            i += 1;
            continue;
        }

        // No resolution. Under Pandoc, the bracket pair is still a
        // recognisable reference shape (full / collapsed / shortcut) —
        // tag the opener with `unresolved_ref` so emission wraps it
        // in an `UNRESOLVED_REFERENCE` node, and mark the closer
        // matched so it doesn't fall through to a literal `]` token.
        // Under CommonMark, behavior unchanged: deactivate the opener,
        // brackets emit as literal text.
        //
        // Empty-component shapes (`[]`, `[][]`) aren't reference
        // patterns even in spirit — pandoc-native treats them as
        // literal text — so skip wrapping.
        let unresolved_shape = if !is_commonmark {
            let (end, has_substantive_label) =
                if let Some((suffix_end, label_raw)) = &full_ref_suffix {
                    (*suffix_end, !normalize_label(label_raw).is_empty())
                } else if is_collapsed {
                    (collapsed_suffix_end, !link_text_norm.is_empty())
                } else {
                    (after_close, !link_text_norm.is_empty())
                };
            if has_substantive_label {
                Some(UnresolvedRefShape {
                    close_event: i as u32,
                    text_end,
                    end,
                })
            } else {
                None
            }
        } else {
            None
        };
        if let IrEvent::OpenBracket {
            active,
            unresolved_ref,
            ..
        } = &mut events[o]
        {
            *active = false;
            *unresolved_ref = unresolved_shape;
        }
        if unresolved_shape.is_some()
            && let IrEvent::CloseBracket { matched, .. } = &mut events[i]
        {
            *matched = true;
        }
        let _ = &mut o;
        i += 1;
    }
}

fn find_active_opener(events: &[IrEvent], close_idx: usize) -> Option<usize> {
    (0..close_idx).rev().find(|&i| {
        matches!(
            &events[i],
            IrEvent::OpenBracket {
                active: true,
                resolution: None,
                ..
            }
        )
    })
}

fn deactivate_earlier_link_openers(events: &mut [IrEvent], open_idx: usize) {
    for ev in &mut events[..open_idx] {
        if let IrEvent::OpenBracket {
            is_image: false,
            active,
            resolution: None,
            ..
        } = ev
        {
            *active = false;
        }
    }
}

fn mark_opener_resolved(events: &mut [IrEvent], open_idx: usize) {
    if let IrEvent::OpenBracket { active, .. } = &mut events[open_idx] {
        *active = false;
    }
}

#[allow(clippy::too_many_arguments)]
fn commit_resolution(
    events: &mut [IrEvent],
    open_idx: usize,
    close_idx: usize,
    text_start: usize,
    text_end: usize,
    suffix_start: usize,
    suffix_end: usize,
    kind: LinkKind,
) {
    if let IrEvent::OpenBracket { resolution, .. } = &mut events[open_idx] {
        *resolution = Some(BracketResolution {
            close_event: close_idx as u32,
            text_start,
            text_end,
            suffix_start,
            suffix_end,
            kind,
        });
    }
    if let IrEvent::CloseBracket { matched, .. } = &mut events[close_idx] {
        *matched = true;
    }
}

/// Try to parse `(dest)` or `(dest "title")` inline link suffix starting
/// at `text[pos]`. Returns `(end_pos_exclusive, dest, title)`.
fn try_inline_suffix(text: &str, pos: usize) -> Option<(usize, String, Option<String>)> {
    let bytes = text.as_bytes();
    if pos >= bytes.len() || bytes[pos] != b'(' {
        return None;
    }
    let mut p = pos + 1;
    // Skip leading whitespace.
    while p < bytes.len() && matches!(bytes[p], b' ' | b'\t' | b'\n') {
        p += 1;
    }
    // Empty `()` — link with empty destination.
    if p < bytes.len() && bytes[p] == b')' {
        return Some((p + 1, String::new(), None));
    }

    // Parse destination.
    let (dest, dest_end) = parse_link_destination(text, p)?;
    p = dest_end;

    // Skip whitespace.
    while p < bytes.len() && matches!(bytes[p], b' ' | b'\t' | b'\n') {
        p += 1;
    }

    // Optional title.
    let mut title = None;
    if p < bytes.len() && matches!(bytes[p], b'"' | b'\'' | b'(') {
        let (t, t_end) = parse_link_title(text, p)?;
        title = Some(t);
        p = t_end;
        while p < bytes.len() && matches!(bytes[p], b' ' | b'\t' | b'\n') {
            p += 1;
        }
    }

    if p >= bytes.len() || bytes[p] != b')' {
        return None;
    }
    Some((p + 1, dest, title))
}

fn parse_link_destination(text: &str, start: usize) -> Option<(String, usize)> {
    let bytes = text.as_bytes();
    if start >= bytes.len() {
        return None;
    }
    if bytes[start] == b'<' {
        // <bracketed>
        let mut p = start + 1;
        let begin = p;
        while p < bytes.len() && bytes[p] != b'>' && bytes[p] != b'\n' && bytes[p] != b'<' {
            if bytes[p] == b'\\' && p + 1 < bytes.len() {
                p += 2;
            } else {
                p += 1;
            }
        }
        if p >= bytes.len() || bytes[p] != b'>' {
            return None;
        }
        let dest = text[begin..p].to_string();
        Some((dest, p + 1))
    } else {
        // unbracketed: balanced parens, no spaces, no controls
        let mut p = start;
        let mut paren_depth: i32 = 0;
        while p < bytes.len() {
            let b = bytes[p];
            if b == b'\\' && p + 1 < bytes.len() {
                p += 2;
                continue;
            }
            if b == b'(' {
                paren_depth += 1;
                p += 1;
                continue;
            }
            if b == b')' {
                if paren_depth == 0 {
                    break;
                }
                paren_depth -= 1;
                p += 1;
                continue;
            }
            if b == b' ' || b == b'\t' || b == b'\n' || b < 0x20 || b == 0x7f {
                break;
            }
            p += 1;
        }
        if p == start || paren_depth != 0 {
            return None;
        }
        Some((text[start..p].to_string(), p))
    }
}

fn parse_link_title(text: &str, start: usize) -> Option<(String, usize)> {
    let bytes = text.as_bytes();
    let q = bytes[start];
    let close = match q {
        b'"' => b'"',
        b'\'' => b'\'',
        b'(' => b')',
        _ => return None,
    };
    let mut p = start + 1;
    let begin = p;
    while p < bytes.len() {
        let b = bytes[p];
        if b == b'\\' && p + 1 < bytes.len() {
            p += 2;
            continue;
        }
        if b == close {
            let title = text[begin..p].to_string();
            return Some((title, p + 1));
        }
        p += 1;
    }
    None
}

/// Try to parse `[label]` after a `]`. Returns `(suffix_end, label_raw)`.
/// For the collapsed form `[]`, returns `None` here (handled separately
/// by `is_collapsed_marker`).
fn try_full_reference_suffix(text: &str, pos: usize) -> Option<(usize, String)> {
    let bytes = text.as_bytes();
    if pos >= bytes.len() || bytes[pos] != b'[' {
        return None;
    }
    let label_start = pos + 1;
    let mut p = label_start;
    let mut escape_next = false;
    while p < bytes.len() {
        if escape_next {
            escape_next = false;
            p += 1;
            continue;
        }
        match bytes[p] {
            b'\\' => {
                escape_next = true;
                p += 1;
            }
            b']' => break,
            b'[' => return None,
            b'\n' => {
                p += 1;
            }
            _ => p += 1,
        }
    }
    if p >= bytes.len() || bytes[p] != b']' {
        return None;
    }
    let label = text[label_start..p].to_string();
    if label.is_empty() {
        return None;
    }
    Some((p + 1, label))
}

fn is_collapsed_marker(text: &str, pos: usize) -> bool {
    text.as_bytes().get(pos) == Some(&b'[') && text.as_bytes().get(pos + 1) == Some(&b']')
}

// ============================================================================
// Bracket plan — byte-position-keyed view of resolved brackets, consumed by
// the existing emission walk in `core::parse_inline_range_impl`.
// ============================================================================

/// Disposition of a single bracket byte after [`process_brackets`].
#[derive(Debug, Clone)]
pub enum BracketDispo {
    /// `[` or `![` of a resolved link/image. Emission emits the LINK/IMAGE
    /// node and skips past `suffix_end`.
    Open {
        is_image: bool,
        text_start: usize,
        text_end: usize,
        suffix_start: usize,
        suffix_end: usize,
        kind: LinkKind,
    },
    /// Pandoc-only: `[` or `![` of a bracket-shape reference pattern
    /// whose label didn't resolve. Emission wraps `[start, end)` in an
    /// `UNRESOLVED_REFERENCE` node so downstream tools can attach
    /// behavior to the bracket-shape pattern. `text_start..text_end` is
    /// the inner text range (between the outer `[`/`![` and `]`).
    UnresolvedReference {
        is_image: bool,
        text_start: usize,
        text_end: usize,
        end: usize,
    },
    /// Bracket byte (one of `[`, `]`, or `!`) that fell through to literal
    /// text. Emission accumulates into the surrounding text run.
    Literal,
}

/// A byte-keyed view of the IR's bracket resolutions.
#[derive(Debug, Default, Clone)]
pub struct BracketPlan {
    by_pos: BTreeMap<usize, BracketDispo>,
}

impl BracketPlan {
    pub fn lookup(&self, pos: usize) -> Option<&BracketDispo> {
        self.by_pos.get(&pos)
    }

    pub fn is_empty(&self) -> bool {
        self.by_pos.is_empty()
    }
}

/// A standalone Pandoc inline construct recognised by `build_ir` and
/// dispatched directly from the emission walk. Carries the construct's
/// full source range so the emission walk can slice the content for the
/// existing `emit_*` helpers without re-running the recognition.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConstructDispo {
    /// `^[note text]` — emit via `emit_inline_footnote` after slicing
    /// the inner content.
    InlineFootnote { end: usize },
    /// `<span ...>...</span>` — emit via `emit_native_span` after
    /// re-parsing the open-tag attributes from the source range.
    NativeSpan { end: usize },
    /// `[^id]` — emit via `emit_footnote_reference` after extracting
    /// the label id from the source range.
    FootnoteReference { end: usize },
    /// `[@cite]` — emit via `emit_bracketed_citation` after slicing
    /// the inner content.
    BracketedCitation { end: usize },
    /// `@key` or `-@key` — emit via `emit_bare_citation` (or
    /// `emit_crossref` when `is_quarto_crossref_key` matches and
    /// `extensions.quarto_crossrefs` is enabled).
    BareCitation { end: usize },
    /// `[content]{attrs}` — emit via `emit_bracketed_span` after
    /// slicing the inner content and attribute string.
    BracketedSpan { end: usize },
}

/// A byte-keyed view of the IR's standalone Pandoc constructs that the
/// emission walk consumes directly: inline footnotes, native spans,
/// footnote references, bracketed citations, bare citations, and
/// bracketed spans. Recognition is authoritative in `build_ir` under
/// `Dialect::Pandoc`; the dispatcher's legacy branches for these
/// constructs (`^[`, `<span>`, `[^id]`, `[@cite]`, `@cite` / `-@cite`,
/// `[text]{attrs}`) are gated to `Dialect::CommonMark` only and only
/// fire when the relevant extension is explicitly enabled.
#[derive(Debug, Default, Clone)]
pub struct ConstructPlan {
    by_pos: BTreeMap<usize, ConstructDispo>,
}

impl ConstructPlan {
    pub fn lookup(&self, pos: usize) -> Option<&ConstructDispo> {
        self.by_pos.get(&pos)
    }

    pub fn is_empty(&self) -> bool {
        self.by_pos.is_empty()
    }
}

/// Build a [`ConstructPlan`] from the resolved IR. Each
/// `Construct { kind: InlineFootnote | NativeSpan, .. }` becomes one
/// entry keyed at its start byte.
pub fn build_construct_plan(events: &[IrEvent]) -> ConstructPlan {
    let mut by_pos: BTreeMap<usize, ConstructDispo> = BTreeMap::new();
    for ev in events {
        if let IrEvent::Construct { start, end, kind } = ev {
            match kind {
                ConstructKind::InlineFootnote => {
                    by_pos.insert(*start, ConstructDispo::InlineFootnote { end: *end });
                }
                ConstructKind::NativeSpan => {
                    by_pos.insert(*start, ConstructDispo::NativeSpan { end: *end });
                }
                ConstructKind::FootnoteReference => {
                    by_pos.insert(*start, ConstructDispo::FootnoteReference { end: *end });
                }
                ConstructKind::BracketedCitation => {
                    by_pos.insert(*start, ConstructDispo::BracketedCitation { end: *end });
                }
                ConstructKind::BareCitation => {
                    by_pos.insert(*start, ConstructDispo::BareCitation { end: *end });
                }
                ConstructKind::BracketedSpan => {
                    by_pos.insert(*start, ConstructDispo::BracketedSpan { end: *end });
                }
                _ => {}
            }
        }
    }
    ConstructPlan { by_pos }
}

/// Build a [`BracketPlan`] from the resolved IR. Each `OpenBracket`
/// resolution becomes an [`BracketDispo::Open`] keyed at the opener's
/// start byte. Unresolved openers and unmatched closers become
/// `BracketDispo::Literal` so the emission path can recognise them
/// without re-parsing.
pub fn build_bracket_plan(events: &[IrEvent]) -> BracketPlan {
    let mut by_pos: BTreeMap<usize, BracketDispo> = BTreeMap::new();
    for ev in events {
        match ev {
            IrEvent::OpenBracket {
                start,
                is_image,
                resolution: Some(res),
                ..
            } => {
                by_pos.insert(
                    *start,
                    BracketDispo::Open {
                        is_image: *is_image,
                        text_start: res.text_start,
                        text_end: res.text_end,
                        suffix_start: res.suffix_start,
                        suffix_end: res.suffix_end,
                        kind: res.kind.clone(),
                    },
                );
            }
            IrEvent::OpenBracket {
                start,
                end,
                is_image,
                resolution: None,
                unresolved_ref: Some(shape),
                ..
            } => {
                by_pos.insert(
                    *start,
                    BracketDispo::UnresolvedReference {
                        is_image: *is_image,
                        text_start: *end,
                        text_end: shape.text_end,
                        end: shape.end,
                    },
                );
            }
            IrEvent::OpenBracket {
                start,
                is_image,
                resolution: None,
                unresolved_ref: None,
                ..
            } => {
                let len = if *is_image { 2 } else { 1 };
                for off in 0..len {
                    by_pos.insert(*start + off, BracketDispo::Literal);
                }
            }
            IrEvent::CloseBracket {
                pos,
                matched: false,
            } => {
                by_pos.insert(*pos, BracketDispo::Literal);
            }
            _ => {}
        }
    }
    BracketPlan { by_pos }
}

/// One-shot helper: build the IR, run all passes, and return the
/// bundled [`InlinePlans`] (emphasis dispositions, bracket resolutions,
/// and standalone Pandoc constructs) — packaged together so the inline
/// emission path can consume them in one go for either dialect.
///
/// Pass ordering follows the CommonMark §6.3 reference impl: bracket
/// resolution runs first, then emphasis is processed *scoped per resolved
/// bracket pair's inner event range*, then once more on the residual
/// top-level events. This prevents emphasis pairs from forming across a
/// link's bracket boundary, which the previous "all-emphasis-then-all-
/// brackets" order got wrong (e.g. spec example #473).
pub fn build_full_plans(
    text: &str,
    start: usize,
    end: usize,
    config: &ParserOptions,
) -> InlinePlans {
    let mut scratch = ScratchEvents::checkout();
    let bundle = scratch.inner.as_mut().unwrap();
    bundle.events.clear();
    bundle.bracket_pairs.clear();
    bundle.excluded.clear();

    build_ir_into(text, start, end, config, &mut bundle.events);
    // §6.3 bracket resolution runs for both dialects. Under CommonMark
    // it enforces refdef-aware shortcut/collapsed/full-ref resolution
    // and the §6.3 link-in-link deactivation rule. Under Pandoc it
    // performs shape-only resolution (any non-empty label resolves) and
    // skips the deactivation pass — pandoc-native is outer-wins for
    // nested links and the dispatcher's `suppress_inner_links` flag
    // suppresses inner LINK emission during LINK-text recursion.
    process_brackets(
        &mut bundle.events,
        text,
        config.refdef_labels.as_ref(),
        config.dialect,
    );

    // Scoped emphasis pass per resolved bracket pair, innermost first.
    // We collect (open_idx, close_idx) pairs of resolved brackets and run
    // emphasis only over the events strictly between them. Innermost-first
    // ordering matters: an outer link wraps emphasis that wraps an inner
    // link, and the inner link's inner range must be paired before the
    // outer's inner range so the top-level pass sees consistent state.
    // Include both resolved-link bracket pairs and Pandoc unresolved-
    // reference bracket pairs in the scoping set. The latter wrap into
    // an `UNRESOLVED_REFERENCE` CST node, which is just as much a tree
    // boundary for emphasis as a resolved `LINK` — emphasis must not
    // pair across the wrapper's brackets, otherwise the emission walk
    // produces a non-tree-shaped CST.
    bundle.bracket_pairs.extend(
        bundle
            .events
            .iter()
            .enumerate()
            .filter_map(|(i, ev)| match ev {
                IrEvent::OpenBracket {
                    resolution: Some(res),
                    ..
                } => Some((i, res.close_event as usize)),
                IrEvent::OpenBracket {
                    resolution: None,
                    unresolved_ref: Some(shape),
                    ..
                } => Some((i, shape.close_event as usize)),
                _ => None,
            }),
    );
    // Innermost-first: sort by close_idx ascending, then open_idx descending.
    bundle
        .bracket_pairs
        .sort_by(|a, b| a.1.cmp(&b.1).then(b.0.cmp(&a.0)));
    // Iterate pairs by index so we can hold &mut bundle.events while
    // reading bundle.bracket_pairs (split borrow on disjoint fields).
    for i in 0..bundle.bracket_pairs.len() {
        let (open_idx, close_idx) = bundle.bracket_pairs[i];
        process_emphasis_in_range(&mut bundle.events, open_idx + 1, close_idx, config.dialect);
    }

    // Pandoc-only degrade pass for unresolved bracket-shape patterns
    // whose interior left any delim-run byte unmatched after the scoped
    // emphasis pass. Pandoc-native degrades such brackets to literal `[`
    // / `]` text — the user's intent was clearly not a reference. The
    // bracket_pairs entry stays so the inner delims remain in the
    // top-level exclusion mask (otherwise they'd re-enter pairing and
    // could form Emph spans with delims outside, which pandoc never
    // does — see the bug_2_emphasis_crosses_brackets_pandoc fixture).
    // Flipping `unresolved_ref` to `None` makes `build_bracket_plan`
    // emit `BracketDispo::Literal` for the bracket bytes; flipping
    // `CloseBracket.matched` to `false` does the same for the `]`.
    for i in 0..bundle.bracket_pairs.len() {
        let (open_idx, close_idx) = bundle.bracket_pairs[i];
        let is_unresolved = matches!(
            &bundle.events[open_idx],
            IrEvent::OpenBracket {
                resolution: None,
                unresolved_ref: Some(_),
                ..
            }
        );
        if !is_unresolved {
            continue;
        }
        if !range_has_unmatched_delim_bytes(&bundle.events, open_idx + 1, close_idx) {
            continue;
        }
        if let IrEvent::OpenBracket { unresolved_ref, .. } = &mut bundle.events[open_idx] {
            *unresolved_ref = None;
        }
        if let IrEvent::CloseBracket { matched, .. } = &mut bundle.events[close_idx] {
            *matched = false;
        }
    }

    // Top-level emphasis pass: handles delim runs that fall outside any
    // resolved bracket pair.
    let len = bundle.events.len();
    if bundle.bracket_pairs.is_empty() {
        // Fast path: no resolved brackets means no exclusion mask needed —
        // skip the resize-and-fill pass entirely. Common for prose
        // paragraphs without inline links.
        process_emphasis_in_range_filtered(&mut bundle.events, 0, len, None, config.dialect);
    } else {
        // Build exclusion bitmap: any delim run whose event index lies
        // inside a resolved bracket pair is excluded from the top-level
        // pass. Implements the §6.3 boundary rule: emphasis at the top
        // level must not pair across a link's brackets.
        bundle.excluded.resize(len, false);
        for &(open_idx, close_idx) in &bundle.bracket_pairs {
            for slot in bundle
                .excluded
                .iter_mut()
                .take(close_idx)
                .skip(open_idx + 1)
            {
                *slot = true;
            }
        }
        process_emphasis_in_range_filtered(
            &mut bundle.events,
            0,
            len,
            Some(&bundle.excluded),
            config.dialect,
        );
    }

    InlinePlans {
        emphasis: build_emphasis_plan(&bundle.events),
        brackets: build_bracket_plan(&bundle.events),
        constructs: build_construct_plan(&bundle.events),
    }
}

/// Returns true if any [`IrEvent::DelimRun`] in the event range
/// `[lo, hi)` has byte coverage from its `matches` vec that is less
/// than the run length — i.e. at least one byte of the run failed to
/// pair as emphasis. Used by the Pandoc unresolved-reference degrade
/// pass in [`build_full_plans`].
///
/// Delim runs whose flanking rules forbid both opening *and* closing
/// (e.g. intraword `_` inside `foo_bar`) are skipped: those bytes were
/// never a pairing candidate, so an "unmatched" count for them isn't
/// evidence of a failed emphasis attempt. Without this exclusion every
/// URL or identifier with an underscore inside an unresolved bracket
/// pair would spuriously degrade the bracket-shape to literal text.
fn range_has_unmatched_delim_bytes(events: &[IrEvent], lo: usize, hi: usize) -> bool {
    let hi = hi.min(events.len());
    for ev in &events[lo..hi] {
        if let IrEvent::DelimRun {
            start,
            end,
            matches,
            can_open,
            can_close,
            ..
        } = ev
        {
            if !can_open && !can_close {
                continue;
            }
            let total = end - start;
            let matched: usize = matches.iter().map(|m| m.len as usize).sum();
            if matched < total {
                return true;
            }
        }
    }
    false
}

/// Thread-local pool of scratch buffers used by [`build_full_plans`].
///
/// `build_full_plans` checks out one bundle for the duration of the call
/// and returns it on drop so the next call (or a recursive nested call
/// from an inline emitter) reuses the allocations. The pool is
/// per-thread — the parser is single-threaded — and bounded so a
/// long-running editor session can't accumulate stale capacity.
struct ScratchEvents {
    inner: Option<ScratchBundle>,
}

#[derive(Default)]
struct ScratchBundle {
    events: Vec<IrEvent>,
    bracket_pairs: Vec<(usize, usize)>,
    excluded: Vec<bool>,
}

thread_local! {
    static IR_EVENT_POOL: std::cell::RefCell<Vec<ScratchBundle>> =
        const { std::cell::RefCell::new(Vec::new()) };
}

impl ScratchEvents {
    fn checkout() -> Self {
        let bundle = IR_EVENT_POOL
            .with(|p| p.borrow_mut().pop())
            .unwrap_or_default();
        Self {
            inner: Some(bundle),
        }
    }
}

impl Drop for ScratchEvents {
    fn drop(&mut self) {
        if let Some(mut bundle) = self.inner.take() {
            bundle.events.clear();
            bundle.bracket_pairs.clear();
            bundle.excluded.clear();
            // Cap pool depth at 8 (deepest realistic nested-link recursion)
            // and drop any bundle whose `events` grew past 8K (a single
            // pathological paragraph shouldn't pin a huge allocation
            // forever).
            if bundle.events.capacity() <= 8192 {
                IR_EVENT_POOL.with(|p| {
                    let mut pool = p.borrow_mut();
                    if pool.len() < 8 {
                        pool.push(bundle);
                    }
                });
            }
        }
    }
}

/// Bundle of plans produced by [`build_full_plans`] and consumed by the
/// inline emission walk.
#[derive(Debug, Default, Clone)]
pub struct InlinePlans {
    pub emphasis: EmphasisPlan,
    pub brackets: BracketPlan,
    pub constructs: ConstructPlan,
}

/// Convert the IR's delim-run match decisions into an [`EmphasisPlan`],
/// preserving the byte-keyed disposition shape the existing emission walk
/// consumes.
///
/// Each match on a [`DelimRun`](IrEvent::DelimRun) produces one entry in
/// the plan: the opener side records `Open` with the partner's source
/// byte and length; the closer side records `Close`. Bytes within a run
/// that are *not* covered by any match get a `Literal` entry, which the
/// emission walk uses to coalesce unmatched delimiter bytes with
/// surrounding plain text.
pub fn build_emphasis_plan(events: &[IrEvent]) -> EmphasisPlan {
    let mut by_pos: BTreeMap<usize, DelimChar> = BTreeMap::new();
    for ev in events {
        if let IrEvent::DelimRun {
            start,
            end,
            matches,
            ..
        } = ev
        {
            for m in matches {
                let pos = *start + m.offset_in_run as usize;
                let partner_run_start = match &events[m.partner_event as usize] {
                    IrEvent::DelimRun { start: ps, .. } => *ps,
                    _ => continue,
                };
                let partner_pos = partner_run_start + m.partner_offset as usize;
                if m.is_opener {
                    by_pos.insert(
                        pos,
                        DelimChar::Open {
                            len: m.len,
                            partner: partner_pos,
                            partner_len: m.len,
                            kind: m.kind,
                        },
                    );
                } else {
                    by_pos.insert(pos, DelimChar::Close);
                }
            }
            // Any remaining bytes (not covered by a match) are literal.
            for pos in *start..*end {
                by_pos.entry(pos).or_insert(DelimChar::Literal);
            }
        }
    }
    EmphasisPlan::from_dispositions(by_pos)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::options::Flavor;
    use crate::parser::inlines::inline_ir::DelimChar;
    use std::sync::Arc;

    fn cm_opts() -> ParserOptions {
        let flavor = Flavor::CommonMark;
        ParserOptions {
            flavor,
            dialect: crate::options::Dialect::for_flavor(flavor),
            extensions: crate::options::Extensions::for_flavor(flavor),
            pandoc_compat: crate::options::PandocCompat::default(),
            refdef_labels: None,
        }
    }

    fn refdefs<I: IntoIterator<Item = &'static str>>(labels: I) -> RefdefMap {
        Arc::new(labels.into_iter().map(|s| s.to_string()).collect())
    }

    #[test]
    fn ir_event_range_covers_all_variants() {
        let txt = IrEvent::Text { start: 0, end: 5 };
        assert_eq!(txt.range(), (0, 5));

        let close = IrEvent::CloseBracket {
            pos: 7,
            matched: false,
        };
        assert_eq!(close.range(), (7, 8));

        let open = IrEvent::OpenBracket {
            start: 1,
            end: 3,
            is_image: true,
            active: true,
            resolution: None,
            unresolved_ref: None,
        };
        assert_eq!(open.range(), (1, 3));
    }

    #[test]
    fn scan_records_text_and_delim_run() {
        let opts = cm_opts();
        let ir = build_ir("foo *bar*", 0, 9, &opts);
        // Expect: Text "foo ", DelimRun "*", Text "bar", DelimRun "*"
        assert!(matches!(ir[0], IrEvent::Text { start: 0, end: 4 }));
        assert!(matches!(
            ir[1],
            IrEvent::DelimRun {
                ch: b'*',
                start: 4,
                end: 5,
                ..
            }
        ));
        assert!(matches!(ir[2], IrEvent::Text { start: 5, end: 8 }));
        assert!(matches!(
            ir[3],
            IrEvent::DelimRun {
                ch: b'*',
                start: 8,
                end: 9,
                ..
            }
        ));
    }

    #[test]
    fn scan_records_brackets() {
        let opts = cm_opts();
        let ir = build_ir("[foo]", 0, 5, &opts);
        assert!(matches!(
            ir[0],
            IrEvent::OpenBracket {
                start: 0,
                end: 1,
                is_image: false,
                ..
            }
        ));
        assert!(matches!(ir[1], IrEvent::Text { start: 1, end: 4 }));
        assert!(matches!(
            ir[2],
            IrEvent::CloseBracket {
                pos: 4,
                matched: false
            }
        ));
    }

    #[test]
    fn scan_records_image_bracket() {
        let opts = cm_opts();
        let ir = build_ir("![alt]", 0, 6, &opts);
        assert!(matches!(
            ir[0],
            IrEvent::OpenBracket {
                start: 0,
                end: 2,
                is_image: true,
                ..
            }
        ));
    }

    #[test]
    fn scan_handles_code_span_opacity() {
        let opts = cm_opts();
        let ir = build_ir("a `*x*` b", 0, 9, &opts);
        // Code span `*x*` should be a Construct, NOT delim runs.
        let has_delim_run = ir.iter().any(|e| matches!(e, IrEvent::DelimRun { .. }));
        assert!(
            !has_delim_run,
            "code span content should not produce delim runs"
        );
        assert!(ir.iter().any(|e| matches!(
            e,
            IrEvent::Construct {
                kind: ConstructKind::CodeSpan,
                ..
            }
        )));
    }

    #[test]
    fn process_emphasis_simple_pair() {
        let opts = cm_opts();
        let mut ir = build_ir("*foo*", 0, 5, &opts);
        process_emphasis(&mut ir, opts.dialect);
        // First DelimRun (open) gets a match.
        let opener = ir
            .iter()
            .find(|e| matches!(e, IrEvent::DelimRun { start: 0, .. }))
            .unwrap();
        if let IrEvent::DelimRun { matches, .. } = opener {
            assert_eq!(matches.len(), 1);
            assert!(matches[0].is_opener);
            assert_eq!(matches[0].kind, EmphasisKind::Emph);
        }
    }

    #[test]
    fn brackets_resolve_inline_link() {
        let opts = cm_opts();
        let mut ir = build_ir("[foo](/url)", 0, 11, &opts);
        process_brackets(&mut ir, "[foo](/url)", None, opts.dialect);
        let open = ir
            .iter()
            .find(|e| matches!(e, IrEvent::OpenBracket { start: 0, .. }))
            .unwrap();
        if let IrEvent::OpenBracket { resolution, .. } = open {
            let r = resolution.as_ref().expect("inline link resolved");
            assert!(matches!(r.kind, LinkKind::Inline { .. }));
            if let LinkKind::Inline { dest, .. } = &r.kind {
                assert_eq!(dest, "/url");
            }
        }
    }

    #[test]
    fn brackets_shortcut_resolves_only_with_refdef() {
        let opts = cm_opts();
        let text = "[foo]";
        let map = refdefs(["foo"]);
        let mut ir = build_ir(text, 0, text.len(), &opts);
        process_brackets(&mut ir, text, Some(&map), opts.dialect);
        let open = ir
            .iter()
            .find(|e| matches!(e, IrEvent::OpenBracket { start: 0, .. }))
            .unwrap();
        if let IrEvent::OpenBracket { resolution, .. } = open {
            assert!(matches!(
                resolution.as_ref().unwrap().kind,
                LinkKind::ShortcutReference
            ));
        }
    }

    #[test]
    fn brackets_shortcut_falls_through_without_refdef() {
        // CMark example #523 mechanic: `[bar* baz]` is not a refdef, so
        // it must NOT resolve as a link — the brackets stay literal so
        // the inner `*` becomes available to the outer emphasis scanner.
        let opts = cm_opts();
        let text = "[bar* baz]";
        let mut ir = build_ir(text, 0, text.len(), &opts);
        process_brackets(&mut ir, text, None, opts.dialect);
        let open = ir
            .iter()
            .find(|e| matches!(e, IrEvent::OpenBracket { start: 0, .. }))
            .unwrap();
        if let IrEvent::OpenBracket { resolution, .. } = open {
            assert!(resolution.is_none(), "no refdef → bracket stays literal");
        }
    }

    /// Spec #473: `*[bar*](/url)`. The link `[bar*](/url)` resolves; the
    /// outer `*...*` MUST NOT pair across the link's bracket boundary,
    /// because the inner `*` belongs to the link text.
    #[test]
    fn full_plans_emphasis_does_not_cross_resolved_link_boundary() {
        let opts = cm_opts();
        let text = "*[bar*](/url)";
        let plans = build_full_plans(text, 0, text.len(), &opts);
        // The leading `*` (at byte 0) must NOT be matched as an emphasis
        // opener — there's no closer outside the link, and the inner `*`
        // (at byte 5) is inside the resolved link's text range so it must
        // not be paired with byte 0.
        assert!(
            matches!(plans.emphasis.lookup(0), Some(DelimChar::Literal) | None),
            "outer `*` at byte 0 must not pair across link boundary, got {:?}",
            plans.emphasis.lookup(0)
        );
        // The link `[bar*](/url)` must resolve (opener at byte 1).
        assert!(
            matches!(plans.brackets.lookup(1), Some(BracketDispo::Open { .. })),
            "link [bar*](/url) must resolve at byte 1"
        );
    }

    fn pandoc_opts() -> ParserOptions {
        let flavor = Flavor::Pandoc;
        ParserOptions {
            flavor,
            dialect: crate::options::Dialect::for_flavor(flavor),
            extensions: crate::options::Extensions::for_flavor(flavor),
            pandoc_compat: crate::options::PandocCompat::default(),
            refdef_labels: None,
        }
    }

    /// Bug #2 (a): unresolved Pandoc bracket-shape with unmatched delim
    /// inside its text degrades to literal `[`/`]`. Outer emphasis pair
    /// across the (now-literal) brackets must form.
    #[test]
    fn full_plans_unresolved_bracket_degrades_when_inner_delim_unmatched() {
        let opts = pandoc_opts();
        let text = "*foo [bar*] baz*";
        let plans = build_full_plans(text, 0, text.len(), &opts);
        assert!(
            matches!(plans.brackets.lookup(5), Some(BracketDispo::Literal) | None),
            "degraded `[` at byte 5 must be Literal/None, got {:?}",
            plans.brackets.lookup(5)
        );
        assert!(
            matches!(plans.emphasis.lookup(0), Some(DelimChar::Open { .. })),
            "outer `*` at byte 0 must open Emph after degrade, got {:?}",
            plans.emphasis.lookup(0)
        );
    }

    /// Intraword `_` (e.g. inside a URL like
    /// `hyperparameter_optimization`) is not flanking — `can_open` and
    /// `can_close` are both false — so it can never pair as emphasis.
    /// The degrade pass must not treat such delim runs as "failed
    /// emphasis attempts" and demote the surrounding bracket-shape to
    /// literal text, otherwise every URL/identifier inside an
    /// unresolved reference round-trips through `\[` / `\]` escapes
    /// under `tex_math_single_backslash` and reparses as display math.
    #[test]
    fn full_plans_unresolved_bracket_keeps_wrapper_with_intraword_underscore() {
        let opts = pandoc_opts();
        let text = "[foo_bar more]";
        let plans = build_full_plans(text, 0, text.len(), &opts);
        assert!(
            matches!(
                plans.brackets.lookup(0),
                Some(BracketDispo::UnresolvedReference { .. })
            ),
            "wrapper must be preserved across intraword `_`, got {:?}",
            plans.brackets.lookup(0)
        );
    }

    /// Bug #2 (b): unresolved Pandoc bracket whose interior emphasis
    /// pairs cleanly keeps the wrapper (linter/LSP hook).
    #[test]
    fn full_plans_unresolved_bracket_keeps_wrapper_when_inner_paired() {
        let opts = pandoc_opts();
        let text = "[foo *bar*]";
        let plans = build_full_plans(text, 0, text.len(), &opts);
        assert!(
            matches!(
                plans.brackets.lookup(0),
                Some(BracketDispo::UnresolvedReference { .. })
            ),
            "wrapper must be preserved when inner emph pairs, got {:?}",
            plans.brackets.lookup(0)
        );
    }

    /// Spec #533: `[foo *bar [baz][ref]*][ref]` with `[ref]: /uri`.
    /// Inner `[baz][ref]` resolves as a link; §6.3 link-in-link rule
    /// deactivates the outer `[foo ...][ref]` so it falls through to
    /// literal brackets. Emphasis `*bar [baz][ref]*` wraps the inner link.
    #[test]
    fn full_plans_link_in_link_suppression_for_reference_links() {
        let opts = cm_opts();
        let text = "[foo *bar [baz][ref]*][ref]";
        let mut opts_with_refs = opts.clone();
        let labels: HashSet<String> = ["ref".to_string()].into_iter().collect();
        opts_with_refs.refdef_labels = Some(std::sync::Arc::new(labels));
        let plans = build_full_plans(text, 0, text.len(), &opts_with_refs);

        // Inner `[baz][ref]` opener is at byte 10 — must resolve.
        assert!(
            matches!(plans.brackets.lookup(10), Some(BracketDispo::Open { .. })),
            "inner [baz][ref] must resolve at byte 10, got {:?}",
            plans.brackets.lookup(10)
        );
        // Outer `[foo ...][ref]` opener is at byte 0 — must NOT resolve
        // (link-in-link suppression).
        assert!(
            matches!(plans.brackets.lookup(0), Some(BracketDispo::Literal) | None),
            "outer [foo ...][ref] must fall through to literal at byte 0, got {:?}",
            plans.brackets.lookup(0)
        );
        // Trailing `[ref]` after the outer `]` is at byte 22 — it's a
        // standalone shortcut reference and must resolve.
        assert!(
            matches!(plans.brackets.lookup(22), Some(BracketDispo::Open { .. })),
            "trailing [ref] must resolve at byte 22, got {:?}",
            plans.brackets.lookup(22)
        );
        // Emphasis `*...*` at bytes 5 and 20 must pair — the scoped
        // emphasis pass over the (deactivated) outer bracket's inner
        // event range pairs these.
        assert!(
            matches!(plans.emphasis.lookup(5), Some(DelimChar::Open { .. })),
            "emphasis opener at byte 5 must pair, got {:?}",
            plans.emphasis.lookup(5)
        );
    }
}