Markdown parser fork with extended syntax for personal use.
1use markdown::{
2 mdast::{Definition, Node, Root},
3 message, to_html, to_html_with_options, to_mdast,
4 unist::Position,
5 CompileOptions, Constructs, Options, ParseOptions,
6};
7use pretty_assertions::assert_eq;
8
9#[test]
10fn definition() -> Result<(), message::Message> {
11 let danger = Options {
12 compile: CompileOptions {
13 allow_dangerous_html: true,
14 allow_dangerous_protocol: true,
15 ..Default::default()
16 },
17 ..Default::default()
18 };
19
20 assert_eq!(
21 to_html("[foo]: /url \"title\"\n\n[foo]"),
22 "<p><a href=\"/url\" title=\"title\">foo</a></p>",
23 "should support link definitions"
24 );
25
26 assert_eq!(
27 to_html("[foo]:\n\n/url\n\n[foo]"),
28 "<p>[foo]:</p>\n<p>/url</p>\n<p>[foo]</p>",
29 "should not support blank lines before destination"
30 );
31
32 assert_eq!(
33 to_html(" [foo]: \n /url \n 'the title' \n\n[foo]"),
34 "<p><a href=\"/url\" title=\"the title\">foo</a></p>",
35 "should support whitespace and line endings in definitions"
36 );
37
38 assert_eq!(
39 to_html("[a]:b 'c'\n\n[a]"),
40 "<p><a href=\"b\" title=\"c\">a</a></p>",
41 "should support no whitespace after `:` in definitions"
42 );
43
44 assert_eq!(
45 to_html("[Foo*bar\\]]:my_(url) 'title (with parens)'\n\n[Foo*bar\\]]"),
46 "<p><a href=\"my_(url)\" title=\"title (with parens)\">Foo*bar]</a></p>",
47 "should support complex definitions (1)"
48 );
49
50 assert_eq!(
51 to_html("[Foo bar]:\n<my url>\n'title'\n\n[Foo bar]"),
52 "<p><a href=\"my%20url\" title=\"title\">Foo bar</a></p>",
53 "should support complex definitions (2)"
54 );
55
56 assert_eq!(
57 to_html("[foo]: /url '\ntitle\nline1\nline2\n'\n\n[foo]"),
58 "<p><a href=\"/url\" title=\"\ntitle\nline1\nline2\n\">foo</a></p>",
59 "should support line endings in titles"
60 );
61
62 assert_eq!(
63 to_html("[foo]: /url 'title\n\nwith blank line'\n\n[foo]"),
64 "<p>[foo]: /url 'title</p>\n<p>with blank line'</p>\n<p>[foo]</p>",
65 "should not support blank lines in titles"
66 );
67
68 assert_eq!(
69 to_html("[foo]:\n/url\n\n[foo]"),
70 "<p><a href=\"/url\">foo</a></p>",
71 "should support definitions w/o title"
72 );
73
74 assert_eq!(
75 to_html("[foo]:\n\n[foo]"),
76 "<p>[foo]:</p>\n<p>[foo]</p>",
77 "should not support definitions w/o destination"
78 );
79
80 assert_eq!(
81 to_html("[foo]: <>\n\n[foo]"),
82 "<p><a href=\"\">foo</a></p>",
83 "should support definitions w/ explicit empty destinations"
84 );
85
86 assert_eq!(
87 to_html_with_options("[foo]: <bar>(baz)\n\n[foo]", &danger)?,
88 "<p>[foo]: <bar>(baz)</p>\n<p>[foo]</p>",
89 "should not support definitions w/ no whitespace between destination and title"
90 );
91
92 assert_eq!(
93 to_html("[foo]: /url\\bar\\*baz \"foo\\\"bar\\baz\"\n\n[foo]"),
94 "<p><a href=\"/url%5Cbar*baz\" title=\"foo"bar\\baz\">foo</a></p>",
95 "should support character escapes in destinations and titles"
96 );
97
98 assert_eq!(
99 to_html("[foo]\n\n[foo]: url"),
100 "<p><a href=\"url\">foo</a></p>\n",
101 "should support a link before a definition"
102 );
103
104 assert_eq!(
105 to_html("[foo]: first\n[foo]: second\n\n[foo]"),
106 "<p><a href=\"first\">foo</a></p>",
107 "should match w/ the first definition"
108 );
109
110 assert_eq!(
111 to_html("[FOO]: /url\n\n[Foo]"),
112 "<p><a href=\"/url\">Foo</a></p>",
113 "should match w/ case-insensitive (1)"
114 );
115
116 assert_eq!(
117 to_html("[ΑΓΩ]: /φου\n\n[αγω]"),
118 "<p><a href=\"/%CF%86%CE%BF%CF%85\">αγω</a></p>",
119 "should match w/ case-insensitive (2)"
120 );
121
122 assert_eq!(
123 to_html("[ı]: a\n\n[I]"),
124 "<p><a href=\"a\">I</a></p>",
125 "should match w/ undotted turkish i (1)"
126 );
127 assert_eq!(
128 to_html("[I]: a\n\n[ı]"),
129 "<p><a href=\"a\">ı</a></p>",
130 "should match w/ undotted turkish i (2)"
131 );
132 // Ref: <https://spec.commonmark.org/dingus/?text=%5Bi%5D%3A%20a%0A%0A%5Bİ%5D>
133 // GFM parses the same (last checked: 2022-07-11).
134 assert_eq!(
135 to_html("[i]: a\n\n[İ]"),
136 "<p>[İ]</p>",
137 "should *not* match w/ dotted turkish i (1)"
138 );
139 // Ref: <https://spec.commonmark.org/dingus/?text=%5Bİ%5D%3A%20a%0A%0A%5Bi%5D>
140 // GFM parses the same (last checked: 2022-07-11).
141 assert_eq!(
142 to_html("[İ]: a\n\n[i]"),
143 "<p>[i]</p>",
144 "should *not* match w/ dotted turkish i (2)"
145 );
146
147 assert_eq!(
148 to_html("[foo]: /url"),
149 "",
150 "should not contribute anything w/o reference (1)"
151 );
152
153 assert_eq!(
154 to_html("[\nfoo\n]: /url\nbar"),
155 "<p>bar</p>",
156 "should not contribute anything w/o reference (2)"
157 );
158
159 assert_eq!(
160 to_html("[foo]: /url \"title\" \n\n[foo]"),
161 "<p><a href=\"/url\" title=\"title\">foo</a></p>",
162 "should support whitespace after title"
163 );
164
165 assert_eq!(
166 to_html("[foo]: /url\n\"title\" \n\n[foo]"),
167 "<p><a href=\"/url\" title=\"title\">foo</a></p>",
168 "should support whitespace after title on a separate line"
169 );
170
171 assert_eq!(
172 to_html("[foo]: /url \"title\" ok"),
173 "<p>[foo]: /url "title" ok</p>",
174 "should not support non-whitespace content after definitions (1)"
175 );
176
177 assert_eq!(
178 to_html("[foo]: /url\n\"title\" ok"),
179 "<p>"title" ok</p>",
180 "should not support non-whitespace content after definitions (2)"
181 );
182
183 assert_eq!(
184 to_html(" [foo]: /url \"title\"\n\n[foo]"),
185 "<pre><code>[foo]: /url "title"\n</code></pre>\n<p>[foo]</p>",
186 "should prefer indented code over definitions"
187 );
188
189 assert_eq!(
190 to_html("```\n[foo]: /url\n```\n\n[foo]"),
191 "<pre><code>[foo]: /url\n</code></pre>\n<p>[foo]</p>",
192 "should not support definitions in fenced code"
193 );
194
195 assert_eq!(
196 to_html("Foo\n[bar]: /baz\n\n[bar]"),
197 "<p>Foo\n[bar]: /baz</p>\n<p>[bar]</p>",
198 "should not support definitions in paragraphs"
199 );
200
201 assert_eq!(
202 to_html("# [Foo]\n[foo]: /url\n> bar"),
203 "<h1><a href=\"/url\">Foo</a></h1>\n<blockquote>\n<p>bar</p>\n</blockquote>",
204 "should not support definitions in headings"
205 );
206
207 assert_eq!(
208 to_html("[foo]: /url\nbar\n===\n[foo]"),
209 "<h1>bar</h1>\n<p><a href=\"/url\">foo</a></p>",
210 "should support setext headings after definitions"
211 );
212
213 assert_eq!(
214 to_html("[a]: b\n="),
215 "<p>=</p>",
216 "should not support setext heading underlines after definitions (1)"
217 );
218
219 assert_eq!(
220 to_html("[foo]: /url\n===\n[foo]"),
221 "<p>===\n<a href=\"/url\">foo</a></p>",
222 "should not support setext heading underlines after definitions (2)"
223 );
224
225 assert_eq!(
226 to_html(
227 "[foo]: /foo-url \"foo\"\n[bar]: /bar-url\n \"bar\"\n[baz]: /baz-url\n\n[foo],\n[bar],\n[baz]"),
228 "<p><a href=\"/foo-url\" title=\"foo\">foo</a>,\n<a href=\"/bar-url\" title=\"bar\">bar</a>,\n<a href=\"/baz-url\">baz</a></p>",
229 "should support definitions after definitions"
230 );
231
232 assert_eq!(
233 to_html("> [foo]: /url\n\n[foo]"),
234 "<blockquote>\n</blockquote>\n<p><a href=\"/url\">foo</a></p>",
235 "should support definitions in block quotes (1)"
236 );
237
238 assert_eq!(
239 to_html("> [a]: <> 'b\n> c'"),
240 "<blockquote>\n</blockquote>",
241 "should support definitions in block quotes (2)"
242 );
243
244 assert_eq!(
245 to_html("> [a]\n\n[a]: b (c\n)"),
246 "<blockquote>\n<p><a href=\"b\" title=\"c\n\">a</a></p>\n</blockquote>\n",
247 "should support definitions in block quotes (3)"
248 );
249
250 // Extra
251 assert_eq!(
252 to_html("[\\[\\+\\]]: example.com\n\nLink: [\\[\\+\\]]."),
253 "<p>Link: <a href=\"example.com\">[+]</a>.</p>",
254 "should match w/ character escapes"
255 );
256
257 assert_eq!(
258 to_html("[x]: \\\" \\(\\)\\\"\n\n[x]"),
259 "<p><a href=\"%22%20()%22\">x</a></p>",
260 "should support character escapes & references in unenclosed destinations"
261 );
262
263 assert_eq!(
264 to_html("[x]: <\\> \\+\\>>\n\n[x]"),
265 "<p><a href=\"%3E%20+%3E\">x</a></p>",
266 "should support character escapes & references in enclosed destinations"
267 );
268
269 assert_eq!(
270 to_html("[x]: <\n\n[x]"),
271 "<p>[x]: <</p>\n<p>[x]</p>",
272 "should not support a line ending at start of enclosed destination"
273 );
274
275 assert_eq!(
276 to_html("[x]: <x\n\n[x]"),
277 "<p>[x]: <x</p>\n<p>[x]</p>",
278 "should not support a line ending in enclosed destination"
279 );
280
281 assert_eq!(
282 to_html("[x]: \u{000b}a\n\n[x]"),
283 "<p>[x]: \u{000b}a</p>\n<p>[x]</p>",
284 "should not support ascii control characters at the start of destination"
285 );
286
287 assert_eq!(
288 to_html("[x]: a\u{000b}b\n\n[x]"),
289 "<p>[x]: a\u{000b}b</p>\n<p>[x]</p>",
290 "should not support ascii control characters in destination"
291 );
292
293 assert_eq!(
294 to_html("[x]: <\u{000b}a>\n\n[x]"),
295 "<p><a href=\"%0Ba\">x</a></p>",
296 "should support ascii control characters at the start of enclosed destination"
297 );
298
299 assert_eq!(
300 to_html("[x]: <a\u{000b}b>\n\n[x]"),
301 "<p><a href=\"a%0Bb\">x</a></p>",
302 "should support ascii control characters in enclosed destinations"
303 );
304
305 assert_eq!(
306 to_html("[x]: a \"\\\"\"\n\n[x]"),
307 "<p><a href=\"a\" title=\""\">x</a></p>",
308 "should support character escapes at the start of a title"
309 );
310
311 assert_eq!(
312 to_html("[x]: a \"'\"\n\n[x]"),
313 "<p><a href=\"a\" title=\"'\">x</a></p>",
314 "should support double quoted titles"
315 );
316
317 assert_eq!(
318 to_html("[x]: a '\"'\n\n[x]"),
319 "<p><a href=\"a\" title=\""\">x</a></p>",
320 "should support single quoted titles"
321 );
322
323 assert_eq!(
324 to_html("[x]: a (\"')\n\n[x]"),
325 "<p><a href=\"a\" title=\""'\">x</a></p>",
326 "should support paren enclosed titles"
327 );
328
329 assert_eq!(
330 to_html("[x]: a(()\n\n[x]"),
331 "<p>[x]: a(()</p>\n<p>[x]</p>",
332 "should not support more opening than closing parens in the destination"
333 );
334
335 assert_eq!(
336 to_html("[x]: a(())\n\n[x]"),
337 "<p><a href=\"a(())\">x</a></p>",
338 "should support balanced opening and closing parens in the destination"
339 );
340
341 assert_eq!(
342 to_html("[x]: a())\n\n[x]"),
343 "<p>[x]: a())</p>\n<p>[x]</p>",
344 "should not support more closing than opening parens in the destination"
345 );
346
347 assert_eq!(
348 to_html("[x]: a \t\n\n[x]"),
349 "<p><a href=\"a\">x</a></p>",
350 "should support trailing whitespace after a destination"
351 );
352
353 assert_eq!(
354 to_html("[x]: a \"X\" \t\n\n[x]"),
355 "<p><a href=\"a\" title=\"X\">x</a></p>",
356 "should support trailing whitespace after a title"
357 );
358
359 assert_eq!(
360 to_html("[&©&]: example.com/&©& \"&©&\"\n\n[&©&]"),
361 "<p><a href=\"example.com/&%C2%A9&\" title=\"&©&\">&©&</a></p>",
362 "should support character references in definitions"
363 );
364
365 assert_eq!(
366 to_html("[x]:\nexample.com\n\n[x]"),
367 "<p><a href=\"example.com\">x</a></p>",
368 "should support a line ending before a destination"
369 );
370
371 assert_eq!(
372 to_html("[x]: \t\nexample.com\n\n[x]"),
373 "<p><a href=\"example.com\">x</a></p>",
374 "should support whitespace before a destination"
375 );
376
377 // See: <https://github.com/commonmark/commonmark.js/issues/192>
378 assert_eq!(
379 to_html("[x]: <> \"\"\n[][x]"),
380 "<p><a href=\"\"></a></p>",
381 "should ignore an empty title"
382 );
383
384 assert_eq!(
385 to_html_with_options("[a]\n\n[a]: <b<c>", &danger)?,
386 "<p>[a]</p>\n<p>[a]: <b<c></p>",
387 "should not support a less than in an enclosed destination"
388 );
389
390 assert_eq!(
391 to_html("[a]\n\n[a]: b(c"),
392 "<p>[a]</p>\n<p>[a]: b(c</p>",
393 "should not support an extra left paren (`(`) in a raw destination"
394 );
395
396 assert_eq!(
397 to_html("[a]\n\n[a]: b)c"),
398 "<p>[a]</p>\n<p>[a]: b)c</p>",
399 "should not support an extra right paren (`)`) in a raw destination"
400 );
401
402 assert_eq!(
403 to_html("[a]\n\n[a]: b)c"),
404 "<p>[a]</p>\n<p>[a]: b)c</p>",
405 "should not support an extra right paren (`)`) in a raw destination"
406 );
407
408 assert_eq!(
409 to_html("[a]\n\n[a]: a(1(2(3(4()))))b"),
410 "<p><a href=\"a(1(2(3(4()))))b\">a</a></p>\n",
411 "should support 4 or more sets of parens in a raw destination (link resources don’t)"
412 );
413
414 assert_eq!(
415 to_html("[a]\n\n[a]: aaa)"),
416 "<p>[a]</p>\n<p>[a]: aaa)</p>",
417 "should not support a final (unbalanced) right paren in a raw destination"
418 );
419
420 assert_eq!(
421 to_html("[a]\n\n[a]: aaa) \"a\""),
422 "<p>[a]</p>\n<p>[a]: aaa) "a"</p>",
423 "should not support a final (unbalanced) right paren in a raw destination “before” a title"
424 );
425
426 assert_eq!(
427 to_html(" [a]: b \"c\"\n [d]: e\n [f]: g \"h\"\n [i]: j\n\t[k]: l (m)\n\t n [k] o"),
428 "<p>n <a href=\"l\" title=\"m\">k</a> o</p>",
429 "should support subsequent indented definitions"
430 );
431
432 assert_eq!(
433 to_html("[a\n b]: c\n\n[a\n b]"),
434 "<p><a href=\"c\">a\nb</a></p>",
435 "should support line prefixes in definition labels"
436 );
437
438 assert_eq!(
439 to_html("[a]: )\n\n[a]"),
440 "<p>[a]: )</p>\n<p>[a]</p>",
441 "should not support definitions w/ only a closing paren as a raw destination"
442 );
443
444 assert_eq!(
445 to_html("[a]: )b\n\n[a]"),
446 "<p>[a]: )b</p>\n<p>[a]</p>",
447 "should not support definitions w/ closing paren + more text as a raw destination"
448 );
449
450 assert_eq!(
451 to_html("[a]: b)\n\n[a]"),
452 "<p>[a]: b)</p>\n<p>[a]</p>",
453 "should not support definitions w/ text + a closing paren as a raw destination"
454 );
455
456 assert_eq!(
457 to_html("[\na\n=\n]: b"),
458 "<h1>[\na</h1>\n<p>]: b</p>",
459 "should prefer setext headings over definition labels"
460 );
461
462 assert_eq!(
463 to_html("[a]: b '\nc\n=\n'"),
464 "<h1>[a]: b '\nc</h1>\n<p>'</p>",
465 "should prefer setext headings over definition titles"
466 );
467
468 assert_eq!(
469 to_html("[\n***\n]: b"),
470 "<p>[</p>\n<hr />\n<p>]: b</p>",
471 "should prefer thematic breaks over definition labels"
472 );
473
474 assert_eq!(
475 to_html("[a]: b '\n***\n'"),
476 "<p>[a]: b '</p>\n<hr />\n<p>'</p>",
477 "should prefer thematic breaks over definition titles"
478 );
479
480 assert_eq!(
481 to_html("[\n```\n]: b"),
482 "<p>[</p>\n<pre><code>]: b\n</code></pre>\n",
483 "should prefer code (fenced) over definition labels"
484 );
485
486 assert_eq!(
487 to_html("[a]: b '\n```\n'"),
488 "<p>[a]: b '</p>\n<pre><code>'\n</code></pre>\n",
489 "should prefer code (fenced) over definition titles"
490 );
491
492 assert_eq!(
493 to_html_with_options(
494 "[foo]: /url \"title\"",
495 &Options {
496 parse: ParseOptions {
497 constructs: Constructs {
498 definition: false,
499 ..Default::default()
500 },
501 ..Default::default()
502 },
503 ..Default::default()
504 }
505 )?,
506 "<p>[foo]: /url "title"</p>",
507 "should support turning off definitions"
508 );
509
510 assert_eq!(
511 to_mdast("[a]: <b> 'c'", &Default::default())?,
512 Node::Root(Root {
513 children: vec![Node::Definition(Definition {
514 url: "b".into(),
515 identifier: "a".into(),
516 label: Some("a".into()),
517 title: Some("c".into()),
518 position: Some(Position::new(1, 1, 0, 1, 13, 12))
519 })],
520 position: Some(Position::new(1, 1, 0, 1, 13, 12))
521 }),
522 "should support definitions as `Definition`s in mdast"
523 );
524
525 Ok(())
526}