Markdown parser fork with extended syntax for personal use.
at hack 325 lines 11 kB view raw
1use markdown::{ 2 mdast::{Link, Node, Paragraph, Root, Text}, 3 message, to_html, to_html_with_options, to_mdast, 4 unist::Position, 5 CompileOptions, Constructs, Options, ParseOptions, 6}; 7use pretty_assertions::assert_eq; 8 9#[test] 10fn autolink() -> Result<(), message::Message> { 11 let danger = Options { 12 compile: CompileOptions { 13 allow_dangerous_html: true, 14 allow_dangerous_protocol: true, 15 ..Default::default() 16 }, 17 ..Default::default() 18 }; 19 20 assert_eq!( 21 to_html("<http://foo.bar.baz>"), 22 "<p><a href=\"http://foo.bar.baz\">http://foo.bar.baz</a></p>", 23 "should support protocol autolinks (1)" 24 ); 25 26 assert_eq!( 27 to_html("<http://foo.bar.baz/test?q=hello&id=22&boolean>"), 28 "<p><a href=\"http://foo.bar.baz/test?q=hello&amp;id=22&amp;boolean\">http://foo.bar.baz/test?q=hello&amp;id=22&amp;boolean</a></p>", 29 "should support protocol autolinks (2)" 30 ); 31 32 assert_eq!( 33 to_html("<irc://foo.bar:2233/baz>"), 34 "<p><a href=\"irc://foo.bar:2233/baz\">irc://foo.bar:2233/baz</a></p>", 35 "should support protocol autolinks w/ non-HTTP schemes" 36 ); 37 38 assert_eq!( 39 to_html("<MAILTO:FOO@BAR.BAZ>"), 40 "<p><a href=\"MAILTO:FOO@BAR.BAZ\">MAILTO:FOO@BAR.BAZ</a></p>", 41 "should support protocol autolinks in uppercase" 42 ); 43 44 assert_eq!( 45 to_html("<a+b+c:d>"), 46 "<p><a href=\"\">a+b+c:d</a></p>", 47 "should support protocol autolinks w/ incorrect URIs (1, default)" 48 ); 49 50 assert_eq!( 51 to_html_with_options("<a+b+c:d>", &danger)?, 52 "<p><a href=\"a+b+c:d\">a+b+c:d</a></p>", 53 "should support protocol autolinks w/ incorrect URIs (1, danger)" 54 ); 55 56 assert_eq!( 57 to_html("<made-up-scheme://foo,bar>"), 58 "<p><a href=\"\">made-up-scheme://foo,bar</a></p>", 59 "should support protocol autolinks w/ incorrect URIs (2, default)" 60 ); 61 62 assert_eq!( 63 to_html_with_options("<made-up-scheme://foo,bar>", &danger)?, 64 "<p><a href=\"made-up-scheme://foo,bar\">made-up-scheme://foo,bar</a></p>", 65 "should support protocol autolinks w/ incorrect URIs (2, danger)" 66 ); 67 68 assert_eq!( 69 to_html("<http://../>"), 70 "<p><a href=\"http://../\">http://../</a></p>", 71 "should support protocol autolinks w/ incorrect URIs (3)" 72 ); 73 74 assert_eq!( 75 to_html_with_options("<localhost:5001/foo>", &danger)?, 76 "<p><a href=\"localhost:5001/foo\">localhost:5001/foo</a></p>", 77 "should support protocol autolinks w/ incorrect URIs (4)" 78 ); 79 80 assert_eq!( 81 to_html("<http://foo.bar/baz bim>"), 82 "<p>&lt;http://foo.bar/baz bim&gt;</p>", 83 "should not support protocol autolinks w/ spaces" 84 ); 85 86 assert_eq!( 87 to_html("<http://example.com/\\[\\>"), 88 "<p><a href=\"http://example.com/%5C%5B%5C\">http://example.com/\\[\\</a></p>", 89 "should not support character escapes in protocol autolinks" 90 ); 91 92 assert_eq!( 93 to_html("<foo@bar.example.com>"), 94 "<p><a href=\"mailto:foo@bar.example.com\">foo@bar.example.com</a></p>", 95 "should support email autolinks (1)" 96 ); 97 98 assert_eq!( 99 to_html("<foo+special@Bar.baz-bar0.com>"), 100 "<p><a href=\"mailto:foo+special@Bar.baz-bar0.com\">foo+special@Bar.baz-bar0.com</a></p>", 101 "should support email autolinks (2)" 102 ); 103 104 assert_eq!( 105 to_html("<a@b.c>"), 106 "<p><a href=\"mailto:a@b.c\">a@b.c</a></p>", 107 "should support email autolinks (3)" 108 ); 109 110 assert_eq!( 111 to_html("<foo\\+@bar.example.com>"), 112 "<p>&lt;foo+@bar.example.com&gt;</p>", 113 "should not support character escapes in email autolinks" 114 ); 115 116 assert_eq!( 117 to_html("<>"), 118 "<p>&lt;&gt;</p>", 119 "should not support empty autolinks" 120 ); 121 122 assert_eq!( 123 to_html("< http://foo.bar >"), 124 "<p>&lt; http://foo.bar &gt;</p>", 125 "should not support autolinks w/ space" 126 ); 127 128 assert_eq!( 129 to_html("<m:abc>"), 130 "<p>&lt;m:abc&gt;</p>", 131 "should not support autolinks w/ a single character for a scheme" 132 ); 133 134 assert_eq!( 135 to_html("<foo.bar.baz>"), 136 "<p>&lt;foo.bar.baz&gt;</p>", 137 "should not support autolinks w/o a colon or at sign" 138 ); 139 140 assert_eq!( 141 to_html("http://example.com"), 142 "<p>http://example.com</p>", 143 "should not support protocol autolinks w/o angle brackets" 144 ); 145 146 assert_eq!( 147 to_html("foo@bar.example.com"), 148 "<p>foo@bar.example.com</p>", 149 "should not support email autolinks w/o angle brackets" 150 ); 151 152 // Extra: 153 assert_eq!( 154 to_html("<*@example.com>"), 155 "<p><a href=\"mailto:*@example.com\">*@example.com</a></p>", 156 "should support autolinks w/ atext (1)" 157 ); 158 assert_eq!( 159 to_html("<a*@example.com>"), 160 "<p><a href=\"mailto:a*@example.com\">a*@example.com</a></p>", 161 "should support autolinks w/ atext (2)" 162 ); 163 assert_eq!( 164 to_html("<aa*@example.com>"), 165 "<p><a href=\"mailto:aa*@example.com\">aa*@example.com</a></p>", 166 "should support autolinks w/ atext (3)" 167 ); 168 169 assert_eq!( 170 to_html("<aaa©@example.com>"), 171 "<p>&lt;aaa©@example.com&gt;</p>", 172 "should support non-atext in email autolinks local part (1)" 173 ); 174 assert_eq!( 175 to_html("<a*a©@example.com>"), 176 "<p>&lt;a*a©@example.com&gt;</p>", 177 "should support non-atext in email autolinks local part (2)" 178 ); 179 180 assert_eq!( 181 to_html("<asd@.example.com>"), 182 "<p>&lt;asd@.example.com&gt;</p>", 183 "should not support a dot after an at sign in email autolinks" 184 ); 185 assert_eq!( 186 to_html("<asd@e..xample.com>"), 187 "<p>&lt;asd@e..xample.com&gt;</p>", 188 "should not support a dot after another dot in email autolinks" 189 ); 190 191 assert_eq!( 192 to_html( 193 "<asd@012345678901234567890123456789012345678901234567890123456789012>"), 194 "<p><a href=\"mailto:asd@012345678901234567890123456789012345678901234567890123456789012\">asd@012345678901234567890123456789012345678901234567890123456789012</a></p>", 195 "should support 63 character in email autolinks domains" 196 ); 197 198 assert_eq!( 199 to_html("<asd@0123456789012345678901234567890123456789012345678901234567890123>"), 200 "<p>&lt;asd@0123456789012345678901234567890123456789012345678901234567890123&gt;</p>", 201 "should not support 64 character in email autolinks domains" 202 ); 203 204 assert_eq!( 205 to_html( 206 "<asd@012345678901234567890123456789012345678901234567890123456789012.a>"), 207 "<p><a href=\"mailto:asd@012345678901234567890123456789012345678901234567890123456789012.a\">asd@012345678901234567890123456789012345678901234567890123456789012.a</a></p>", 208 "should support a TLD after a 63 character domain in email autolinks" 209 ); 210 211 assert_eq!( 212 to_html("<asd@0123456789012345678901234567890123456789012345678901234567890123.a>"), 213 "<p>&lt;asd@0123456789012345678901234567890123456789012345678901234567890123.a&gt;</p>", 214 "should not support a TLD after a 64 character domain in email autolinks" 215 ); 216 217 assert_eq!( 218 to_html( 219 "<asd@a.012345678901234567890123456789012345678901234567890123456789012>"), 220 "<p><a href=\"mailto:asd@a.012345678901234567890123456789012345678901234567890123456789012\">asd@a.012345678901234567890123456789012345678901234567890123456789012</a></p>", 221 "should support a 63 character TLD in email autolinks" 222 ); 223 224 assert_eq!( 225 to_html("<asd@a.0123456789012345678901234567890123456789012345678901234567890123>"), 226 "<p>&lt;asd@a.0123456789012345678901234567890123456789012345678901234567890123&gt;</p>", 227 "should not support a 64 character TLD in email autolinks" 228 ); 229 230 assert_eq!( 231 to_html("<asd@-example.com>"), 232 "<p>&lt;asd@-example.com&gt;</p>", 233 "should not support a dash after `@` in email autolinks" 234 ); 235 236 assert_eq!( 237 to_html("<asd@e-xample.com>"), 238 "<p><a href=\"mailto:asd@e-xample.com\">asd@e-xample.com</a></p>", 239 "should support a dash after other domain characters in email autolinks" 240 ); 241 242 assert_eq!( 243 to_html("<asd@e--xample.com>"), 244 "<p><a href=\"mailto:asd@e--xample.com\">asd@e--xample.com</a></p>", 245 "should support a dash after another dash in email autolinks" 246 ); 247 248 assert_eq!( 249 to_html("<asd@example-.com>"), 250 "<p>&lt;asd@example-.com&gt;</p>", 251 "should not support a dash before a dot in email autolinks" 252 ); 253 254 assert_eq!( 255 to_html("<@example.com>"), 256 "<p>&lt;@example.com&gt;</p>", 257 "should not support an at sign at the start of email autolinks" 258 ); 259 260 assert_eq!( 261 to_html_with_options( 262 "<a@b.co>", 263 &Options { 264 parse: ParseOptions { 265 constructs: Constructs { 266 autolink: false, 267 ..Default::default() 268 }, 269 ..Default::default() 270 }, 271 ..Default::default() 272 } 273 )?, 274 "<p>&lt;a@b.co&gt;</p>", 275 "should support turning off autolinks" 276 ); 277 278 assert_eq!( 279 to_mdast( 280 "a <https://alpha.com> b <bravo@charlie.com> c.", 281 &Default::default() 282 )?, 283 Node::Root(Root { 284 children: vec![Node::Paragraph(Paragraph { 285 children: vec![ 286 Node::Text(Text { 287 value: "a ".into(), 288 position: Some(Position::new(1, 1, 0, 1, 3, 2)) 289 }), 290 Node::Link(Link { 291 url: "https://alpha.com".into(), 292 title: None, 293 children: vec![Node::Text(Text { 294 value: "https://alpha.com".into(), 295 position: Some(Position::new(1, 4, 3, 1, 21, 20)) 296 }),], 297 position: Some(Position::new(1, 3, 2, 1, 22, 21)) 298 }), 299 Node::Text(Text { 300 value: " b ".into(), 301 position: Some(Position::new(1, 22, 21, 1, 25, 24)) 302 }), 303 Node::Link(Link { 304 url: "mailto:bravo@charlie.com".into(), 305 title: None, 306 children: vec![Node::Text(Text { 307 value: "bravo@charlie.com".into(), 308 position: Some(Position::new(1, 26, 25, 1, 43, 42)) 309 }),], 310 position: Some(Position::new(1, 25, 24, 1, 44, 43)) 311 }), 312 Node::Text(Text { 313 value: " c.".into(), 314 position: Some(Position::new(1, 44, 43, 1, 47, 46)) 315 }) 316 ], 317 position: Some(Position::new(1, 1, 0, 1, 47, 46)) 318 })], 319 position: Some(Position::new(1, 1, 0, 1, 47, 46)) 320 }), 321 "should support autolinks as `Link`s in mdast" 322 ); 323 324 Ok(()) 325}