cmark-writer 0.7.4

A CommonMark writer implementation in Rust for serializing AST nodes to CommonMark format
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
use crate::ast::{HtmlElement, ListItem, Node};
use log;

#[cfg(feature = "gfm")]
use crate::ast::{TableAlignment, TaskListStatus};

use super::{utils, HtmlWriteError, HtmlWriteResult, HtmlWriterOptions};
use html_escape;

/// HTML writer for serializing CommonMark AST nodes to HTML.
///
/// `HtmlWriter` provides a flexible API for generating HTML content from AST nodes. It can be used:
/// - Directly with individual nodes through methods like `write_node`
/// - For building HTML elements programmatically using the tag and attribute methods
/// - As part of the CommonMarkWriter's HTML rendering process
/// - In custom node implementations via the `html_impl=true` attribute
///
/// # Examples
///
/// ## Basic usage
///
/// ```rust
/// use cmark_writer::{HtmlWriter, Node};
///
/// let mut writer = HtmlWriter::new();
/// let para = Node::Paragraph(vec![Node::Text("Hello, world!".to_string())]);
/// writer.write_node(&para).unwrap();
///
/// let output = writer.into_string();
/// assert_eq!(output, "<p>Hello, world!</p>\n");
/// ```
///
/// ## Building HTML elements manually
///
/// ```rust
/// use cmark_writer::HtmlWriter;
///
/// let mut writer = HtmlWriter::new();
///
/// // Create a custom HTML element
/// writer.start_tag("div").unwrap();
/// writer.attribute("class", "container").unwrap();
/// writer.finish_tag().unwrap();
///
/// writer.start_tag("h1").unwrap();
/// writer.finish_tag().unwrap();
/// writer.text("Welcome").unwrap();
/// writer.end_tag("h1").unwrap();
///
/// writer.end_tag("div").unwrap();
///
/// let output = writer.into_string();
/// assert_eq!(output, "<div class=\"container\"><h1>Welcome</h1></div>");
/// ```
#[derive(Debug)]
pub struct HtmlWriter {
    /// Writer options
    pub options: HtmlWriterOptions,
    /// Buffer for storing the output text
    buffer: String,
    /// Whether a tag is currently opened
    tag_opened: bool,
}

impl HtmlWriter {
    /// Creates a new HTML writer with default options.
    pub fn new() -> Self {
        Self::with_options(HtmlWriterOptions::default())
    }

    /// Creates a new HTML writer with the specified options.
    pub fn with_options(options: HtmlWriterOptions) -> Self {
        HtmlWriter {
            options,
            buffer: String::new(),
            tag_opened: false,
        }
    }

    /// Consumes the writer and returns the generated HTML string.
    pub fn into_string(mut self) -> String {
        self.ensure_tag_closed().unwrap();
        self.buffer
    }

    // --- Low-level HTML writing primitives ---

    fn ensure_tag_closed(&mut self) -> HtmlWriteResult<()> {
        if self.tag_opened {
            self.buffer.push('>');
            self.tag_opened = false;
        }
        Ok(())
    }

    fn start_tag_internal(&mut self, tag_name: &str) -> HtmlWriteResult<()> {
        self.ensure_tag_closed()?;
        self.buffer.push('<');
        self.buffer.push_str(tag_name);
        self.tag_opened = true;
        Ok(())
    }

    /// Starts an HTML tag with the given name.
    ///
    /// This is a public wrapper around start_tag_internal.
    pub fn start_tag(&mut self, tag_name: &str) -> HtmlWriteResult<()> {
        self.start_tag_internal(tag_name)
    }

    fn attribute_internal(&mut self, key: &str, value: &str) -> HtmlWriteResult<()> {
        if !self.tag_opened {
            return Err(HtmlWriteError::InvalidHtmlTag(
                "Cannot write attribute: no tag is currently open.".to_string(),
            ));
        }
        self.buffer.push(' ');
        self.buffer.push_str(key);
        self.buffer.push_str("=\"");
        self.buffer
            .push_str(html_escape::encode_text(value).as_ref());
        self.buffer.push('"');
        Ok(())
    }

