ferromark 0.1.2

Ultra-high-performance Markdown to HTML compiler
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
//! ferromark: Ultra-high-performance Markdown to HTML compiler
//!
//! This crate provides a streaming Markdown parser optimized for speed,
//! targeting 20-30% better throughput than existing Rust parsers.
//!
//! # Design Principles
//! - No AST: streaming events only
//! - No regex: pure byte-level scanning
//! - No backtracking: O(n) time on all inputs
//! - Minimal allocations: ranges into input buffer
//!
//! # Future Optimizations
//! - `simdutf` / `simdutf8`: SIMD-accelerated UTF-8 validation for input
//! - NEON intrinsics for ARM: inline marker scanning
//! - Loop unrolling in hot paths (4x unroll like md4c)

pub mod block;
pub mod cursor;
pub mod escape;
pub mod footnote;
pub mod inline;
pub mod limits;
pub mod link_ref;
pub mod range;
pub mod render;

// Re-export primary types
pub use block::{Alignment, BlockEvent, BlockParser, CalloutType, fixup_list_tight};
pub use footnote::FootnoteStore;
pub use inline::{InlineEvent, InlineParser};
pub use link_ref::{LinkRefDef, LinkRefStore};
pub use range::Range;
pub use render::HtmlWriter;

/// Parsing/rendering options.
#[derive(Debug, Clone, Copy)]
pub struct Options {
    /// Allow raw inline and block HTML.
    pub allow_html: bool,
    /// Resolve link reference definitions and reference-style links.
    pub allow_link_refs: bool,
    /// Enable GFM table extension.
    pub tables: bool,
    /// Enable GFM strikethrough extension (`~~text~~`).
    pub strikethrough: bool,
    /// Enable GFM task list extension (`[ ]` / `[x]`).
    pub task_lists: bool,
    /// Enable GFM autolink literals extension (bare URLs, www, emails).
    pub autolink_literals: bool,
    /// Enable GFM disallowed raw HTML extension (filter dangerous tags).
    pub disallowed_raw_html: bool,
    /// Enable footnotes extension (`[^label]` references and `[^label]:` definitions).
    pub footnotes: bool,
    /// Enable front matter detection (`---`/`+++` delimited metadata at document start).
    pub front_matter: bool,
    /// Generate GitHub-compatible heading IDs (`<h1 id="slug">`).
    pub heading_ids: bool,
    /// Enable math spans (`$inline$` and `$$display$$`).
    pub math: bool,
    /// Enable GitHub-style callouts/admonitions (`> [!NOTE]`, `> [!WARNING]`, etc.).
    pub callouts: bool,
}

impl Default for Options {
    fn default() -> Self {
        Self {
            allow_html: true,
            allow_link_refs: true,
            tables: true,
            strikethrough: true,
            task_lists: true,
            autolink_literals: false,
            disallowed_raw_html: true,
            footnotes: false,
            front_matter: false,
            heading_ids: true,
            math: false,
            callouts: true,
        }
    }
}

/// Result of parsing Markdown with front matter extraction.
pub struct ParseResult<'a> {
    /// Rendered HTML output.
    pub html: String,
    /// Raw front matter content (between delimiters), if detected.
    pub front_matter: Option<&'a str>,
}

/// Extract front matter from the start of a document.
///
/// Returns `Some((content, rest_offset))` where `content` is the raw text between
/// delimiters and `rest_offset` is the byte offset where the remaining markdown begins.
/// Returns `None` if no valid front matter is found.
fn extract_front_matter(input: &str) -> Option<(&str, usize)> {
    let bytes = input.as_bytes();
    if bytes.len() < 3 {
        return None;
    }

    // Determine delimiter character: must be exactly 3 of `-` or `+` at byte 0
    let delim_char = match bytes[0] {
        b'-' | b'+' => bytes[0],
        _ => return None,
    };

    // Verify exactly 3 delimiter chars (not 4+)
    if bytes.len() < 3 || bytes[1] != delim_char || bytes[2] != delim_char {
        return None;
    }

    // After the 3 delimiter chars, only whitespace allowed before newline
    let mut pos = 3;
    if pos < bytes.len() && bytes[pos] == delim_char {
        // 4+ delimiter chars — not front matter
        return None;
    }
    while pos < bytes.len() && (bytes[pos] == b' ' || bytes[pos] == b'\t') {
        pos += 1;
    }

    // Must hit newline (or end of input for degenerate case, but that means no closing)
    if pos >= bytes.len() {
        return None;
    }
    if bytes[pos] == b'\r' {
        pos += 1;
    }
    if pos >= bytes.len() || bytes[pos] != b'\n' {
        return None;
    }
    pos += 1;

    let content_start = pos;

    // Search for closing delimiter
    loop {
        if pos >= bytes.len() {
            // No closing delimiter found
            return None;
        }

        // Check if current line is a closing delimiter
        let line_start = pos;
        if pos + 2 < bytes.len()
            && bytes[pos] == delim_char
            && bytes[pos + 1] == delim_char
            && bytes[pos + 2] == delim_char
        {
            let mut p = pos + 3;
            // Must not have 4+ delimiter chars
            if p < bytes.len() && bytes[p] == delim_char {
                // Not a closing delimiter, skip this line
            } else {
                // Optional trailing whitespace
                while p < bytes.len() && (bytes[p] == b' ' || bytes[p] == b'\t') {
                    p += 1;
                }
                // Must be at newline or EOF
                let at_end = p >= bytes.len()
                    || bytes[p] == b'\n'
                    || (bytes[p] == b'\r' && p + 1 < bytes.len() && bytes[p + 1] == b'\n');

                if at_end {
                    let content = &input[content_start..line_start];
                    // Advance past the closing delimiter line
                    let mut rest = p;
                    if rest < bytes.len() {
                        if bytes[rest] == b'\r' {
                            rest += 1;
                        }
                        if rest < bytes.len() && bytes[rest] == b'\n' {
                            rest += 1;
                        }
                    }
                    return Some((content, rest));
                }
            }
        }

        // Skip to next line
        while pos < bytes.len() && bytes[pos] != b'\n' {
            pos += 1;
        }
        if pos < bytes.len() {
            pos += 1; // skip \n
        }

        // Safety: if we haven't advanced past line_start, force progress
        if pos <= line_start {
            break;
        }
    }

    None
}

/// Parse Markdown and return both HTML and front matter (if present).
///
/// Uses default options with `front_matter: true`.
///
/// # Example
/// ```
/// let result = ferromark::parse("---\ntitle: Hello\n---\n# Content");
/// assert_eq!(result.front_matter, Some("title: Hello\n"));
/// assert!(result.html.contains("Content</h1>"));
/// ```
pub fn parse(input: &str) -> ParseResult<'_> {
    let options = Options {
        front_matter: true,
        ..Options::default()
    };
    parse_with_options(input, &options)
}

