cmark-writer 0.8.0

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
//! Node definitions for the CommonMark AST.

use super::html::HtmlElement;
use crate::traits::CustomNode;
use ecow::EcoString;
use std::boxed::Box;

/// Code block type according to CommonMark specification
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum CodeBlockType {
    /// Indented code block - composed of one or more indented chunks, each preceded by four or more spaces
    Indented,
    /// Fenced code block - surrounded by backtick or tilde fences
    #[default]
    Fenced,
}

/// Heading type according to CommonMark specification
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum HeadingType {
    /// ATX Type - Beginning with #
    #[default]
    Atx,
    /// Setext Type - Underlined or overlined text
    Setext,
}

/// Table column alignment options for GFM tables
#[cfg(feature = "gfm")]
#[derive(Debug, Clone, PartialEq, Default)]
pub enum TableAlignment {
    /// Left alignment (default)
    #[default]
    Left,
    /// Center alignment
    Center,
    /// Right alignment
    Right,
    /// No specific alignment specified
    None,
}

/// Task list item status for GFM task lists
#[cfg(feature = "gfm")]
#[derive(Debug, Clone, PartialEq)]
pub enum TaskListStatus {
    /// Checked/completed task
    Checked,
    /// Unchecked/incomplete task
    Unchecked,
}

/// Main node type, representing an element in a CommonMark document
#[derive(Debug)]
pub enum Node {
    /// Root document node, contains child nodes
    Document(Vec<Node>),

    // Leaf blocks
    // Thematic breaks
    /// Thematic break (horizontal rule)
    ThematicBreak,

    // ATX headings & Setext headings
    /// Heading, contains level (1-6) and inline content
    Heading {
        /// Heading level, 1-6
        level: u8,
        /// Heading content, containing inline elements
        content: Vec<Node>,
        /// Heading type (ATX or Setext)
        heading_type: HeadingType,
    },

    // Indented code blocks & Fenced code blocks
    /// Code block, containing optional language identifier and content
    CodeBlock {
        /// Optional language identifier (None for indented code blocks, Some for fenced code blocks)
        language: Option<EcoString>,
        /// Code content
        content: EcoString,
        /// The type of code block (Indented or Fenced)
        block_type: CodeBlockType,
    },

    // HTML blocks
    /// HTML block
    HtmlBlock(EcoString),

    // Link reference definitions
    /// Link reference definition
    LinkReferenceDefinition {
        /// Link label (used for reference)
        label: EcoString,
        /// Link destination URL
        destination: EcoString,
        /// Optional link title
        title: Option<EcoString>,
    },

    // Paragraphs
    /// Paragraph node, containing inline elements
    Paragraph(Vec<Node>),

    // Blank lines - typically handled during parsing, not represented in AST

    // Container blocks
    // Block quotes
    /// Block quote, containing any block-level elements
    BlockQuote(Vec<Node>),

    // & List items and Lists
    /// Ordered list, containing starting number and list items
    OrderedList {
        /// List starting number
        start: u32,
        /// List items
        items: Vec<ListItem>,
    },

    /// Unordered list, containing list items
    UnorderedList(Vec<ListItem>),

    /// Table (extension to CommonMark)
    Table {
        /// Header cells
        headers: Vec<Node>,
        /// Column alignments for the table
        #[cfg(feature = "gfm")]
        alignments: Vec<TableAlignment>,
        /// Table rows, each row containing multiple cells
        rows: Vec<Vec<Node>>,
    },

    // Inlines
    // Code spans
    /// Inline code
    InlineCode(EcoString),

    // Emphasis and strong emphasis
    /// Emphasis (italic)
    Emphasis(Vec<Node>),

    /// Strong emphasis (bold)
    Strong(Vec<Node>),

    /// Strikethrough (GFM extension)
    Strikethrough(Vec<Node>),

    // Links
    /// Link
    Link {
        /// Link URL
        url: EcoString,
        /// Optional link title
        title: Option<EcoString>,
        /// Link text
        content: Vec<Node>,
    },

    /// Reference link
    ReferenceLink {
        /// Link reference label
        label: EcoString,
        /// Link text content (optional, if empty it's a shortcut reference)
        content: Vec<Node>,
    },

    // Images
    /// Image
    Image {
        /// Image URL
        url: EcoString,
        /// Optional image title
        title: Option<EcoString>,
        /// Alternative text, containing inline elements
        alt: Vec<Node>,
    },

    // Autolinks
    /// Autolink (URI or email wrapped in < and >)
    Autolink {
        /// Link URL
        url: EcoString,
        /// Whether this is an email autolink
        is_email: bool,
    },

    /// GFM Extended Autolink (without angle brackets, automatically detected)
    ExtendedAutolink(EcoString),

