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)?, "", "should support links" ); assert_eq!( to_html_with_options("[link](/uri)", &danger)?, "", "should support links w/o title" ); assert_eq!( to_html_with_options("[link]()", &danger)?, "", "should support links w/o destination" ); assert_eq!( to_html_with_options("[link](<>)", &danger)?, "", "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)?, "", "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]([link](
[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)?, "", "should support links w/ destinations w/ escaped parens" ); assert_eq!( to_html_with_options("[link](foo(and(bar)))", &danger)?, "", "should support links w/ destinations w/ balanced parens" ); assert_eq!( to_html_with_options("[link](foo\\(and\\(bar\\))", &danger)?, "", "should support links w/ destinations w/ escaped parens" ); assert_eq!( to_html_with_options("[link]([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)?, "", "should support the other quotes in titles" ); assert_eq!( to_html_with_options("[link]( /uri\n \"title\" )", &danger)?, "", "should support whitespace around destination and title (1)" ); assert_eq!( to_html_with_options("[link](\t\n/uri \"title\")", &danger)?, "", "should support whitespace around destination and title (2)" ); assert_eq!( to_html_with_options("[link](/uri \"title\"\t\n)", &danger)?, "", "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)?, "", "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)?, "", "should support characer escapes" ); assert_eq!( to_html_with_options("[link *foo **bar** `#`*](/uri)", &danger)?, "", "should support content" ); assert_eq!( to_html_with_options("[](/uri)", &danger)?, "", "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("](uri2)](uri3)", &danger)?, "*foo*
", "should prefer links over emphasis (1)" ); assert_eq!( to_html_with_options("[foo *bar](baz*)", &danger)?, "", "should prefer links over emphasis (2)" ); assert_eq!( to_html_with_options("[foo[foo
[foo](/uri)
[foohttp://example.com/?search=](uri)
", "should prefer autolinks over links" ); assert_eq!( to_html_with_options("[foo[foohttp://example.com/?search=](uri)
", "should prefer autolinks over links" ); // Extra assert_eq!( to_html_with_options("[]()", &danger)?, "", "should support an empty link" ); // See:[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)?, "", "should support 1 set of parens" ); assert_eq!( to_html_with_options("[a](1(2()))", &danger)?, "", "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)?, "", "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)?, "", "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)?, "", "should support a single NUL character as a link resource" ); Ok(()) }