Something went wrong. Try again.
Markdown parser fork with extended syntax for personal use.
Something went wrong. Try again.
16 kB · 498 lines
Rust
at hack
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499use markdown::{message, to_html_with_options, CompileOptions, Constructs, Options, ParseOptions};use pretty_assertions::assert_eq;
#[test]fn wikilinks() -> Result<(), message::Message> { let backlinks = Options { compile: CompileOptions { ..Default::default() }, parse: ParseOptions { constructs: Constructs { wikilink: true, ..Default::default() }, ..Default::default() }, };
assert_eq!( to_html_with_options("[[backlink]]()", &backlinks).unwrap(), "<p><strong>backlink</strong>()</p>", "should support backlinks" );
Ok(())}
#[test]fn wikilink_retrocompat() -> Result<(), message::Message> { let danger = Options { parse: ParseOptions { constructs: Constructs { wikilink: true, ..Default::default() }, ..Default::default() }, compile: CompileOptions { allow_dangerous_html: true, allow_dangerous_protocol: true, ..Default::default() }, ..Default::default() };
assert_eq!( to_html_with_options("[link](/uri \"title\")", &danger)?, "<p><a href=\"/uri\" title=\"title\">link</a></p>", "should support links" );
assert_eq!( to_html_with_options("[link](/uri)", &danger)?, "<p><a href=\"/uri\">link</a></p>", "should support links w/o title" );
assert_eq!( to_html_with_options("[link]()", &danger)?, "<p><a href=\"\">link</a></p>", "should support links w/o destination" );
assert_eq!( to_html_with_options("[link](<>)", &danger)?, "<p><a href=\"\">link</a></p>", "should support links w/ empty enclosed destination" );
assert_eq!( to_html_with_options("[link](/my uri)", &danger)?, "<p>[link](/my uri)</p>", "should not support links w/ spaces in destination" );
assert_eq!( to_html_with_options("[link](</my uri>)", &danger)?, "<p><a href=\"/my%20uri\">link</a></p>", "should support links w/ spaces in enclosed destination" );
assert_eq!( to_html_with_options("[link](foo\nbar)", &danger)?, "<p>[link](foo\nbar)</p>", "should not support links w/ line endings in destination" );
assert_eq!( to_html_with_options("[link](<foo\nbar>)", &danger)?, "<p>[link](<foo\nbar>)</p>", "should not support links w/ line endings in enclosed destination" );
assert_eq!( to_html_with_options("[a](<b)c>)", &danger)?, "<p><a href=\"b)c\">a</a></p>", "should support links w/ closing parens in destination" );
assert_eq!( to_html_with_options("[link](<foo\\>)", &danger)?, "<p>[link](<foo>)</p>", "should not support links w/ enclosed destinations w/o end" );
assert_eq!( to_html_with_options("[a](<b)c\n[a](<b)c>\n[a](<b>c)", &danger)?, "<p>[a](<b)c\n[a](<b)c>\n[a](<b>c)</p>", "should not support links w/ unmatched enclosed destinations" );
assert_eq!( to_html_with_options("[link](\\(foo\\))", &danger)?, "<p><a href=\"(foo)\">link</a></p>", "should support links w/ destinations w/ escaped parens" );
assert_eq!( to_html_with_options("[link](foo(and(bar)))", &danger)?, "<p><a href=\"foo(and(bar))\">link</a></p>", "should support links w/ destinations w/ balanced parens" );
assert_eq!( to_html_with_options("[link](foo\\(and\\(bar\\))", &danger)?, "<p><a href=\"foo(and(bar)\">link</a></p>", "should support links w/ destinations w/ escaped parens" );
assert_eq!( to_html_with_options("[link](<foo(and(bar)>)", &danger)?, "<p><a href=\"foo(and(bar)\">link</a></p>", "should support links w/ enclosed destinations w/ parens" );
assert_eq!( to_html_with_options("[link](foo\\)\\:)", &danger)?, "<p><a href=\"foo):\">link</a></p>", "should support links w/ escapes in destinations" );
assert_eq!( to_html_with_options("[link](#fragment)", &danger)?, "<p><a href=\"#fragment\">link</a></p>", "should support links w/ destinations to fragments" );
assert_eq!( to_html_with_options("[link](http://example.com#fragment)", &danger)?, "<p><a href=\"http://example.com#fragment\">link</a></p>", "should support links w/ destinations to URLs w/ fragments" );
assert_eq!( to_html_with_options("[link](http://example.com?foo=3#frag)", &danger)?, "<p><a href=\"http://example.com?foo=3#frag\">link</a></p>", "should support links w/ destinations to URLs w/ search and fragments" );
assert_eq!( to_html_with_options("[link](foo\\bar)", &danger)?, "<p><a href=\"foo%5Cbar\">link</a></p>", "should not support non-punctuation character escapes in links" );
assert_eq!( to_html_with_options("[link](foo%20bä)", &danger)?, "<p><a href=\"foo%20b%C3%A4\">link</a></p>", "should support character references in links" );
assert_eq!( to_html_with_options("[link](\"title\")", &danger)?, "<p><a href=\"%22title%22\">link</a></p>", "should not support links w/ only a title" );
assert_eq!( to_html_with_options("[link](/url \"title\")", &danger)?, "<p><a href=\"/url\" title=\"title\">link</a></p>", "should support titles w/ double quotes" );
assert_eq!( to_html_with_options("[link](/url 'title')", &danger)?, "<p><a href=\"/url\" title=\"title\">link</a></p>", "should support titles w/ single quotes" );
assert_eq!( to_html_with_options("[link](/url (title))", &danger)?, "<p><a href=\"/url\" title=\"title\">link</a></p>", "should support titles w/ parens" );
assert_eq!( to_html_with_options("[link](/url \"title \\\""\")", &danger)?, "<p><a href=\"/url\" title=\"title ""\">link</a></p>", "should support character references and escapes in titles" );
assert_eq!( to_html_with_options("[link](/url \"title\")", &danger)?, "<p><a href=\"/url%C2%A0%22title%22\">link</a></p>", "should not support unicode whitespace between destination and title" );
assert_eq!( to_html_with_options("[link](/url \"title \"and\" title\")", &danger)?, "<p>[link](/url "title "and" title")</p>", "should not support nested balanced quotes in title" );
assert_eq!( to_html_with_options("[link](/url 'title \"and\" title')", &danger)?, "<p><a href=\"/url\" title=\"title "and" title\">link</a></p>", "should support the other quotes in titles" );
assert_eq!( to_html_with_options("[link]( /uri\n \"title\" )", &danger)?, "<p><a href=\"/uri\" title=\"title\">link</a></p>", "should support whitespace around destination and title (1)" );
assert_eq!( to_html_with_options("[link](\t\n/uri \"title\")", &danger)?, "<p><a href=\"/uri\" title=\"title\">link</a></p>", "should support whitespace around destination and title (2)" );
assert_eq!( to_html_with_options("[link](/uri \"title\"\t\n)", &danger)?, "<p><a href=\"/uri\" title=\"title\">link</a></p>", "should support whitespace around destination and title (3)" );
assert_eq!( to_html_with_options("[link] (/uri)", &danger)?, "<p>[link] (/uri)</p>", "should not support whitespace between label and resource" );
assert_eq!( to_html_with_options("[link [foo [bar]]](/uri)", &danger)?, "<p><a href=\"/uri\">link [foo [bar]]</a></p>", "should support balanced brackets" );
assert_eq!( to_html_with_options("[link] bar](/uri)", &danger)?, "<p>[link] bar](/uri)</p>", "should not support unbalanced brackets (1)" );
assert_eq!( to_html_with_options("[link [bar](/uri)", &danger)?, "<p>[link <a href=\"/uri\">bar</a></p>", "should not support unbalanced brackets (2)" );
assert_eq!( to_html_with_options("[link \\[bar](/uri)", &danger)?, "<p><a href=\"/uri\">link [bar</a></p>", "should support characer escapes" );
assert_eq!( to_html_with_options("[link *foo **bar** `#`*](/uri)", &danger)?, "<p><a href=\"/uri\">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>", "should support content" );
assert_eq!( to_html_with_options("[](/uri)", &danger)?, "<p><a href=\"/uri\"><img src=\"moon.jpg\" alt=\"moon\" /></a></p>", "should support an image as content" );
assert_eq!( to_html_with_options("[foo [bar](/uri)](/uri)", &danger)?, "<p>[foo <a href=\"/uri\">bar</a>](/uri)</p>", "should not support links in links (1)" );
assert_eq!( to_html_with_options("[foo *[bar [baz](/uri)](/uri)*](/uri)", &danger)?, "<p>[foo <em>[bar <a href=\"/uri\">baz</a>](/uri)</em>](/uri)</p>", "should not support links in links (2)" );
assert_eq!( to_html_with_options("](uri2)](uri3)", &danger)?, "<p><img src=\"uri3\" alt=\"[foo](uri2)\" /></p>", "should not support links in links (3)" );
assert_eq!( to_html_with_options("*[foo*](/uri)", &danger)?, "<p>*<a href=\"/uri\">foo*</a></p>", "should prefer links over emphasis (1)" );
assert_eq!( to_html_with_options("[foo *bar](baz*)", &danger)?, "<p><a href=\"baz*\">foo *bar</a></p>", "should prefer links over emphasis (2)" );
assert_eq!( to_html_with_options("[foo <bar attr=\"](baz)\">", &danger)?, "<p>[foo <bar attr=\"](baz)\"></p>", "should prefer HTML over links" );
assert_eq!( to_html_with_options("[foo`](/uri)`", &danger)?, "<p>[foo<code>](/uri)</code></p>", "should prefer code over links" );
assert_eq!( to_html_with_options("[foo<http://example.com/?search=](uri)>", &danger)?, "<p>[foo<a href=\"http://example.com/?search=%5D(uri)\">http://example.com/?search=](uri)</a></p>", "should prefer autolinks over links" );
assert_eq!( to_html_with_options("[foo<http://example.com/?search=](uri)>", &danger)?, "<p>[foo<a href=\"http://example.com/?search=%5D(uri)\">http://example.com/?search=](uri)</a></p>", "should prefer autolinks over links" );
// Extra assert_eq!( to_html_with_options("[]()", &danger)?, "<p><a href=\"\"></a></p>", "should support an empty link" );
// See: <https://github.com/commonmark/commonmark.js/issues/192> assert_eq!( to_html_with_options("[](<> \"\")", &danger)?, "<p><a href=\"\"></a></p>", "should ignore an empty title" );
assert_eq!( to_html_with_options("[a](<b>\"c\")", &danger)?, "<p>[a](<b>"c")</p>", "should require whitespace between enclosed destination and title" );
assert_eq!( to_html_with_options("[](<", &danger)?, "<p>[](<</p>", "should not support an unclosed enclosed destination" );
assert_eq!( to_html_with_options("[](", &danger)?, "<p>[](</p>", "should not support an unclosed destination" );
assert_eq!( to_html_with_options("[](\\<)", &danger)?, "<p><a href=\"%3C\"></a></p>", "should support unenclosed link destination starting w/ escapes" );
assert_eq!( to_html_with_options("[](<\\<>)", &danger)?, "<p><a href=\"%3C\"></a></p>", "should support enclosed link destination starting w/ escapes" );
assert_eq!( to_html_with_options("[](\\", &danger)?, "<p>[](\\</p>", "should not support unenclosed link destination starting w/ an incorrect escape" );
assert_eq!( to_html_with_options("[](<\\", &danger)?, "<p>[](<\\</p>", "should not support enclosed link destination starting w/ an incorrect escape" );
assert_eq!( to_html_with_options("[](a \"", &danger)?, "<p>[](a "</p>", "should not support an eof in a link title (1)" );
assert_eq!( to_html_with_options("[](a '", &danger)?, "<p>[](a '</p>", "should not support an eof in a link title (2)" );
assert_eq!( to_html_with_options("[](a (", &danger)?, "<p>[](a (</p>", "should not support an eof in a link title (3)" );
assert_eq!( to_html_with_options("[](a \"\\", &danger)?, "<p>[](a "\\</p>", "should not support an eof in a link title escape (1)" );
assert_eq!( to_html_with_options("[](a '\\", &danger)?, "<p>[](a '\\</p>", "should not support an eof in a link title escape (2)" );
assert_eq!( to_html_with_options("[](a (\\", &danger)?, "<p>[](a (\\</p>", "should not support an eof in a link title escape (3)" );
assert_eq!( to_html_with_options("[](a \"\\\"\")", &danger)?, "<p><a href=\"a\" title=\""\"></a></p>", "should support a character escape to start a link title (1)" );
assert_eq!( to_html_with_options("[](a '\\'')", &danger)?, "<p><a href=\"a\" title=\"\'\"></a></p>", "should support a character escape to start a link title (2)" );
assert_eq!( to_html_with_options("[](a (\\)))", &danger)?, "<p><a href=\"a\" title=\")\"></a></p>", "should support a character escape to start a link title (3)" );
assert_eq!( to_html_with_options( "[&©&](example.com/&©& \"&©&\")", &danger )?, "<p><a href=\"example.com/&%C2%A9&\" title=\"&©&\">&©&</a></p>", "should support character references in links" );
assert_eq!( to_html_with_options("[a](1())", &danger)?, "<p><a href=\"1()\">a</a></p>", "should support 1 set of parens" );
assert_eq!( to_html_with_options("[a](1(2()))", &danger)?, "<p><a href=\"1(2())\">a</a></p>", "should support 2 sets of parens" );
assert_eq!( to_html_with_options( "[a](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()))))))))))))))))))))))))))))))))", &danger)?, "<p><a href=\"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())))))))))))))))))))))))))))))))\">a</a></p>", "should support 32 sets of parens" );
assert_eq!( to_html_with_options( "[a](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())))))))))))))))))))))))))))))))))", &danger)?, "<p>[a](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())))))))))))))))))))))))))))))))))</p>", "should not support 33 or more sets of parens" );
assert_eq!( to_html_with_options("[a](b \"\n c\")", &danger)?, "<p><a href=\"b\" title=\"\nc\">a</a></p>", "should support an eol at the start of a title" );
assert_eq!( to_html_with_options("[a](b( \"c\")", &danger)?, "<p>[a](b( "c")</p>", "should not support whitespace when unbalanced in a raw destination" );
assert_eq!( to_html_with_options("[a](\0)", &danger)?, "<p><a href=\"%EF%BF%BD\">a</a></p>", "should support a single NUL character as a link resource" );
Ok(())}