    /// Adds an attribute to the currently open tag.
    ///
    /// This is a public wrapper around attribute_internal.
    pub fn attribute(&mut self, key: &str, value: &str) -> HtmlWriteResult<()> {
        self.attribute_internal(key, value)
    }

    fn finish_tag_internal(&mut self) -> HtmlWriteResult<()> {
        if self.tag_opened {
            self.buffer.push('>');
            self.tag_opened = false;
        }
        Ok(())
    }

    /// Finishes the current open tag.
    ///
    /// This is a public wrapper around finish_tag_internal.
    pub fn finish_tag(&mut self) -> HtmlWriteResult<()> {
        self.finish_tag_internal()
    }

    fn end_tag_internal(&mut self, tag_name: &str) -> HtmlWriteResult<()> {
        self.ensure_tag_closed()?;
        self.buffer.push_str("</");
        self.buffer.push_str(tag_name);
        self.buffer.push('>');
        Ok(())
    }

    /// Closes an HTML tag with the given name.
    ///
    /// This is a public wrapper around end_tag_internal.
    pub fn end_tag(&mut self, tag_name: &str) -> HtmlWriteResult<()> {
        self.end_tag_internal(tag_name)
    }

    fn text_internal(&mut self, text: &str) -> HtmlWriteResult<()> {
        self.ensure_tag_closed()?;
        self.buffer
            .push_str(html_escape::encode_text(text).as_ref());
        Ok(())
    }

    /// Writes text content, escaping HTML special characters.
    ///
    /// This is a public wrapper around text_internal.
    pub fn text(&mut self, text: &str) -> HtmlWriteResult<()> {
        self.text_internal(text)
    }

    /// Writes a string to the output, escaping HTML special characters.
    ///
    /// This is an alias for `text` method, provided for compatibility with
    /// the CustomNodeWriter trait interface.
    pub fn write_str(&mut self, s: &str) -> HtmlWriteResult<()> {
        self.text(s)
    }

    fn self_closing_tag_internal(&mut self, tag_name: &str) -> HtmlWriteResult<()> {
        self.ensure_tag_closed()?;
        self.buffer.push('<');
        self.buffer.push_str(tag_name);
        self.buffer.push_str(" />");
        self.tag_opened = false;
        Ok(())
    }

    fn finish_self_closing_tag_internal(&mut self) -> HtmlWriteResult<()> {
        if !self.tag_opened {
            return Err(HtmlWriteError::InvalidHtmlTag(
                "Cannot finish self-closing tag: no tag is currently open.".to_string(),
            ));
        }
        self.buffer.push_str(" />");
        self.tag_opened = false;
        Ok(())
    }

    /// Finishes the current open tag as a self-closing tag.
    ///
    /// This is a public wrapper around finish_self_closing_tag_internal.
    pub fn finish_self_closing_tag(&mut self) -> HtmlWriteResult<()> {
        self.finish_self_closing_tag_internal()
    }

    fn raw_html_internal(&mut self, html: &str) -> HtmlWriteResult<()> {
        self.ensure_tag_closed()?;
        self.buffer.push_str(html);
        Ok(())
    }

    /// Writes raw HTML content directly to the output.
    ///
    /// This method allows adding arbitrary HTML content without escaping.
    /// It should be used with caution as it can introduce security issues
    /// if used with untrusted input.
    pub fn raw_html(&mut self, html: &str) -> HtmlWriteResult<()> {
        self.raw_html_internal(html)
    }

    // --- Main Node Dispatcher ---

