cmark_writer/
ast.rs

1//! Abstract Syntax Tree for CommonMark document structure.
2//!
3//! This module defines various node types for representing CommonMark documents,
4//! including headings, paragraphs, lists, code blocks, etc.
5
6/// Represents a node type in a CommonMark document
7#[derive(Debug, Clone, PartialEq)]
8pub enum Node {
9    /// Root document node, containing child nodes
10    Document(Vec<Node>),
11
12    /// Heading, containing level (1-6) and content
13    Heading {
14        /// Heading level, 1-6
15        level: u8,
16        /// Heading content, containing inline elements
17        content: Vec<Node>,
18    },
19
20    /// Paragraph node, containing inline elements
21    Paragraph(Vec<Node>),
22
23    /// Block quote, containing any block-level elements
24    BlockQuote(Vec<Node>),
25
26    /// Code block, containing optional language identifier and content
27    CodeBlock {
28        /// Optional language identifier
29        language: Option<String>,
30        /// Code content
31        content: String,
32    },
33
34    /// Unordered list, containing list items
35    UnorderedList(Vec<ListItem>),
36
37    /// Ordered list, containing starting number and list items
38    OrderedList {
39        /// List starting number
40        start: u32,
41        /// List items
42        items: Vec<ListItem>,
43    },
44
45    /// Thematic break (horizontal rule)
46    ThematicBreak,
47
48    /// Table
49    Table {
50        /// Header cells
51        headers: Vec<Node>,
52        /// Table rows, each containing multiple cells
53        rows: Vec<Vec<Node>>,
54        /// Column alignments
55        alignments: Vec<Alignment>,
56    },
57
58    /// Link
59    Link {
60        /// Link URL
61        url: String,
62        /// Optional link title
63        title: Option<String>,
64        /// Link text content
65        content: Vec<Node>,
66    },
67
68    /// Image
69    Image {
70        /// Image URL
71        url: String,
72        /// Optional image title
73        title: Option<String>,
74        /// Image alt text
75        alt: String,
76    },
77
78    /// Emphasis (italic)
79    Emphasis(Vec<Node>),
80
81    /// Strong emphasis (bold)
82    Strong(Vec<Node>),
83
84    /// Strikethrough text
85    Strike(Vec<Node>),
86
87    /// Inline code
88    InlineCode(String),
89
90    /// Plain text
91    Text(String),
92
93    /// Inline container, content is written inline without any formatting or line breaks
94    Inline(Vec<Node>),
95
96    /// HTML block
97    Html(String),
98
99    /// Custom HTML element with attributes and children
100    HtmlElement(HtmlElement),
101
102    /// Soft line break (single newline)
103    SoftBreak,
104
105    /// Hard line break (two spaces followed by newline or backslash followed by newline)
106    HardBreak,
107}
108
109/// Represents a list item
110#[derive(Debug, Clone, PartialEq)]
111pub struct ListItem {
112    /// List item content, containing one or more block-level elements
113    pub content: Vec<Node>,
114    /// Whether this is a task list item
115    pub is_task: bool,
116    /// Whether the task is completed
117    pub task_completed: bool,
118}
119
120/// Table column alignment
121#[derive(Debug, Clone, Copy, PartialEq)]
122pub enum Alignment {
123    /// No specified alignment
124    None,
125    /// Left alignment
126    Left,
127    /// Center alignment
128    Center,
129    /// Right alignment
130    Right,
131}
132
133/// Represents an HTML attribute with name and value
134#[derive(Debug, Clone, PartialEq)]
135pub struct HtmlAttribute {
136    /// Attribute name
137    pub name: String,
138    /// Attribute value
139    pub value: String,
140}
141
142/// Represents an HTML element with tag name, attributes and child nodes
143#[derive(Debug, Clone, PartialEq)]
144pub struct HtmlElement {
145    /// Element tag name
146    pub tag: String,
147    /// Element attributes
148    pub attributes: Vec<HtmlAttribute>,
149    /// Element children
150    pub children: Vec<Node>,
151    /// Whether this is a self-closing tag (e.g., <img />)
152    pub self_closing: bool,
153}