/// Parse Markdown with options and return both HTML and front matter.
///
/// Front matter is only extracted when `options.front_matter` is `true`.
pub fn parse_with_options<'a>(input: &'a str, options: &Options) -> ParseResult<'a> {
    let (front_matter, markdown) = if options.front_matter {
        match extract_front_matter(input) {
            Some((fm, offset)) => (Some(fm), &input[offset..]),
            None => (None, input),
        }
    } else {
        (None, input)
    };

    let html = to_html_with_options(markdown, options);
    ParseResult { html, front_matter }
}

/// Convert Markdown to HTML.
///
/// This is the primary API for simple use cases.
///
/// # Example
/// ```
/// let html = ferromark::to_html("# Hello\n\nWorld");
/// assert!(html.contains("Hello</h1>"));
/// assert!(html.contains("<p>World</p>"));
/// ```
pub fn to_html(input: &str) -> String {
    let mut writer = HtmlWriter::with_capacity_for(input.len());
    render_to_writer(input.as_bytes(), &mut writer, &Options::default());
    writer.into_string()
}

/// Convert Markdown to HTML, writing into a provided buffer.
///
/// This avoids allocation if the buffer has sufficient capacity.
pub fn to_html_into(input: &str, out: &mut Vec<u8>) {
    to_html_into_with_options(input, out, &Options::default());
}

/// Convert Markdown to HTML with options.
///
/// When `options.front_matter` is `true`, any front matter at the start of the
/// document is silently stripped before parsing.
pub fn to_html_with_options(input: &str, options: &Options) -> String {
    let markdown = if options.front_matter {
        match extract_front_matter(input) {
            Some((_, offset)) => &input[offset..],
            None => input,
        }
    } else {
        input
    };
    let mut writer = HtmlWriter::with_capacity_for(markdown.len());
    render_to_writer(markdown.as_bytes(), &mut writer, options);
    writer.into_string()
}

/// Convert Markdown to HTML into a provided buffer with options.
///
/// When `options.front_matter` is `true`, any front matter at the start of the
/// document is silently stripped before parsing.
pub fn to_html_into_with_options(input: &str, out: &mut Vec<u8>, options: &Options) {
    let markdown = if options.front_matter {
        match extract_front_matter(input) {
            Some((_, offset)) => &input[offset..],
            None => input,
        }
    } else {
        input
    };
    out.clear();
    out.reserve(markdown.len() + markdown.len() / 4);
    let mut writer = HtmlWriter::with_capacity(0);
    // Use the provided buffer directly
    std::mem::swap(writer.buffer_mut(), out);
    render_to_writer(markdown.as_bytes(), &mut writer, options);
    std::mem::swap(writer.buffer_mut(), out);
}

/// State for collecting paragraph content before inline parsing.
struct ParagraphState {
    /// Collected text content (joined with newlines).
    content: Vec<u8>,
    /// Whether we're currently in a paragraph.
    in_paragraph: bool,
}

impl ParagraphState {
    fn new() -> Self {
        Self {
            content: Vec::with_capacity(256),
            in_paragraph: false,
        }
    }

    fn start(&mut self) {
        self.in_paragraph = true;
        self.content.clear();
    }

    fn add_text(&mut self, text: &[u8]) {
        self.content.extend_from_slice(text);
    }

    fn add_soft_break(&mut self) {
        self.content.push(b'\n');
    }

    fn finish(&mut self) -> &[u8] {
        self.in_paragraph = false;
        // CommonMark: strip trailing spaces/tabs from paragraph content
        while self
            .content
            .last()
            .is_some_and(|&b| b == b' ' || b == b'\t')
        {
            self.content.pop();
        }
        &self.content
    }
}

/// State for collecting heading content before inline parsing.
struct HeadingState {
    /// Collected text content (joined with newlines).
    content: Vec<u8>,
    /// Whether we're currently in a heading.
    in_heading: bool,
    /// Current heading level (stored for deferred tag emission).
    level: u8,
}

impl HeadingState {
    fn new() -> Self {
        Self {
            content: Vec::with_capacity(64),
            in_heading: false,
            level: 0,
        }
    }

    fn start(&mut self) {
        self.in_heading = true;
        self.content.clear();
    }

    fn add_text(&mut self, text: &[u8]) {
        self.content.extend_from_slice(text);
    }

    fn add_soft_break(&mut self) {
        self.content.push(b'\n');
    }

    fn finish(&mut self) -> &[u8] {
        self.in_heading = false;
        while self
            .content
            .last()
            .is_some_and(|&b| b == b' ' || b == b'\t')
        {
            self.content.pop();
        }
        &self.content
    }
}

/// Tracker for deduplicating heading IDs.
struct HeadingIdTracker {
    used: Vec<String>,
}

impl HeadingIdTracker {
    fn new() -> Self {
        Self { used: Vec::new() }
    }

    /// Generate a unique slug, appending `-1`, `-2`, etc. on collision.
    fn unique_slug(&mut self, base: String) -> String {
        let slug = if base.is_empty() {
            "heading".to_string()
        } else {
            base
        };
        let count = self.used.iter().filter(|s| **s == slug).count();
        let result = if count == 0 {
            slug.clone()
        } else {
            format!("{}-{}", slug, count)
        };
        self.used.push(slug);
        result
    }
}

/// Generate a GitHub-compatible slug from raw heading text.
///
/// Steps:
/// 1. Strip inline markup delimiters (`*`, `_`, `~`, `` ` ``, `[`, `]`, `!`, `#`)
/// 2. Lowercase
/// 3. Replace whitespace runs with `-`
/// 4. Remove chars that are not alphanumeric, `-`, `_`, or space
/// 5. Strip leading/trailing `-`
fn generate_slug(raw: &[u8]) -> String {
    let mut slug = Vec::with_capacity(raw.len());
    let mut prev_was_space = false;

    for &b in raw {
        // Strip inline markup delimiters (keep _ since it's valid in slugs)
        if matches!(b, b'*' | b'~' | b'`' | b'[' | b']' | b'!' | b'#') {
            continue;
        }

        if b == b' ' || b == b'\t' || b == b'\n' || b == b'\r' {
            if !prev_was_space && !slug.is_empty() {
                slug.push(b'-');
                prev_was_space = true;
            }
            continue;
        }

        prev_was_space = false;

        // Lowercase ASCII
        let ch = if b.is_ascii_uppercase() { b + 32 } else { b };

        // Keep alphanumeric, hyphen, underscore, and multibyte UTF-8
        if ch.is_ascii_alphanumeric() || ch == b'-' || ch == b'_' || ch >= 0x80 {
            slug.push(ch);
        }
    }

    // Strip trailing hyphen
    while slug.last() == Some(&b'-') {
        slug.pop();
    }
    // Strip leading hyphen
    while slug.first() == Some(&b'-') {
        slug.remove(0);
    }

    String::from_utf8(slug).unwrap_or_default()
}

