mdream 1.5.10

Fastest HTML-to-Markdown converter. Zero dependencies, streaming support.
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
//! Markdown output: tag enter/exit emission, buffer writing, spacing.

use super::*;

/// Bytes rejected from a fenced-code info string.
///
/// Two classes, for two different reasons:
/// - `` ` ``, `~` and the C0/DEL controls can break the construct itself — a
///   marker run extends or terminates the fence, a line terminator ends the
///   opening line and starts a new block.
/// - `"`, `'`, `<`, `>` and `&` are inert in CommonMark, but renderers
///   interpolate the info string into `<code class="language-…">`, so mdream
///   does not hand them markup characters.
///
/// Everything else is accepted, including `_`, `/` and non-ASCII names.
#[inline]
const fn is_unsafe_fence_info_byte(byte: u8) -> bool {
  byte < 0x20 || matches!(byte, 0x7F | b'`' | b'~' | b'"' | b'\'' | b'<' | b'>' | b'&')
}

const DESTINATION_ESCAPES: [u8; 16] = [0, 0, 0, 0, 0, 0, 0, 80, 0, 0, 0, 16, 0, 0, 0, 0];
const TITLE_ESCAPES: [u8; 16] = [0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0];
const IMAGE_DESCRIPTION_ESCAPES: [u8; 16] = [0, 0, 0, 0, 64, 4, 0, 16, 0, 0, 0, 184, 1, 0, 0, 64];

#[inline(always)]
fn in_byte_set(byte: u8, set: &[u8; 16]) -> bool {
  byte < 128 && set[(byte >> 3) as usize] & (1 << (byte & 7)) != 0
}

#[inline(never)]
fn write_ascii_escaped(output: &mut String, value: &str, escapes: &[u8; 16]) {
  let bytes = value.as_bytes();
  let mut copied = 0usize;
  let mut index = 0usize;
  while index < bytes.len() {
    if in_byte_set(bytes[index], escapes) {
      output.push_str(&value[copied..index]);
      output.push('\\');
      copied = index;
    }
    index += 1;
  }
  output.push_str(&value[copied..]);
}

/// Bytes forcing a destination into angle brackets: tab, LF, FF, CR, space,
/// `(`, `)`, and every byte of [`DESTINATION_ESCAPES`] (must stay a superset).
const DESTINATION_NEEDS_ANGLE: [u8; 16] = [0, 54, 0, 0, 1, 3, 0, 80, 0, 0, 0, 16, 0, 0, 0, 0];

fn write_markdown_destination(output: &mut String, destination: &str) {
  let bytes = destination.as_bytes();
  let mut index = 0usize;
  while index < bytes.len() && !in_byte_set(bytes[index], &DESTINATION_NEEDS_ANGLE) {
    index += 1;
  }
  if index == bytes.len() {
    output.push_str(destination);
    return;
  }

  output.push('<');
  write_ascii_escaped(output, destination, &DESTINATION_ESCAPES);
  output.push('>');
}

fn write_markdown_resource(output: &mut String, destination: &str, title: Option<&str>) {
  output.push('(');
  write_markdown_destination(output, destination);
  if let Some(title) = title
    && !title.is_empty()
  {
    output.push_str(" \"");
    write_ascii_escaped(output, title, &TITLE_ESCAPES);
    output.push('"');
  }
  output.push(')');
}

fn write_image_description(output: &mut String, alt: &str) {
  write_ascii_escaped(output, alt, &IMAGE_DESCRIPTION_ESCAPES);
}

#[inline(never)]
#[allow(clippy::cast_possible_truncation)] // `parsed` is bounded by the `u32` maximum argument.
fn parse_bounded_u32(value: &str, max: u32) -> Option<u32> {
  let value = value.trim_ascii().as_bytes();
  let mut index = usize::from(value.first() == Some(&b'+'));
  if index == value.len() {
    return None;
  }
  let mut parsed = 0u64;
  while index < value.len() {
    let byte = value[index];
    let digit = byte.wrapping_sub(b'0');
    if digit > 9 {
      return None;
    }
    parsed = parsed * 10 + u64::from(digit);
    if parsed > u64::from(max) {
      return None;
    }
    index += 1;
  }
  Some(parsed as u32)
}

impl ConvertState {
  #[inline]
  fn inline_marker_type(tag_id: u8) -> Option<u8> {
    // The kind is the delimiter identity: one value per distinct delimiter
    // string, so tags sharing a delimiter share a kind.
    match tag_id {
      TAG_STRONG | TAG_B | TAG_DFN => Some(0),
      TAG_EM | TAG_I | TAG_FIGCAPTION => Some(1),
      TAG_DEL | TAG_S | TAG_STRIKE => Some(2),
      TAG_CITE => Some(3),
      TAG_KBD | TAG_CODE | TAG_SAMP | TAG_VAR => Some(4),
      TAG_Q => Some(5),
      _ => None,
    }
  }

  fn max_backtick_run(value: &str) -> usize {
    let mut max = 0usize;
    let mut run = 0usize;
    for byte in value.bytes() {
      if byte == b'`' {
        run += 1;
        max = max.max(run);
      } else {
        run = 0;
      }
    }
    max
  }

  fn max_line_leading_run(value: &str, marker: u8, indent: &str) -> usize {
    value
      .split('\n')
      .map(|line| {
        let line = line.strip_prefix(indent).unwrap_or(line);
        let bytes = line.as_bytes();
        let mut index = 0usize;
        while index < bytes.len() && index < 3 && bytes[index] == b' ' {
          index += 1;
        }
        bytes[index..]
          .iter()
          .take_while(|&&byte| byte == marker)
          .count()
      })
      .max()
      .unwrap_or(0)
  }

  #[cold]
  #[inline(never)]
  fn finalize_code_span(&mut self, span: CodeSpanState) -> String {
    // A pipe splits the row even inside a code span; `\|` is GFM's escape and is
    // honoured there. Content sits at the buffer tail, so this shifts no offset.
    if self.depth_map[TAG_TABLE as usize] > 0 && self.buffer[span.content_start..].contains('|') {
      let escaped = self.buffer[span.content_start..].replace('|', "\\|");
      self.buffer.truncate(span.content_start);
      self.buffer.push_str(&escaped);
    }
    let max_run = Self::max_backtick_run(&self.buffer[span.content_start..]);
    let delimiter = "`".repeat((max_run + 1).max(1));
    let content = &self.buffer[span.content_start..];
    let padded = content.starts_with('`') || content.ends_with('`');
    let mut opening = String::with_capacity(
      span.content_start - span.output_start + delimiter.len() + usize::from(padded),
    );
    opening.push_str(&self.buffer[span.output_start..span.content_start - 1]);
    opening.push_str(&delimiter);
    if padded {
      opening.push(' ');
    }
    self
      .buffer
      .replace_range(span.output_start..span.content_start, &opening);
    if padded {
      format!(" {delimiter}")
    } else {
      delimiter
    }
  }

  #[cold]
  #[inline(never)]
  fn start_code_fence(
    &mut self,
    output_start: usize,
    content_start: usize,
    language: String,
    indent: String,
  ) {
    let marker_offset = self.buffer[output_start..content_start]
      .rfind(MARKDOWN_CODE_BLOCK)
      .expect("code fence opener missing from output");
    self.code_fence = Some(CodeFenceState {
      output_start,
      marker_offset,
      content_start,
      indent,
      language,
    });
  }

  #[cold]
  #[inline(never)]
  fn finalize_code_fence(&mut self) -> Option<String> {
    let fence = self.code_fence.take()?;
    let marker = if fence.language.contains('`') {
      b'~'
    } else {
      b'`'
    };
    let max_run =
      Self::max_line_leading_run(&self.buffer[fence.content_start..], marker, &fence.indent);
    let delimiter = (marker as char).to_string().repeat((max_run + 1).max(3));
    let marker_start = fence.output_start + fence.marker_offset;
    self.buffer.replace_range(
      marker_start..marker_start + MARKDOWN_CODE_BLOCK.len(),
      &delimiter,
    );
    Some(delimiter)
  }

  fn blockquote_offset(content: &str, list_indent: &str, offset: usize) -> usize {
    let mut source_start = 0usize;
    let mut output_start = 0usize;

    for line in content.split_inclusive('\n') {
      let line_content = line.strip_suffix('\n').unwrap_or(line);
      let removed = usize::from(!list_indent.is_empty() && line_content.starts_with(list_indent))
        * list_indent.len();
      let unindented_len = line_content.len().saturating_sub(removed);
      let prefix_len = list_indent.len() + 1 + usize::from(unindented_len > 0);
      let line_end = source_start + line_content.len();

      if offset <= line_end {
        return output_start + prefix_len + offset.saturating_sub(source_start + removed);
      }

      source_start += line.len();
      output_start += prefix_len + unindented_len + usize::from(line.ends_with('\n'));
    }

    output_start
  }

  #[cold]
  #[inline(never)]
  fn finalize_blockquote(&mut self) {
    let Some(frame) = self.blockquotes.pop() else {
      return;
    };
    let content_end = self
      .buffer
      .trim_end_matches(|c: char| c.is_ascii_whitespace())
      .len();
    if content_end < frame.content_start
      || (content_end == frame.content_start && self.has_streamed_output)
    {
      return;
    }
    let content = &self.buffer[frame.content_start..content_end];
    let mut quoted = String::with_capacity(
      content.len() + (frame.list_indent.len() + 2) * (content.matches('\n').count() + 1),
    );

    for (index, line) in content.split('\n').enumerate() {
      if index > 0 {
        quoted.push('\n');
      }
      quoted.push_str(&frame.list_indent);
      quoted.push('>');
      let unindented = if !frame.list_indent.is_empty() {
        line.strip_prefix(&frame.list_indent).unwrap_or(line)
      } else {
        line
      };
      if !unindented.is_empty() {
        quoted.push(' ');
        quoted.push_str(unindented);
      }
    }

    for (bracket_start, link_end) in &mut self.fragment_links {
      if *bracket_start >= frame.content_start && *link_end <= content_end {
        *bracket_start = frame.content_start
          + Self::blockquote_offset(
            content,
            &frame.list_indent,
            *bracket_start - frame.content_start,
          );
        *link_end = frame.content_start
          + Self::blockquote_offset(content, &frame.list_indent, *link_end - frame.content_start);
      }
    }

    self.buffer.truncate(frame.content_start);
    self.buffer.push_str(&quoted);
    self.last_content_cache_len = quoted.len();
  }

  pub(crate) fn flush_streaming_blockquote_lines(&mut self) {
    const FLUSH_THRESHOLD: usize = 8 * 1024;
    if self.buffer.len() < FLUSH_THRESHOLD
      || self.blockquotes.is_empty()
      || self.clean_flags & CLEAN_FRAGMENTS != 0
      || self.has_frontmatter
      || self.has_extraction
    {
      return;
    }

    let Some(mut flush_end) = self.buffer.rfind('\n').map(|index| index + 1) else {
      return;
    };
    if self
      .blockquotes
      .iter()
      .any(|frame| frame.content_start >= flush_end)
    {
      return;
    }

    let shared_start = self.blockquotes[0].content_start;
    if self
      .blockquotes
      .iter()
      .all(|frame| frame.content_start == shared_start && frame.list_indent.is_empty())
    {
      let content = &self.buffer[shared_start..flush_end];
      let quoted_prefix = "> ".repeat(self.blockquotes.len());
      let blank_prefix = quoted_prefix.trim_end();
      let mut quoted =
        String::with_capacity(content.len() + quoted_prefix.len() * content.matches('\n').count());
      for line in content.split_inclusive('\n') {
        let line = line.strip_suffix('\n').unwrap_or(line);
        if line.is_empty() {
          quoted.push_str(blank_prefix);
        } else {
          quoted.push_str(&quoted_prefix);
          quoted.push_str(line);
        }
        quoted.push('\n');
      }
      self.buffer.replace_range(shared_start..flush_end, &quoted);
      flush_end = shared_start + quoted.len();
      for frame in &mut self.blockquotes {
        frame.content_start = flush_end;
      }
      self.last_content_cache_len = self.buffer.len() - flush_end;
      return;
    }

    let frames = self.blockquotes.clone();
    for frame in frames.iter().rev() {
      let content = &self.buffer[frame.content_start..flush_end];
      let mut quoted = String::with_capacity(
        content.len() + (frame.list_indent.len() + 2) * content.matches('\n').count(),
      );
      for line in content.split_inclusive('\n') {
        let line = line.strip_suffix('\n').unwrap_or(line);
        quoted.push_str(&frame.list_indent);
        quoted.push('>');
        let unindented = if !frame.list_indent.is_empty() {
          line.strip_prefix(&frame.list_indent).unwrap_or(line)
        } else {
          line
        };
        if !unindented.is_empty() {
          quoted.push(' ');
          quoted.push_str(unindented);
        }
        quoted.push('\n');
      }
      self
        .buffer
        .replace_range(frame.content_start..flush_end, &quoted);
      flush_end = frame.content_start + quoted.len();
    }

    for frame in &mut self.blockquotes {
      frame.content_start = flush_end;
    }
    self.last_content_cache_len = self.buffer.len() - flush_end;
  }