    /// Writes an AST `Node` to HTML using the configured options.
    pub fn write_node(&mut self, node: &Node) -> HtmlWriteResult<()> {
        match node {
            Node::Document(children) => self.write_document_node(children),
            Node::Paragraph(children) => self.write_paragraph_node(children),
            Node::Text(text) => self.write_text_node(text),
            Node::Heading { level, content, .. } => self.write_heading_node(*level, content),
            Node::Emphasis(children) => self.write_emphasis_node(children),
            Node::Strong(children) => self.write_strong_node(children),
            Node::ThematicBreak => self.write_thematic_break_node(),
            Node::InlineCode(code) => self.write_inline_code_node(code),
            Node::CodeBlock {
                language, content, ..
            } => self.write_code_block_node(language, content),
            Node::HtmlBlock(block_content) => self.write_html_block_node(block_content),
            Node::HtmlElement(element) => self.write_html_element_node(element),
            Node::SoftBreak => self.write_soft_break_node(),
            Node::HardBreak => self.write_hard_break_node(),
            Node::Link {
                url,
                title,
                content,
            } => self.write_link_node(url, title, content),
            Node::Image { url, title, alt } => self.write_image_node(url, title, alt),
            Node::BlockQuote(children) => self.write_blockquote_node(children),
            Node::OrderedList { start, items } => self.write_ordered_list_node(*start, items),
            Node::UnorderedList(items) => self.write_unordered_list_node(items),
            #[cfg(feature = "gfm")]
            Node::Strikethrough(children) => self.write_strikethrough_node(children),
            Node::Table {
                headers,
                #[cfg(feature = "gfm")]
                alignments,
                rows,
            } => self.write_table_node(
                headers,
                #[cfg(feature = "gfm")]
                alignments,
                rows,
            ),
            Node::Autolink { url, is_email } => self.write_autolink_node(url, *is_email),
            #[cfg(feature = "gfm")]
            Node::ExtendedAutolink(url) => self.write_extended_autolink_node(url),
            Node::LinkReferenceDefinition { .. } => Ok(()), // Definitions are not rendered in final HTML
            Node::ReferenceLink { label, content } => {
                self.write_reference_link_node(label, content)
            }
            Node::Custom(custom_node) => {
                // Call the CustomNode's html_write method, which handles the HTML rendering
                custom_node.html_write(self)
            }
            // Fallback for node types not handled, especially if GFM is off and GFM nodes appear
            #[cfg(not(feature = "gfm"))]
            Node::ExtendedAutolink(url) => {
                // Handle GFM specific nodes explicitly if feature is off
                log::warn!("ExtendedAutolink encountered but GFM feature is not enabled. Rendering as text: {}", url);
                self.text_internal(url)
            }
            // All node types are handled above, but keeping this for future extensibility
            #[allow(unreachable_patterns)]
            _ => Err(HtmlWriteError::UnsupportedNodeType(format!("{:?}", node))),
        }
    }

    // --- Node-Specific Writing Methods (Internal) ---

    fn write_document_node(&mut self, children: &[Node]) -> HtmlWriteResult<()> {
        for child in children {
            self.write_node(child)?;
            // Optionally add newlines between major block elements in HTML source
            if child.is_block() && !self.buffer.ends_with('\n') {
                // self.raw_html_internal("\n")?;
            }
        }
        Ok(())
    }

    fn write_paragraph_node(&mut self, children: &[Node]) -> HtmlWriteResult<()> {
        self.start_tag_internal("p")?;
        self.finish_tag_internal()?;
        for child in children {
            self.write_node(child)?;
        }
        self.end_tag_internal("p")?;
        self.raw_html_internal("\n")?;
        Ok(())
    }

    fn write_text_node(&mut self, text: &str) -> HtmlWriteResult<()> {
        self.text_internal(text)
    }

    fn write_heading_node(&mut self, level: u8, content: &[Node]) -> HtmlWriteResult<()> {
        let tag_name = format!("h{}", level.clamp(1, 6));
        self.start_tag_internal(&tag_name)?;
        self.finish_tag_internal()?;
        for child in content {
            self.write_node(child)?;
        }
        self.end_tag_internal(&tag_name)?;
        self.raw_html_internal("\n")?;
        Ok(())
    }

    fn write_emphasis_node(&mut self, children: &[Node]) -> HtmlWriteResult<()> {
        self.start_tag_internal("em")?;
        self.finish_tag_internal()?;
        for child in children {
            self.write_node(child)?;
        }
        self.end_tag_internal("em")?;
        Ok(())
    }

    fn write_strong_node(&mut self, children: &[Node]) -> HtmlWriteResult<()> {
        self.start_tag_internal("strong")?;
        self.finish_tag_internal()?;
        for child in children {
            self.write_node(child)?;
        }
        self.end_tag_internal("strong")?;
        Ok(())
    }

