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
|
#![allow(dead_code)]
// ^-- fix later
extern crate alloc;
extern crate micromark;
use alloc::{
fmt,
string::{String, ToString},
vec::Vec,
};
use micromark::{mdast::AttributeContent, unist::Position};
/// Nodes.
#[derive(Clone, PartialEq)]
pub enum Node {
/// Root.
Root(Root),
/// Element.
Element(Element),
/// Document type.
Doctype(Doctype),
/// Comment.
Comment(Comment),
/// Text.
Text(Text),
// MDX being passed through.
/// MDX: JSX element.
MdxJsxElement(MdxJsxElement),
/// MDX.js ESM.
MdxjsEsm(MdxjsEsm),
// MDX: expression.
MdxExpression(MdxExpression),
}
impl fmt::Debug for Node {
// Debug the wrapped struct.
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Node::Root(x) => write!(f, "{:?}", x),
Node::Element(x) => write!(f, "{:?}", x),
Node::Doctype(x) => write!(f, "{:?}", x),
Node::Comment(x) => write!(f, "{:?}", x),
Node::Text(x) => write!(f, "{:?}", x),
Node::MdxJsxElement(x) => write!(f, "{:?}", x),
Node::MdxExpression(x) => write!(f, "{:?}", x),
Node::MdxjsEsm(x) => write!(f, "{:?}", x),
}
}
}
fn children_to_string(children: &[Node]) -> String {
children.iter().map(ToString::to_string).collect()
}
impl ToString for Node {
fn to_string(&self) -> String {
match self {
// Parents.
Node::Root(x) => children_to_string(&x.children),
Node::Element(x) => children_to_string(&x.children),
Node::MdxJsxElement(x) => children_to_string(&x.children),
// Literals.
Node::Comment(x) => x.value.clone(),
Node::Text(x) => x.value.clone(),
Node::MdxExpression(x) => x.value.clone(),
Node::MdxjsEsm(x) => x.value.clone(),
// Voids.
Node::Doctype(_) => "".to_string(),
}
}
}
impl Node {
#[must_use]
pub fn children(&self) -> Option<&Vec<Node>> {
match self {
// Parent.
Node::Root(x) => Some(&x.children),
Node::Element(x) => Some(&x.children),
Node::MdxJsxElement(x) => Some(&x.children),
// Non-parent.
_ => None,
}
}
pub fn children_mut(&mut self) -> Option<&mut Vec<Node>> {
match self {
// Parent.
Node::Root(x) => Some(&mut x.children),
Node::Element(x) => Some(&mut x.children),
Node::MdxJsxElement(x) => Some(&mut x.children),
// Non-parent.
_ => None,
}
}
pub fn position(&self) -> Option<&Position> {
match self {
Node::Root(x) => x.position.as_ref(),
Node::Element(x) => x.position.as_ref(),
Node::Doctype(x) => x.position.as_ref(),
Node::Comment(x) => x.position.as_ref(),
Node::Text(x) => x.position.as_ref(),
Node::MdxJsxElement(x) => x.position.as_ref(),
Node::MdxExpression(x) => x.position.as_ref(),
Node::MdxjsEsm(x) => x.position.as_ref(),
}
}
pub fn position_mut(&mut self) -> Option<&mut Position> {
match self {
Node::Root(x) => x.position.as_mut(),
Node::Element(x) => x.position.as_mut(),
Node::Doctype(x) => x.position.as_mut(),
Node::Comment(x) => x.position.as_mut(),
Node::Text(x) => x.position.as_mut(),
Node::MdxJsxElement(x) => x.position.as_mut(),
Node::MdxExpression(x) => x.position.as_mut(),
Node::MdxjsEsm(x) => x.position.as_mut(),
}
}
pub fn position_set(&mut self, position: Option<Position>) {
match self {
Node::Root(x) => x.position = position,
Node::Element(x) => x.position = position,
Node::Doctype(x) => x.position = position,
Node::Comment(x) => x.position = position,
Node::Text(x) => x.position = position,
Node::MdxJsxElement(x) => x.position = position,
Node::MdxExpression(x) => x.position = position,
Node::MdxjsEsm(x) => x.position = position,
}
}
}
/// Document.
///
/// ```html
/// > | a
/// ^
/// ```
#[derive(Clone, Debug, PartialEq)]
pub struct Root {
// Parent.
/// Content model.
pub children: Vec<Node>,
/// Positional info.
pub position: Option<Position>,
}
/// Document type.
///
/// ```html
/// > | <!doctype html>
/// ^^^^^^^^^^^^^^^
/// ```
// To do: clone.
#[derive(Clone, Debug, PartialEq)]
pub struct Element {
pub tag_name: String,
pub properties: Vec<(String, PropertyValue)>,
// Parent.
pub children: Vec<Node>,
/// Positional info.
pub position: Option<Position>,
}
#[derive(Clone, Debug, PartialEq)]
pub enum PropertyItem {
Number(f32),
String(String),
}
#[derive(Clone, Debug, PartialEq)]
pub enum PropertyValue {
Number(f32),
Boolean(bool),
String(String),
CommaSeparated(Vec<PropertyItem>),
SpaceSeparated(Vec<PropertyItem>),
}
/// Document type.
///
/// ```html
/// > | <!doctype html>
/// ^^^^^^^^^^^^^^^
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Doctype {
// Void.
/// Positional info.
pub position: Option<Position>,
}
/// Comment.
///
/// ```html
/// > | <!-- a -->
/// ^^^^^^^^^^
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Comment {
// Text.
/// Content model.
pub value: String,
/// Positional info.
pub position: Option<Position>,
}
/// Text.
///
/// ```html
/// > | a
/// ^
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Text {
// Text.
/// Content model.
pub value: String,
/// Positional info.
pub position: Option<Position>,
}
/// MDX: JSX element.
///
/// ```markdown
/// > | <a />
/// ^^^^^
/// ```
#[derive(Clone, Debug, PartialEq)]
pub struct MdxJsxElement {
// Parent.
/// Content model.
pub children: Vec<Node>,
/// Positional info.
pub position: Option<Position>,
// JSX element.
/// Name.
///
/// Fragments have no name.
pub name: Option<String>,
/// Attributes.
pub attributes: Vec<AttributeContent>,
}
/// MDX: expression.
///
/// ```markdown
/// > | {a}
/// ^^^
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MdxExpression {
// Literal.
/// Content model.
pub value: String,
/// Positional info.
pub position: Option<Position>,
}
/// MDX: ESM.
///
/// ```markdown
/// > | import a from 'b'
/// ^^^^^^^^^^^^^^^^^
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MdxjsEsm {
// Literal.
/// Content model.
pub value: String,
/// Positional info.
pub position: Option<Position>,
}
|