  /// Emit markdown for entering the element currently on top of self.stack.
  #[inline]
  pub(crate) fn emit_enter_element(&mut self) {
    let stack_len = self.stack.len();
    if stack_len == 0 {
      return;
    }

    // Excluded nodes (including parsed template descendants) must return before
    // deferred <pre> handling so an inert subtree cannot mutate output state.
    if self.stack[stack_len - 1].excluded_from_markdown {
      self.last_node_is_inline = self.stack[stack_len - 1].is_inline;
      return;
    }

    if self.list_rule_pending && !self.stack[stack_len - 1].excludes_text_nodes {
      self.flush_list_rule();
    }

    // Deferred <pre> code fence (issue #97): open a bare <pre>'s fence right
    // before its first non-whitespace child. A direct <code> child keeps
    // fence ownership; a deeper/other first child opens the <pre>'s own fence.
    if !self.plain_text && self.pre_fence_pending {
      let tid = self.stack[stack_len - 1].tag_id;
      if tid == Some(TAG_CODE)
        && stack_len >= 2
        && self.stack[stack_len - 2].tag_id == Some(TAG_PRE)
      {
        self.pre_fence_pending = false;
      } else if tid != Some(TAG_PRE) {
        self.flush_pre_fence();
      }
    }
    // Arm the deferral when entering a <pre>; the fence (with this <pre>'s own
    // language) is emitted lazily above for the no-<code> case. Skipped inside
    // a table cell, where the <pre> is emitted as raw HTML instead (issue #147).
    if !self.plain_text
      && self.stack[stack_len - 1].tag_id == Some(TAG_PRE)
      && !self.in_table_cell()
    {
      let lang = Self::get_language_from_class(self.stack[stack_len - 1].attributes.get("class"))
        .to_string();
      self.pre_fence_pending = true;
      self.pre_fence_lang = lang;
    }

    // Phase 1: read from node + compute output (borrows self.stack immutably)
    let tag_id: Option<u8>;
    let is_inline: bool;
    let node_spacing: Option<[u8; 2]>;
    let mut output: Option<Cow<'static, str>>;
    // True when `output` is a user-supplied override enter string — emit it
    // verbatim without synthesizing a separating space (issue #93).
    let enter_is_literal: bool;
    {
      let (ancestors, last) = self.stack.split_at(stack_len - 1);
      let node = &last[0];

      tag_id = node.tag_id;

      // Check override is_inline
      let override_config = if self.has_tag_overrides {
        self
          .options
          .plugins
          .as_ref()
          .and_then(|p| p.tag_overrides.as_ref())
          .and_then(|ovs| ovs.iter().find(|(k, _)| k == node.name()).map(|(_, v)| v))
      } else {
        None
      };
      is_inline = override_config
        .and_then(|ov| ov.is_inline)
        .unwrap_or(node.is_inline);
      node_spacing = override_config.and_then(|ov| ov.spacing).or(node.spacing);

      // Table state reads (tag_id.is_some() is sufficient — all table tags have handlers)
      if tag_id.is_some() {
        if tag_id == Some(TAG_TABLE) {
          if self.depth_map[TAG_TABLE as usize] <= 1 {
            self.table_rendered_table = false;
            self.table_header_cells = 0;
          }
          self.table_column_alignments.clear();
        } else if tag_id == Some(TAG_TR) {
          self.table_current_row_cells = 0;
        } else if tag_id == Some(TAG_TH) {
          let align_val = node.attributes.get("align").map_or(0u8, |s| {
            match s.as_bytes().first().copied().unwrap_or(0) | 0x20 {
              b'l' => 1, // left
              b'c' => 2, // center
              b'r' => 3, // right
              _ => 0,
            }
          });
          if align_val != 0 || self.table_column_alignments.len() <= self.table_current_row_cells {
            self.table_column_alignments.push(align_val);
          }
        }
      }

      // Check override enter string
      output = if let Some(ov) = override_config {
        if let Some(ref s) = ov.enter {
          enter_is_literal = true;
          Some(Cow::Owned(s.clone()))
        } else {
          enter_is_literal = false;
          self.get_enter_output(node, ancestors)
        }
      } else {
        enter_is_literal = false;
        self.get_enter_output(node, ancestors)
      };
    }
    // Phase 1 ends — self.stack borrow released

    // Phase 2: calculate new lines + write buffer
    let new_line_config = self.calculate_new_line_config(tag_id, node_spacing);
    let quote_at_start = self
      .blockquotes
      .last()
      .is_some_and(|frame| frame.content_start == self.buffer.len());
    let configured_new_lines = if quote_at_start {
      0
    } else {
      new_line_config[0]
    };

    // Clean mode — single guard for all clean checks
    if self.clean_flags != 0
      && let Some(id) = tag_id
    {
      if id == TAG_A {
        // emptyLinks: skip hrefs that cannot represent meaningful navigation.
        if self.clean_flags & CLEAN_EMPTY_LINKS != 0 {
          let node = &self.stack[self.stack.len() - 1];
          if let Some(href) = node.attributes.get("href")
            && is_empty_link_href(href)
          {
            self.skip_current_link = true;
            self.last_node_is_inline = is_inline;
            return;
          }
          self.skip_current_link = false;
        }
      } else if id == TAG_IMG && self.clean_flags & CLEAN_EMPTY_IMAGES != 0 {
        let node = &self.stack[self.stack.len() - 1];
        let alt = node.attributes.get("alt").map_or("", String::as_str);
        if alt.is_empty() {
          self.last_node_is_inline = is_inline;
          return;
        }
      }
    }

    // Whitespace immediately before <br> has no visual effect in HTML. Let the
    // explicit line boundary subsume it so the output has no trailing spaces.
    if tag_id == Some(TAG_BR)
      && !enter_is_literal
      && self.depth_map[TAG_PRE as usize] == 0
      && output.as_deref().is_some_and(|value| value.ends_with('\n'))
    {
      self.trim_trailing_spaces();
      if output.as_deref() == Some("\n") && self.buffer.ends_with("\n\n") {
        output = None;
      }
    }

    let output_start = self.buffer.len();
    self.write_output(
      true,
      is_inline,
      configured_new_lines,
      output.as_deref(),
      enter_is_literal,
    );

    if !self.plain_text && !enter_is_literal && tag_id == Some(TAG_LI) && !self.in_table_cell() {
      self.record_item_marker(self.stack[stack_len - 1].index, output_start);
    }

    if !self.plain_text && !enter_is_literal && tag_id == Some(TAG_BLOCKQUOTE) {
      if !self.blockquotes.is_empty() && self.buffer.ends_with("\n\n") {
        self.buffer.pop();
        // Frames anchored at the old end move with the popped byte. Siblings can
        // share an offset, so this is not only the innermost. See `trim_floor`.
        for frame in &mut self.blockquotes {
          frame.content_start = frame.content_start.min(self.buffer.len());
        }
      }
      self.blockquotes.push(BlockquoteFrame {
        content_start: self.buffer.len(),
        list_indent: self.list_indent.clone(),
      });
    }

    if !enter_is_literal && tag_id == Some(TAG_CODE) {
      if self.depth_map[TAG_PRE as usize] == 0 {
        if !self.in_raw_html_block()
          && let Some(emitted) = output.as_deref()
        {
          self.code_spans.push(CodeSpanState {
            output_start: self.buffer.len() - emitted.len(),
            content_start: self.buffer.len(),
          });
        }
      } else if !self.pre_fence_open
        && !self.in_table_cell()
        && let Some(emitted) = output.as_deref()
      {
        let output_start = self.buffer.len() - emitted.len();
        let language =
          Self::get_language_from_class(self.stack[stack_len - 1].attributes.get("class"))
            .to_string();
        self.start_code_fence(
          output_start,
          self.buffer.len(),
          language,
          self.list_indent.clone(),
        );
        self.pre_fence_open = true;
      }
    }

    // After write_output, the emitted `[` (if any) is the last byte of the
    // buffer. Stash that exact position so emit_exit_element can find the
    // bracket in O(1) instead of scanning forward.
    if tag_id == Some(TAG_A) {
      let buf_len = self.buffer.len();
      self.link_bracket_pos = if buf_len > 0 && self.buffer.as_bytes()[buf_len - 1] == b'[' {
        buf_len - 1
      } else {
        buf_len
      };
    }

    if !enter_is_literal
      && let Some(id) = tag_id
      && (id != TAG_CODE || (self.depth_map[TAG_PRE as usize] == 0 && !self.in_raw_html_block()))
      && let Some(inline_marker_type) = Self::inline_marker_type(id)
      && let Some(emitted) = output.as_deref()
      && !emitted.is_empty()
    {
      self.open_markers.push((
        inline_marker_type,
        self.buffer.len() - emitted.len(),
        self.buffer.len(),
      ));
    } else if !self.open_markers.is_empty()
      && output
        .as_deref()
        .is_some_and(|o| o.as_bytes().iter().any(|&b| !is_whitespace(b)))
    {
      self.open_markers.clear();
    }

    // A block boundary makes an enclosing inline marker permanent even when
    // the block has not emitted content yet. Release streamed output promptly.
    if tag_id.is_some() && !is_inline && !self.open_markers.is_empty() {
      self.open_markers.clear();
    }

    // Clean: track heading start for slug collection
    if self.clean_flags & CLEAN_FRAGMENTS != 0
      && let Some(id) = tag_id
      && id.wrapping_sub(TAG_H1) < 6
      && self.depth_map[TAG_A as usize] == 0
    {
      self.in_heading = true;
      self.heading_buffer_start = self.buffer.len();
    }
  }