    fn write_thematic_break_node(&mut self) -> HtmlWriteResult<()> {
        self.self_closing_tag_internal("hr")?;
        self.raw_html_internal("\n")?;
        Ok(())
    }

    fn write_inline_code_node(&mut self, code: &str) -> HtmlWriteResult<()> {
        self.start_tag_internal("code")?;
        self.finish_tag_internal()?;
        self.text_internal(code)?;
        self.end_tag_internal("code")?;
        Ok(())
    }

    fn write_code_block_node(
        &mut self,
        language: &Option<String>,
        content: &str,
    ) -> HtmlWriteResult<()> {
        self.start_tag_internal("pre")?;
        self.finish_tag_internal()?; // Finish <pre> before potentially adding attributes to <code> or <span>
        self.start_tag_internal("code")?;
        if let Some(prefix) = &self.options.code_block_language_class_prefix {
            if let Some(lang) = language {
                if !lang.is_empty() {
                    self.attribute_internal("class", &format!("{}{}", prefix, lang.trim()))?;
                }
            }
        }
        self.finish_tag_internal()?;
        self.text_internal(content)?;
        self.end_tag_internal("code")?;
        self.end_tag_internal("pre")?;
        self.raw_html_internal("\n")?;
        Ok(())
    }

    fn write_html_block_node(&mut self, block_content: &str) -> HtmlWriteResult<()> {
        self.raw_html_internal(block_content)?;
        if !block_content.ends_with('\n') {
            self.raw_html_internal("\n")?;
        }
        Ok(())
    }

    fn write_html_element_node(&mut self, element: &HtmlElement) -> HtmlWriteResult<()> {
        #[cfg(feature = "gfm")]
        if self.options.enable_gfm
            && self
                .options
                .gfm_disallowed_html_tags
                .iter()
                .any(|tag| tag.eq_ignore_ascii_case(&element.tag))
        {
            log::debug!("GFM: Textualizing disallowed HTML tag: <{}>", element.tag);
            self.textualize_full_element_node(element)?;
            return Ok(());
        }

        if !utils::is_safe_tag_name(&element.tag) {
            if self.options.strict {
                return Err(HtmlWriteError::InvalidHtmlTag(element.tag.clone()));
            } else {
                log::warn!(
                    "Invalid HTML tag name '{}' encountered. Textualizing in non-strict mode.",
                    element.tag
                );
                self.textualize_full_element_node(element)?;
                return Ok(());
            }
        }

        self.start_tag_internal(&element.tag)?;
        for attr in &element.attributes {
            if !utils::is_safe_attribute_name(&attr.name) {
                if self.options.strict {
                    return Err(HtmlWriteError::InvalidHtmlAttribute(attr.name.clone()));
                } else {
                    log::warn!("Invalid HTML attribute name '{}' in tag '{}'. Textualizing attribute in non-strict mode.", attr.name, element.tag);
                    // Simple textualization of the attribute itself
                    self.buffer.push(' ');
                    self.buffer.push_str(&attr.name);
                    self.buffer.push_str("=\"");
                    self.buffer
                        .push_str(html_escape::encode_text(&attr.value).as_ref()); // Attribute value should be escaped
                    self.buffer.push('"');
                    continue;
                }
            }
            self.attribute_internal(&attr.name, &attr.value)?;
        }

        if element.self_closing {
            self.finish_self_closing_tag_internal()?;
        } else {
            self.finish_tag_internal()?;
            for child in &element.children {
                self.write_node(child)?;
            }
            self.end_tag_internal(&element.tag)?;
        }
        // Determine if a newline is appropriate based on whether the original HTML tag is block or inline.
        // This information isn't readily available, so we might add a newline if it's a common block tag.
        // For now, omitting conditional newline for brevity.
        Ok(())
    }