    // Raw HTML
    /// HTML inline element
    HtmlElement(HtmlElement),

    // Hard line breaks
    /// Hard break (two spaces followed by a line break, or backslash followed by a line break)
    HardBreak,

    // Soft line breaks
    /// Soft break (single line break)
    SoftBreak,

    // Textual content
    /// Plain text
    Text(EcoString),

    /// Custom node that allows users to implement their own writing behavior
    Custom(Box<dyn CustomNode>),
}

impl Default for Node {
    fn default() -> Self {
        Node::Document(vec![])
    }
}

impl Clone for Node {
    fn clone(&self) -> Self {
        match self {
            Node::Document(nodes) => Node::Document(nodes.clone()),
            Node::ThematicBreak => Node::ThematicBreak,
            Node::Heading {
                level,
                content,
                heading_type,
            } => Node::Heading {
                level: *level,
                content: content.clone(),
                heading_type: *heading_type,
            },
            Node::CodeBlock {
                language,
                content,
                block_type,
            } => Node::CodeBlock {
                language: language.clone(),
                content: content.clone(),
                block_type: *block_type,
            },
            Node::HtmlBlock(html) => Node::HtmlBlock(html.clone()),
            Node::LinkReferenceDefinition {
                label,
                destination,
                title,
            } => Node::LinkReferenceDefinition {
                label: label.clone(),
                destination: destination.clone(),
                title: title.clone(),
            },
            Node::Paragraph(content) => Node::Paragraph(content.clone()),
            Node::BlockQuote(content) => Node::BlockQuote(content.clone()),
            Node::OrderedList { start, items } => Node::OrderedList {
                start: *start,
                items: items.clone(),
            },
            Node::UnorderedList(items) => Node::UnorderedList(items.clone()),
            #[cfg(feature = "gfm")]
            Node::Table {
                headers,
                alignments,
                rows,
            } => Node::Table {
                headers: headers.clone(),
                alignments: alignments.clone(),
                rows: rows.clone(),
            },
            #[cfg(not(feature = "gfm"))]
            Node::Table { headers, rows } => Node::Table {
                headers: headers.clone(),
                rows: rows.clone(),
            },
            Node::InlineCode(code) => Node::InlineCode(code.clone()),
            Node::Emphasis(content) => Node::Emphasis(content.clone()),
            Node::Strong(content) => Node::Strong(content.clone()),
            Node::Strikethrough(content) => Node::Strikethrough(content.clone()),
            Node::Link {
                url,
                title,
                content,
            } => Node::Link {
                url: url.clone(),
                title: title.clone(),
                content: content.clone(),
            },
            Node::ReferenceLink { label, content } => Node::ReferenceLink {
                label: label.clone(),
                content: content.clone(),
            },
            Node::Image { url, title, alt } => Node::Image {
                url: url.clone(),
                title: title.clone(),
                alt: alt.clone(),
            },
            Node::Autolink { url, is_email } => Node::Autolink {
                url: url.clone(),
                is_email: *is_email,
            },
            Node::ExtendedAutolink(url) => Node::ExtendedAutolink(url.clone()),
            Node::HtmlElement(element) => Node::HtmlElement(element.clone()),
            Node::HardBreak => Node::HardBreak,
            Node::SoftBreak => Node::SoftBreak,
            Node::Text(text) => Node::Text(text.clone()),
            Node::Custom(_custom) => {
                // 暂时不支持自定义节点的克隆,因为我们简化了设计
                // 用户应该使用 Format trait 而不是直接使用 Custom 节点
                panic!("Custom node cloning not supported in simplified design")
            }
        }
    }
}

