-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
mdx_jsx_flow.rs
228 lines (202 loc) · 7.27 KB
/
mdx_jsx_flow.rs
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
use markdown::{
mdast::{List, ListItem, MdxJsxFlowElement, Node, Paragraph, Root, Text},
to_html_with_options, to_mdast,
unist::Position,
Constructs, Options, ParseOptions,
};
use pretty_assertions::assert_eq;
#[test]
fn mdx_jsx_flow_agnostic() -> Result<(), String> {
let mdx = Options {
parse: ParseOptions::mdx(),
..Default::default()
};
assert_eq!(
to_html_with_options("<a />", &mdx)?,
"",
"should support a self-closing element"
);
// Note: in MDX, indented code is turned off:
assert_eq!(
to_html_with_options(
" <a />",
&Options {
parse: ParseOptions {
constructs: Constructs {
html_flow: false,
mdx_jsx_flow: true,
..Default::default()
},
..Default::default()
},
..Default::default()
}
)?,
"<pre><code><a />\n</code></pre>",
"should prefer indented code over jsx if it’s enabled"
);
assert_eq!(
to_html_with_options(
" <a />",
&Options {
parse: ParseOptions {
constructs: Constructs {
html_flow: false,
mdx_jsx_flow: true,
..Default::default()
},
..Default::default()
},
..Default::default()
}
)?,
"",
"should support indented jsx if indented code is enabled"
);
assert_eq!(
to_html_with_options("<a></a>", &mdx)?,
"",
"should support a closed element"
);
assert_eq!(
to_html_with_options("<a>\nb\n</a>", &mdx)?,
"<p>b</p>\n",
"should support an element w/ content"
);
assert_eq!(
to_html_with_options("<a>\n- b\n</a>", &mdx)?,
"<ul>\n<li>b</li>\n</ul>\n",
"should support an element w/ containers as content"
);
assert_eq!(
to_html_with_options("<a b c:d e=\"\" f={/* g */} {...h} />", &mdx)?,
"",
"should support attributes"
);
Ok(())
}
// Flow is mostly the same as `text`, so we only test the relevant
// differences.
#[test]
fn mdx_jsx_flow_essence() -> Result<(), String> {
let mdx = Options {
parse: ParseOptions::mdx(),
..Default::default()
};
assert_eq!(
to_html_with_options("<a />", &mdx)?,
"",
"should support an element"
);
assert_eq!(
to_html_with_options("<a>\n- b\n</a>", &mdx)?,
"<ul>\n<li>b</li>\n</ul>\n",
"should support an element around a container"
);
assert_eq!(
to_html_with_options("<x\n y\n> \nb\n </x>", &mdx)?,
"<p>b</p>\n",
"should support a dangling `>` in a tag (not a block quote)"
);
assert_eq!(
to_html_with_options("<a> \nb\n </a>", &mdx)?,
"<p>b</p>\n",
"should support trailing initial and final whitespace around tags"
);
assert_eq!(
to_html_with_options("<a> <b>\t\nc\n </b> </a>", &mdx)?,
"<p>c</p>\n",
"should support tags after tags"
);
// This is to make sure `document` passes errors through properly.
assert_eq!(
to_html_with_options("* <!a>\n1. b", &mdx)
.err()
.unwrap(),
"1:4: Unexpected character `!` (U+0021) before name, expected a character that can start a name, such as a letter, `$`, or `_` (note: to create a comment in MDX, use `{/* text */}`)",
"should handle crash in containers gracefully"
);
assert_eq!(
to_html_with_options("> <X\n/>", &mdx).err().unwrap(),
"2:1: Unexpected lazy line in jsx in container, expected line to be prefixed with `>` when in a block quote, whitespace when in a list, etc",
"should not support lazy flow (1)"
);
assert_eq!(
to_html_with_options("> a\n> <X\n/>", &mdx)
.err()
.unwrap(),
"3:1: Unexpected lazy line in jsx in container, expected line to be prefixed with `>` when in a block quote, whitespace when in a list, etc",
"should not support lazy flow (2)"
);
assert_eq!(
to_html_with_options("> <a b='\nc'/>", &mdx)
.err()
.unwrap(),
"2:1: Unexpected lazy line in jsx in container, expected line to be prefixed with `>` when in a block quote, whitespace when in a list, etc",
"should not support lazy flow (3)"
);
assert_eq!(
to_html_with_options("> <a b='c\n'/>", &mdx)
.err()
.unwrap(),
"2:1: Unexpected lazy line in jsx in container, expected line to be prefixed with `>` when in a block quote, whitespace when in a list, etc",
"should not support lazy flow (4)"
);
assert_eq!(
to_html_with_options("> <a b='c\nd'/>", &mdx)
.err()
.unwrap(),
"2:1: Unexpected lazy line in jsx in container, expected line to be prefixed with `>` when in a block quote, whitespace when in a list, etc",
"should not support lazy flow (5)"
);
assert_eq!(
to_html_with_options("> <a b={c\nd}/>", &mdx)
.err()
.unwrap(),
"2:1: Unexpected lazy line in expression in container, expected line to be prefixed with `>` when in a block quote, whitespace when in a list, etc",
"should not support lazy flow (6)"
);
assert_eq!(
to_html_with_options("> <a {b\nc}/>", &mdx)
.err()
.unwrap(),
"2:1: Unexpected lazy line in expression in container, expected line to be prefixed with `>` when in a block quote, whitespace when in a list, etc",
"should not support lazy flow (7)"
);
assert_eq!(
to_html_with_options("> a\n<X />", &mdx)?,
"<blockquote>\n<p>a</p>\n</blockquote>\n",
"should not support lazy flow (8)"
);
assert_eq!(
to_mdast("<>\n * a\n</>", &mdx.parse)?,
Node::Root(Root {
children: vec![Node::MdxJsxFlowElement(MdxJsxFlowElement {
name: None,
attributes: vec![],
children: vec![Node::List(List {
ordered: false,
spread: false,
start: None,
children: vec![Node::ListItem(ListItem {
checked: None,
spread: false,
children: vec![Node::Paragraph(Paragraph {
children: vec![Node::Text(Text {
value: "a".into(),
position: Some(Position::new(2, 5, 7, 2, 6, 8))
}),],
position: Some(Position::new(2, 5, 7, 2, 6, 8))
})],
position: Some(Position::new(2, 1, 3, 2, 6, 8))
})],
position: Some(Position::new(2, 1, 3, 2, 6, 8))
})],
position: Some(Position::new(1, 1, 0, 3, 4, 12))
})],
position: Some(Position::new(1, 1, 0, 3, 4, 12))
}),
"should support mdx jsx (flow) as `MdxJsxFlowElement`s in mdast"
);
Ok(())
}