    fn textualize_full_element_node(&mut self, element: &HtmlElement) -> HtmlWriteResult<()> {
        self.text_internal("<")?;
        self.text_internal(&element.tag)?;
        for attr in &element.attributes {
            self.text_internal(" ")?;
            self.text_internal(&attr.name)?;
            self.text_internal("=")?;
            self.text_internal("\"")?;
            self.text_internal(&attr.value)?; // Value is part of the text, so it's escaped by text_internal
            self.text_internal("\"")?;
        }
        if element.self_closing {
            self.text_internal(" />")?;
        } else {
            self.text_internal(">")?;
            for child in &element.children {
                self.write_node(child)?; // Children are rendered normally
            }
            self.text_internal("</")?;
            self.text_internal(&element.tag)?;
            self.text_internal(">")?;
        }
        Ok(())
    }

    fn write_soft_break_node(&mut self) -> HtmlWriteResult<()> {
        // Soft line breaks in CommonMark are rendered as a newline in HTML source,
        // or a space if the line break was for wrapping.
        // Most browsers will treat a newline in HTML as a single space.
        self.raw_html_internal("\n")
    }

    fn write_hard_break_node(&mut self) -> HtmlWriteResult<()> {
        self.self_closing_tag_internal("br")?;
        self.raw_html_internal("\n")
    }

    fn write_link_node(
        &mut self,
        url: &str,
        title: &Option<String>,
        content: &[Node],
    ) -> HtmlWriteResult<()> {
        self.start_tag_internal("a")?;
        self.attribute_internal("href", url)?;
        if let Some(title_str) = title {
            if !title_str.is_empty() {
                self.attribute_internal("title", title_str)?;
            }
        }
        self.finish_tag_internal()?;
        for child in content {
            self.write_node(child)?;
        }
        self.end_tag_internal("a")?;
        Ok(())
    }

    fn write_image_node(
        &mut self,
        url: &str,
        title: &Option<String>,
        alt: &[Node],
    ) -> HtmlWriteResult<()> {
        self.start_tag_internal("img")?;
        self.attribute_internal("src", url)?;
        let mut alt_text_buffer = String::new();
        render_nodes_to_plain_text(alt, &mut alt_text_buffer, &self.options);
        self.attribute_internal("alt", &alt_text_buffer)?;
        if let Some(title_str) = title {
            if !title_str.is_empty() {
                self.attribute_internal("title", title_str)?;
            }
        }
        self.finish_self_closing_tag_internal()?;
        Ok(())
    }

    fn write_blockquote_node(&mut self, children: &[Node]) -> HtmlWriteResult<()> {
        self.start_tag_internal("blockquote")?;
        self.finish_tag_internal()?;
        self.raw_html_internal("\n")?;
        for child in children {
            self.write_node(child)?;
        }
        self.end_tag_internal("blockquote")?;
        self.raw_html_internal("\n")?;
        Ok(())
    }

    fn write_list_item_node_content(&mut self, item_content: &[Node]) -> HtmlWriteResult<()> {
        // This is a simplified handling. CommonMark's "tight" vs "loose" list rules
        // determine if paragraph tags are used inside <li> for paragraphs.
        // If the first/only child of <li> content is a paragraph, and it's a tight list,
        // the <p> tags are often omitted.
        // This implementation will render <p> if Node::Paragraph is present.
        let mut add_newline_before_next_child = false;
        for child_node in item_content.iter() {
            if add_newline_before_next_child {
                self.raw_html_internal("\n")?;
                add_newline_before_next_child = false;
            }
            self.write_node(child_node)?;
            if child_node.is_block() {
                add_newline_before_next_child = true;
            }
        }
        Ok(())
    }