/// State for collecting table cell content before inline parsing.
struct CellState {
    /// Collected text content.
    content: Vec<u8>,
    /// Whether we're currently in a cell.
    in_cell: bool,
}

impl CellState {
    fn new() -> Self {
        Self {
            content: Vec::with_capacity(64),
            in_cell: false,
        }
    }

    fn start(&mut self) {
        self.in_cell = true;
        self.content.clear();
    }

    fn add_text(&mut self, text: &[u8]) {
        // In table cells, \| is a table-level escape meaning literal |
        // Replace \| with | before inline parsing
        let mut i = 0;
        while i < text.len() {
            if text[i] == b'\\' && i + 1 < text.len() && text[i + 1] == b'|' {
                self.content.push(b'|');
                i += 2;
            } else {
                self.content.push(text[i]);
                i += 1;
            }
        }
    }

    fn finish(&mut self) -> &[u8] {
        self.in_cell = false;
        // Trim trailing whitespace
        while self
            .content
            .last()
            .is_some_and(|&b| b == b' ' || b == b'\t')
        {
            self.content.pop();
        }
        &self.content
    }
}

/// Render Markdown to an HtmlWriter.
fn render_to_writer(input: &[u8], writer: &mut HtmlWriter, options: &Options) {
    // Parse blocks
    let mut parser = BlockParser::new_with_options(input, *options);
    let mut events = Vec::with_capacity((input.len() / 16).max(64));
    parser.parse(&mut events);
    let link_refs = parser.take_link_refs();
    let footnote_store = if options.footnotes {
        Some(parser.take_footnote_store())
    } else {
        None
    };

    // Fix up list tight status (ListStart gets its tight value from ListEnd)
    fixup_list_tight(&mut events);

    // Create inline parser for text content
    let mut inline_parser = InlineParser::new();
    let mut inline_events = Vec::with_capacity(64);

    // State for accumulating paragraph content
    let mut para_state = ParagraphState::new();
    let mut heading_state = HeadingState::new();
    let mut cell_state = CellState::new();

    // Track tight/loose status for nested lists (stack - (tight, blockquote_depth_at_start))
    let mut tight_list_stack: Vec<(bool, u32)> = Vec::new();

    // Track if we just started a tight list item (need newline before block content)
    let mut at_tight_li_start = false;

    // Track if we need newline before next block element (after paragraph in tight list)
    let mut need_newline_before_block = false;

    // Track if we need a newline after <li> in loose list (deferred until content appears)
    let mut pending_loose_li_newline = false;

    // Track blockquote depth (paragraphs in blockquotes always get <p> tags)
    let mut blockquote_depth = 0u32;

    // Track whether we're in a table header
    let mut in_table_head = false;

    // Pending task checkbox (emitted at start of first paragraph in list item)
    let mut pending_task = block::TaskState::None;

    // Track footnote reference order (def_index -> sequential number)
    let mut footnote_order: Vec<usize> = Vec::new();
    let fn_store_ref = footnote_store.as_ref();

    // Heading ID tracker for deduplication
    let mut heading_id_tracker = HeadingIdTracker::new();

    // Track callout type for each open blockquote (None = regular blockquote)
    let mut callout_stack: Vec<Option<block::CalloutType>> = Vec::new();

    // Render events to HTML
    for event in &events {
        render_block_event(
            input,
            event,
            writer,
            &mut inline_parser,
            &mut inline_events,
            &mut para_state,
            &mut heading_state,
            &mut cell_state,
            &mut tight_list_stack,
            &mut at_tight_li_start,
            &mut need_newline_before_block,
            &mut pending_loose_li_newline,
            &mut blockquote_depth,
            &mut in_table_head,
            &mut pending_task,
            &link_refs,
            fn_store_ref,
            &mut footnote_order,
            &mut heading_id_tracker,
            &mut callout_stack,
            options,
        );
    }

    // Render footnote section at document end
    if let Some(fn_store) = &footnote_store {
        if !footnote_order.is_empty() {
            render_footnote_section(
                input,
                fn_store,
                &footnote_order,
                writer,
                &mut inline_parser,
                &mut inline_events,
                &link_refs,
                options,
            );
        }
    }
}

