Something went wrong. Try again.
Markdown parser fork with extended syntax for personal use.
Something went wrong. Try again.
12 kB · 356 lines
Rust
at hack
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357use markdown::{ mdast::{Definition, Image, ImageReference, Node, Paragraph, ReferenceKind, Root, Text}, message, to_html, to_html_with_options, to_mdast, unist::Position, CompileOptions, Constructs, Options, ParseOptions,};use pretty_assertions::assert_eq;
#[test]fn image() -> Result<(), message::Message> { assert_eq!( to_html("[link](/uri \"title\")"), "<p><a href=\"/uri\" title=\"title\">link</a></p>", "should support links" ); assert_eq!( to_html(""), "<p><img src=\"/url\" alt=\"foo\" title=\"title\" /></p>", "should support image w/ resource" );
assert_eq!( to_html("[foo *bar*]: train.jpg \"train & tracks\"\n\n![foo *bar*]"), "<p><img src=\"train.jpg\" alt=\"foo bar\" title=\"train & tracks\" /></p>", "should support image as shortcut reference" );
assert_eq!( to_html("](/url2)"), "<p><img src=\"/url2\" alt=\"foo bar\" /></p>", "should “support” images in images" );
assert_eq!( to_html("](/url2)"), "<p><img src=\"/url2\" alt=\"foo bar\" /></p>", "should “support” links in images" );
assert_eq!( to_html("[foo *bar*]: train.jpg \"train & tracks\"\n\n![foo *bar*][]"), "<p><img src=\"train.jpg\" alt=\"foo bar\" title=\"train & tracks\" /></p>", "should support “content” in images" );
assert_eq!( to_html("[FOOBAR]: train.jpg \"train & tracks\"\n\n![foo *bar*][foobar]"), "<p><img src=\"train.jpg\" alt=\"foo bar\" title=\"train & tracks\" /></p>", "should support “content” in images" );
assert_eq!( to_html(""), "<p><img src=\"train.jpg\" alt=\"foo\" /></p>", "should support images w/o title" );
assert_eq!( to_html("My "), "<p>My <img src=\"/path/to/train.jpg\" alt=\"foo bar\" title=\"title\" /></p>", "should support images w/ lots of whitespace" );
assert_eq!( to_html(""), "<p><img src=\"url\" alt=\"foo\" /></p>", "should support images w/ enclosed destinations" );
assert_eq!( to_html(""), "<p><img src=\"/url\" alt=\"\" /></p>", "should support images w/ empty labels" );
assert_eq!( to_html("[bar]: /url\n\n![foo][bar]"), "<p><img src=\"/url\" alt=\"foo\" /></p>", "should support full references (1)" );
assert_eq!( to_html("[BAR]: /url\n\n![foo][bar]"), "<p><img src=\"/url\" alt=\"foo\" /></p>", "should support full references (2)" );
assert_eq!( to_html("[foo]: /url \"title\"\n\n![foo][]"), "<p><img src=\"/url\" alt=\"foo\" title=\"title\" /></p>", "should support collapsed references (1)" );
assert_eq!( to_html("[*foo* bar]: /url \"title\"\n\n![*foo* bar][]"), "<p><img src=\"/url\" alt=\"foo bar\" title=\"title\" /></p>", "should support collapsed references (2)" );
assert_eq!( to_html("[foo]: /url \"title\"\n\n![Foo][]"), "<p><img src=\"/url\" alt=\"Foo\" title=\"title\" /></p>", "should support case-insensitive labels" );
assert_eq!( to_html("[foo]: /url \"title\"\n\n![foo] \n[]"), "<p><img src=\"/url\" alt=\"foo\" title=\"title\" />\n[]</p>", "should not support whitespace between sets of brackets" );
assert_eq!( to_html("[foo]: /url \"title\"\n\n![foo]"), "<p><img src=\"/url\" alt=\"foo\" title=\"title\" /></p>", "should support shortcut references (1)" );
assert_eq!( to_html("[*foo* bar]: /url \"title\"\n\n![*foo* bar]"), "<p><img src=\"/url\" alt=\"foo bar\" title=\"title\" /></p>", "should support shortcut references (2)" );
assert_eq!( to_html("[[foo]]: /url \"title\"\n\n![[foo]]"), "<p>[[foo]]: /url "title"</p>\n<p>![[foo]]</p>", "should not support link labels w/ unescaped brackets" );
assert_eq!( to_html("[foo]: /url \"title\"\n\n![Foo]"), "<p><img src=\"/url\" alt=\"Foo\" title=\"title\" /></p>", "should support case-insensitive label matching" );
assert_eq!( to_html("[foo]: /url \"title\"\n\n!\\[foo]"), "<p>![foo]</p>", "should “support” an escaped bracket instead of an image" );
assert_eq!( to_html("[foo]: /url \"title\"\n\n\\![foo]"), "<p>!<a href=\"/url\" title=\"title\">foo</a></p>", "should support an escaped bang instead of an image, but still have a link" );
// Extra assert_eq!( to_html("![foo]()"), "<p><img src=\"\" alt=\"foo\" /></p>", "should support images w/o destination" );
assert_eq!( to_html(""), "<p><img src=\"\" alt=\"foo\" /></p>", "should support images w/ explicit empty destination" );
assert_eq!( to_html(""), "<p><img src=\"example.png\" alt=\"\" /></p>", "should support images w/o alt" );
assert_eq!( to_html(""), "<p><img src=\"bravo.png\" alt=\"alpha\" /></p>", "should support images w/ empty title (1)" );
assert_eq!( to_html(""), "<p><img src=\"bravo.png\" alt=\"alpha\" /></p>", "should support images w/ empty title (2)" );
assert_eq!( to_html(")"), "<p><img src=\"bravo.png\" alt=\"alpha\" /></p>", "should support images w/ empty title (3)" );
assert_eq!( to_html(""), "<p><img src=\"example.com/&%C2%A9&\" alt=\"&©&\" title=\"&©&\" /></p>", "should support character references in images" );
// Extra // See: <https://github.com/commonmark/commonmark.js/issues/192> assert_eq!( to_html(""), "<p><img src=\"\" alt=\"\" /></p>", "should ignore an empty title" );
assert_eq!( to_html_with_options( "![x]()", &Options { parse: ParseOptions { constructs: Constructs { label_start_image: false, ..Default::default() }, ..Default::default() }, ..Default::default() } )?, "<p>!<a href=\"\">x</a></p>", "should support turning off label start (image)" );
assert_eq!( to_html(")"), "<p><img src=\"\" alt=\"\" /></p>", "should ignore non-http protocols by default" );
assert_eq!( to_html_with_options( ")", &Options { compile: CompileOptions { allow_dangerous_protocol: true, ..Default::default() }, ..Default::default() } )?, "<p><img src=\"javascript:alert(1)\" alt=\"\" /></p>", "should allow non-http protocols w/ `allowDangerousProtocol`" );
assert_eq!( to_html_with_options( ")", &Options { compile: CompileOptions { allow_any_img_src: true, allow_dangerous_protocol: false, ..Default::default() }, ..Default::default() } )?, "<p><img src=\"javascript:alert(1)\" alt=\"\" /></p>", "should allow non-http protocols with the `allow_any_img_src` option" );
assert_eq!( to_mdast( "a ![alpha]() b  c.", &Default::default() )?, Node::Root(Root { children: vec![Node::Paragraph(Paragraph { children: vec![ Node::Text(Text { value: "a ".into(), position: Some(Position::new(1, 1, 0, 1, 3, 2)) }), Node::Image(Image { alt: "alpha".into(), url: String::new(), title: None, position: Some(Position::new(1, 3, 2, 1, 13, 12)) }), Node::Text(Text { value: " b ".into(), position: Some(Position::new(1, 13, 12, 1, 16, 15)) }), Node::Image(Image { alt: "bravo".into(), url: "charlie".into(), title: Some("delta".into()), position: Some(Position::new(1, 16, 15, 1, 41, 40)) }), Node::Text(Text { value: " c.".into(), position: Some(Position::new(1, 41, 40, 1, 44, 43)) }) ], position: Some(Position::new(1, 1, 0, 1, 44, 43)) })], position: Some(Position::new(1, 1, 0, 1, 44, 43)) }), "should support image (resource) as `Image`s in mdast" );
assert_eq!( to_mdast( "[x]: y\n\na ![x] b ![x][] c ![d][x] e.", &Default::default() )?, Node::Root(Root { children: vec![ Node::Definition(Definition { identifier: "x".into(), label: Some("x".into()), url: "y".into(), title: None, position: Some(Position::new(1, 1, 0, 1, 7, 6)) }), Node::Paragraph(Paragraph { children: vec![ Node::Text(Text { value: "a ".into(), position: Some(Position::new(3, 1, 8, 3, 3, 10)) }), Node::ImageReference(ImageReference { reference_kind: ReferenceKind::Shortcut, identifier: "x".into(), label: Some("x".into()), alt: "x".into(), position: Some(Position::new(3, 3, 10, 3, 7, 14)) }), Node::Text(Text { value: " b ".into(), position: Some(Position::new(3, 7, 14, 3, 10, 17)) }), Node::ImageReference(ImageReference { reference_kind: ReferenceKind::Collapsed, identifier: "x".into(), label: Some("x".into()), alt: "x".into(), position: Some(Position::new(3, 10, 17, 3, 16, 23)) }), Node::Text(Text { value: " c ".into(), position: Some(Position::new(3, 16, 23, 3, 19, 26)) }), Node::ImageReference(ImageReference { reference_kind: ReferenceKind::Full, identifier: "x".into(), label: Some("x".into()), alt: "d".into(), position: Some(Position::new(3, 19, 26, 3, 26, 33)) }), Node::Text(Text { value: " e.".into(), position: Some(Position::new(3, 26, 33, 3, 29, 36)) }), ], position: Some(Position::new(3, 1, 8, 3, 29, 36)) }), ], position: Some(Position::new(1, 1, 0, 3, 29, 36)) }), "should support image (reference) as `ImageReference`s in mdast" ); Ok(())}