impl PartialEq for Node {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Node::Document(a), Node::Document(b)) => a == b,
            (Node::ThematicBreak, Node::ThematicBreak) => true,
            (
                Node::Heading {
                    level: l1,
                    content: c1,
                    heading_type: h1,
                },
                Node::Heading {
                    level: l2,
                    content: c2,
                    heading_type: h2,
                },
            ) => l1 == l2 && c1 == c2 && h1 == h2,
            (
                Node::CodeBlock {
                    language: l1,
                    content: c1,
                    block_type: b1,
                },
                Node::CodeBlock {
                    language: l2,
                    content: c2,
                    block_type: b2,
                },
            ) => l1 == l2 && c1 == c2 && b1 == b2,
            (Node::HtmlBlock(a), Node::HtmlBlock(b)) => a == b,
            (
                Node::LinkReferenceDefinition {
                    label: l1,
                    destination: d1,
                    title: t1,
                },
                Node::LinkReferenceDefinition {
                    label: l2,
                    destination: d2,
                    title: t2,
                },
            ) => l1 == l2 && d1 == d2 && t1 == t2,
            (Node::Paragraph(a), Node::Paragraph(b)) => a == b,
            (Node::BlockQuote(a), Node::BlockQuote(b)) => a == b,
            (
                Node::OrderedList {
                    start: s1,
                    items: i1,
                },
                Node::OrderedList {
                    start: s2,
                    items: i2,
                },
            ) => s1 == s2 && i1 == i2,
            (Node::UnorderedList(a), Node::UnorderedList(b)) => a == b,
            #[cfg(feature = "gfm")]
            (
                Node::Table {
                    headers: h1,
                    alignments: a1,
                    rows: r1,
                },
                Node::Table {
                    headers: h2,
                    alignments: a2,
                    rows: r2,
                },
            ) => h1 == h2 && a1 == a2 && r1 == r2,
            #[cfg(not(feature = "gfm"))]
            (
                Node::Table {
                    headers: h1,
                    rows: r1,
                },
                Node::Table {
                    headers: h2,
                    rows: r2,
                },
            ) => h1 == h2 && r1 == r2,
            (Node::InlineCode(a), Node::InlineCode(b)) => a == b,
            (Node::Emphasis(a), Node::Emphasis(b)) => a == b,
            (Node::Strong(a), Node::Strong(b)) => a == b,
            #[cfg(feature = "gfm")]
            (Node::Strikethrough(a), Node::Strikethrough(b)) => a == b,
            (
                Node::Link {
                    url: u1,
                    title: t1,
                    content: c1,
                },
                Node::Link {
                    url: u2,
                    title: t2,
                    content: c2,
                },
            ) => u1 == u2 && t1 == t2 && c1 == c2,
            (
                Node::ReferenceLink {
                    label: l1,
                    content: c1,
                },
                Node::ReferenceLink {
                    label: l2,
                    content: c2,
                },
            ) => l1 == l2 && c1 == c2,
            (
                Node::Image {
                    url: u1,
                    title: t1,
                    alt: a1,
                },
                Node::Image {
                    url: u2,
                    title: t2,
                    alt: a2,
                },
            ) => u1 == u2 && t1 == t2 && a1 == a2,
            (
                Node::Autolink {
                    url: u1,
                    is_email: e1,
                },
                Node::Autolink {
                    url: u2,
                    is_email: e2,
                },
            ) => u1 == u2 && e1 == e2,
            #[cfg(feature = "gfm")]
            (Node::ExtendedAutolink(a), Node::ExtendedAutolink(b)) => a == b,
            (Node::HtmlElement(a), Node::HtmlElement(b)) => a == b,
            (Node::HardBreak, Node::HardBreak) => true,
            (Node::SoftBreak, Node::SoftBreak) => true,
            (Node::Text(a), Node::Text(b)) => a == b,
            (Node::Custom(a), Node::Custom(b)) => a.eq_box(&**b),
            _ => false,
        }
    }
}

/// List item type
#[derive(Debug, Clone, PartialEq)]
pub enum ListItem {
    /// Unordered list item
    Unordered {
        /// List item content, containing one or more block-level elements
        content: Vec<Node>,
    },
    /// Ordered list item
    Ordered {
        /// Optional item number for ordered lists, allowing manual numbering
        number: Option<u32>,
        /// List item content, containing one or more block-level elements
        content: Vec<Node>,
    },
    /// Task list item (GFM extension)
    #[cfg(feature = "gfm")]
    Task {
        /// Task completion status
        status: TaskListStatus,
        /// List item content, containing one or more block-level elements
        content: Vec<Node>,
    },
}

impl Node {
    /// Check if a node is a block-level node
    pub fn is_block(&self) -> bool {
        matches!(
            self,
            Node::Document(_)
                // Leaf blocks
                | Node::ThematicBreak
                | Node::Heading { .. }
                | Node::CodeBlock { .. }
                | Node::HtmlBlock(_)
                | Node::LinkReferenceDefinition { .. }
                | Node::Paragraph(_)
                // Container blocks
                | Node::BlockQuote(_)
                | Node::OrderedList { .. }
                | Node::UnorderedList(_)
                | Node::Table { .. }

                | Node::Custom(_)
        )
    }

    /// Check if a node is an inline node
    pub fn is_inline(&self) -> bool {
        matches!(
            self,
            // Inlines
            // Code spans
            Node::InlineCode(_)
                // Emphasis and strong emphasis
                | Node::Emphasis(_)
                | Node::Strong(_)
                | Node::Strikethrough(_)
                // Links
                | Node::Link { .. }
                | Node::ReferenceLink { .. }
                // Images
                | Node::Image { .. }
                // Autolinks
                | Node::Autolink { .. }
                | Node::ExtendedAutolink(_)
                // Raw HTML
                | Node::HtmlElement(_)
                // Hard line breaks
                | Node::HardBreak
                // Soft line breaks
                | Node::SoftBreak
                // Textual content
                | Node::Text(_)

                | Node::Custom(_)
        )
    }

