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 /// HTML block
94 Html(String),
95
96 /// Custom HTML element with attributes and children
97 HtmlElement(HtmlElement),
98
99 /// Soft line break (single newline)
100 SoftBreak,
101
102 /// Hard line break (two spaces followed by newline or backslash followed by newline)
103 HardBreak,
104}
105
106/// Represents a list item
107#[derive(Debug, Clone, PartialEq)]
108pub struct ListItem {
109 /// List item content, containing one or more block-level elements
110 pub content: Vec<Node>,
111 /// Whether this is a task list item
112 pub is_task: bool,
113 /// Whether the task is completed
114 pub task_completed: bool,
115}
116
117/// Table column alignment
118#[derive(Debug, Clone, Copy, PartialEq)]
119pub enum Alignment {
120 /// No specified alignment
121 None,
122 /// Left alignment
123 Left,
124 /// Center alignment
125 Center,
126 /// Right alignment
127 Right,
128}
129
130/// Represents an HTML attribute with name and value
131#[derive(Debug, Clone, PartialEq)]
132pub struct HtmlAttribute {
133 /// Attribute name
134 pub name: String,
135 /// Attribute value
136 pub value: String,
137}
138
139/// Represents an HTML element with tag name, attributes and child nodes
140#[derive(Debug, Clone, PartialEq)]
141pub struct HtmlElement {
142 /// Element tag name
143 pub tag: String,
144 /// Element attributes
145 pub attributes: Vec<HtmlAttribute>,
146 /// Element children
147 pub children: Vec<Node>,
148 /// Whether this is a self-closing tag (e.g., <img />)
149 pub self_closing: bool,
150}