/// Render a single block event to HTML.
#[allow(clippy::too_many_arguments)]
fn render_block_event(
    input: &[u8],
    event: &BlockEvent,
    writer: &mut HtmlWriter,
    inline_parser: &mut InlineParser,
    inline_events: &mut Vec<InlineEvent>,
    para_state: &mut ParagraphState,
    heading_state: &mut HeadingState,
    cell_state: &mut CellState,
    tight_list_stack: &mut Vec<(bool, u32)>,
    at_tight_li_start: &mut bool,
    need_newline_before_block: &mut bool,
    pending_loose_li_newline: &mut bool,
    blockquote_depth: &mut u32,
    in_table_head: &mut bool,
    pending_task: &mut block::TaskState,
    link_refs: &LinkRefStore,
    footnote_store: Option<&FootnoteStore>,
    footnote_order: &mut Vec<usize>,
    heading_id_tracker: &mut HeadingIdTracker,
    callout_stack: &mut Vec<Option<block::CalloutType>>,
    options: &Options,
) {
    // Check if we're in a tight list (innermost list is tight)
    // BUT: paragraphs inside blockquotes that started AFTER the list need <p> tags
    let in_tight_list = tight_list_stack
        .last()
        .is_some_and(|(tight, bq_depth_at_start)| {
            *tight && *blockquote_depth <= *bq_depth_at_start
        });

    match event {
        BlockEvent::ParagraphStart => {
            // Write pending newline from loose list item start
            if *pending_loose_li_newline {
                writer.newline();
                *pending_loose_li_newline = false;
            }
            // In tight lists, don't emit <p> tags
            if !in_tight_list {
                writer.paragraph_start();
            }
            para_state.start();
            // Paragraph content is inline, so we don't add newline
            *at_tight_li_start = false;
        }
        BlockEvent::ParagraphEnd => {
            // Check if we're in a tight list (innermost list is tight)
            // BUT: paragraphs inside blockquotes that started AFTER the list need </p> tags
            let in_tight_list =
                tight_list_stack
                    .last()
                    .is_some_and(|(tight, bq_depth_at_start)| {
                        *tight && *blockquote_depth <= *bq_depth_at_start
                    });

            // Parse all accumulated paragraph content at once
            let content = para_state.finish();

            // Emit pending task checkbox before paragraph content
            emit_pending_task_checkbox(pending_task, writer);

            if !content.is_empty() {
                inline_events.clear();
                inline_events.reserve((content.len() / 8).max(8));
                let refs = if options.allow_link_refs {
                    Some(link_refs)
                } else {
                    None
                };
                inline_parser.parse_with_options(
                    content,
                    refs,
                    options.allow_html,
                    options.strikethrough,
                    options.autolink_literals,
                    options.math,
                    footnote_store,
                    inline_events,
                );

                // Render inline events
                let mut image_state = None;
                for inline_event in inline_events.iter() {
                    render_inline_event(
                        content,
                        inline_event,
                        writer,
                        &mut image_state,
                        link_refs,
                        options.disallowed_raw_html,
                        footnote_store,
                        footnote_order,
                    );
                }
            }
            // In tight lists, don't emit </p> tags
            if !in_tight_list {
                writer.paragraph_end();
            } else {
                // Mark that we need newline before next block element
                *need_newline_before_block = true;
            }
        }
        BlockEvent::HeadingStart { level } => {
            if *need_newline_before_block {
                writer.newline();
                *need_newline_before_block = false;
            }
            if *at_tight_li_start {
                writer.newline();
                *at_tight_li_start = false;
            }
            // Defer heading open tag to HeadingEnd so we can generate the slug
            // from collected content before emitting the tag.
            heading_state.start();
            heading_state.level = *level;
        }
        BlockEvent::HeadingEnd { level } => {
            let content = heading_state.finish();

            // Emit heading open tag (deferred from HeadingStart)
            if options.heading_ids {
                let slug = generate_slug(content);
                let id = heading_id_tracker.unique_slug(slug);
                writer.heading_start_with_id(*level, &id);
            } else {
                writer.heading_start(*level);
            }

            if !content.is_empty() {
                inline_events.clear();
                inline_events.reserve((content.len() / 8).max(8));
                let refs = if options.allow_link_refs {
                    Some(link_refs)
                } else {
                    None
                };
                inline_parser.parse_with_options(
                    content,
                    refs,
                    options.allow_html,
                    options.strikethrough,
                    options.autolink_literals,
                    options.math,
                    footnote_store,
                    inline_events,
                );

                let mut image_state = None;
                for inline_event in inline_events.iter() {
                    render_inline_event(
                        content,
                        inline_event,
                        writer,
                        &mut image_state,
                        link_refs,
                        options.disallowed_raw_html,
                        footnote_store,
                        footnote_order,
                    );
                }
            }
            writer.heading_end(*level);
        }
        BlockEvent::ThematicBreak => {
            // If we're at the start of a tight list item, add newline before block content
            if *at_tight_li_start {
                writer.newline();
                *at_tight_li_start = false;
            }
            writer.thematic_break();
        }
        BlockEvent::HtmlBlockStart => {
            // Write pending newline from loose list item start
            if *pending_loose_li_newline {
                writer.newline();
                *pending_loose_li_newline = false;
            }
            // If we're at the start of a tight list item, add newline before block content
            if *at_tight_li_start {
                writer.newline();
                *at_tight_li_start = false;
            }
        }
        BlockEvent::HtmlBlockText(range) => {
            if options.disallowed_raw_html {
                writer.write_html_filtered(range.slice(input));
            } else {
                writer.write_bytes(range.slice(input));
            }
        }
        BlockEvent::HtmlBlockEnd => {}
        BlockEvent::SoftBreak => {
            if para_state.in_paragraph {
                para_state.add_soft_break();
            } else if heading_state.in_heading {
                heading_state.add_soft_break();
            } else {
                writer.write_str("\n");
            }
        }
        BlockEvent::Text(range) => {
            let text = range.slice(input);
            if para_state.in_paragraph {
                // Accumulate for later parsing
                para_state.add_text(text);
            } else if heading_state.in_heading {
                heading_state.add_text(text);
            } else if cell_state.in_cell {
                cell_state.add_text(text);
            } else {
                // Parse immediately (e.g., heading content)
                inline_events.clear();
                let refs = if options.allow_link_refs {
                    Some(link_refs)
                } else {
                    None
                };
                inline_parser.parse_with_options(
                    text,
                    refs,
                    options.allow_html,
                    options.strikethrough,
                    options.autolink_literals,
                    options.math,
                    footnote_store,
                    inline_events,
                );

                // Render inline events
                let mut image_state = None;
                for inline_event in inline_events.iter() {
                    render_inline_event(
                        text,
                        inline_event,
                        writer,
                        &mut image_state,
                        link_refs,
                        options.disallowed_raw_html,
                        footnote_store,
                        footnote_order,
                    );
                }
            }
        }
        BlockEvent::Code(range) => {
            // Code block content - no inline parsing
            writer.write_escaped_text(range.slice(input));
        }
        BlockEvent::VirtualSpaces(count) => {
            // Emit spaces for tab expansion in indented code blocks
            for _ in 0..*count {
                writer.write_byte(b' ');
            }
        }
        BlockEvent::CodeBlockStart { info } => {
            // Write pending newline from loose list item start
            if *pending_loose_li_newline {
                writer.newline();
                *pending_loose_li_newline = false;
            }
            // If we're at the start of a tight list item, add newline before block content
            if *at_tight_li_start {
                writer.newline();
                *at_tight_li_start = false;
            }
            let lang = info.as_ref().map(|r| r.slice(input));
            writer.code_block_start(lang);
        }
        BlockEvent::CodeBlockEnd => {
            writer.code_block_end();
        }
        BlockEvent::BlockQuoteStart { callout } => {
            // Write pending newline from loose list item start
            if *pending_loose_li_newline {
                writer.newline();
                *pending_loose_li_newline = false;
            }
            // If we need newline (after paragraph in tight list), add it
            if *need_newline_before_block {
                writer.newline();
                *need_newline_before_block = false;
            }
            // If we're at the start of a tight list item, add newline before block content
            if *at_tight_li_start {
                writer.newline();
                *at_tight_li_start = false;
            }
            *blockquote_depth += 1;
            callout_stack.push(*callout);
            if let Some(ct) = callout {
                writer.callout_start(*ct);
            } else {
                writer.blockquote_start();
            }
        }
        BlockEvent::BlockQuoteEnd => {
            *blockquote_depth = blockquote_depth.saturating_sub(1);
            match callout_stack.pop() {
                Some(Some(_)) => writer.callout_end(),
                _ => writer.blockquote_end(),
            }
        }
        BlockEvent::ListStart { kind, tight } => {
            // Write pending newline from loose list item start
            if *pending_loose_li_newline {
                writer.newline();
                *pending_loose_li_newline = false;
            }
            // If we need newline (after paragraph in tight list), add it
            if *need_newline_before_block {
                writer.newline();
                *need_newline_before_block = false;
            }
            // If we're at the start of a tight list item, add newline before nested list
            if *at_tight_li_start {
                writer.newline();
                *at_tight_li_start = false;
            }
            // Push the tight status and current blockquote depth for this list
            tight_list_stack.push((*tight, *blockquote_depth));
            match kind {
                block::ListKind::Unordered => writer.ul_start(),
                block::ListKind::Ordered { start, .. } => {
                    writer.ol_start(if *start == 1 { None } else { Some(*start) })
                }
            }
        }
        BlockEvent::ListEnd { kind, .. } => {
            match kind {
                block::ListKind::Unordered => writer.ul_end(),
                block::ListKind::Ordered { .. } => writer.ol_end(),
            }
            // Pop the tight status for this list
            tight_list_stack.pop();
        }
        BlockEvent::ListItemStart { task } => {
            writer.li_start();
            // In loose lists, defer newline until content appears (for empty items)
            if !in_tight_list {
                *pending_loose_li_newline = true;
            } else {
                // In tight lists, mark that we may need newline if block content follows
                *at_tight_li_start = true;
            }
            // Store task state for rendering at the start of paragraph content
            if options.task_lists {
                *pending_task = *task;
            }
        }
        BlockEvent::ListItemEnd => {
            *at_tight_li_start = false;
            *need_newline_before_block = false;
            *pending_loose_li_newline = false;
            *pending_task = block::TaskState::None;
            writer.li_end();
        }

        // --- Table events ---
        BlockEvent::TableStart => {
            if *pending_loose_li_newline {
                writer.newline();
                *pending_loose_li_newline = false;
            }
            if *need_newline_before_block {
                writer.newline();
                *need_newline_before_block = false;
            }
            if *at_tight_li_start {
                writer.newline();
                *at_tight_li_start = false;
            }
            writer.table_start();
        }
        BlockEvent::TableEnd => {
            writer.table_end();
        }
        BlockEvent::TableHeadStart => {
            *in_table_head = true;
            writer.thead_start();
        }
        BlockEvent::TableHeadEnd => {
            *in_table_head = false;
            writer.thead_end();
        }
        BlockEvent::TableBodyStart => {
            writer.tbody_start();
        }
        BlockEvent::TableBodyEnd => {
            writer.tbody_end();
        }
        BlockEvent::TableRowStart => {
            writer.tr_start();
        }
        BlockEvent::TableRowEnd => {
            writer.tr_end();
        }
        BlockEvent::TableCellStart { alignment } => {
            if *in_table_head {
                writer.th_start(*alignment);
            } else {
                writer.td_start(*alignment);
            }
            cell_state.start();
        }
        BlockEvent::TableCellEnd => {
            let content = cell_state.finish();
            if !content.is_empty() {
                inline_events.clear();
                inline_events.reserve((content.len() / 8).max(8));
                let refs = if options.allow_link_refs {
                    Some(link_refs)
                } else {
                    None
                };
                inline_parser.parse_with_options(
                    content,
                    refs,
                    options.allow_html,
                    options.strikethrough,
                    options.autolink_literals,
                    options.math,
                    footnote_store,
                    inline_events,
                );

                let mut image_state = None;
                for inline_event in inline_events.iter() {
                    render_inline_event(
                        content,
                        inline_event,
                        writer,
                        &mut image_state,
                        link_refs,
                        options.disallowed_raw_html,
                        footnote_store,
                        footnote_order,
                    );
                }
            }
            if *in_table_head {
                writer.th_end();
            } else {
                writer.td_end();
            }
        }
    }
}