  /// Emit markdown for exiting an element (node already popped from stack).
  #[inline]
  pub(crate) fn emit_exit_element(&mut self, node: &ElementNode) {
    if node.excluded_from_markdown {
      self.last_node_is_inline = node.is_inline;
      return;
    }

    let tag_id = node.tag_id;
    if tag_id == Some(TAG_LI) {
      self.list_rule_pending = false;
    }
    let closes_own_pre_fence = tag_id == Some(TAG_PRE) && self.pre_fence_open;

    // Check override
    let override_config = if self.has_tag_overrides {
      self
        .options
        .plugins
        .as_ref()
        .and_then(|p| p.tag_overrides.as_ref())
        .and_then(|ovs| ovs.iter().find(|(k, _)| k == node.name()).map(|(_, v)| v))
    } else {
      None
    };

    let is_inline = override_config
      .and_then(|ov| ov.is_inline)
      .unwrap_or(node.is_inline);

    let cell_span =
      if matches!(tag_id, Some(TAG_TH | TAG_TD)) && self.depth_map[TAG_TABLE as usize] <= 1 {
        let span = Self::cell_span(node);
        self.table_current_row_cells += span as usize;
        span
      } else {
        1
      };

    let mut output: Option<Cow<'static, str>> = None;
    let mut table_separator: Option<String> = None;

    // Check override exit string
    let has_override = if let Some(ov) = override_config {
      if let Some(ref s) = ov.exit {
        output = Some(Cow::Owned(s.clone()));
        true
      } else {
        false
      }
    } else {
      false
    };

    if !has_override {
      // Special case: TR table separator
      if tag_id == Some(TAG_TR) && !self.plain_text {
        if !self.table_rendered_table && self.depth_map[TAG_TABLE as usize] <= 1 {
          self.table_rendered_table = true;
          let col_count = self
            .table_current_row_cells
            .max(self.table_column_alignments.len());
          let indent = if self.depth_map[TAG_LI as usize] > 0 {
            self.list_indent.as_str()
          } else {
            ""
          };
          let mut sep = String::with_capacity(col_count * 7 + 5 + indent.len());
          sep.push_str(" |\n");
          sep.push_str(indent);
          sep.push('|');
          for i in 0..col_count {
            let align = self.table_column_alignments.get(i).copied().unwrap_or(0);
            sep.push(' ');
            sep.push_str(match align {
              1 => ":---",
              2 => ":---:",
              3 => "---:",
              _ => "---",
            });
            sep.push_str(" |");
          }
          self.table_header_cells = col_count;
          table_separator = Some(sep);
        } else {
          output = self.get_exit_output(node, cell_span);
        }
      } else if self.plain_text || tag_id != Some(TAG_A) {
        output = self.get_exit_output(node, cell_span);
      }
    }
    let closing_code_span = if !has_override
      && tag_id == Some(TAG_CODE)
      && self.depth_map[TAG_PRE as usize] == 0
      && !self.in_raw_html_block()
    {
      self.code_spans.pop()
    } else {
      None
    };

    let node_spacing = if let Some(ov) = override_config {
      ov.spacing.or(node.spacing)
    } else {
      node.spacing
    };

    if !has_override
      && !self.plain_text
      && tag_id == Some(TAG_HR)
      && !self.in_table_cell()
      && self.depth_map[TAG_LI as usize] > 0
    {
      self.list_rule_pending = true;
    }

    if !self.plain_text && tag_id == Some(TAG_BLOCKQUOTE) && !self.blockquotes.is_empty() {
      self.finalize_blockquote();
    }

    let new_line_config = self.calculate_new_line_config(tag_id, node_spacing);
    let configured_new_lines = if tag_id == Some(TAG_HR) && !self.blockquotes.is_empty() {
      new_line_config[1].min(1)
    } else {
      new_line_config[1]
    };

    // Clean mode exit — single guard. Skipped for overridden anchors,
    // whose custom exit output isn't the default `[…](…)` shape.
    if !self.plain_text && self.clean_flags != 0 && tag_id == Some(TAG_A) && !has_override {
      // emptyLinks: skip exit for skipped links
      if self.skip_current_link {
        self.skip_current_link = false;
        self.last_node_is_inline = is_inline;
        return;
      }

      // Find actual [ position: scan from recorded pos (write_output may have inserted newlines before it)
      let buf_len = self.buffer.len();
      let bracket_pos = {
        let mut pos = self.link_bracket_pos;
        let buf = self.buffer.as_bytes();
        while pos < buf.len() && buf[pos] != b'[' {
          pos += 1;
        }
        pos
      };
      // Guard: if bracket not found, bracket_pos == buf_len; text_start would overflow
      if bracket_pos >= buf_len {
        self.last_node_is_inline = is_inline;
        return;
      }
      let text_start = bracket_pos + 1;
      let link_text = if text_start <= buf_len && self.buffer.is_char_boundary(text_start) {
        &self.buffer[text_start..buf_len]
      } else {
        ""
      };
      let text_len = buf_len.saturating_sub(text_start);

      // emptyLinkText: [](url) → drop entirely
      if self.clean_flags & CLEAN_EMPTY_LINK_TEXT != 0 && link_text.trim().is_empty() {
        self.buffer.truncate(bracket_pos);
        self.last_node_is_inline = is_inline;
        return;
      }

      // selfLinkHeadings: ## [Title](#slug) → ## Title
      if self.clean_flags & CLEAN_SELF_LINK_HEADINGS != 0 {
        let in_heading = (TAG_H1..=TAG_H6).any(|h| self.depth_map[h as usize] > 0);
        if in_heading
          && let Some(href) = node.attributes.get("href")
          && href.starts_with('#')
          && text_len > 0
        {
          // Remove [ and keep text only — use truncate+copy without intermediate String
          let new_len = bracket_pos + text_len;
          // SAFETY: bracket_pos < text_start are within buffer bounds (guarded above).
          // We copy link text backwards over "[", then truncate. Preserves valid UTF-8.
          #[allow(unsafe_code)]
          unsafe {
            let buf = self.buffer.as_mut_vec();
            std::ptr::copy(
              buf.as_ptr().add(text_start),
              buf.as_mut_ptr().add(bracket_pos),
              text_len,
            );
            buf.set_len(new_len);
          }
          self.last_content_cache_len = text_len;
          self.last_node_is_inline = is_inline;
          return;
        }
      }

      // redundantLinks: [url](url) → url
      if self.clean_flags & CLEAN_REDUNDANT_LINKS != 0
        && let Some(href) = node.attributes.get("href")
        && let resolved = resolve_url(
          href,
          self.options.origin.as_deref(),
          self.options.clean_urls,
        )
        && link_text == resolved.as_ref()
        && text_len > 0
      {
        // Remove [ and keep text only — use truncate+copy without intermediate String
        let new_len = bracket_pos + text_len;
        // SAFETY: same invariants as self-link heading case. Preserves valid UTF-8.
        #[allow(unsafe_code)]
        unsafe {
          let buf = self.buffer.as_mut_vec();
          std::ptr::copy(
            buf.as_ptr().add(text_start),
            buf.as_mut_ptr().add(bracket_pos),
            text_len,
          );
          buf.set_len(new_len);
        }
        self.last_content_cache_len = text_len;
        self.last_node_is_inline = is_inline;
        return;
      }
    }

    if let Some(id) = tag_id
      && id.wrapping_sub(TAG_H1) < 6
      && self.depth_map[TAG_A as usize] == 0
    {
      if self.in_heading {
        let slug = slugify_heading(&self.buffer[self.heading_buffer_start..]);
        if !slug.is_empty() {
          self.heading_slugs.push(slug);
        }
        self.in_heading = false;
      }
      // Raw `<hN>` in a table cell and plain text both write no ATX prefix, so
      // there is no closing sequence to protect.
      if !self.plain_text && !self.in_table_cell() {
        self.escape_trailing_heading_hashes();
      }
    }

    // TAG_A exit: write ](url) directly to buffer — zero allocation
    if !self.plain_text
      && !has_override
      && tag_id == Some(TAG_A)
      && table_separator.is_none()
      && self.depth_map[TAG_PRE as usize] == 0
    {
      // Handle whitespace trimming (write_output with None)
      self.write_output(false, is_inline, configured_new_lines, None, false);
      // Write link close directly
      if let Some(href) = node.attributes.get("href") {
        let resolved = resolve_url(
          href,
          self.options.origin.as_deref(),
          self.options.clean_urls,
        );
        let resolved = resolved.as_ref();
        let mut title = node.attributes.get("title").map_or("", String::as_str);
        if !title.is_empty() && self.last_content_cache_len > 0 {
          let buf_len = self.buffer.len();
          let start = buf_len.saturating_sub(self.last_content_cache_len);
          if self.buffer.is_char_boundary(start) {
            let cache = &self.buffer[start..];
            if cache == title {
              title = "";
            }
          }
        }
        // GFM autolink shorthand: when href equals text content and is a
        // bare absolute URI (http(s)://, ftp://, mailto:), emit `<href>`
        // instead of the verbose `[href](href)`. link_bracket_pos points
        // directly at the `[` byte (set in emit_enter_element), so this
        // is an O(1) check. `[` is single-byte UTF-8, so `bp + 1` is
        // always a char boundary once `buf_bytes[bp]` is confirmed `[`.
        if title.is_empty() && is_autolink_uri(resolved) {
          let bp = self.link_bracket_pos;
          let buf_bytes = self.buffer.as_bytes();
          if bp < buf_bytes.len() && buf_bytes[bp] == b'[' && &self.buffer[bp + 1..] == resolved {
            self.buffer.truncate(bp);
            self.buffer.push('<');
            self.buffer.push_str(resolved);
            self.buffer.push('>');
            self.last_content_cache_len = self.buffer.len() - bp;
            self.last_node_is_inline = is_inline;
            return;
          }
        }
        self.buffer.push(']');
        write_markdown_resource(
          &mut self.buffer,
          resolved,
          (!title.is_empty()).then_some(title),
        );
        // The cache is a length, not an offset: the link starts at its `[`.
        // Saturating because `link_bracket_pos` is `buffer.len()` when no `[`
        // was emitted.
        self.last_content_cache_len = self.buffer.len().saturating_sub(self.link_bracket_pos);
      }
      // Record fragment link position for deferred fixup
      if self.clean_flags & CLEAN_FRAGMENTS != 0
        && let Some(href) = node.attributes.get("href")
        && href.starts_with('#')
        && href.len() > 1
      {
        // link_bracket_pos now points exactly at `[` (set in emit_enter_element).
        self
          .fragment_links
          .push((self.link_bracket_pos, self.buffer.len()));
      }
      self.last_node_is_inline = is_inline;
      return;
    }

    // Empty pair: only the enter marker was written, so drop it instead of emitting a close.
    if !has_override
      && let Some(id) = tag_id
      && (id != TAG_CODE || (self.depth_map[TAG_PRE as usize] == 0 && !self.in_raw_html_block()))
      && let Some(inline_marker_type) = Self::inline_marker_type(id)
      && output.as_deref().is_some_and(|emitted| !emitted.is_empty())
      && let Some((open_type, output_start, content_start)) = self.open_markers.pop()
    {
      if open_type == inline_marker_type
        && content_start <= self.buffer.len()
        && self.buffer.as_bytes()[content_start..]
          .iter()
          .all(|&b| is_whitespace(b))
      {
        // `output_start` includes a separator owned by the opener (inline
        // code in a list can emit " `"), but excludes normal surrounding
        // spacing synthesized by write_output.
        self.buffer.truncate(output_start);
        self.last_content_cache_len = 0;
        self.last_node_is_inline = is_inline;
        return;
      }

      // A mismatched or externally modified opener cannot be dropped. Its
      // output makes every enclosing marker non-empty, so release them all.
      self.open_markers.clear();
    }

    if !self.open_markers.is_empty()
      && (has_override
        || (tag_id.is_some() && !is_inline)
        || output
          .as_deref()
          .is_some_and(|o| o.as_bytes().iter().any(|&b| !is_whitespace(b))))
    {
      self.open_markers.clear();
    }

    if let Some(span) = closing_code_span {
      output = Some(Cow::Owned(self.finalize_code_span(span)));
    }
    if !has_override
      && closes_own_pre_fence
      && let Some(delimiter) = self.finalize_code_fence()
      && let Some(exit) = output.as_deref()
    {
      output = Some(Cow::Owned(exit.replacen(
        MARKDOWN_CODE_BLOCK,
        &delimiter,
        1,
      )));
    }

    // Get effective output
    let effective: Option<&str> = if let Some(ref sep) = table_separator {
      Some(sep.as_str())
    } else {
      output.as_deref()
    };

    if tag_id == Some(TAG_LI) && !self.plain_text {
      self.resolve_item_marker(true);
    }

    self.write_output(false, is_inline, configured_new_lines, effective, false);

    // Reset <pre> fence deferral once the element closes (issue #97).
    if tag_id == Some(TAG_PRE) {
      // The closing fence consumed the trailing newline; clear the whitespace
      // flags too, or the next node trims the blank line through the fence.
      if self.pre_fence_open {
        self.last_text_node_contains_whitespace = false;
        self.has_last_text_node = false;
      }
      self.pre_fence_pending = false;
      self.pre_fence_open = false;
    }

    // Record fragment link position for deferred fixup (no String alloc)
    if !self.plain_text
      && self.clean_flags & CLEAN_FRAGMENTS != 0
      && tag_id == Some(TAG_A)
      && let Some(href) = node.attributes.get("href")
      && href.starts_with('#')
      && href.len() > 1
    {
      self
        .fragment_links
        .push((self.link_bracket_pos, self.buffer.len()));
    }
  }

  /// Emit a bare <pre>'s opening code fence (issue #97). Mirrors the
  /// <code>-in-<pre> enter formatting: indented and newline-padded inside a
  /// list item, otherwise a plain ```lang opener. Marks the <pre> as owning
  /// the fence so a nested <code> does not double up and the <pre> exit emits
  /// the matching closing fence.
  fn flush_pre_fence(&mut self) {
    if self.plain_text {
      self.pre_fence_pending = false;
      return;
    }

    self.pre_fence_pending = false;
    self.pre_fence_open = true;
    let li_depth = self.depth_map[TAG_LI as usize];
    let fence = if li_depth > 0 {
      // A blank line between the marker and the fence ends the item, leaving the
      // block a sibling of the list, so a pending marker takes no separator.
      let indent = self.list_indent.as_str();
      let open = self.block_open_prefix(indent).unwrap_or(Cow::Borrowed(""));
      format!("{open}```{1}\n{0}", indent, self.pre_fence_lang)
    } else {
      format!("```{}\n", self.pre_fence_lang)
    };
    let output_start = self.buffer.len();
    self.last_content_cache_len = fence.len();
    self.buffer.push_str(&fence);
    self.start_code_fence(
      output_start,
      self.buffer.len(),
      self.pre_fence_lang.clone(),
      self.list_indent.clone(),
    );
    self.last_node_is_inline = false;
  }

  /// Emit markdown for a text node (no TextNode allocation).
  #[inline]
  pub(crate) fn emit_text(
    &mut self,
    text: &str,
    contains_whitespace: bool,
    depth: usize,
    index: usize,
  ) {
    let has_inline_gfm_hazard = text
      .bytes()
      .any(|byte| GFM_BYTE_FLAGS[byte as usize] & (GFM_HAZARD_BIT | GFM_NEWLINE_BIT) != 0);
    self.text_buffer_has_inline_gfm_hazard |= has_inline_gfm_hazard;
    self.emit_text_with_generated_markdown(text, contains_whitespace, depth, index, None, None);
  }

