Markdown parser fork with extended syntax for personal use.
1use markdown::mdast::{Code, Node};
2use mdast_util_to_markdown::{
3 to_markdown as to, to_markdown_with_options as to_md_with_opts, Options,
4};
5use pretty_assertions::assert_eq;
6
7#[test]
8fn text() {
9 assert_eq!(
10 to_md_with_opts(
11 &Node::Code(Code {
12 value: String::from("a"),
13 position: None,
14 lang: None,
15 meta: None
16 }),
17 &Options {
18 fences: false,
19 ..Default::default()
20 }
21 )
22 .unwrap(),
23 " a\n",
24 "should support code w/ a value (indent)"
25 );
26
27 assert_eq!(
28 to(&Node::Code(Code {
29 value: String::from("a"),
30 position: None,
31 lang: None,
32 meta: None
33 }))
34 .unwrap(),
35 "```\na\n```\n",
36 "should support code w/ a value (fences)"
37 );
38
39 assert_eq!(
40 to(&Node::Code(Code {
41 value: String::new(),
42 position: None,
43 lang: Some("a".to_string()),
44 meta: None
45 }))
46 .unwrap(),
47 "```a\n```\n",
48 "should support code w/ a lang"
49 );
50
51 assert_eq!(
52 to(&Node::Code(Code {
53 value: String::new(),
54 position: None,
55 lang: None,
56 meta: Some("a".to_string())
57 }))
58 .unwrap(),
59 "```\n```\n",
60 "should support (ignore) code w/ only a meta"
61 );
62
63 assert_eq!(
64 to(&Node::Code(Code {
65 value: String::new(),
66 position: None,
67 lang: Some("a".to_string()),
68 meta: Some("b".to_string())
69 }))
70 .unwrap(),
71 "```a b\n```\n",
72 "should support code w/ lang and meta"
73 );
74
75 assert_eq!(
76 to(&Node::Code(Code {
77 value: String::new(),
78 position: None,
79 lang: Some("a b".to_string()),
80 meta: None
81 }))
82 .unwrap(),
83 "```a b\n```\n",
84 "should encode a space in `lang`"
85 );
86
87 assert_eq!(
88 to(&Node::Code(Code {
89 value: String::new(),
90 position: None,
91 lang: Some("a\nb".to_string()),
92 meta: None
93 }))
94 .unwrap(),
95 "```a
b\n```\n",
96 "should encode a line ending in `lang`"
97 );
98
99 assert_eq!(
100 to(&Node::Code(Code {
101 value: String::new(),
102 position: None,
103 lang: Some("a`b".to_string()),
104 meta: None
105 }))
106 .unwrap(),
107 "```a`b\n```\n",
108 "should encode a grave accent in `lang`"
109 );
110
111 assert_eq!(
112 to(&Node::Code(Code {
113 value: String::new(),
114 position: None,
115 lang: Some("a\\-b".to_string()),
116 meta: None
117 }))
118 .unwrap(),
119 "```a\\\\-b\n```\n",
120 "should escape a backslash in `lang`"
121 );
122
123 assert_eq!(
124 to(&Node::Code(Code {
125 value: String::new(),
126 position: None,
127 lang: Some("x".to_string()),
128 meta: Some("a b".to_string())
129 }))
130 .unwrap(),
131 "```x a b\n```\n",
132 "should not encode a space in `meta`"
133 );
134
135 assert_eq!(
136 to(&Node::Code(Code {
137 value: String::new(),
138 position: None,
139 lang: Some("x".to_string()),
140 meta: Some("a\nb".to_string())
141 }))
142 .unwrap(),
143 "```x a
b\n```\n",
144 "should encode a line ending in `meta`"
145 );
146
147 assert_eq!(
148 to(&Node::Code(Code {
149 value: String::new(),
150 position: None,
151 lang: Some("x".to_string()),
152 meta: Some("a`b".to_string())
153 }))
154 .unwrap(),
155 "```x a`b\n```\n",
156 "should encode a grave accent in `meta`"
157 );
158
159 assert_eq!(
160 to(&Node::Code(Code {
161 value: String::new(),
162 position: None,
163 lang: Some("x".to_string()),
164 meta: Some("a\\-b".to_string())
165 }))
166 .unwrap(),
167 "```x a\\\\-b\n```\n",
168 "should escape a backslash in `meta`"
169 );
170
171 assert_eq!(
172 to_md_with_opts(
173 &Node::Code(Code {
174 value: String::new(),
175 position: None,
176 lang: None,
177 meta: None
178 }),
179 &Options {
180 fence: '~',
181 ..Default::default()
182 }
183 )
184 .unwrap(),
185 "~~~\n~~~\n",
186 "should support fenced code w/ tildes when `fence: \"~\"`"
187 );
188
189 assert_eq!(
190 to_md_with_opts(
191 &Node::Code(Code {
192 value: String::new(),
193 position: None,
194 lang: Some("a`b".to_string()),
195 meta: None
196 }),
197 &Options {
198 fence: '~',
199 ..Default::default()
200 }
201 )
202 .unwrap(),
203 "~~~a`b\n~~~\n",
204 "should not encode a grave accent when using tildes for fences"
205 );
206
207 assert_eq!(
208 to(&Node::Code(Code {
209 value: String::from("```\nasd\n```"),
210 position: None,
211 lang: None,
212 meta: None
213 }))
214 .unwrap(),
215 "````\n```\nasd\n```\n````\n",
216 "should use more grave accents for fences if there are streaks of grave accents in the value (fences)"
217 );
218
219 assert_eq!(
220 to_md_with_opts(
221 &Node::Code(Code {
222 value: String::from("~~~\nasd\n~~~"),
223 position: None,
224 lang: None,
225 meta: None
226 }),
227 &Options {
228 fence: '~',
229 ..Default::default()
230 }
231 )
232 .unwrap(),
233 "~~~~\n~~~\nasd\n~~~\n~~~~\n",
234 "should use more tildes for fences if there are streaks of tildes in the value (fences)"
235 );
236
237 assert_eq!(
238 to(&Node::Code(Code {
239 value: String::from("b"),
240 position: None,
241 lang: Some("a".to_string()),
242 meta: None
243 }))
244 .unwrap(),
245 "```a\nb\n```\n",
246 "should use a fence if there is an info"
247 );
248
249 assert_eq!(
250 to(&Node::Code(Code {
251 value: String::from(" "),
252 position: None,
253 lang: None,
254 meta: None
255 }))
256 .unwrap(),
257 "```\n \n```\n",
258 "should use a fence if there is only whitespace"
259 );
260
261 assert_eq!(
262 to(&Node::Code(Code {
263 value: String::from("\na"),
264 position: None,
265 lang: None,
266 meta: None
267 }))
268 .unwrap(),
269 "```\n\na\n```\n",
270 "should use a fence if there first line is blank (void)"
271 );
272
273 assert_eq!(
274 to(&Node::Code(Code {
275 value: String::from(" \na"),
276 position: None,
277 lang: None,
278 meta: None
279 }))
280 .unwrap(),
281 "```\n \na\n```\n",
282 "should use a fence if there first line is blank (filled)"
283 );
284
285 assert_eq!(
286 to(&Node::Code(Code {
287 value: String::from("a\n"),
288 position: None,
289 lang: None,
290 meta: None
291 }))
292 .unwrap(),
293 "```\na\n\n```\n",
294 "should use a fence if there last line is blank (void)"
295 );
296
297 assert_eq!(
298 to(&Node::Code(Code {
299 value: String::from("a\n "),
300 position: None,
301 lang: None,
302 meta: None
303 }))
304 .unwrap(),
305 "```\na\n \n```\n",
306 "should use a fence if there last line is blank (filled)"
307 );
308
309 assert_eq!(
310 to_md_with_opts(
311 &Node::Code(Code {
312 value: String::from(" a\n\n b"),
313 position: None,
314 lang: None,
315 meta: None
316 }),
317 &Options {
318 fences: false,
319 ..Default::default()
320 }
321 )
322 .unwrap(),
323 " a\n\n b\n",
324 "should use an indent if the value is indented"
325 );
326}