use 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(), "

backlink()

", "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)?, "

link

", "should support links" ); assert_eq!( to_html_with_options("[link](/uri)", &danger)?, "

link

", "should support links w/o title" ); assert_eq!( to_html_with_options("[link]()", &danger)?, "

link

", "should support links w/o destination" ); assert_eq!( to_html_with_options("[link](<>)", &danger)?, "

link

", "should support links w/ empty enclosed destination" ); assert_eq!( to_html_with_options("[link](/my uri)", &danger)?, "

[link](/my uri)

", "should not support links w/ spaces in destination" ); assert_eq!( to_html_with_options("[link]()", &danger)?, "

link

", "should support links w/ spaces in enclosed destination" ); assert_eq!( to_html_with_options("[link](foo\nbar)", &danger)?, "

[link](foo\nbar)

", "should not support links w/ line endings in destination" ); assert_eq!( to_html_with_options("[link]()", &danger)?, "

[link]()

", "should not support links w/ line endings in enclosed destination" ); assert_eq!( to_html_with_options("[a]()", &danger)?, "

a

", "should support links w/ closing parens in destination" ); assert_eq!( to_html_with_options("[link]()", &danger)?, "

[link](<foo>)

", "should not support links w/ enclosed destinations w/o end" ); assert_eq!( to_html_with_options("[a](\n[a](c)", &danger)?, "

[a](<b)c\n[a](<b)c>\n[a](c)

", "should not support links w/ unmatched enclosed destinations" ); assert_eq!( to_html_with_options("[link](\\(foo\\))", &danger)?, "

link

", "should support links w/ destinations w/ escaped parens" ); assert_eq!( to_html_with_options("[link](foo(and(bar)))", &danger)?, "

link

", "should support links w/ destinations w/ balanced parens" ); assert_eq!( to_html_with_options("[link](foo\\(and\\(bar\\))", &danger)?, "

link

", "should support links w/ destinations w/ escaped parens" ); assert_eq!( to_html_with_options("[link]()", &danger)?, "

link

", "should support links w/ enclosed destinations w/ parens" ); assert_eq!( to_html_with_options("[link](foo\\)\\:)", &danger)?, "

link

", "should support links w/ escapes in destinations" ); assert_eq!( to_html_with_options("[link](#fragment)", &danger)?, "

link

", "should support links w/ destinations to fragments" ); assert_eq!( to_html_with_options("[link](http://example.com#fragment)", &danger)?, "

link

", "should support links w/ destinations to URLs w/ fragments" ); assert_eq!( to_html_with_options("[link](http://example.com?foo=3#frag)", &danger)?, "

link

", "should support links w/ destinations to URLs w/ search and fragments" ); assert_eq!( to_html_with_options("[link](foo\\bar)", &danger)?, "

link

", "should not support non-punctuation character escapes in links" ); assert_eq!( to_html_with_options("[link](foo%20bä)", &danger)?, "

link

", "should support character references in links" ); assert_eq!( to_html_with_options("[link](\"title\")", &danger)?, "

link

", "should not support links w/ only a title" ); assert_eq!( to_html_with_options("[link](/url \"title\")", &danger)?, "

link

", "should support titles w/ double quotes" ); assert_eq!( to_html_with_options("[link](/url 'title')", &danger)?, "

link

", "should support titles w/ single quotes" ); assert_eq!( to_html_with_options("[link](/url (title))", &danger)?, "

link

", "should support titles w/ parens" ); assert_eq!( to_html_with_options("[link](/url \"title \\\""\")", &danger)?, "

link

", "should support character references and escapes in titles" ); assert_eq!( to_html_with_options("[link](/url \"title\")", &danger)?, "

link

", "should not support unicode whitespace between destination and title" ); assert_eq!( to_html_with_options("[link](/url \"title \"and\" title\")", &danger)?, "

[link](/url "title "and" title")

", "should not support nested balanced quotes in title" ); assert_eq!( to_html_with_options("[link](/url 'title \"and\" title')", &danger)?, "

link

", "should support the other quotes in titles" ); assert_eq!( to_html_with_options("[link]( /uri\n \"title\" )", &danger)?, "

link

", "should support whitespace around destination and title (1)" ); assert_eq!( to_html_with_options("[link](\t\n/uri \"title\")", &danger)?, "

link

", "should support whitespace around destination and title (2)" ); assert_eq!( to_html_with_options("[link](/uri \"title\"\t\n)", &danger)?, "

link

", "should support whitespace around destination and title (3)" ); assert_eq!( to_html_with_options("[link] (/uri)", &danger)?, "

[link] (/uri)

", "should not support whitespace between label and resource" ); assert_eq!( to_html_with_options("[link [foo [bar]]](/uri)", &danger)?, "

link [foo [bar]]

", "should support balanced brackets" ); assert_eq!( to_html_with_options("[link] bar](/uri)", &danger)?, "

[link] bar](/uri)

", "should not support unbalanced brackets (1)" ); assert_eq!( to_html_with_options("[link [bar](/uri)", &danger)?, "

[link bar

", "should not support unbalanced brackets (2)" ); assert_eq!( to_html_with_options("[link \\[bar](/uri)", &danger)?, "

link [bar

", "should support characer escapes" ); assert_eq!( to_html_with_options("[link *foo **bar** `#`*](/uri)", &danger)?, "

link foo bar #

", "should support content" ); assert_eq!( to_html_with_options("[![moon](moon.jpg)](/uri)", &danger)?, "

\"moon\"

", "should support an image as content" ); assert_eq!( to_html_with_options("[foo [bar](/uri)](/uri)", &danger)?, "

[foo bar](/uri)

", "should not support links in links (1)" ); assert_eq!( to_html_with_options("[foo *[bar [baz](/uri)](/uri)*](/uri)", &danger)?, "

[foo [bar baz](/uri)](/uri)

", "should not support links in links (2)" ); assert_eq!( to_html_with_options("![[[foo](uri1)](uri2)](uri3)", &danger)?, "

\"[foo](uri2)\"

", "should not support links in links (3)" ); assert_eq!( to_html_with_options("*[foo*](/uri)", &danger)?, "

*foo*

", "should prefer links over emphasis (1)" ); assert_eq!( to_html_with_options("[foo *bar](baz*)", &danger)?, "

foo *bar

", "should prefer links over emphasis (2)" ); assert_eq!( to_html_with_options("[foo ", &danger)?, "

[foo

", "should prefer HTML over links" ); assert_eq!( to_html_with_options("[foo`](/uri)`", &danger)?, "

[foo](/uri)

", "should prefer code over links" ); assert_eq!( to_html_with_options("[foo", &danger)?, "

[foohttp://example.com/?search=](uri)

", "should prefer autolinks over links" ); assert_eq!( to_html_with_options("[foo", &danger)?, "

[foohttp://example.com/?search=](uri)

", "should prefer autolinks over links" ); // Extra assert_eq!( to_html_with_options("[]()", &danger)?, "

", "should support an empty link" ); // See: assert_eq!( to_html_with_options("[](<> \"\")", &danger)?, "

", "should ignore an empty title" ); assert_eq!( to_html_with_options("[a](\"c\")", &danger)?, "

[a]("c")

", "should require whitespace between enclosed destination and title" ); assert_eq!( to_html_with_options("[](<", &danger)?, "

[](<

", "should not support an unclosed enclosed destination" ); assert_eq!( to_html_with_options("[](", &danger)?, "

[](

", "should not support an unclosed destination" ); assert_eq!( to_html_with_options("[](\\<)", &danger)?, "

", "should support unenclosed link destination starting w/ escapes" ); assert_eq!( to_html_with_options("[](<\\<>)", &danger)?, "

", "should support enclosed link destination starting w/ escapes" ); assert_eq!( to_html_with_options("[](\\", &danger)?, "

[](\\

", "should not support unenclosed link destination starting w/ an incorrect escape" ); assert_eq!( to_html_with_options("[](<\\", &danger)?, "

[](<\\

", "should not support enclosed link destination starting w/ an incorrect escape" ); assert_eq!( to_html_with_options("[](a \"", &danger)?, "

[](a "

", "should not support an eof in a link title (1)" ); assert_eq!( to_html_with_options("[](a '", &danger)?, "

[](a '

", "should not support an eof in a link title (2)" ); assert_eq!( to_html_with_options("[](a (", &danger)?, "

[](a (

", "should not support an eof in a link title (3)" ); assert_eq!( to_html_with_options("[](a \"\\", &danger)?, "

[](a "\\

", "should not support an eof in a link title escape (1)" ); assert_eq!( to_html_with_options("[](a '\\", &danger)?, "

[](a '\\

", "should not support an eof in a link title escape (2)" ); assert_eq!( to_html_with_options("[](a (\\", &danger)?, "

[](a (\\

", "should not support an eof in a link title escape (3)" ); assert_eq!( to_html_with_options("[](a \"\\\"\")", &danger)?, "

", "should support a character escape to start a link title (1)" ); assert_eq!( to_html_with_options("[](a '\\'')", &danger)?, "

", "should support a character escape to start a link title (2)" ); assert_eq!( to_html_with_options("[](a (\\)))", &danger)?, "

", "should support a character escape to start a link title (3)" ); assert_eq!( to_html_with_options( "[&©&](example.com/&©& \"&©&\")", &danger )?, "

&©&

", "should support character references in links" ); assert_eq!( to_html_with_options("[a](1())", &danger)?, "

a

", "should support 1 set of parens" ); assert_eq!( to_html_with_options("[a](1(2()))", &danger)?, "

a

", "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)?, "

a

", "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)?, "

[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())))))))))))))))))))))))))))))))))

", "should not support 33 or more sets of parens" ); assert_eq!( to_html_with_options("[a](b \"\n c\")", &danger)?, "

a

", "should support an eol at the start of a title" ); assert_eq!( to_html_with_options("[a](b( \"c\")", &danger)?, "

[a](b( "c")

", "should not support whitespace when unbalanced in a raw destination" ); assert_eq!( to_html_with_options("[a](\0)", &danger)?, "

a

", "should support a single NUL character as a link resource" ); Ok(()) }