  pub(crate) fn emit_text_with_generated_markdown(
    &mut self,
    text: &str,
    contains_whitespace: bool,
    depth: usize,
    index: usize,
    generated_prefix: Option<&str>,
    generated_suffix: Option<&str>,
  ) {
    let has_inline_gfm_hazard = std::mem::take(&mut self.text_buffer_has_inline_gfm_hazard);
    if text.is_empty() {
      return;
    }

    if self.list_rule_pending {
      if text.as_bytes().iter().all(|&byte| is_whitespace(byte)) {
        return;
      }
      self.flush_list_rule();
    }

    if self.pending_inline_whitespace {
      if text.as_bytes().iter().all(|&b| is_whitespace(b)) {
        return;
      }
      let last = self.last_output_byte();
      let first = text.as_bytes()[0];
      if !matches!(last, Some(b' ' | b'\n' | b'\t') | None) && !is_whitespace(first) {
        self.buffer.push(' ');
      }
      self.pending_inline_whitespace = false;
    }

    // Open a deferred <pre> fence before its first non-whitespace text.
    if self.pre_fence_pending
      && text
        .as_bytes()
        .iter()
        .any(|&b| b != b' ' && b != b'\t' && b != b'\n' && b != b'\r')
    {
      self.flush_pre_fence();
    }
    // Still pending means this <pre> has only seen whitespace so far; drop it
    // so an empty/whitespace-only <pre> emits nothing and never leaks between
    // surrounding blocks (issue #97).
    if self.pre_fence_pending {
      return;
    }

    if self.plain_text && self.depth_map[TAG_PRE as usize] > 0 && self.buffer.is_empty() {
      self.preserve_leading_whitespace = true;
    }

    let buf_bytes = self.buffer.as_bytes();
    let buf_len = buf_bytes.len();
    let last_char = if buf_len > 0 {
      buf_bytes[buf_len - 1]
    } else if self.has_streamed_output {
      // The buffer was drained (and possibly trimmed) empty, but earlier output
      // ended with this byte. Spacing must be decided against it, not `0`, so a
      // word separator that one-shot keeps is not dropped across the boundary.
      self.flushed_tail[1]
    } else {
      0
    };

    if text.len() == 1
      && text.as_bytes()[0] == b' '
      && matches!(last_char, b' ' | b'\n' | b'\t' | b'\r')
    {
      self.last_text_node_contains_whitespace = contains_whitespace;
      self.has_last_text_node = true;
      self.last_text_node_depth = depth;
      self.last_text_node_index = index;
      self.last_node_is_inline = false;
      return;
    }

    // Indent code block content inside a list item so every line starts at
    // the list item's content column. CommonMark closes the list item when
    // a line is indented less than that column, so we prepend list_indent
    // on top of any existing in-source indentation. Blank lines are left
    // alone so they stay blank.
    let li_depth = self.depth_map[TAG_LI as usize] as usize;
    let indented_storage;
    let text = if !self.plain_text
      && self.depth_map[TAG_PRE as usize] > 0
      && li_depth > 0
      && (text.contains('\n') || last_char == b'\n')
    {
      let indent = self.list_indent.as_str();
      let mut out = String::with_capacity(text.len() + indent.len() * 2);
      let bytes = text.as_bytes();
      // Prepend indent for the first line when the buffer ended with a
      // newline (code fence opener). Blank first line stays blank.
      if last_char == b'\n' {
        let first = bytes.first().copied().unwrap_or(0);
        if first != b'\n' && first != 0 {
          out.push_str(indent);
        }
      }
      let mut prev = 0usize;
      for (i, &b) in bytes.iter().enumerate() {
        if b == b'\n' {
          out.push_str(&text[prev..=i]);
          let next = i + 1;
          if next < bytes.len() && bytes[next] != b'\n' {
            out.push_str(indent);
          }
          prev = next;
        }
      }
      out.push_str(&text[prev..]);
      indented_storage = out;
      indented_storage.as_str()
    } else {
      text
    };

    // Inside a table cell the <pre>/<code> is emitted as raw HTML, so every
    // text node must be escaped (so decoded `<`/`&` are not live HTML) and its
    // line breaks folded into <br> (issue #147). Runs on all such text, not
    // only text with newlines, since escaping is always required.
    let cell_storage;
    let text = if !self.plain_text && self.depth_map[TAG_PRE as usize] > 0 && self.in_table_cell() {
      cell_storage = Self::fold_pre_lines_to_br(text);
      cell_storage.as_str()
    } else {
      text
    };

    let inside_raw_html_block = self.in_raw_html_block();
    if inside_raw_html_block {
      self.track_raw_html_markdown_context();
    } else {
      self.raw_html_markdown = false;
      self.raw_html_scanned_to = self.buffer.len();
    }
    let raw_html_storage;
    let text = if !self.plain_text && self.depth_map[TAG_PRE as usize] == 0 && inside_raw_html_block
    {
      raw_html_storage = self.escape_raw_html_text(text);
      raw_html_storage.as_ref()
    } else {
      text
    };

    // Loop-invariant container tests, behind a closure so the byte scan runs
    // only when no cheaper test already decided.
    let in_table = self.depth_map[TAG_TABLE as usize] > 0;
    let in_link = self.depth_map[TAG_A as usize] > 0;
    let in_quote = self.depth_map[TAG_BLOCKQUOTE as usize] > 0;
    let context_has_gfm_hazard = |text: &str| {
      !inside_raw_html_block
        && (in_table || in_link || in_quote)
        && text.bytes().any(|byte| match byte {
          b'|' => in_table,
          b']' => in_link,
          b'>' => in_quote,
          _ => false,
        })
    };
    let escaped_storage;
    let text = if !self.plain_text
      && self.depth_map[TAG_PRE as usize] == 0
      && self.depth_map[TAG_CODE as usize] == 0
      && (has_inline_gfm_hazard
        || context_has_gfm_hazard(text)
        || self.starts_with_gfm_block_candidate(text))
      // Past a blank line a raw-HTML region is Markdown again, so its text needs
      // escaping too — unless this line reopens a raw block.
      && (!inside_raw_html_block
        || (self.raw_html_markdown && !self.line_opens_raw_html_block()))
    {
      #[cfg(test)]
      {
        self.gfm_escape_slow_path_calls += 1;
      }
      escaped_storage = self.escape_gfm_text(text);
      escaped_storage.as_ref()
    } else {
      text
    };

    let generated_storage;
    let text = if generated_prefix.is_some() || generated_suffix.is_some() {
      generated_storage = format!(
        "{}{}{}",
        generated_prefix.unwrap_or_default(),
        text,
        generated_suffix.unwrap_or_default()
      );
      generated_storage.as_str()
    } else {
      text
    };

    if self.wrap_width != 0 && self.can_wrap_here() {
      self.push_text_wrapped(text, last_char);
    } else if !(self.plain_text && self.depth_map[TAG_PRE as usize] > 0)
      && self.should_add_spacing_before_text(last_char, text)
    {
      self.buffer.push(' ');
      self.last_content_cache_len = text.len() + 1;
      self.buffer.push_str(text);
    } else {
      self.last_content_cache_len = text.len();
      self.buffer.push_str(text);
    }

    if !self.open_markers.is_empty() && text.as_bytes().iter().any(|&b| !is_whitespace(b)) {
      self.open_markers.clear();
    }

    self.last_text_node_contains_whitespace = contains_whitespace;
    self.has_last_text_node = true;
    self.last_text_node_depth = depth;
    self.last_text_node_index = index;
    self.last_node_is_inline = false;
  }

