extern crate micromark; use micromark::{ mdast::{Link, Node, Paragraph, Root, Text}, micromark, micromark_to_mdast, micromark_with_options, unist::Position, CompileOptions, Constructs, Options, ParseOptions, }; use pretty_assertions::assert_eq; #[test] fn autolink() -> Result<(), String> { let danger = Options { compile: CompileOptions { allow_dangerous_html: true, allow_dangerous_protocol: true, ..CompileOptions::default() }, ..Options::default() }; assert_eq!( micromark(""), "

http://foo.bar.baz

", "should support protocol autolinks (1)" ); assert_eq!( micromark(""), "

http://foo.bar.baz/test?q=hello&id=22&boolean

", "should support protocol autolinks (2)" ); assert_eq!( micromark(""), "

irc://foo.bar:2233/baz

", "should support protocol autolinks w/ non-HTTP schemes" ); assert_eq!( micromark(""), "

MAILTO:FOO@BAR.BAZ

", "should support protocol autolinks in uppercase" ); assert_eq!( micromark(""), "

a+b+c:d

", "should support protocol autolinks w/ incorrect URIs (1, default)" ); assert_eq!( micromark_with_options("", &danger)?, "

a+b+c:d

", "should support protocol autolinks w/ incorrect URIs (1, danger)" ); assert_eq!( micromark(""), "

made-up-scheme://foo,bar

", "should support protocol autolinks w/ incorrect URIs (2, default)" ); assert_eq!( micromark_with_options("", &danger)?, "

made-up-scheme://foo,bar

", "should support protocol autolinks w/ incorrect URIs (2, danger)" ); assert_eq!( micromark(""), "

http://../

", "should support protocol autolinks w/ incorrect URIs (3)" ); assert_eq!( micromark_with_options("", &danger)?, "

localhost:5001/foo

", "should support protocol autolinks w/ incorrect URIs (4)" ); assert_eq!( micromark(""), "

<http://foo.bar/baz bim>

", "should not support protocol autolinks w/ spaces" ); assert_eq!( micromark(""), "

http://example.com/\\[\\

", "should not support character escapes in protocol autolinks" ); assert_eq!( micromark(""), "

foo@bar.example.com

", "should support email autolinks (1)" ); assert_eq!( micromark(""), "

foo+special@Bar.baz-bar0.com

", "should support email autolinks (2)" ); assert_eq!( micromark(""), "

a@b.c

", "should support email autolinks (3)" ); assert_eq!( micromark(""), "

<foo+@bar.example.com>

", "should not support character escapes in email autolinks" ); assert_eq!( micromark("<>"), "

<>

", "should not support empty autolinks" ); assert_eq!( micromark("< http://foo.bar >"), "

< http://foo.bar >

", "should not support autolinks w/ space" ); assert_eq!( micromark(""), "

<m:abc>

", "should not support autolinks w/ a single character for a scheme" ); assert_eq!( micromark(""), "

<foo.bar.baz>

", "should not support autolinks w/o a colon or at sign" ); assert_eq!( micromark("http://example.com"), "

http://example.com

", "should not support protocol autolinks w/o angle brackets" ); assert_eq!( micromark("foo@bar.example.com"), "

foo@bar.example.com

", "should not support email autolinks w/o angle brackets" ); // Extra: assert_eq!( micromark("<*@example.com>"), "

*@example.com

", "should support autolinks w/ atext (1)" ); assert_eq!( micromark(""), "

a*@example.com

", "should support autolinks w/ atext (2)" ); assert_eq!( micromark(""), "

aa*@example.com

", "should support autolinks w/ atext (3)" ); assert_eq!( micromark(""), "

<aaa©@example.com>

", "should support non-atext in email autolinks local part (1)" ); assert_eq!( micromark(""), "

<a*a©@example.com>

", "should support non-atext in email autolinks local part (2)" ); assert_eq!( micromark(""), "

<asd@.example.com>

", "should not support a dot after an at sign in email autolinks" ); assert_eq!( micromark(""), "

<asd@e..xample.com>

", "should not support a dot after another dot in email autolinks" ); assert_eq!( micromark( ""), "

asd@012345678901234567890123456789012345678901234567890123456789012

", "should support 63 character in email autolinks domains" ); assert_eq!( micromark(""), "

<asd@0123456789012345678901234567890123456789012345678901234567890123>

", "should not support 64 character in email autolinks domains" ); assert_eq!( micromark( ""), "

asd@012345678901234567890123456789012345678901234567890123456789012.a

", "should support a TLD after a 63 character domain in email autolinks" ); assert_eq!( micromark(""), "

<asd@0123456789012345678901234567890123456789012345678901234567890123.a>

", "should not support a TLD after a 64 character domain in email autolinks" ); assert_eq!( micromark( ""), "

asd@a.012345678901234567890123456789012345678901234567890123456789012

", "should support a 63 character TLD in email autolinks" ); assert_eq!( micromark(""), "

<asd@a.0123456789012345678901234567890123456789012345678901234567890123>

", "should not support a 64 character TLD in email autolinks" ); assert_eq!( micromark(""), "

<asd@-example.com>

", "should not support a dash after `@` in email autolinks" ); assert_eq!( micromark(""), "

asd@e-xample.com

", "should support a dash after other domain characters in email autolinks" ); assert_eq!( micromark(""), "

asd@e--xample.com

", "should support a dash after another dash in email autolinks" ); assert_eq!( micromark(""), "

<asd@example-.com>

", "should not support a dash before a dot in email autolinks" ); assert_eq!( micromark_with_options( "", &Options { parse: ParseOptions { constructs: Constructs { autolink: false, ..Constructs::default() }, ..ParseOptions::default() }, ..Options::default() } )?, "

<a@b.co>

", "should support turning off autolinks" ); assert_eq!( micromark_to_mdast( "a b c.", &ParseOptions::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::Link(Link { url: "https://alpha.com".into(), title: None, children: vec![Node::Text(Text { value: "https://alpha.com".into(), position: Some(Position::new(1, 4, 3, 1, 21, 20)) }),], position: Some(Position::new(1, 3, 2, 1, 22, 21)) }), Node::Text(Text { value: " b ".into(), position: Some(Position::new(1, 22, 21, 1, 25, 24)) }), Node::Link(Link { url: "mailto:bravo@charlie.com".into(), title: None, children: vec![Node::Text(Text { value: "bravo@charlie.com".into(), position: Some(Position::new(1, 26, 25, 1, 43, 42)) }),], position: Some(Position::new(1, 25, 24, 1, 44, 43)) }), Node::Text(Text { value: " c.".into(), position: Some(Position::new(1, 44, 43, 1, 47, 46)) }) ], position: Some(Position::new(1, 1, 0, 1, 47, 46)) })], position: Some(Position::new(1, 1, 0, 1, 47, 46)) }), "should support autolinks as `Link`s in mdast" ); Ok(()) }