    /// Get the type name of the node for debugging and error messages
    pub fn type_name(&self) -> &'static str {
        match self {
            Node::Document(_) => "Document",
            Node::ThematicBreak => "ThematicBreak",
            Node::Heading { .. } => "Heading",
            Node::CodeBlock { .. } => "CodeBlock",
            Node::HtmlBlock(_) => "HtmlBlock",
            Node::LinkReferenceDefinition { .. } => "LinkReferenceDefinition",
            Node::Paragraph(_) => "Paragraph",
            Node::BlockQuote(_) => "BlockQuote",
            Node::OrderedList { .. } => "OrderedList",
            Node::UnorderedList(_) => "UnorderedList",
            Node::Table { .. } => "Table",
            Node::InlineCode(_) => "InlineCode",
            Node::Emphasis(_) => "Emphasis",
            Node::Strong(_) => "Strong",
            Node::Strikethrough(_) => "Strikethrough",
            Node::Link { .. } => "Link",
            Node::ReferenceLink { .. } => "ReferenceLink",
            Node::Image { .. } => "Image",
            Node::Autolink { .. } => "Autolink",
            Node::ExtendedAutolink(_) => "ExtendedAutolink",
            Node::HtmlElement(_) => "HtmlElement",
            Node::HardBreak => "HardBreak",
            Node::SoftBreak => "SoftBreak",
            Node::Text(_) => "Text",
            Node::Custom(_) => "Custom",
        }
    }
    /// Create a heading node
    ///
    /// # Arguments
    /// * `level` - Heading level (1-6)
    /// * `content` - Heading content
    ///
    /// # Returns
    /// A new heading node, default ATX type
    pub fn heading(level: u8, content: Vec<Node>) -> Self {
        Node::Heading {
            level,
            content,
            heading_type: HeadingType::default(),
        }
    }

    /// Create a code block node
    ///
    /// # Arguments
    /// * `language` - Optional language identifier
    /// * `content` - Code content
    ///
    /// # Returns
    /// A new code block node, default Fenced type
    pub fn code_block(language: Option<EcoString>, content: EcoString) -> Self {
        Node::CodeBlock {
            language,
            content,
            block_type: CodeBlockType::default(),
        }
    }

    /// Create a strikethrough node
    ///
    /// # Arguments
    /// * `content` - Content to be struck through
    ///
    /// # Returns
    /// A new strikethrough node
    pub fn strikethrough(content: Vec<Node>) -> Self {
        Node::Strikethrough(content)
    }

    /// Create a task list item
    ///
    /// # Arguments
    /// * `status` - Task completion status
    /// * `content` - Task content
    ///
    /// # Returns
    /// A new task list item
    #[cfg(feature = "gfm")]
    pub fn task_list_item(status: TaskListStatus, content: Vec<Node>) -> Self {
        Node::UnorderedList(vec![ListItem::Task { status, content }])
    }

    /// Create a table with alignment
    ///
    /// # Arguments
    /// * `headers` - Table header cells
    /// * `alignments` - Column alignments
    /// * `rows` - Table rows
    ///
    /// # Returns
    /// A new table node with alignment information
    #[cfg(feature = "gfm")]
    pub fn table_with_alignment(
        headers: Vec<Node>,
        alignments: Vec<TableAlignment>,
        rows: Vec<Vec<Node>>,
    ) -> Self {
        Node::Table {
            headers,
            alignments,
            rows,
        }
    }
    /// Check if a custom node is of a specific type, and return a reference to that type
    pub fn as_custom_type<T: CustomNode + 'static>(&self) -> Option<&T> {
        if let Node::Custom(node) = self {
            node.as_any().downcast_ref::<T>()
        } else {
            None
        }
    }

    /// Check if a node is a custom node of a specific type
    pub fn is_custom_type<T: CustomNode + 'static>(&self) -> bool {
        self.as_custom_type::<T>().is_some()
    }
}

// Implement Format traits for Node
impl crate::traits::Format<crate::writer::CommonMarkWriter> for Node {
    fn format(
        &self,
        writer: &mut crate::writer::CommonMarkWriter,
    ) -> crate::error::WriteResult<()> {
        // For individual nodes being formatted directly (like in legacy tests),
        // use content writing without automatic trailing newlines unless it's a block
        if self.is_block() {
            writer.write_node(self)
        } else {
            // For inline elements, write content without automatic trailing newlines
            writer.write_node_content(self)
        }
    }
}

impl crate::traits::Format<crate::writer::HtmlWriter> for Node {
    fn format(&self, writer: &mut crate::writer::HtmlWriter) -> crate::error::WriteResult<()> {
        writer.write_node_internal(self).map_err(Into::into)
    }
}