  /// Escape GFM syntax originating in an HTML text node. Generated tag
  /// markers are written elsewhere and never pass through this path.
  #[inline]
  fn escape_gfm_text<'a>(&self, text: &'a str) -> Cow<'a, str> {
    let in_table = self.depth_map[TAG_TABLE as usize] > 0;
    let in_link = self.depth_map[TAG_A as usize] > 0;
    let in_blockquote = self.depth_map[TAG_BLOCKQUOTE as usize] > 0;
    let mut line_indent = self.markdown_line_indent();
    let mut ordered_digits = 0u8;
    let bytes = text.as_bytes();
    let mut output: Option<String> = None;
    let mut copied_until = 0usize;
    let mut index = 0usize;

    while index < bytes.len() {
      let byte = bytes[index];

      if GFM_BYTE_FLAGS[byte as usize] & GFM_TEXT_ACTIVE_BIT == 0 {
        while index < bytes.len()
          && GFM_BYTE_FLAGS[bytes[index] as usize] & GFM_TEXT_ACTIVE_BIT == 0
        {
          index += 1;
        }
        line_indent = None;
        ordered_digits = 0;
        continue;
      }

      // A `\&` guarding a decoded entity reference is emitted by the entity
      // decoder; preserve the pair verbatim so this pass never doubles the slash.
      if byte == b'\\'
        && bytes.get(index + 1).is_some_and(|&next| {
          next == b'&' && Self::is_entity_reference_after_ampersand(&bytes[index + 1..])
        })
      {
        line_indent = None;
        ordered_digits = 0;
        index += 2;
        continue;
      }

      if in_table && matches!(byte, b'\n' | b'\r') {
        let out = output.get_or_insert_with(|| String::with_capacity(text.len() + 8));
        out.push_str(&text[copied_until..index]);
        out.push_str(if byte == b'\n' { "&#10;" } else { "&#13;" });
        copied_until = index + 1;
        index += 1;
        continue;
      }

      let mut should_escape = matches!(byte, b'\\' | b'*' | b'_' | b'~' | b'`' | b'[')
        || (byte == b']' && in_link)
        || (byte == b'|' && in_table)
        || (byte == b'>' && in_blockquote)
        || (byte == b'<'
          && bytes
            .get(index + 1)
            .is_some_and(|next| next.is_ascii_alphabetic() || matches!(next, b'!' | b'/' | b'?')));

      if !should_escape && line_indent.is_some() {
        if byte == b'#' {
          let mut end = index + 1;
          while end < bytes.len() && bytes[end] == b'#' {
            end += 1;
          }
          should_escape =
            end - index <= 6 && Self::is_markdown_marker_whitespace(bytes.get(end).copied());
        } else if byte == b'-' || byte == b'+' {
          should_escape = Self::is_markdown_marker_whitespace(bytes.get(index + 1).copied())
            || (byte == b'-' && Self::is_thematic_break(&bytes[index..], byte));
        } else if byte == b'>' {
          should_escape = true;
        }
      } else if !should_escape && ordered_digits > 0 && (byte == b'.' || byte == b')') {
        should_escape = Self::is_markdown_marker_whitespace(bytes.get(index + 1).copied());
      }

      if should_escape {
        let out = output.get_or_insert_with(|| String::with_capacity(text.len() + 8));
        out.push_str(&text[copied_until..index]);
        out.push('\\');
        out.push(byte as char);
        copied_until = index + 1;
      }

      if byte == b'\n' {
        line_indent = Some(0);
        ordered_digits = 0;
      } else if let Some(indent) = line_indent {
        if byte == b' ' && indent < 3 {
          line_indent = Some(indent + 1);
        } else {
          ordered_digits = u8::from(byte.is_ascii_digit());
          line_indent = None;
        }
      } else if ordered_digits > 0 {
        ordered_digits = if byte.is_ascii_digit() && ordered_digits < 9 {
          ordered_digits + 1
        } else {
          0
        };
      }
      index += 1;
    }

    if let Some(mut out) = output {
      out.push_str(&text[copied_until..]);
      Cow::Owned(out)
    } else {
      Cow::Borrowed(text)
    }
  }

  #[inline]
  fn starts_with_gfm_block_candidate(&self, text: &str) -> bool {
    // Text-side reject first: prose that cannot open a block skips the buffer
    // scan entirely, which is most text nodes.
    match text.as_bytes().first() {
      Some(b' ' | b'#' | b'-' | b'+' | b'>' | b'0'..=b'9') => {}
      _ => return false,
    }
    let Some(mut indent) = self.markdown_line_indent() else {
      return false;
    };
    for byte in text.bytes() {
      if byte == b' ' && indent < 3 {
        indent += 1;
        continue;
      }
      return matches!(byte, b'#' | b'-' | b'+' | b'>' | b'0'..=b'9');
    }
    false
  }

  /// GFM reads a heading's trailing `#` run as an ATX closing sequence and drops
  /// it along with the text it was meant to be, so the run is escaped. Only a run
  /// preceded by whitespace (or forming the whole heading) closes a heading.
  fn escape_trailing_heading_hashes(&mut self) {
    let bytes = self.buffer.as_bytes();
    // Cheap reject: almost no heading ends in `#`.
    let mut end = bytes.len();
    while end > 0 && matches!(bytes[end - 1], b' ' | b'\t') {
      end -= 1;
    }
    if end == 0 || bytes[end - 1] != b'#' {
      return;
    }
    // Content begins past the opening `#` prefix, which this pass must not touch.
    let line = bytes[..end]
      .iter()
      .rposition(|&byte| byte == b'\n')
      .map_or(0, |i| i + 1);
    let mut start = line;
    while start < bytes.len() && bytes[start] == b'#' {
      start += 1;
    }
    start += usize::from(bytes.get(start) == Some(&b' '));
    if end < start {
      return;
    }
    let mut run = end;
    while run > start && bytes[run - 1] == b'#' {
      run -= 1;
    }
    if run < end && (run == start || matches!(bytes[run - 1], b' ' | b'\t')) {
      self.buffer.insert(run, '\\');
    }
  }

  /// Whether `end` sits just past a list marker that is all its line holds:
  /// optional indent, `-`/`*`/`+` or `N.`/`N)`, and nothing else.
  fn list_marker_line_start(&self, bytes: &[u8], end: usize) -> bool {
    let mut index = end - 1;
    match bytes[index] {
      b'.' | b')' => {
        let digits = index;
        while index > 0 && bytes[index - 1].is_ascii_digit() {
          index -= 1;
        }
        if index == digits || digits - index > 9 {
          return false;
        }
      }
      b'-' | b'*' | b'+' => {}
      _ => return false,
    }
    let mut spaces = 0;
    while index > 0 && bytes[index - 1] == b' ' && spaces < 3 {
      index -= 1;
      spaces += 1;
    }
    if index == 0 {
      return !self.has_streamed_output || self.flushed_tail[1] == b'\n';
    }
    bytes[index - 1] == b'\n'
  }

  /// Arm the empty-item guard for the `<li>` whose marker was just written from
  /// `output_start`. A marker line continuing the paragraph above turns a lone
  /// marker into a setext underline: the text becomes a heading and the item
  /// disappears. A blank line, or a sibling marker, opens its own block instead.
  fn record_item_marker(&mut self, index: usize, output_start: usize) {
    // Only a list's first item can follow a paragraph, so every later sibling
    // skips the line scan below.
    if index != 0 {
      self.empty_item_hazard = false;
      return;
    }
    let output_start = output_start.min(self.buffer.len());
    let bytes = self.buffer.as_bytes();
    let line_start = bytes[output_start..]
      .iter()
      .rposition(|&byte| byte == b'\n')
      .map_or(output_start, |i| output_start + i + 1);
    // Draining removes the front of the buffer, so read through it the way
    // `write_output` does: `flushed_tail` holds the two bytes before `buffer[0]`.
    let before = |offset: usize| -> u8 {
      match line_start.checked_sub(offset) {
        Some(at) => bytes[at],
        None if self.has_streamed_output => self.flushed_tail[2 - (offset - line_start)],
        None => 0,
      }
    };
    let starts_document = line_start == 0 && !self.has_streamed_output;
    let blank_above = before(1) != b'\n' || before(2) == b'\n';
    self.empty_item_hazard = !starts_document && !blank_above;
    self.empty_item_line_start = line_start;
    self.empty_item_len = self.buffer.len();
  }

  /// Give a marker that ended up alone on its line the blank line it needs: the
  /// item is empty, or opened with a block starting on the next line. While
  /// nothing has been written since the marker the answer is still open, so only
  /// the `<li>` exit forces one.
  pub(crate) fn resolve_item_marker(&mut self, at_exit: bool) {
    if !self.empty_item_hazard {
      return;
    }
    // A rewrite behind the recorded length followed by multi-byte content can
    // leave the offset mid-codepoint, which panics on slicing.
    let mut end = self.empty_item_len.min(self.buffer.len());
    while end > 0 && !self.buffer.is_char_boundary(end) {
      end -= 1;
    }
    let tail = self.buffer[end..].trim_start_matches(' ');
    if tail.is_empty() && !at_exit {
      return;
    }
    self.empty_item_hazard = false;
    if !tail.is_empty() && !tail.starts_with('\n') {
      return;
    }
    self.buffer.insert(self.empty_item_line_start, '\n');
    // The insertion moves every cached buffer offset at or past the line.
    self.line_start_scanned_to = usize::MAX;
    self.raw_html_scanned_to = self.raw_html_scanned_to.min(self.empty_item_line_start);
  }

  #[inline]
  fn is_markdown_marker_whitespace(byte: Option<u8>) -> bool {
    matches!(byte, None | Some(b' ' | b'\t' | b'\n' | b'\r'))
  }

  #[inline]
  fn is_thematic_break(value: &[u8], marker: u8) -> bool {
    let mut count = 0u8;
    for &byte in value {
      if byte == marker {
        count = count.saturating_add(1);
      } else if byte == b'\n' || byte == b'\r' {
        break;
      } else if byte != b' ' && byte != b'\t' {
        return false;
      }
    }
    count >= 3
  }

  #[inline]
  fn is_entity_reference_after_ampersand(value: &[u8]) -> bool {
    let mut index = 1usize;
    if value.get(index) == Some(&b'#') {
      index += 1;
      let hex = matches!(value.get(index), Some(b'x' | b'X'));
      if hex {
        index += 1;
      }
      let start = index;
      while let Some(&byte) = value.get(index) {
        if byte.is_ascii_digit() || (hex && byte.is_ascii_hexdigit()) {
          index += 1;
        } else {
          break;
        }
      }
      return index > start && value.get(index) == Some(&b';');
    }
    let start = index;
    while value.get(index).is_some_and(u8::is_ascii_alphanumeric) {
      index += 1;
    }
    index > start && value.get(index) == Some(&b';')
  }

  /// Current leading-space count when a GFM block marker may start here.
  #[inline]
  fn markdown_line_indent(&self) -> Option<u8> {
    let bytes = self.buffer.as_bytes();
    let mut spaces = 0u8;
    for (offset, &byte) in bytes.iter().enumerate().rev() {
      if byte == b'\n' {
        return Some(spaces);
      }
      if byte != b' ' || spaces == 3 {
        // A list marker is block prefix too: its content column opens a fresh
        // block context, so `- # h` escapes exactly like `# h` would.
        return self.list_marker_line_start(bytes, offset + 1).then_some(0);
      }
      spaces += 1;
    }
    if self.buffer.is_empty() && self.has_streamed_output {
      if self.flushed_tail[1] == b'\n' {
        Some(0)
      } else {
        None
      }
    } else {
      Some(spaces)
    }
  }

  /// Whether prose at the current position may be hard-wrapped. Code blocks
  /// (`<pre>`/`<code>`), table cells, and headings are emitted verbatim so
  /// wrapping never corrupts fences, table rows, or heading lines.
  #[inline]
  fn can_wrap_here(&self) -> bool {
    self.depth_map[TAG_PRE as usize] == 0
      && self.depth_map[TAG_CODE as usize] == 0
      && !self.in_table_cell()
      && !self.in_heading()
  }

  #[inline]
  pub(crate) fn in_heading(&self) -> bool {
    self.depth_map[TAG_H1 as usize] > 0
      || self.depth_map[TAG_H2 as usize] > 0
      || self.depth_map[TAG_H3 as usize] > 0
      || self.depth_map[TAG_H4 as usize] > 0
      || self.depth_map[TAG_H5 as usize] > 0
      || self.depth_map[TAG_H6 as usize] > 0
  }

  /// Prepare `<pre>` content for raw-HTML emission inside a GFM table cell
  /// (issue #147): fold literal line breaks into `<br>` so the value stays on
  /// one row, encode `|`, and HTML-escape `&`, `<`, `>` so decoded source (e.g.
  /// `<script>`) is not evaluated as live HTML. Leading and trailing breaks are
  /// dropped; a `\r\n` pair counts as one break.
  fn fold_pre_lines_to_br(value: &str) -> String {
    let bytes = value.as_bytes();
    let mut start = 0usize;
    while start < bytes.len() && (bytes[start] == b'\n' || bytes[start] == b'\r') {
      start += 1;
    }
    let mut end = bytes.len();
    while end > start && (bytes[end - 1] == b'\n' || bytes[end - 1] == b'\r') {
      end -= 1;
    }
    // `start`/`end` land on ASCII newline bytes, so slicing here is UTF-8 safe.
    let trimmed = &value[start..end];
    let mut out = String::with_capacity(trimmed.len());
    let mut chars = trimmed.chars().peekable();
    while let Some(c) = chars.next() {
      if c == '\r' {
        out.push_str("<br>");
        if chars.peek() == Some(&'\n') {
          chars.next();
        }
      } else if c == '\n' {
        out.push_str("<br>");
      } else if c == '&' {
        out.push_str("&amp;");
      } else if c == '<' {
        out.push_str("&lt;");
      } else if c == '>' {
        out.push_str("&gt;");
      } else if c == '|' {
        out.push_str("&#124;");
      } else {
        out.push(c);
      }
    }
    out
  }

  fn escape_raw_html_text<'a>(&self, value: &'a str) -> Cow<'a, str> {
    let in_table = self.depth_map[TAG_TABLE as usize] > 0;
    let in_link = self.depth_map[TAG_A as usize] > 0;
    let mut output: Option<String> = None;
    let mut copied_until = 0usize;

    let bytes = value.as_bytes();
    let mut index = 0usize;
    while index < bytes.len() {
      let byte = bytes[index];
      let replacement = match byte {
        b'&' => Some("&amp;"),
        b'<' => Some("&lt;"),
        b'>' => Some("&gt;"),
        b'\n' => Some("&#10;"),
        b'\r' => Some("&#13;"),
        b'|' if in_table => Some("&#124;"),
        b'[' if in_link => Some("&#91;"),
        b']' if in_link => Some("&#93;"),
        _ => None,
      };
      if let Some(replacement) = replacement {
        let out = output.get_or_insert_with(|| String::with_capacity(value.len() + 8));
        out.push_str(&value[copied_until..index]);
        out.push_str(replacement);
        copied_until = index + 1;
      }
      index += 1;
    }

    if let Some(mut output) = output {
      output.push_str(&value[copied_until..]);
      Cow::Owned(output)
    } else {
      Cow::Borrowed(value)
    }
  }

  /// Lowest offset a trailing-whitespace trim may cut back to. Open fences,
  /// code spans, and blockquotes record offsets whose bytes are delimiter, not
  /// spacing; cutting behind one corrupts the block and leaves `content_start`
  /// past the buffer end, which panics on finalize.
  #[inline]
  fn trim_floor(&self) -> usize {
    let mut floor = 0usize;
    if let Some(fence) = &self.code_fence {
      floor = floor.max(fence.content_start);
    }
    if let Some(span) = self.code_spans.last() {
      floor = floor.max(span.content_start);
    }
    if let Some(frame) = self.blockquotes.last() {
      floor = floor.max(frame.content_start);
    }
    floor
  }

  /// Byte the next output will follow, or `None` only at the true start of
  /// output. Draining, or a rewrite that trims what draining retained, can empty
  /// the buffer mid-document; `flushed_tail` then holds the bytes before it.
  #[inline]
  fn last_output_byte(&self) -> Option<u8> {
    match self.buffer.as_bytes().last().copied() {
      Some(byte) => Some(byte),
      None if self.has_streamed_output => Some(self.flushed_tail[1]),
      None => None,
    }
  }

  #[inline]
  fn trim_trailing_spaces(&mut self) {
    let floor = self.trim_floor();
    if self.buffer.len() > floor {
      let trimmed_len = floor + self.buffer[floor..].trim_end_matches(' ').len();
      self.buffer.truncate(trimmed_len);
    }
  }

  #[inline]
  /// Note whether the open raw-HTML region has been broken by a blank line.
  fn track_raw_html_markdown_context(&mut self) {
    if self.raw_html_markdown {
      return;
    }
    let len = self.buffer.len();
    // Byte scanning, so a resume point inside a multi-byte character is fine.
    let from = self.raw_html_scanned_to.min(len).saturating_sub(1);
    if self.buffer.as_bytes()[from..]
      .windows(2)
      .any(|pair| pair == b"\n\n")
    {
      self.raw_html_markdown = true;
    }
    self.raw_html_scanned_to = len;
  }

  /// Whether the current line opens a raw HTML block, which suspends Markdown
  /// again until the next blank line.
  fn line_opens_raw_html_block(&mut self) -> bool {
    let len = self.buffer.len();
    let bytes = self.buffer.as_bytes();
    // Only bytes appended since the last call can move the line start; a buffer
    // that shrank behind the cache (a trim, or a streaming drain) rebuilds it.
    if self.line_start_scanned_to > len || self.line_start > len {
      self.line_start = bytes[..len]
        .iter()
        .rposition(|&byte| byte == b'\n')
        .map_or(0, |i| i + 1);
    } else if let Some(i) = bytes[self.line_start_scanned_to..len]
      .iter()
      .rposition(|&byte| byte == b'\n')
    {
      self.line_start = self.line_start_scanned_to + i + 1;
    }
    self.line_start_scanned_to = len;

    let line = &bytes[self.line_start..len];
    let indent = line
      .iter()
      .take(3)
      .take_while(|&&byte| byte == b' ')
      .count();
    line.get(indent) == Some(&b'<')
  }

  pub(crate) fn in_raw_html_block(&self) -> bool {
    self.depth_map[TAG_DETAILS as usize] > 0
      || self.depth_map[TAG_SUMMARY as usize] > 0
      || self.depth_map[TAG_ADDRESS as usize] > 0
      || self.depth_map[TAG_DL as usize] > 0
      || self.depth_map[TAG_DT as usize] > 0
      || self.depth_map[TAG_DD as usize] > 0
  }

  /// Character count of the current (unterminated) buffer line, i.e. since the
  /// last `\n`. This is the live output column, including any block prefix
  /// (`> `, list indent) already written for the line.
  #[inline]
  fn current_column(&self) -> usize {
    let bytes = self.buffer.as_bytes();
    let mut i = bytes.len();
    while i > 0 && bytes[i - 1] != b'\n' {
      i -= 1;
    }
    let buffered_column = self.buffer[i..].chars().count();
    if i == 0 {
      self.buffer_start_column.saturating_add(buffered_column)
    } else {
      buffered_column
    }
  }

  /// Separation a block needs to open at the content column `prefix` describes,
  /// or `None` when the buffer already sits there.
  fn block_open_prefix(&self, prefix: &str) -> Option<Cow<'static, str>> {
    let bytes = self.buffer.as_bytes();
    match bytes.last() {
      None => None,
      Some(b' ') => {
        // A trailing space can be a pending list marker already at the content
        // column, or ordinary text (`"item "`) that still has to break as a
        // paragraph. Only the former needs no separator.
        let end = bytes.len() - trailing_spaces(bytes);
        if end > 0 && bytes[end - 1] != b'\n' && !self.list_marker_line_start(bytes, end) {
          Some(Cow::Owned(format!("\n\n{prefix}")))
        } else {
          None
        }
      }
      Some(b'\n') => (!prefix.is_empty()).then(|| Cow::Owned(prefix.to_string())),
      // Directly under text the block reads as a setext underline or a lazy
      // continuation, so it has to break the paragraph first.
      _ => Some(Cow::Owned(format!("\n\n{prefix}"))),
    }
  }

  /// Continuation prefix re-emitted at the start of each continued line so the
  /// content stays inside its block context. Built by walking the open
  /// ancestor stack outermost-first so blockquote markers (`> `) and list-item
  /// indentation interleave in the real nesting order: `<li><blockquote>` →
  /// `  > `, `<blockquote><li>` → `>   `. A flat "all quotes then all indent"
  /// prefix would corrupt the Markdown structure of nested blocks.
  fn continuation_prefix(&self) -> String {
    if self.plain_text {
      return String::new();
    }

    let mut p = String::new();
    let mut li_idx = 0usize;
    for node in &self.stack {
      match node.tag_id {
        Some(TAG_BLOCKQUOTE) if self.blockquotes.is_empty() => p.push_str("> "),
        Some(TAG_LI) => {
          // Each open <li> contributes its marker-width of spaces, in
          // the same order they were pushed onto list_indent_widths.
          let w = self.list_indent_widths.get(li_idx).copied().unwrap_or(2) as usize;
          for _ in 0..w {
            p.push(' ');
          }
          li_idx += 1;
        }
        _ => {}
      }
    }
    p
  }

  fn flush_list_rule(&mut self) {
    let prefix = self.continuation_prefix();
    self.buffer.reserve(2 + prefix.len());
    self.buffer.push_str("\n\n");
    self.buffer.push_str(&prefix);
    self.last_content_cache_len = 2 + prefix.len();
    self.list_rule_pending = false;
  }

  /// Push `text` into the buffer, hard-wrapping on spaces so no output line
  /// exceeds `self.wrap_width` characters. Words are never split, so a single
  /// token longer than the width (e.g. a URL) overflows rather than breaking.
  /// A break only ever replaces an inter-word space, so words joined across
  /// inline boundaries (e.g. `foo**bar**`) stay intact.
  fn push_text_wrapped(&mut self, text: &str, last_char: u8) {
    let width = self.wrap_width;
    // A leading/trailing space in `text` is significant inter-word separation
    // across an inline boundary (e.g. `… </a> now`); the non-wrap path keeps
    // it by pushing `text` verbatim, so preserve it here too. `split(' ')`
    // would otherwise discard it as an empty segment.
    let leading_space = text.starts_with(' ');
    let trailing_space = text.ends_with(' ');
    let first_needs_space = leading_space || self.should_add_spacing_before_text(last_char, text);
    let prefix = self.continuation_prefix();
    let prefix_len = prefix.chars().count();
    let buf_start = self.buffer.len();
    let mut col = self.current_column();
    let mut first = true;

    for word in text.split(' ') {
      if word.is_empty() {
        continue;
      }
      let word_len = word.chars().count();
      let need_space = if first { first_needs_space } else { true };

      if need_space && col > prefix_len && col + 1 + word_len > width {
        self.buffer.push('\n');
        self.buffer.push_str(&prefix);
        col = prefix_len;
      } else if need_space {
        self.buffer.push(' ');
        col += 1;
      }
      self.buffer.push_str(word);
      col += word_len;
      first = false;
    }

    // Preserve a trailing separator space (unless we emitted nothing or the
    // line already ends in whitespace) so the next inline run stays separated.
    if trailing_space && !matches!(self.buffer.as_bytes().last(), Some(b' ' | b'\n') | None) {
      self.buffer.push(' ');
    }
    self.last_content_cache_len = self.buffer.len() - buf_start;
  }

  /// Emit frontmatter content.
  pub(crate) fn emit_frontmatter(&mut self, content: &str) {
    if !content.is_empty() {
      self.last_content_cache_len = content.len();
      self.buffer.push_str(content);
    }
  }

  #[inline]
  pub(crate) fn get_enter_output(
    &self,
    node: &ElementNode,
    _ancestors: &[ElementNode],
  ) -> Option<Cow<'static, str>> {
    if self.plain_text {
      return self.get_text_enter_output(node);
    }

    let tag_id = node.tag_id?;
    if self.depth_map[TAG_PRE as usize] > 0 && suppresses_formatting_in_pre(tag_id) {
      return None;
    }
    match tag_id {
      TAG_DETAILS => Some(Cow::Borrowed("<details>")),
      TAG_SUMMARY => Some(Cow::Borrowed("<summary>")),
      // Inside a table cell a fenced code block would split the GFM row; emit
      // raw <pre> and let the content newlines become <br> (issue #147).
      TAG_PRE if self.in_table_cell() => Some(Cow::Borrowed("<pre>")),
      TAG_BR => {
        if self.in_table_cell() || self.in_heading() || self.in_raw_html_block() {
          Some(Cow::Borrowed("<br>"))
        // Hard-break markers are literal content inside code.
        } else if self.depth_map[TAG_PRE as usize] > 0 || self.depth_map[TAG_CODE as usize] > 0 {
          Some(Cow::Borrowed("\n"))
        } else {
          let prefix = self.continuation_prefix();
          if prefix.is_empty() {
            Some(Cow::Borrowed("  \n"))
          } else {
            Some(Cow::Owned(format!("  \n{prefix}")))
          }
        }
      }
      TAG_H1 | TAG_H2 | TAG_H3 | TAG_H4 | TAG_H5 | TAG_H6 => {
        let depth = (tag_id - TAG_H1) as usize;
        // A `#` prefix needs its own line, which a GFM row cannot give it.
        if self.depth_map[TAG_A as usize] > 0 || self.in_table_cell() {
          {
            static H_OPEN: [&str; 6] = ["<h1>", "<h2>", "<h3>", "<h4>", "<h5>", "<h6>"];
            Some(Cow::Borrowed(H_OPEN[depth]))
          }
        } else {
          Some(Cow::Borrowed(HEADING_PREFIXES[depth]))
        }
      }
      TAG_HR => {
        if self.in_table_cell() {
          // A thematic break cannot end a GFM row; raw <hr> can sit in a cell.
          return Some(Cow::Borrowed("<hr>"));
        }
        // `continuation_prefix` walks the open element stack and allocates, so
        // only a rule that can actually carry a prefix asks for one.
        if self.depth_map[TAG_LI as usize] == 0 && self.depth_map[TAG_BLOCKQUOTE as usize] == 0 {
          return Some(Cow::Borrowed(MARKDOWN_HORIZONTAL_RULE));
        }
        let prefix = self.continuation_prefix();
        if prefix.is_empty() {
          return Some(Cow::Borrowed(MARKDOWN_HORIZONTAL_RULE));
        }
        Some(match self.block_open_prefix(&prefix) {
          // Sharing the marker's line, where `---` would make the whole line a
          // thematic break and take the item with it.
          None => Cow::Borrowed(MARKDOWN_HORIZONTAL_RULE_ALT),
          Some(open) => Cow::Owned(format!("{open}{MARKDOWN_HORIZONTAL_RULE}")),
        })
      }
      TAG_STRONG | TAG_B => {
        if self.depth_map[TAG_B as usize] > 1 {
          Some(Cow::Borrowed(""))
        } else {
          Some(Cow::Borrowed(MARKDOWN_STRONG))
        }
      }
      TAG_EM | TAG_I => {
        if self.depth_map[TAG_I as usize] > 1 {
          Some(Cow::Borrowed(""))
        } else {
          Some(Cow::Borrowed(MARKDOWN_EMPHASIS))
        }
      }
      TAG_DEL | TAG_S | TAG_STRIKE => Some(Cow::Borrowed(MARKDOWN_STRIKETHROUGH)),
      TAG_SUB => Some(Cow::Borrowed("<sub>")),
      TAG_SUP => Some(Cow::Borrowed("<sup>")),
      TAG_INS => Some(Cow::Borrowed("<ins>")),
      TAG_P => {
        if self.depth_map[TAG_LI as usize] > 0 && !self.in_table_cell() {
          let last_char = self.buffer.as_bytes().last().copied().unwrap_or(0);
          if last_char != 0 && last_char != b' ' && last_char != b'\n' {
            let indent = self.list_indent.as_str();
            let mut s = String::with_capacity(2 + indent.len());
            s.push_str("\n\n");
            s.push_str(indent);
            return Some(Cow::Owned(s));
          }
        }
        None
      }
      // A caption has no handler of its own, so inside a list item nothing writes
      // its content column: glued to preceding text it swallows the table's first
      // row, and at column 0 it takes the table out of the item.
      TAG_CAPTION if self.depth_map[TAG_LI as usize] > 0 && !self.in_table_cell() => {
        self.block_open_prefix(&self.list_indent)
      }
      TAG_BLOCKQUOTE => {
        // The completed subtree receives quote prefixes once every structural
        // newline is known. Preserve the list marker's trailing space here.
        (self.depth_map[TAG_LI as usize] > 0).then_some(Cow::Borrowed("\n"))
      }
      TAG_CODE => {
        if self.depth_map[TAG_PRE as usize] > 0 {
          // Inside a table cell emit raw <code> so no fence newline splits
          // the GFM row (issue #147). The enclosing <pre> emitted raw <pre>.
          if self.in_table_cell() {
            return Some(Cow::Borrowed("<code>"));
          }
          // A fence is already open for this <pre> — the <pre> opened it (mixed
          // text + <code> children) or an earlier <code> sibling did.
          if self.pre_fence_open {
            return None;
          }
          let lang = Self::get_language_from_class(node.attributes.get("class"));
          let li_depth = self.depth_map[TAG_LI as usize] as usize;
          if li_depth > 0 {
            let indent = self.list_indent.as_str();
            // A blank line between the marker and the fence ends the item, leaving
            // the block a sibling of the list.
            let open = self.block_open_prefix(indent).unwrap_or(Cow::Borrowed(""));
            let mut s = String::with_capacity(open.len() + indent.len() + 4 + lang.len() + 1);
            s.push_str(&open);
            s.push_str("```");
            s.push_str(lang);
            s.push('\n');
            s.push_str(indent);
            Some(Cow::Owned(s))
          } else if lang.is_empty() {
            Some(Cow::Borrowed("```\n"))
          } else {
            let mut s = String::with_capacity(4 + lang.len());
            s.push_str("```");
            s.push_str(lang);
            s.push('\n');
            Some(Cow::Owned(s))
          }
        } else if self.in_raw_html_block() {
          Some(Cow::Borrowed("<code>"))
        } else if self.depth_map[TAG_LI as usize] > 0 {
          // Inline code inside a list item: collapse the paragraph
          // boundary with a separator space when following text, but
          // not when the buffer just emitted a wrapper opener where
          // a leading space would break the pairing or leak into the
          // wrapper content. Covers emphasis (`*`, `_`),
          // strikethrough (`~`), link text (`[`), HTML passthrough
          // (`>`), and whitespace. A trailing backtick does NOT
          // suppress: two adjacent `<code>` elements must be
          // separated with a space so CommonMark parses them as two
          // code spans rather than merging into one (` `a``b` ` →
          // single span with literal content ``a``b``).
          let last_char = self.buffer.as_bytes().last().copied().unwrap_or(0);
          if last_char != 0
            && !matches!(
              last_char,
              b' ' | b'\n' | b'\t' | b'*' | b'_' | b'~' | b'[' | b'>'
            )
          {
            Some(Cow::Borrowed(" `"))
          } else {
            Some(Cow::Borrowed(MARKDOWN_INLINE_CODE))
          }
        } else {
          Some(Cow::Borrowed(MARKDOWN_INLINE_CODE))
        }
      }
      TAG_UL => {
        if self.in_table_cell() {
          Some(Cow::Borrowed("<ul>"))
        } else {
          None
        }
      }
      TAG_OL => {
        if self.in_table_cell() {
          Some(Cow::Borrowed("<ol>"))
        } else {
          None
        }
      }
      TAG_LI => {
        if self.in_table_cell() {
          return Some(Cow::Borrowed("<li>"));
        }
        // Parent determines marker: <ol> → "N. " (digits of N + 2
        // columns), else "- " (2 columns). The indent emitted here is
        // the parent's accumulated list_indent — this LI's own marker
        // contribution is pushed onto list_indent AFTER this output
        // is written to the buffer.
        let ordered = _ancestors.last().filter(|p| p.tag_id == Some(TAG_OL));
        let mut s = String::with_capacity(self.list_indent.len() + 6);
        s.push_str(&self.list_indent);
        if let Some(list) = ordered {
          use std::fmt::Write;
          let _ = write!(s, "{}. ", Self::ordered_item_number(list, node.index));
        } else {
          s.push_str("- ");
        }
        Some(Cow::Owned(s))
      }
      TAG_A => {
        if node.attributes.contains_key("href") {
          Some(Cow::Borrowed("["))
        } else {
          None
        }
      }
      TAG_IMG => {
        let alt = node.attributes.get("alt").map_or("", String::as_str);
        let src = node.attributes.get("src").map_or("", String::as_str);
        let resolved_src =
          resolve_url(src, self.options.origin.as_deref(), self.options.clean_urls);
        {
          let title = node.attributes.get("title").map(String::as_str);
          let mut s = String::with_capacity(
            alt.len() + resolved_src.len() + title.map_or(5, |title| title.len() + 8),
          );
          s.push_str("![");
          write_image_description(&mut s, alt);
          s.push(']');
          write_markdown_resource(&mut s, &resolved_src, title);
          Some(Cow::Owned(s))
        }
      }
      TAG_TABLE => {
        if self.in_table_cell() {
          Some(Cow::Borrowed("<table>"))
        } else {
          None
        }
      }
      TAG_THEAD => {
        if self.in_table_cell() {
          Some(Cow::Borrowed("<thead>"))
        } else {
          None
        }
      }
      TAG_TR => {
        if self.in_table_cell() {
          return Some(Cow::Borrowed("<tr>"));
        }
        let indent = if self.depth_map[TAG_LI as usize] > 0 {
          self.list_indent.as_str()
        } else {
          ""
        };
        // A row must open its own line at the item's content column: sharing one
        // with preceding content (a `<caption>`) leaves the header as prose and
        // the delimiter row never forms a table.
        match self.line_state_before_row() {
          LineBeforeRow::Row if indent.is_empty() => Some(Cow::Borrowed("\n| ")),
          LineBeforeRow::Content if indent.is_empty() => Some(Cow::Borrowed("\n\n| ")),
          LineBeforeRow::Row => Some(Cow::Owned(format!("\n{indent}| "))),
          LineBeforeRow::Content => Some(Cow::Owned(format!("\n\n{indent}| "))),
          // A pending list marker already supplies the column; only a fresh line
          // needs the indent written.
          LineBeforeRow::Open
            if !indent.is_empty() && self.buffer.as_bytes().last() == Some(&b'\n') =>
          {
            Some(Cow::Owned(format!("{indent}| ")))
          }
          LineBeforeRow::Open => Some(Cow::Borrowed("| ")),
        }
      }
      TAG_TH | TAG_TD => {
        if self.depth_map[TAG_TABLE as usize] > 1 {
          return Some(Cow::Borrowed(if tag_id == TAG_TH {
            "<th>"
          } else {
            "<td>"
          }));
        }
        if node.index == 0 {
          Some(Cow::Borrowed(""))
        } else if self.table_header_cells > 0
          && self.table_current_row_cells >= self.table_header_cells
        {
          // GFM discards cells past the delimiter row's width, so this one folds
          // into the previous cell rather than vanishing.
          Some(Cow::Borrowed(" "))
        } else {
          Some(Cow::Borrowed(" | "))
        }
      }
      TAG_CENTER => {
        if self.depth_map[TAG_TABLE as usize] > 1 {
          Some(Cow::Borrowed("<center>"))
        } else {
          None
        }
      }
      TAG_KBD | TAG_SAMP | TAG_VAR => Some(Cow::Borrowed("`")),
      TAG_ABBR | TAG_SMALL | TAG_TIME | TAG_BDO | TAG_RUBY | TAG_RT | TAG_RP => {
        Some(Cow::Borrowed(""))
      }
      TAG_MARK => Some(Cow::Borrowed("<mark>")),
      TAG_Q => Some(Cow::Borrowed("\"")),
      TAG_U => Some(Cow::Borrowed("<u>")),
      TAG_CITE => Some(Cow::Borrowed("*")),
      TAG_FIGCAPTION => Some(Cow::Borrowed(MARKDOWN_EMPHASIS)),
      TAG_DFN => Some(Cow::Borrowed("**")),
      TAG_ADDRESS => Some(Cow::Borrowed("<address>")),
      TAG_DL => Some(Cow::Borrowed("<dl>")),
      TAG_DT => Some(Cow::Borrowed("<dt>")),
      TAG_DD => Some(Cow::Borrowed("<dd>")),
      _ => None,
    }
  }

  #[inline]
  pub(crate) fn get_exit_output(
    &self,
    node: &ElementNode,
    cell_span: u8,
  ) -> Option<Cow<'static, str>> {
    if self.plain_text {
      return Self::get_text_exit_output(node);
    }

    let tag_id = node.tag_id?;
    if self.depth_map[TAG_PRE as usize] > 0 && suppresses_formatting_in_pre(tag_id) {
      return None;
    }
    match tag_id {
      // Inside a table cell the trailing block break would split the GFM row,
      // so emit the raw close tags with no newlines (issue #147).
      TAG_DETAILS if self.in_table_cell() => Some(Cow::Borrowed("</details>")),
      TAG_SUMMARY if self.in_table_cell() => Some(Cow::Borrowed("</summary>")),
      TAG_DETAILS => Some(Cow::Borrowed("</details>\n\n")),
      TAG_SUMMARY => Some(Cow::Borrowed("</summary>\n\n")),
      TAG_H1 | TAG_H2 | TAG_H3 | TAG_H4 | TAG_H5 | TAG_H6 => {
        let depth = (tag_id - TAG_H1 + 1) as usize;
        if self.depth_map[TAG_A as usize] > 0 || self.in_table_cell() {
          {
            static H_CLOSE: [&str; 6] = ["</h1>", "</h2>", "</h3>", "</h4>", "</h5>", "</h6>"];
            Some(Cow::Borrowed(H_CLOSE[depth - 1]))
          }
        } else {
          None
        }
      }
      TAG_STRONG | TAG_B => {
        if self.depth_map[TAG_B as usize] > 1 {
          Some(Cow::Borrowed(""))
        } else {
          Some(Cow::Borrowed(MARKDOWN_STRONG))
        }
      }
      TAG_EM | TAG_I => {
        if self.depth_map[TAG_I as usize] > 1 {
          Some(Cow::Borrowed(""))
        } else {
          Some(Cow::Borrowed(MARKDOWN_EMPHASIS))
        }
      }
      TAG_DEL | TAG_S | TAG_STRIKE => Some(Cow::Borrowed(MARKDOWN_STRIKETHROUGH)),
      TAG_SUB => Some(Cow::Borrowed("</sub>")),
      TAG_SUP => Some(Cow::Borrowed("</sup>")),
      TAG_INS => Some(Cow::Borrowed("</ins>")),
      TAG_CODE => {
        if self.depth_map[TAG_PRE as usize] > 0 {
          // Raw <code> close inside a table cell (issue #147).
          if self.in_table_cell() {
            return Some(Cow::Borrowed("</code>"));
          }
          // The <pre> exit owns the closing fence, so a text sibling after this
          // </code> still lands inside the block.
          None
        } else if self.in_raw_html_block() {
          Some(Cow::Borrowed("</code>"))
        } else {
          Some(Cow::Borrowed(MARKDOWN_INLINE_CODE))
        }
      }
      // Raw <pre> close inside a table cell (issue #147).
      TAG_PRE if self.in_table_cell() => Some(Cow::Borrowed("</pre>")),
      // Bare <pre> (no <code> child) closing fence (issue #97). Only emitted
      // when the <pre> opened its own fence; otherwise a <code> child or an
      // empty/whitespace-only <pre> means there is nothing to close.
      TAG_PRE => {
        if !self.pre_fence_open {
          return None;
        }
        let li_depth = self.depth_map[TAG_LI as usize] as usize;
        if li_depth > 0 {
          let indent = self.list_indent.as_str();
          let mut s = String::with_capacity(1 + indent.len() * 2 + 5);
          s.push('\n');
          s.push_str(indent);
          s.push_str("```\n\n");
          s.push_str(indent);
          Some(Cow::Owned(s))
        } else {
          Some(Cow::Borrowed("\n```"))
        }
      }
      TAG_UL => {
        if self.in_table_cell() {
          Some(Cow::Borrowed("</ul>"))
        } else {
          None
        }
      }
      TAG_OL => {
        if self.in_table_cell() {
          Some(Cow::Borrowed("</ol>"))
        } else {
          None
        }
      }
      TAG_LI => {
        if self.in_table_cell() {
          Some(Cow::Borrowed("</li>"))
        } else {
          None
        }
      }
      TAG_TR => {
        if self.in_table_cell() || self.depth_map[TAG_TABLE as usize] > 1 {
          Some(Cow::Borrowed("</tr>"))
        } else {
          Some(Cow::Borrowed(" |"))
        }
      }
      TAG_TABLE => {
        if self.in_table_cell() {
          Some(Cow::Borrowed("</table>"))
        } else {
          None
        }
      }
      TAG_THEAD => {
        if self.in_table_cell() {
          Some(Cow::Borrowed("</thead>"))
        } else {
          None
        }
      }
      TAG_TH | TAG_TD => {
        if self.depth_map[TAG_TABLE as usize] > 1 {
          Some(Cow::Borrowed(if tag_id == TAG_TH {
            "</th>"
          } else {
            "</td>"
          }))
        } else {
          Self::span_filler(cell_span)
        }
      }
      TAG_CENTER => {
        if self.depth_map[TAG_TABLE as usize] > 1 {
          Some(Cow::Borrowed("</center>"))
        } else {
          None
        }
      }
      TAG_KBD | TAG_SAMP | TAG_VAR => Some(Cow::Borrowed("`")),
      TAG_ABBR | TAG_SMALL | TAG_TIME | TAG_BDO | TAG_RUBY | TAG_RT | TAG_RP => {
        Some(Cow::Borrowed(""))
      }
      TAG_MARK => Some(Cow::Borrowed("</mark>")),
      TAG_Q => Some(Cow::Borrowed("\"")),
      TAG_U => Some(Cow::Borrowed("</u>")),
      TAG_CITE => Some(Cow::Borrowed("*")),
      TAG_FIGCAPTION => Some(Cow::Borrowed(MARKDOWN_EMPHASIS)),
      TAG_DFN => Some(Cow::Borrowed("**")),
      TAG_ADDRESS => Some(Cow::Borrowed("</address>")),
      TAG_DL => Some(Cow::Borrowed("</dl>")),
      TAG_DT => Some(Cow::Borrowed("</dt>")),
      TAG_DD => Some(Cow::Borrowed("</dd>")),
      _ => None,
    }
  }

  #[inline]
  fn get_text_enter_output(&self, node: &ElementNode) -> Option<Cow<'static, str>> {
    let tag_id = node.tag_id?;
    match tag_id {
      TAG_BR => Some(Cow::Borrowed("\n")),
      TAG_P => {
        if self.depth_map[TAG_BLOCKQUOTE as usize] > 0
          || (self.depth_map[TAG_LI as usize] > 0 && !self.in_table_cell())
        {
          let last_char = self.buffer.as_bytes().last().copied().unwrap_or(0);
          if last_char != 0 && last_char != b' ' && last_char != b'\n' {
            return Some(Cow::Borrowed("\n\n"));
          }
        }
        None
      }
      TAG_TD | TAG_TH => {
        if self.depth_map[TAG_TABLE as usize] > 1 {
          None
        } else if node.index == 0 {
          Some(Cow::Borrowed(""))
        } else {
          Some(Cow::Borrowed("\t"))
        }
      }
      TAG_IMG => {
        if let Some(alt) = node.attributes.get("alt") {
          return if alt.is_empty() {
            None
          } else {
            Some(Cow::Owned(alt.clone()))
          };
        }

        if let Some(title) = node
          .attributes
          .get("title")
          .filter(|title| !title.is_empty())
        {
          return Some(Cow::Owned(title.clone()));
        }

        let src = node.attributes.get("src").filter(|src| !src.is_empty())?;
        Some(Cow::Owned(
          resolve_url(src, self.options.origin.as_deref(), self.options.clean_urls).into_owned(),
        ))
      }
      TAG_Q => Some(Cow::Borrowed("\"")),
      _ => None,
    }
  }

  #[inline]
  fn get_text_exit_output(node: &ElementNode) -> Option<Cow<'static, str>> {
    let tag_id = node.tag_id?;
    match tag_id {
      TAG_Q => Some(Cow::Borrowed("\"")),
      _ => None,
    }
  }

  #[inline]
  pub(crate) fn write_output(
    &mut self,
    is_enter: bool,
    is_inline: bool,
    configured_new_lines: u8,
    output: Option<&str>,
    literal: bool,
  ) {
    let output_str = output.unwrap_or("");
    let output_is_line_boundary =
      !literal && (output_str.starts_with('\n') || output_str.starts_with("\\\n"));

    // A separator trimmed from inside a previously closed inline element must
    // sit outside its Markdown delimiter. Resolve it only when later visible
    // inline output begins; block boundaries and line breaks subsume it.
    if self.pending_inline_whitespace && is_enter {
      let first_output = output_str.as_bytes().first().copied();
      if !is_inline
        || output_is_line_boundary
        || configured_new_lines > 0
        || matches!(first_output, Some(b'\n' | b'\r'))
      {
        self.pending_inline_whitespace = false;
      } else if let Some(first) = first_output {
        let last = self.last_output_byte();
        if !matches!(last, Some(b' ' | b'\n' | b'\t') | None) && !is_whitespace(first) {
          self.buffer.push(' ');
        }
        self.pending_inline_whitespace = false;
      }
    } else if self.pending_inline_whitespace && (!is_inline || configured_new_lines > 0) {
      self.pending_inline_whitespace = false;
    }

    // Fast path: no newlines, no output, no whitespace state to manage
    if configured_new_lines == 0
      && output_str.is_empty()
      && !self.last_text_node_contains_whitespace
    {
      self.last_node_is_inline = is_inline;
      return;
    }

    let buf_bytes = self.buffer.as_bytes();
    let buf_len = buf_bytes.len();
    // Draining removes the front of the buffer, so a block boundary counting its
    // preceding newlines from the last two bytes must see through the drain:
    // `flushed_tail` contains the two bytes immediately before `buffer[0]`.
    // Without that context a separator that one-shot trims to one newline can
    // be emitted as two in streaming (e.g. a lone `-` at the buffer start).
    let last_char = if buf_len > 0 {
      buf_bytes[buf_len - 1]
    } else if self.has_streamed_output {
      self.flushed_tail[1]
    } else {
      0
    };
    let second_last_char = if buf_len > 1 {
      buf_bytes[buf_len - 2]
    } else if buf_len == 1 && self.has_streamed_output {
      self.flushed_tail[1]
    } else if self.has_streamed_output {
      self.flushed_tail[0]
    } else {
      0
    };

    // A closing code fence's block-spacing newlines are appended AFTER the
    // backtick or tilde delimiter, so
    // any trailing newlines already in the buffer (blank lines inside <pre>)
    // sit BEFORE the fence and no longer separate this block from the next
    // sibling — leaving ```<sibling> on one line, an invalid fence that never
    // closes. Measure the trailing-newline run from the fence's own tail (0) so
    // the block spacing is not suppressed (#148). Scoped to the fence: other
    // block closers (raw-HTML </dd>/</dl>, etc.) intentionally glue.
    let measure_from_output_tail =
      !is_enter && (output_str.ends_with("```") || output_str.ends_with("~~~"));

    let mut last_new_lines: u8 = 0;
    if !measure_from_output_tail {
      if last_char == b'\n' {
        last_new_lines += 1;
      }
      if second_last_char == b'\n' {
        last_new_lines += 1;
      }
    }

    let new_lines = configured_new_lines.saturating_sub(last_new_lines);

    if new_lines > 0 {
      // An empty buffer at true document start has no preceding block to
      // separate from, so the leading block newlines are suppressed. Mid-stream
      // the buffer can be empty only because earlier output was already yielded
      // and drained; the block separator is still required there, so fall
      // through and emit it (otherwise streaming drops a `\n\n` that one-shot,
      // which never drains, keeps).
      if self.buffer.is_empty() && !self.has_streamed_output {
        if !output_str.is_empty() {
          self.last_content_cache_len = output_str.len();
          self.buffer.push_str(output_str);
        }
        self.last_node_is_inline = is_inline;
        return;
      }

      if last_char == b' ' && !self.buffer.is_empty() {
        self.trim_trailing_spaces();
        // This source whitespace was consumed by the block boundary; do not
        // let its state leak into a later inline event and trim that output.
        self.last_text_node_contains_whitespace = false;
        self.has_last_text_node = false;
      }

      if is_enter {
        for _ in 0..new_lines {
          self.buffer.push('\n');
        }
        if !output_str.is_empty() {
          self.last_content_cache_len = output_str.len();
          self.buffer.push_str(output_str);
        }
      } else {
        if !output_str.is_empty() {
          self.last_content_cache_len = output_str.len();
          self.buffer.push_str(output_str);
        }
        for _ in 0..new_lines {
          self.buffer.push('\n');
        }
      }
    } else {
      if self.last_text_node_contains_whitespace
        && (is_inline || !self.stack.is_empty())
        && (self.depth_map[TAG_PRE as usize] == 0
          || self
            .stack
            .last()
            .is_some_and(|parent| parent.tag_id == Some(TAG_PRE)))
      {
        let h_is_inline = is_inline;
        let collapses = self
          .stack
          .last()
          .is_some_and(|parent| parent.collapses_inner_white_space);
        let has_spacing = self
          .stack
          .last()
          .is_some_and(|parent| parent.spacing.is_some());
        // For exit, the node was already popped, so use the is_inline param
        let is_block = !h_is_inline && !collapses && configured_new_lines > 0;
        let should_trim = !(is_block || h_is_inline && is_enter || is_enter && collapses)
          && !(has_spacing && is_enter);

        if should_trim && self.last_content_cache_len > 0 {
          let cache_len = self.last_content_cache_len;
          let buf_len = self.buffer.len();
          // The cached run can include a fence opener (see `trim_floor`).
          let start = buf_len.saturating_sub(cache_len).max(self.trim_floor());
          if cache_len <= buf_len && start <= buf_len && self.buffer.is_char_boundary(start) {
            let frag = &self.buffer[start..];
            // Trim only ASCII whitespace, not `str::trim_end`'s full Unicode
            // set: a trailing U+00A0 (`&nbsp;`) is meaningful content, and once
            // streaming has yielded it the truncation can't un-send its bytes,
            // so the reach-back would drop the next text's leading char.
            let trimmed_len = frag
              .trim_end_matches(|c: char| c.is_ascii_whitespace())
              .len();
            if start + trimmed_len < buf_len {
              self.buffer.truncate(start + trimmed_len);
              if !is_enter && is_inline {
                self.pending_inline_whitespace = true;
              }
            }
          }
        }
        self.last_text_node_contains_whitespace = false;
        self.has_last_text_node = false;
      }

      if is_enter
        && !literal
        && !output_is_line_boundary
        && !output_str.is_empty()
        && last_char != 0
        && self.needs_spacing(last_char, output_str.as_bytes()[0])
      {
        self.buffer.push(' ');
        self.last_content_cache_len = 1;
      }

      if !output_str.is_empty() {
        self.last_content_cache_len = output_str.len();
        self.buffer.push_str(output_str);
      }
    }
    self.last_node_is_inline = is_inline;
  }

  #[inline]
  pub(crate) fn needs_spacing(&self, last_byte: u8, first_byte: u8) -> bool {
    if matches!(last_byte, b' ' | b'\n' | b'\t') {
      return false;
    }
    if matches!(first_byte, b' ' | b'\n' | b'\t') {
      return false;
    }
    if last_byte == b'|' && first_byte == b'<' && !self.buffer.is_empty() {
      return true;
    }
    if matches!(last_byte, b'[' | b'(' | b'>' | b'*' | b'_' | b'`')
      || matches!(
        first_byte,
        b']' | b')' | b'<' | b'.' | b',' | b'!' | b'?' | b':' | b';' | b'*' | b'_' | b'`'
      )
    {
      return false;
    }
    true
  }

  #[inline]
  pub(crate) fn should_add_spacing_before_text(&self, last_byte: u8, text: &str) -> bool {
    if last_byte == 0
      || last_byte == b'\n'
      || last_byte == b' '
      || last_byte == b'\t'
      || last_byte == b'['
      || last_byte == b'>'
    {
      return false;
    }
    if self.last_node_is_inline {
      return false;
    }
    // Malformed attribute quoting can carry a start tag past its `>` and leave
    // an empty text node behind, so there may be no first byte to inspect.
    let Some(&first_byte) = text.as_bytes().first() else {
      return false;
    };
    if first_byte == b' ' {
      return false;
    }
    if matches!(
      first_byte,
      b'.' | b',' | b'!' | b'?' | b':' | b';' | b'_' | b'*' | b'`' | b')' | b']'
    ) {
      return false;
    }
    true
  }

  #[inline]
  pub(crate) fn calculate_new_line_config(
    &self,
    tag_id: Option<u8>,
    node_spacing: Option<[u8; 2]>,
  ) -> [u8; 2] {
    if self.plain_text
      && tag_id == Some(TAG_PRE)
      && (self.depth_map[TAG_LI as usize] > 0 || self.depth_map[TAG_BLOCKQUOTE as usize] > 0)
    {
      return [1, 1];
    }
    if let Some(id) = tag_id {
      if (id != TAG_LI && self.depth_map[TAG_LI as usize] > 0)
        || (self.plain_text && id != TAG_BLOCKQUOTE && self.depth_map[TAG_BLOCKQUOTE as usize] > 0)
      {
        return NO_SPACING;
      }
    } else if self.depth_map[TAG_LI as usize] > 0 || self.depth_map[TAG_BLOCKQUOTE as usize] > 0 {
      return NO_SPACING;
    }
    // A heading normally keeps its block spacing inside a collapsing parent, but
    // in a table cell that newline would end the row.
    let current_heading_owns_collapse = tag_id.is_some_and(|id| (TAG_H1..=TAG_H6).contains(&id))
      && self.collapse_non_span_depth == 1
      && !self.in_table_cell();
    if self.collapse_non_span_depth > 0 && !current_heading_owns_collapse {
      return NO_SPACING;
    }
    if self.collapse_span_depth > 0 {
      let is_block = tag_id.is_some_and(|id| {
        (TAG_H1..=TAG_H6).contains(&id) || matches!(id, TAG_P | TAG_DIV | TAG_LI)
      });
      if !is_block {
        return NO_SPACING;
      }
    }
    if self.has_tag_overrides {
      // For override spacing, we'd need the node name — but we have tag_id.
      // Use tag_id to get name for override lookup.
      if let Some(id) = tag_id {
        let name = TAG_NAMES[id as usize];
        if let Some(sp) = self
          .options
          .plugins
          .as_ref()
          .and_then(|p| p.tag_overrides.as_ref())
          .and_then(|ovs| ovs.iter().find(|(k, _)| k == name).map(|(_, v)| v))
          .and_then(|ov| ov.spacing)
        {
          return sp;
        }
      }
    }
    node_spacing.unwrap_or(DEFAULT_BLOCK_SPACING)
  }

  #[inline]
  pub(crate) fn get_language_from_class(class_name: Option<&String>) -> &str {
    if let Some(class) = class_name {
      for part in class.split([' ', '\t', '\n', '\u{000C}', '\r']) {
        if let Some(lang) = part.strip_prefix("language-")
          && !lang.is_empty()
          && !lang.bytes().any(is_unsafe_fence_info_byte)
        {
          return lang;
        }
      }
    }
    ""
  }

  fn line_state_before_row(&self) -> LineBeforeRow {
    let bytes = self.buffer.as_bytes();
    // Rows end their line, so the row after a row decides on one byte and never
    // rescans the line it just wrote.
    if matches!(bytes.last(), None | Some(b'\n')) {
      return LineBeforeRow::Open;
    }
    let mut index = bytes.len();
    while index > 0 && bytes[index - 1] != b'\n' {
      index -= 1;
    }
    let line = &bytes[index..];
    let start = line
      .iter()
      .position(|byte| !matches!(byte, b' ' | b'\t'))
      .unwrap_or(line.len());
    match line.get(start) {
      Some(b'|') => LineBeforeRow::Row,
      None => LineBeforeRow::Open,
      // A pending list marker is block prefix, not content: a table's first row
      // belongs on it.
      Some(_) if self.list_marker_line_start(bytes, bytes.len() - trailing_spaces(bytes)) => {
        LineBeforeRow::Open
      }
      Some(_) => LineBeforeRow::Content,
    }
  }

  /// How many columns a cell occupies. GFM has no `colspan`, so a spanned cell
  /// is written as its content followed by empty cells; without them the
  /// delimiter row is too narrow and GFM drops every cell past it.
  #[inline]
  #[allow(clippy::cast_possible_truncation)] // Parsing is explicitly bounded to `u8::MAX`.
  pub(crate) fn cell_span(node: &ElementNode) -> u8 {
    (node
      .attributes
      .get("colspan")
      .and_then(|value| parse_bounded_u32(value, u8::MAX.into()))
      .unwrap_or(1) as u8)
      .clamp(1, MAX_CELL_SPAN)
  }

  fn span_filler(span: u8) -> Option<Cow<'static, str>> {
    match span {
      1 => None,
      span => Some(Cow::Owned(" |".repeat(span as usize - 1))),
    }
  }

  /// Marker number for an `<ol>`'s nth item. GFM numbers a list from its first
  /// item's marker, so only that one has to carry `start`.
  pub(crate) fn ordered_item_number(list: &ElementNode, index: usize) -> u32 {
    list
      .attributes
      .get("start")
      .and_then(|value| parse_bounded_u32(value, MAX_ORDERED_START))
      .unwrap_or(1)
      .saturating_add(index as u32)
      .min(MAX_ORDERED_START)
  }
}