/// Emit a pending task checkbox and reset the state.
#[inline]
fn emit_pending_task_checkbox(pending_task: &mut block::TaskState, writer: &mut HtmlWriter) {
    match *pending_task {
        block::TaskState::Unchecked => {
            writer.write_bytes(b"<input type=\"checkbox\" disabled=\"\" /> ");
        }
        block::TaskState::Checked => {
            writer.write_bytes(b"<input type=\"checkbox\" checked=\"\" disabled=\"\" /> ");
        }
        block::TaskState::None => {}
    }
    *pending_task = block::TaskState::None;
}

/// State for tracking image rendering.
/// Since we need to render: <img src="..." alt="ALT_TEXT_HERE" title="..." />
/// But alt text comes as Text events between ImageStart and ImageEnd,
/// we need to track:
/// 1. The title to render at ImageEnd
/// 2. The nesting depth (to handle nested images like ![foo ![bar](url1)](url2))
struct ImageState {
    title_range: Option<Range>,
    title_bytes: Option<Vec<u8>>,
    /// Nesting depth: 1 = in outermost image, 2+ = in nested image
    depth: u32,
}

/// Render a single inline event to HTML.
#[allow(clippy::too_many_arguments)]
fn render_inline_event(
    text: &[u8],
    event: &InlineEvent,
    writer: &mut HtmlWriter,
    image_state: &mut Option<ImageState>,
    link_refs: &LinkRefStore,
    filter_html: bool,
    footnote_store: Option<&FootnoteStore>,
    footnote_order: &mut Vec<usize>,
) {
    // Check if we're inside an image (for alt text rendering)
    let in_image = image_state.as_ref().is_some_and(|s| s.depth > 0);

    match event {
        InlineEvent::Text(range) => {
            // In image alt text, we still write the text (escaped for attributes)
            if in_image {
                writer.write_escaped_attr(range.slice(text));
            } else {
                // Decode HTML entities and escape for output
                writer.write_text_with_entities(range.slice(text));
            }
        }
        InlineEvent::Code(range) => {
            // In image alt text, just write the code content as plain text
            if in_image {
                let code_content = range.slice(text);
                for &b in code_content {
                    if b == b'\n' {
                        writer.write_str(" ");
                    } else if b == b'<' {
                        writer.write_str("&lt;");
                    } else if b == b'>' {
                        writer.write_str("&gt;");
                    } else if b == b'&' {
                        writer.write_str("&amp;");
                    } else if b == b'"' {
                        writer.write_str("&quot;");
                    } else {
                        writer.buffer_mut().push(b);
                    }
                }
            } else {
                writer.write_str("<code>");
                // CommonMark: line endings in code spans are converted to spaces
                let code_content = range.slice(text);
                for &b in code_content {
                    if b == b'\n' {
                        writer.write_str(" ");
                    } else if b == b'<' {
                        writer.write_str("&lt;");
                    } else if b == b'>' {
                        writer.write_str("&gt;");
                    } else if b == b'&' {
                        writer.write_str("&amp;");
                    } else if b == b'"' {
                        writer.write_str("&quot;");
                    } else {
                        writer.buffer_mut().push(b);
                    }
                }
                writer.write_str("</code>");
            }
        }
        InlineEvent::EmphasisStart => {
            // Suppress HTML tags inside image alt text
            if !in_image {
                writer.write_str("<em>");
            }
        }
        InlineEvent::EmphasisEnd => {
            if !in_image {
                writer.write_str("</em>");
            }
        }
        InlineEvent::StrongStart => {
            if !in_image {
                writer.write_str("<strong>");
            }
        }
        InlineEvent::StrongEnd => {
            if !in_image {
                writer.write_str("</strong>");
            }
        }
        InlineEvent::StrikethroughStart => {
            if !in_image {
                writer.write_str("<del>");
            }
        }
        InlineEvent::StrikethroughEnd => {
            if !in_image {
                writer.write_str("</del>");
            }
        }
        InlineEvent::LinkStart { url, title } => {
            // Suppress link tags inside image alt text
            if !in_image {
                writer.write_str("<a href=\"");
                writer.write_link_url(url.slice(text));
                writer.write_str("\"");
                if let Some(t) = title {
                    writer.write_str(" title=\"");
                    writer.write_link_title(t.slice(text));
                    writer.write_str("\"");
                }
                writer.write_str(">");
            }
        }
        InlineEvent::LinkStartRef { def_index } => {
            if !in_image {
                if let Some(def) = link_refs.get(*def_index as usize) {
                    writer.write_str("<a href=\"");
                    writer.write_link_url(&def.url);
                    writer.write_str("\"");
                    if let Some(title) = &def.title {
                        writer.write_str(" title=\"");
                        writer.write_link_title(title);
                        writer.write_str("\"");
                    }
                    writer.write_str(">");
                }
            }
        }
        InlineEvent::LinkEnd => {
            if !in_image {
                writer.write_str("</a>");
            }
        }
        InlineEvent::ImageStart { url, title } => {
            // If we're already inside an image, just increment depth
            // (the inner image's alt text becomes plain text in outer alt)
            if let Some(state) = image_state.as_mut() {
                state.depth += 1;
            } else {
                // Outermost image - emit the img tag start
                writer.write_str("<img src=\"");
                writer.write_link_url(url.slice(text));
                writer.write_str("\" alt=\"");
                *image_state = Some(ImageState {
                    title_range: *title,
                    title_bytes: None,
                    depth: 1,
                });
            }
        }
        InlineEvent::ImageStartRef { def_index } => {
            if let Some(state) = image_state.as_mut() {
                state.depth += 1;
            } else if let Some(def) = link_refs.get(*def_index as usize) {
                writer.write_str("<img src=\"");
                writer.write_link_url(&def.url);
                writer.write_str("\" alt=\"");
                *image_state = Some(ImageState {
                    title_range: None,
                    title_bytes: def.title.clone(),
                    depth: 1,
                });
            }
        }
        InlineEvent::ImageEnd => {
            if let Some(state) = image_state.as_mut() {
                state.depth -= 1;
                // Only close when we exit the outermost image
                if state.depth == 0 {
                    writer.write_str("\"");
                    // Add title attribute if present
                    let title_range = state.title_range;
                    let title_bytes = state.title_bytes.clone();
                    *image_state = None;
                    if let Some(bytes) = title_bytes {
                        writer.write_str(" title=\"");
                        writer.write_link_title(&bytes);
                        writer.write_str("\"");
                    } else if let Some(title_range) = title_range {
                        writer.write_str(" title=\"");
                        writer.write_link_title(title_range.slice(text));
                        writer.write_str("\"");
                    }
                    writer.write_str(" />");
                }
            }
        }
        InlineEvent::AutolinkLiteral { url, kind } => {
            use crate::inline::AutolinkLiteralKind;
            if in_image {
                writer.write_escaped_attr(url.slice(text));
            } else {
                writer.write_str("<a href=\"");
                match kind {
                    AutolinkLiteralKind::Url => {
                        writer.write_link_url(url.slice(text));
                    }
                    AutolinkLiteralKind::Www => {
                        writer.write_str("http://");
                        writer.write_link_url(url.slice(text));
                    }
                    AutolinkLiteralKind::Email => {
                        writer.write_str("mailto:");
                        writer.write_link_url(url.slice(text));
                    }
                }
                writer.write_str("\">");
                writer.write_escaped_text(url.slice(text));
                writer.write_str("</a>");
            }
        }
        InlineEvent::Autolink { url, is_email } => {
            // In image alt text, just output the URL as plain text
            if in_image {
                writer.write_escaped_attr(url.slice(text));
            } else {
                writer.write_str("<a href=\"");
                if *is_email {
                    writer.write_str("mailto:");
                }
                // URL-encode special chars then HTML-escape
                writer.write_url_encoded(url.slice(text));
                writer.write_str("\">");
                // Display text is shown as-is (with HTML escaping)
                writer.write_escaped_text(url.slice(text));
                writer.write_str("</a>");
            }
        }
        InlineEvent::Html(range) => {
            if in_image {
                writer.write_escaped_attr(range.slice(text));
            } else if filter_html {
                writer.write_html_filtered(range.slice(text));
            } else {
                writer.write_bytes(range.slice(text));
            }
        }
        InlineEvent::SoftBreak => {
            // In image alt text, use space instead of newline
            if in_image {
                writer.write_str(" ");
            } else {
                writer.write_str("\n");
            }
        }
        InlineEvent::HardBreak => {
            // In image alt text, use space instead of <br />
            if in_image {
                writer.write_str(" ");
            } else {
                writer.write_str("<br />\n");
            }
        }
        InlineEvent::EscapedChar(ch) => {
            // Write the escaped character (the actual char, not the backslash)
            let bytes = [*ch];
            if in_image {
                writer.write_escaped_attr(&bytes);
            } else {
                writer.write_escaped_text(&bytes);
            }
        }
        InlineEvent::FootnoteRef { def_index } => {
            if !in_image {
                if let Some(fn_store) = footnote_store {
                    let def_idx = *def_index as usize;
                    // Assign sequential number based on first-appearance order
                    let number =
                        if let Some(pos) = footnote_order.iter().position(|&i| i == def_idx) {
                            pos + 1
                        } else {
                            footnote_order.push(def_idx);
                            footnote_order.len()
                        };
                    if let Some(def) = fn_store.get(def_idx) {
                        writer.write_str("<sup><a href=\"#user-content-fn-");
                        writer.write_string(&def.label);
                        writer.write_str("\" id=\"user-content-fnref-");
                        writer.write_string(&def.label);
                        writer.write_str("\" data-footnote-ref>");
                        let num_str = number.to_string();
                        writer.write_string(&num_str);
                        writer.write_str("</a></sup>");
                    }
                }
            }
        }
        InlineEvent::MathInline(range) => {
            if in_image {
                writer.write_escaped_attr(range.slice(text));
            } else {
                writer.write_str("<code class=\"language-math math-inline\">");
                let content = range.slice(text);
                for &b in content {
                    if b == b'\n' {
                        writer.write_str(" ");
                    } else if b == b'<' {
                        writer.write_str("&lt;");
                    } else if b == b'>' {
                        writer.write_str("&gt;");
                    } else if b == b'&' {
                        writer.write_str("&amp;");
                    } else if b == b'"' {
                        writer.write_str("&quot;");
                    } else {
                        writer.buffer_mut().push(b);
                    }
                }
                writer.write_str("</code>");
            }
        }
        InlineEvent::MathDisplay(range) => {
            if in_image {
                writer.write_escaped_attr(range.slice(text));
            } else {
                writer.write_str("<code class=\"language-math math-display\">");
                let content = range.slice(text);
                for &b in content {
                    if b == b'\n' {
                        writer.write_str(" ");
                    } else if b == b'<' {
                        writer.write_str("&lt;");
                    } else if b == b'>' {
                        writer.write_str("&gt;");
                    } else if b == b'&' {
                        writer.write_str("&amp;");
                    } else if b == b'"' {
                        writer.write_str("&quot;");
                    } else {
                        writer.buffer_mut().push(b);
                    }
                }
                writer.write_str("</code>");
            }
        }
    }
}

