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 /// Inline code
85 InlineCode(String),
86
87 /// Plain text
88 Text(String),
89
90 /// HTML block
91 Html(String),
92
93 /// Soft line break (single newline)
94 SoftBreak,
95
96 /// Hard line break (two spaces followed by newline or backslash followed by newline)
97 HardBreak,
98}
99
100/// Represents a list item
101#[derive(Debug, Clone, PartialEq)]
102pub struct ListItem {
103 /// List item content, containing one or more block-level elements
104 pub content: Vec<Node>,
105 /// Whether this is a task list item
106 pub is_task: bool,
107 /// Whether the task is completed
108 pub task_completed: bool,
109}
110
111/// Table column alignment
112#[derive(Debug, Clone, Copy, PartialEq)]
113pub enum Alignment {
114 /// No specified alignment
115 None,
116 /// Left alignment
117 Left,
118 /// Center alignment
119 Center,
120 /// Right alignment
121 Right,
122}