    fn write_list_item_node(&mut self, item: &ListItem) -> HtmlWriteResult<()> {
        self.start_tag_internal("li")?;

        #[cfg(feature = "gfm")]
        if self.options.enable_gfm {
            if let ListItem::Task { status, .. } = item {
                let class_name = if *status == TaskListStatus::Checked {
                    "task-list-item task-list-item-checked"
                } else {
                    "task-list-item" // GFM spec doesn't mandate a specific class for unchecked, just for checked.
                                     // Some renderers use task-list-item-unchecked.
                };
                self.attribute_internal("class", class_name)?;
            }
        }
        self.finish_tag_internal()?; // Finish <li> tag

        let content = match item {
            ListItem::Unordered { content } => content,
            ListItem::Ordered { content, .. } => content,
            #[cfg(feature = "gfm")]
            ListItem::Task { content, .. } => content,
        };

        #[cfg(feature = "gfm")]
        if self.options.enable_gfm {
            if let ListItem::Task { status, .. } = item {
                self.start_tag_internal("input")?;
                self.attribute_internal("type", "checkbox")?;
                self.attribute_internal("disabled", "")?; // GFM task list items are disabled
                if *status == TaskListStatus::Checked {
                    self.attribute_internal("checked", "")?;
                }
                self.finish_self_closing_tag_internal()?;
                self.raw_html_internal(" ")?; // Space after checkbox
            }
        }
        self.write_list_item_node_content(content)?;
        self.end_tag_internal("li")?;
        self.raw_html_internal("\n")?;
        Ok(())
    }

    fn write_ordered_list_node(&mut self, start: u32, items: &[ListItem]) -> HtmlWriteResult<()> {
        self.start_tag_internal("ol")?;
        if start != 1 {
            self.attribute_internal("start", &start.to_string())?;
        }
        self.finish_tag_internal()?;
        self.raw_html_internal("\n")?;
        for item in items {
            self.write_list_item_node(item)?;
        }
        self.end_tag_internal("ol")?;
        self.raw_html_internal("\n")?;
        Ok(())
    }

    fn write_unordered_list_node(&mut self, items: &[ListItem]) -> HtmlWriteResult<()> {
        self.start_tag_internal("ul")?;
        self.finish_tag_internal()?;
        self.raw_html_internal("\n")?;
        for item in items {
            self.write_list_item_node(item)?;
        }
        self.end_tag_internal("ul")?;
        self.raw_html_internal("\n")?;
        Ok(())
    }

    #[cfg(feature = "gfm")]
    fn write_strikethrough_node(&mut self, children: &[Node]) -> HtmlWriteResult<()> {
        if !self.options.enable_gfm {
            // If GFM is disabled (e.g. via a more granular gfm_strikethrough option if added),
            // render content as is. This case should ideally be guarded by options check.
            log::warn!("Strikethrough node encountered but GFM (or GFM strikethrough) is not enabled. Rendering content as plain.");
            for child in children {
                self.write_node(child)?;
            }
            return Ok(());
        }
        self.start_tag_internal("del")?;
        self.finish_tag_internal()?;
        for child in children {
            self.write_node(child)?;
        }
        self.end_tag_internal("del")?;
        Ok(())
    }