/// Render the footnote section at document end.
#[allow(clippy::too_many_arguments)]
fn render_footnote_section(
    input: &[u8],
    footnote_store: &FootnoteStore,
    footnote_order: &[usize],
    writer: &mut HtmlWriter,
    inline_parser: &mut InlineParser,
    inline_events: &mut Vec<InlineEvent>,
    link_refs: &LinkRefStore,
    options: &Options,
) {
    writer.write_str("<section data-footnotes class=\"footnotes\">\n<ol>\n");

    for (seq_num, &def_idx) in footnote_order.iter().enumerate() {
        let def = match footnote_store.get(def_idx) {
            Some(d) => d,
            None => continue,
        };
        let number = seq_num + 1;

        writer.write_str("<li id=\"user-content-fn-");
        writer.write_string(&def.label);
        writer.write_str("\">\n");

        // Render the footnote's block events
        let fn_events = &def.events;
        let fn_store_ref = Some(footnote_store);
        // We need a separate footnote_order for nested footnote refs inside footnotes
        // but for simplicity, we'll share the same order (GitHub does this too)
        let mut fn_footnote_order: Vec<usize> = Vec::new();

        let mut para_state = ParagraphState::new();
        let mut heading_state = HeadingState::new();
        let mut cell_state = CellState::new();
        let mut tight_list_stack: Vec<(bool, u32)> = Vec::new();
        let mut at_tight_li_start = false;
        let mut need_newline_before_block = false;
        let mut pending_loose_li_newline = false;
        let mut blockquote_depth = 0u32;
        let mut in_table_head = false;
        let mut pending_task = block::TaskState::None;
        let mut fn_heading_id_tracker = HeadingIdTracker::new();
        let mut fn_callout_stack: Vec<Option<block::CalloutType>> = Vec::new();

        // Track if the last event was ParagraphEnd (to insert backref)
        let last_para_end_idx = fn_events
            .iter()
            .rposition(|e| matches!(e, BlockEvent::ParagraphEnd));

        for (i, event) in fn_events.iter().enumerate() {
            // If this is the last ParagraphEnd, we need to inject the backref before closing
            if Some(i) == last_para_end_idx {
                // Flush paragraph content first but don't close the tag yet
                // Actually, we need to render the paragraph content, inject backref, then close
                // The cleanest approach: render normally, then strip the closing </p>\n and re-add with backref

                // Capture writer position before this event
                let pos_before = writer.buffer_mut().len();

                render_block_event(
                    input,
                    event,
                    writer,
                    inline_parser,
                    inline_events,
                    &mut para_state,
                    &mut heading_state,
                    &mut cell_state,
                    &mut tight_list_stack,
                    &mut at_tight_li_start,
                    &mut need_newline_before_block,
                    &mut pending_loose_li_newline,
                    &mut blockquote_depth,
                    &mut in_table_head,
                    &mut pending_task,
                    link_refs,
                    fn_store_ref,
                    &mut fn_footnote_order,
                    &mut fn_heading_id_tracker,
                    &mut fn_callout_stack,
                    options,
                );

                // Check if the output ends with </p>\n and inject backref before it
                let buf = writer.buffer_mut();
                if buf.len() >= pos_before + 5 && buf.ends_with(b"</p>\n") {
                    let insert_pos = buf.len() - 5; // before </p>\n
                    let backref = format!(
                        " <a href=\"#user-content-fnref-{}\" class=\"data-footnote-backref\" aria-label=\"Back to reference {}\">\u{21a9}</a>",
                        def.label, number
                    );
                    let backref_bytes = backref.as_bytes();
                    let suffix = buf[insert_pos..].to_vec();
                    buf.truncate(insert_pos);
                    buf.extend_from_slice(backref_bytes);
                    buf.extend_from_slice(&suffix);
                }
                continue;
            }

            render_block_event(
                input,
                event,
                writer,
                inline_parser,
                inline_events,
                &mut para_state,
                &mut heading_state,
                &mut cell_state,
                &mut tight_list_stack,
                &mut at_tight_li_start,
                &mut need_newline_before_block,
                &mut pending_loose_li_newline,
                &mut blockquote_depth,
                &mut in_table_head,
                &mut pending_task,
                link_refs,
                fn_store_ref,
                &mut fn_footnote_order,
                &mut fn_heading_id_tracker,
                &mut fn_callout_stack,
                options,
            );
        }

        writer.write_str("</li>\n");
    }

    writer.write_str("</ol>\n</section>\n");
}

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

    #[test]
    fn test_basic_paragraph() {
        let html = to_html("Hello, world!");
        assert_eq!(html, "<p>Hello, world!</p>\n");
    }

    #[test]
    fn test_paragraph_escaping() {
        let html = to_html("<script>alert('xss')</script>");
        // GFM disallowed raw HTML: <script> is filtered by default.
        assert_eq!(html, "&lt;script>alert('xss')&lt;/script>");
    }

    #[test]
    fn test_heading_h1() {
        let html = to_html("# Hello");
        assert!(html.contains("Hello</h1>"));
    }

    #[test]
    fn test_heading_h2() {
        let html = to_html("## World");
        assert!(html.contains("World</h2>"));
    }

    #[test]
    fn test_heading_all_levels() {
        for level in 1..=6 {
            let input = format!("{} Heading", "#".repeat(level));
            let html = to_html(&input);
            assert!(
                html.contains(&format!("Heading</h{level}>")),
                "Failed for level {level}: {html}"
            );
        }
    }

    #[test]
    fn test_thematic_break() {
        let html = to_html("---");
        assert_eq!(html, "<hr />\n");
    }

    #[test]
    fn test_thematic_break_variants() {
        assert_eq!(to_html("---"), "<hr />\n");
        assert_eq!(to_html("***"), "<hr />\n");
        assert_eq!(to_html("___"), "<hr />\n");
        assert_eq!(to_html("- - -"), "<hr />\n");
        assert_eq!(to_html("----------"), "<hr />\n");
    }

    #[test]
    fn test_multiple_paragraphs() {
        let html = to_html("First\n\nSecond");
        assert!(html.contains("<p>First</p>"));
        assert!(html.contains("<p>Second</p>"));
    }

    #[test]
    fn test_heading_and_paragraph() {
        let html = to_html("# Title\n\nContent here.");
        assert!(html.contains("Title</h1>"));
        assert!(html.contains("<p>Content here.</p>"));
    }

    #[test]
    fn test_heading_with_closing_hashes() {
        let html = to_html("# Hello #");
        assert!(html.contains("Hello</h1>"));
    }

    #[test]
    fn test_complex_document() {
        let input = r#"# Main Title

This is the first paragraph.

## Section 1

More content here.

---

## Section 2

Final paragraph."#;

        let html = to_html(input);

        assert!(html.contains("Main Title</h1>"));
        assert!(html.contains("Section 1</h2>"));
        assert!(html.contains("Section 2</h2>"));
        assert!(html.contains("<hr />"));
        assert!(html.contains("<p>This is the first paragraph.</p>"));
    }

    #[test]
    fn test_multiline_paragraph() {
        let html = to_html("Line 1\nLine 2\nLine 3");
        // All lines should be in the same paragraph
        assert!(html.starts_with("<p>"));
        assert!(html.contains("Line 1"));
        assert!(html.contains("Line 2"));
        assert!(html.contains("Line 3"));
        assert!(html.ends_with("</p>\n"));
    }

    #[test]
    fn test_empty_input() {
        let html = to_html("");
        assert_eq!(html, "");
    }

    #[test]
    fn test_only_whitespace() {
        let html = to_html("   \n\n   ");
        assert_eq!(html, "");
    }

    #[test]
    fn test_to_html_into() {
        let mut buffer = Vec::new();
        to_html_into("# Test", &mut buffer);
        let html = String::from_utf8(buffer).unwrap();
        assert!(html.contains("Test</h1>"));
    }

    // Code block tests

    #[test]
    fn test_code_block_basic() {
        let html = to_html("```\ncode\n```");
        assert!(html.contains("<pre><code>"));
        assert!(html.contains("code"));
        assert!(html.contains("</code></pre>"));
    }

    #[test]
    fn test_code_block_with_language() {
        let html = to_html("```rust\nfn main() {}\n```");
        assert!(html.contains("<pre><code class=\"language-rust\">"));
        assert!(html.contains("fn main() {}"));
    }

    #[test]
    fn test_code_block_escapes_html() {
        let html = to_html("```\n<script>alert('xss')</script>\n```");
        assert!(html.contains("&lt;script&gt;"));
        assert!(!html.contains("<script>"));
    }

    #[test]
    fn test_code_block_multiline() {
        let html = to_html("```\nline1\nline2\n```");
        assert!(html.contains("line1"));
        assert!(html.contains("line2"));
    }

    #[test]
    fn test_code_block_in_document() {
        let input = r#"# Title

Some text.

```python
print("hello")
```

More text."#;
        let html = to_html(input);
        assert!(html.contains("Title</h1>"));
        assert!(html.contains("<p>Some text.</p>"));
        assert!(html.contains("<pre><code class=\"language-python\">"));
        assert!(html.contains("print"));
        assert!(html.contains("<p>More text.</p>"));
    }

    // Tight/loose list tests

    #[test]
    fn test_tight_list_unordered() {
        let html = to_html("- foo\n- bar\n- baz");
        // Tight list: no <p> tags inside list items
        assert!(html.contains("<li>foo</li>"));
        assert!(html.contains("<li>bar</li>"));
        assert!(html.contains("<li>baz</li>"));
        assert!(!html.contains("<li><p>"));
    }

    #[test]
    fn test_loose_list_unordered() {
        let html = to_html("- foo\n\n- bar\n\n- baz");
        // Loose list: <p> tags inside list items (with newline after <li>)
        assert!(html.contains("<li>\n<p>foo</p>"));
        assert!(html.contains("<li>\n<p>bar</p>"));
        assert!(html.contains("<li>\n<p>baz</p>"));
    }

    #[test]
    fn test_tight_list_ordered() {
        let html = to_html("1. first\n2. second\n3. third");
        // Tight list: no <p> tags
        assert!(html.contains("<li>first</li>"));
        assert!(html.contains("<li>second</li>"));
        assert!(html.contains("<li>third</li>"));
        assert!(!html.contains("<li><p>"));
    }

    #[test]
    fn test_loose_list_ordered() {
        let html = to_html("1. first\n\n2. second");
        // Loose list: <p> tags (with newline after <li>)
        assert!(html.contains("<li>\n<p>first</p>"));
        assert!(html.contains("<li>\n<p>second</p>"));
    }

    // Image tests

    #[test]
    fn test_image_basic() {
        let html = to_html("![alt](image.png)");
        // Should have img tag with src and alt
        assert!(html.contains("<img src=\"image.png\""), "Missing img src");
        assert!(html.contains("alt=\"alt\""), "Missing alt attribute");
        // Should NOT have standalone ! before the img tag
        assert!(!html.contains("!<img"), "Stray ! before img tag");
    }

    #[test]
    fn test_image_with_title() {
        let html = to_html("![alt](image.png \"title\")");
        // Should have img tag with title
        assert!(html.contains("<img"), "No img tag found");
        assert!(html.contains("title=\"title\""), "Missing title attribute");
        assert!(!html.contains("!<img"), "Stray ! before img tag");
    }

    #[test]
    fn test_image_in_text() {
        let html = to_html("text before ![img](url) text after");
        // Image should be between text
        assert!(html.contains("text before"));
        assert!(html.contains("<img src=\"url\""));
        assert!(html.contains("text after"));
    }

    #[test]
    fn test_image_with_nested_emphasis() {
        // CommonMark: alt text should be plain text, not HTML
        let html = to_html("![foo *bar*](/url)");
        // Should have alt="foo bar" (plain text, no <em> tags)
        assert!(
            html.contains("alt=\"foo bar\""),
            "Alt text should be plain: {html}"
        );
        assert!(!html.contains("<em>"), "No <em> tags in alt text");
    }

    #[test]
    fn test_image_with_nested_strong() {
        let html = to_html("![foo **bar**](/url)");
        // Should have alt="foo bar" (plain text, no <strong> tags)
        assert!(
            html.contains("alt=\"foo bar\""),
            "Alt text should be plain: {html}"
        );
        assert!(!html.contains("<strong>"), "No <strong> tags in alt text");
    }
}

#[cfg(test)]
mod entity_tests {
    #[test]
    fn test_html_escape_entities() {
        use html_escape::decode_html_entities;

        assert_eq!(decode_html_entities("&auml;").as_ref(), "ä");
        assert_eq!(decode_html_entities("&#228;").as_ref(), "ä");
        assert_eq!(decode_html_entities("&#xE4;").as_ref(), "ä");
        assert_eq!(decode_html_entities("&amp;").as_ref(), "&");
        assert_eq!(decode_html_entities("foo%20b&auml;").as_ref(), "foo%20bä");
    }
}