extern crate micromark; use micromark::{ mdast::{Definition, LinkReference, Node, Paragraph, ReferenceKind, Root, Text}, micromark, micromark_to_mdast, micromark_with_options, unist::Position, Constructs, Options, }; use pretty_assertions::assert_eq; #[test] fn link_reference() -> Result<(), String> { let danger = Options { allow_dangerous_html: true, allow_dangerous_protocol: true, ..Options::default() }; assert_eq!( micromark("[bar]: /url \"title\"\n\n[foo][bar]"), "

foo

", "should support link references" ); assert_eq!( micromark("[ref]: /uri\n\n[link [foo [bar]]][ref]"), "

link [foo [bar]]

", "should support balanced brackets in link references" ); assert_eq!( micromark("[ref]: /uri\n\n[link \\[bar][ref]"), "

link [bar

", "should support escaped brackets in link references" ); assert_eq!( micromark("[ref]: /uri\n\n[link *foo **bar** `#`*][ref]"), "

link foo bar #

", "should support content in link references" ); assert_eq!( micromark("[ref]: /uri\n\n[![moon](moon.jpg)][ref]"), "

\"moon\"

", "should support images in link references" ); assert_eq!( micromark("[ref]: /uri\n\n[foo [bar](/uri)][ref]"), "

[foo bar]ref

", "should not support links in link references" ); assert_eq!( micromark("[ref]: /uri\n\n[foo *bar [baz][ref]*][ref]"), "

[foo bar baz]ref

", "should not support deep links in link references" ); assert_eq!( micromark("[ref]: /uri\n\n*[foo*][ref]"), "

*foo*

", "should prefer link references over emphasis (1)" ); assert_eq!( micromark("[ref]: /uri\n\n[foo *bar][ref]"), "

foo *bar

", "should prefer link references over emphasis (2)" ); assert_eq!( micromark_with_options("[ref]: /uri\n\n[foo ", &danger)?, "

[foo

", "should prefer HTML over link references" ); assert_eq!( micromark("[ref]: /uri\n\n[foo`][ref]`"), "

[foo][ref]

", "should prefer code over link references" ); assert_eq!( micromark("[ref]: /uri\n\n[foo"), "

[foohttp://example.com/?search=][ref]

", "should prefer autolinks over link references" ); assert_eq!( micromark("[bar]: /url \"title\"\n\n[foo][BaR]"), "

foo

", "should match references to definitions case-insensitively" ); assert_eq!( micromark("[ТОЛПОЙ]: /url\n\n[Толпой][Толпой] is a Russian word."), "

Толпой is a Russian word.

", "should match references to definitions w/ unicode case-folding" ); assert_eq!( micromark("[Foo\n bar]: /url\n\n[Baz][Foo bar]"), "

Baz

", "should match references to definitions w/ collapsing" ); assert_eq!( micromark("[bar]: /url \"title\"\n\n[foo] [bar]"), "

[foo] bar

", "should not support whitespace between label and reference (1)" ); assert_eq!( micromark("[bar]: /url \"title\"\n\n[foo]\n[bar]"), "

[foo]\nbar

", "should not support whitespace between label and reference (2)" ); assert_eq!( micromark("[foo]: /url1\n\n[foo]: /url2\n\n[bar][foo]"), "

bar

", "should prefer earlier definitions" ); assert_eq!( micromark("[foo!]: /url\n\n[bar][foo\\!]"), "

[bar][foo!]

", "should not match references to definitions w/ escapes" ); assert_eq!( micromark("[ref[]: /uri\n\n[foo][ref[]"), "

[ref[]: /uri

\n

[foo][ref[]

", "should not support references w/ brackets (1)" ); assert_eq!( micromark("[ref[bar]]: /uri\n\n[foo][ref[bar]]"), "

[ref[bar]]: /uri

\n

[foo][ref[bar]]

", "should not support references w/ brackets (2)" ); assert_eq!( micromark("[[[foo]]]: /url\n\n[[[foo]]]"), "

[[[foo]]]: /url

\n

[[[foo]]]

", "should not support references w/ brackets (3)" ); assert_eq!( micromark("[ref\\[]: /uri\n\n[foo][ref\\[]"), "

foo

", "should match references to definitions w/ matching escapes" ); assert_eq!( micromark("[bar\\\\]: /uri\n\n[bar\\\\]"), "

bar\\

", "should support escapes" ); assert_eq!( micromark("[]: /uri\n\n[]"), "

[]: /uri

\n

[]

", "should not support empty references" ); assert_eq!( micromark("[\n ]: /uri\n\n[\n ]"), "

[\n]: /uri

\n

[\n]

", "should not support blank references" ); assert_eq!( micromark("[foo]: /url \"title\"\n\n[foo][]"), "

foo

", "should support collaped references" ); assert_eq!( micromark("[*foo* bar]: /url \"title\"\n\n[*foo* bar][]"), "

foo bar

", "should support content in collaped references" ); assert_eq!( micromark("[foo]: /url \"title\"\n\n[Foo][]"), "

Foo

", "should match references to definitions case-insensitively" ); assert_eq!( micromark("[foo]: /url \"title\"\n\n[foo] \n[]"), "

foo\n[]

", "should not support whitespace between label and collaped reference" ); assert_eq!( micromark("[foo]: /url \"title\"\n\n[foo]"), "

foo

", "should support shortcut references" ); assert_eq!( micromark("[*foo* bar]: /url \"title\"\n\n[*foo* bar]"), "

foo bar

", "should support content in shortcut references (1)" ); assert_eq!( micromark("[*foo* bar]: /url \"title\"\n\n[[*foo* bar]]"), "

[foo bar]

", "should support content in shortcut references (2)" ); assert_eq!( micromark("[foo]: /url\n\n[[bar [foo]"), "

[[bar foo

", "should support content in shortcut references (3)" ); assert_eq!( micromark("[foo]: /url \"title\"\n\n[Foo]"), "

Foo

", "should match shortcut references to definitions case-insensitively" ); assert_eq!( micromark("[foo]: /url\n\n[foo] bar"), "

foo bar

", "should support whitespace after a shortcut reference" ); assert_eq!( micromark("[foo]: /url \"title\"\n\n\\[foo]"), "

[foo]

", "should “support” an escaped shortcut reference" ); assert_eq!( micromark("[foo*]: /url\n\n*[foo*]"), "

*foo*

", "should prefer shortcut references over emphasis" ); assert_eq!( micromark("[foo]: /url1\n[bar]: /url2\n\n[foo][bar]"), "

foo

", "should prefer full references over shortcut references" ); assert_eq!( micromark("[foo]: /url1\n\n[foo][]"), "

foo

", "should prefer collapsed references over shortcut references" ); assert_eq!( micromark("[foo]: /url\n\n[foo]()"), "

foo

", "should prefer resources over shortcut references (1)" ); assert_eq!( micromark("[foo]: /url \"title\"\n\n[foo]()"), "

foo

", "should prefer resources over shortcut references (2)" ); assert_eq!( micromark("[foo]: /url1\n\n[foo](not a link)"), "

foo(not a link)

", "should support shortcut references when followed by nonconforming resources" ); assert_eq!( micromark("[baz]: /url\n\n[foo][bar][baz]"), "

[foo]bar

", "stable/unstable (1)" ); assert_eq!( micromark("[baz]: /url1\n[bar]: /url2\n\n[foo][bar][baz]"), "

foobaz

", "stable/unstable (2)" ); assert_eq!( micromark("[baz]: /url1\n[foo]: /url2\n\n[foo][bar][baz]"), "

[foo]bar

", "stable/unstable (3)" ); // Extra // This matches most implimentations, but is not strictly according to spec. // See: assert_eq!( micromark("[x]: /url\n\n[x][ ], [x][\t], [x][\n], [x][]"), "

[x][ ], [x][\t], [x][\n], x

", "should not support whitespace-only full references" ); // See also: assert_eq!( micromark("[+]: example.com\n[\\;]: example.com\n\nWill it link? [\\+], [;]"), "

Will it link? [+], [;]

", "should not support mismatched character escapes in shortcuts" ); assert_eq!( micromark("[©]: example.com\n[&]: example.com\n\nWill it link? [©], [&]"), "

Will it link? [©], [&]

", "should not support mismatched character references in shortcuts" ); assert_eq!( micromark("[+]: example.com\n[\\;]: example.com\n\nWill it link? [\\+][], [;][]"), "

Will it link? [+][], [;][]

", "should not support mismatched character escapes in collapsed" ); assert_eq!( micromark("[©]: example.com\n[&]: example.com\n\nWill it link? [©][], [&][]"), "

Will it link? [©][], [&][]

", "should not support mismatched character references in collapsed" ); assert_eq!( micromark("[+]: example.com\n[\\;]: example.com\n\nWill it link? [a][ \\+ ], [b][ ; ]"), "

Will it link? [a][ + ], [b][ ; ]

", "should not support mismatched character escapes in fulls" ); assert_eq!( micromark( "[©]: example.com\n[&]: example.com\n\nWill it link? [a][ © ], [b][ & ]" ), "

Will it link? [a][ © ], [b][ & ]

", "should not support mismatched character references in fulls" ); assert_eq!( micromark( "[*f*][] [;][] [\\;][] [;][] [*f*;][] [*f*\\;][] [*f*;][] [*f*]: alpha [;]: bravo [\\;]: charlie [;]: delta [*f*;]: echo [*f*\\;]: foxtrot [*f*;]: golf" ), "

f ; ; ; f; f; f;

", "should properly handle labels w/ character references and -escapes, and phrasing" ); // 999 `x` characters. let max = "x".repeat(999); assert_eq!( micromark(format!("[{}]: a\n[y][{}]", max, max).as_str()), "

y

", "should support 999 characters in a reference" ); assert_eq!( micromark(format!("[{}x]: a\n[y][{}x]", max, max).as_str()), format!("

[{}x]: a\n[y][{}x]

", max, max), "should not support 1000 characters in a reference" ); assert_eq!( micromark("[x] missing-colon\n\nWill it link? [x]"), "

[x] missing-colon

\n

Will it link? [x]

", "should not fail on a missing colon in a definition" ); assert_eq!( micromark_with_options( "[x]()", &Options { constructs: Constructs { label_start_link: false, ..Constructs::default() }, ..Options::default() } )?, "

[x]()

", "should support turning off label start (link)" ); assert_eq!( micromark_with_options( "[x]()", &Options { constructs: Constructs { label_end: false, ..Constructs::default() }, ..Options::default() } )?, "

[x]()

", "should support turning off label end" ); assert_eq!( micromark_to_mdast("[x]: y\n\na [x] b [x][] c [d][x] e.", &Options::default())?, Node::Root(Root { children: vec![ Node::Definition(Definition { identifier: "x".to_string(), label: Some("x".to_string()), url: "y".to_string(), title: None, position: Some(Position::new(1, 1, 0, 1, 7, 6)) }), Node::Paragraph(Paragraph { children: vec![ Node::Text(Text { value: "a ".to_string(), position: Some(Position::new(3, 1, 8, 3, 3, 10)) }), Node::LinkReference(LinkReference { reference_kind: ReferenceKind::Shortcut, identifier: "x".to_string(), label: Some("x".to_string()), children: vec![Node::Text(Text { value: "x".to_string(), position: Some(Position::new(3, 4, 11, 3, 5, 12)) }),], position: Some(Position::new(3, 3, 10, 3, 6, 13)) }), Node::Text(Text { value: " b ".to_string(), position: Some(Position::new(3, 6, 13, 3, 9, 16)) }), Node::LinkReference(LinkReference { reference_kind: ReferenceKind::Collapsed, identifier: "x".to_string(), label: Some("x".to_string()), children: vec![Node::Text(Text { value: "x".to_string(), position: Some(Position::new(3, 10, 17, 3, 11, 18)) }),], position: Some(Position::new(3, 9, 16, 3, 14, 21)) }), Node::Text(Text { value: " c ".to_string(), position: Some(Position::new(3, 14, 21, 3, 17, 24)) }), Node::LinkReference(LinkReference { reference_kind: ReferenceKind::Full, identifier: "x".to_string(), label: Some("x".to_string()), children: vec![Node::Text(Text { value: "d".to_string(), position: Some(Position::new(3, 18, 25, 3, 19, 26)) }),], position: Some(Position::new(3, 17, 24, 3, 23, 30)) }), Node::Text(Text { value: " e.".to_string(), position: Some(Position::new(3, 23, 30, 3, 26, 33)) }), ], position: Some(Position::new(3, 1, 8, 3, 26, 33)) }), ], position: Some(Position::new(1, 1, 0, 3, 26, 33)) }), "should support link (reference) as `LinkReference`s in mdast" ); Ok(()) }