    fn write_table_node(
        &mut self,
        headers: &[Node],
        #[cfg(feature = "gfm")] alignments: &[TableAlignment],
        rows: &[Vec<Node>],
    ) -> HtmlWriteResult<()> {
        self.start_tag_internal("table")?;
        self.finish_tag_internal()?;
        self.raw_html_internal("\n")?;

        // Table Head
        self.start_tag_internal("thead")?;
        self.finish_tag_internal()?;
        self.raw_html_internal("\n")?;
        self.start_tag_internal("tr")?;
        self.finish_tag_internal()?;
        self.raw_html_internal("\n")?;

        // Write header cells
        #[cfg(feature = "gfm")]
        for (col_index, header_cell) in headers.iter().enumerate() {
            self.start_tag_internal("th")?;

            // Apply alignment styles if GFM is enabled
            if self.options.enable_gfm && col_index < alignments.len() {
                match alignments[col_index] {
                    TableAlignment::Left => {
                        self.attribute_internal("style", "text-align: left;")?;
                    }
                    TableAlignment::Center => {
                        self.attribute_internal("style", "text-align: center;")?;
                    }
                    TableAlignment::Right => {
                        self.attribute_internal("style", "text-align: right;")?;
                    }
                    TableAlignment::None => {}
                }
            }

            self.finish_tag_internal()?;
            self.write_node(header_cell)?;
            self.end_tag_internal("th")?;
            self.raw_html_internal("\n")?;
        }

        #[cfg(not(feature = "gfm"))]
        for header_cell in headers.iter() {
            self.start_tag_internal("th")?;
            self.finish_tag_internal()?;
            self.write_node(header_cell)?;
            self.end_tag_internal("th")?;
            self.raw_html_internal("\n")?;
        }
        self.end_tag_internal("tr")?;
        self.raw_html_internal("\n")?;
        self.end_tag_internal("thead")?;
        self.raw_html_internal("\n")?;

        // Table Body
        self.start_tag_internal("tbody")?;
        self.finish_tag_internal()?;
        self.raw_html_internal("\n")?;

        // Process each row
        for row_cells in rows {
            self.start_tag_internal("tr")?;
            self.finish_tag_internal()?;
            self.raw_html_internal("\n")?;

            // Process cells with alignment support when GFM is enabled
            #[cfg(feature = "gfm")]
            for (col_index, cell) in row_cells.iter().enumerate() {
                self.start_tag_internal("td")?;

                // Apply alignment styles if GFM is enabled
                if self.options.enable_gfm && col_index < alignments.len() {
                    match alignments[col_index] {
                        TableAlignment::Left => {
                            self.attribute_internal("style", "text-align: left;")?;
                        }
                        TableAlignment::Center => {
                            self.attribute_internal("style", "text-align: center;")?;
                        }
                        TableAlignment::Right => {
                            self.attribute_internal("style", "text-align: right;")?;
                        }
                        TableAlignment::None => {}
                    }
                }

                self.finish_tag_internal()?;
                self.write_node(cell)?;
                self.end_tag_internal("td")?;
                self.raw_html_internal("\n")?;
            }

            // Process cells normally when GFM is not enabled
            #[cfg(not(feature = "gfm"))]
            for cell in row_cells.iter() {
                self.start_tag_internal("td")?;
                self.finish_tag_internal()?;
                self.write_node(cell)?;
                self.end_tag_internal("td")?;
                self.raw_html_internal("\n")?;
            }

            self.end_tag_internal("tr")?;
            self.raw_html_internal("\n")?;
        }

        self.end_tag_internal("tbody")?;
        self.raw_html_internal("\n")?;
        self.end_tag_internal("table")?;
        self.raw_html_internal("\n")?;
        Ok(())
    }

    fn write_autolink_node(&mut self, url: &str, is_email: bool) -> HtmlWriteResult<()> {
        self.start_tag_internal("a")?;
        let href = if is_email && !url.starts_with("mailto:") {
            format!("mailto:{}", url)
        } else {
            url.to_string()
        };
        self.attribute_internal("href", &href)?;
        self.finish_tag_internal()?;
        self.text_internal(url)?;
        self.end_tag_internal("a")?;
        Ok(())
    }

    #[cfg(feature = "gfm")]
    fn write_extended_autolink_node(&mut self, url: &str) -> HtmlWriteResult<()> {
        if !self.options.enable_gfm {
            // Or a more specific gfm_autolinks option
            log::warn!("ExtendedAutolink node encountered but GFM (or GFM autolinks) is not enabled. Rendering as plain text.");
            self.text_internal(url)?;
            return Ok(());
        }
        self.start_tag_internal("a")?;
        self.attribute_internal("href", url)?; // Assumes URL is already a valid href
        self.finish_tag_internal()?;
        self.text_internal(url)?;
        self.end_tag_internal("a")?;
        Ok(())
    }