#[cfg(test)]
mod tests {
  use super::ConvertState;
  use crate::types::{HTMLToMarkdownOptions, OutputFormat};

  #[test]
  fn empty_drained_buffer_counts_two_flushed_newlines() {
    let mut state = ConvertState::new(HTMLToMarkdownOptions::default(), 64, OutputFormat::Markdown);
    state.has_streamed_output = true;
    state.flushed_tail = [b'\n', b'\n'];

    state.write_output(true, false, 2, Some("next"), false);

    assert_eq!(state.buffer, "next");
  }

  // Draining, and then a rewrite trimming what draining retained, leaves the
  // buffer empty mid-document; only a buffer nothing has streamed past is the
  // start of output.
  #[test]
  fn last_output_byte_sees_through_a_drain() {
    let mut state = ConvertState::new(HTMLToMarkdownOptions::default(), 64, OutputFormat::Markdown);
    assert_eq!(state.last_output_byte(), None);

    state.has_streamed_output = true;
    state.flushed_tail = *b"xy";
    assert_eq!(state.last_output_byte(), Some(b'y'));

    state.buffer.push('z');
    assert_eq!(state.last_output_byte(), Some(b'z'));
  }

  #[test]
  fn safe_prose_skips_the_gfm_escape_slow_path() {
    let mut state = ConvertState::new(HTMLToMarkdownOptions::default(), 64, OutputFormat::Markdown);

    assert!(
      state
        .process_html("<p>ordinary prose with 123 numbers and punctuation.</p>")
        .is_empty()
    );

    assert_eq!(state.gfm_escape_slow_path_calls, 0);
    assert_eq!(
      state.get_markdown(),
      "ordinary prose with 123 numbers and punctuation."
    );
  }

  #[test]
  fn syntax_and_entities_use_the_gfm_escape_slow_path() {
    let mut state = ConvertState::new(HTMLToMarkdownOptions::default(), 64, OutputFormat::Markdown);

    assert!(
      state
        .process_html("<p>* literal</p><p>&#42; decoded</p>")
        .is_empty()
    );

    assert_eq!(state.gfm_escape_slow_path_calls, 2);
    assert_eq!(state.get_markdown(), "\\* literal\n\n\\* decoded");
  }
}