    fn write_reference_link_node(&mut self, label: &str, content: &[Node]) -> HtmlWriteResult<()> {
        // HTML rendering expects links to be resolved. If a ReferenceLink node is still present,
        // it means resolution failed or wasn't performed.
        // CommonMark dictates rendering the source text.
        if self.options.strict {
            return Err(HtmlWriteError::UnsupportedNodeType(format!(
                "Unresolved reference link '[{}{}]' found in strict mode. Pre-resolve links for HTML output.",
                render_nodes_to_plain_text_string(content, &self.options), // Get text of content
                label
            )));
        }

        log::warn!(
            "Unresolved reference link for label '{}'. Rendering as plain text.",
            label
        );
        // Render as plain text: [content][label] or [label]
        self.text_internal("[")?;
        let content_text = render_nodes_to_plain_text_string(content, &self.options);
        if content.is_empty() || content_text == label {
            // Handle [label] and [label][]
            self.text_internal(label)?;
        } else {
            // Handle [text][label]
            // In this case, `content` is rendered as its textual representation inside the first brackets
            // This might mean rendering resolved inline nodes within `content` as text.
            for node_in_content in content {
                self.write_node(node_in_content)?; // This will render HTML if content has e.g. <em>
            }
        }
        self.text_internal("]")?; // Closing first bracket set
                                  // If it's not a collapsed reference `[label]` and not `[text][]` (where label is implicitly text)
                                  // then we need the `[label]` part.
                                  // Shortcut `[label]` is already handled by `content_text == label`.
                                  // Full `[text][label]` requires the second bracket.
                                  // Collapsed `[text][]` implies label is derived from text, also handled.
                                  // This logic can be tricky; CommonMark source rendering is the safest fallback.
        if !(content_text == label && content.len() == 1 && matches!(content[0], Node::Text(_))) {
            // Avoid double [label][label] if content was just Node::Text(label)
            if !(content.is_empty() && label.is_empty()) {
                // Avoids `[][]` if both are empty (unlikely)
                // This part ensures `[label]` for `[text][label]` or `[label][]` forms
                // if content is not simply the label text itself.
                let is_explicit_full_or_collapsed_form = !content.is_empty(); // e.g. [foo][bar] or [baz][]
                if is_explicit_full_or_collapsed_form {
                    self.text_internal("[")?;
                    self.text_internal(label)?; // The actual reference label
                    self.text_internal("]")?;
                }
            }
        }
        Ok(())
    }
}

impl Default for HtmlWriter {
    fn default() -> Self {
        Self::new()
    }
}

/// Helper function to render a slice of AST nodes to a plain text string.
/// Used for 'alt' text in images or for textual representation of link content.
fn render_nodes_to_plain_text(nodes: &[Node], buffer: &mut String, _options: &HtmlWriterOptions) {
    for node in nodes {
        match node {
            Node::Text(text) => buffer.push_str(text),
            Node::Emphasis(children) | Node::Strong(children) => {
                render_nodes_to_plain_text(children, buffer, _options);
            }
            #[cfg(feature = "gfm")]
            Node::Strikethrough(children) => {
                render_nodes_to_plain_text(children, buffer, _options);
            }
            Node::Link { content, .. } => render_nodes_to_plain_text(content, buffer, _options),
            Node::Image { alt, .. } => render_nodes_to_plain_text(alt, buffer, _options), // Recursively get alt text
            Node::InlineCode(code) => buffer.push_str(code),
            Node::SoftBreak | Node::HardBreak => buffer.push(' '), // Represent breaks as spaces in alt text
            Node::HtmlElement(element) => {
                // Strip HTML tags, but render their text content
                render_nodes_to_plain_text(&element.children, buffer, _options);
            }
            Node::Autolink { url, .. } | Node::ExtendedAutolink(url) => buffer.push_str(url),
            // Block elements are generally not expected in contexts like 'alt' text,
            // but if they are, extract their text content.
            Node::Paragraph(children)
            | Node::BlockQuote(children)
            | Node::Heading {
                content: children, ..
            } => {
                render_nodes_to_plain_text(children, buffer, _options);
                buffer.push(' '); // Add a space after block content for readability
            }
            _ => {} // Ignore other node types (e.g., ThematicBreak, Table, List) for plain text rendering.
        }
    }
}

/// Convenience wrapper for `render_nodes_to_plain_text` that returns a String.
fn render_nodes_to_plain_text_string(nodes: &[Node], options: &HtmlWriterOptions) -> String {
    let mut s = String::new();
    render_nodes_to_plain_text(nodes, &mut s, options);
    s
}

// Example of how CustomNode's html_write might be expected by the HtmlWriter:
// (This would be part of the CustomNode trait definition and its implementations)
// pub trait CustomNode {
//     // ... other methods ...
//     fn html_write(&self, options: &HtmlRenderOptions) -> Result<String, String>; // String for error msg
// }