extern crate markdown; use markdown::{ mdast::{Node, Paragraph, Root, Text}, to_html, to_html_with_options, to_mdast, unist::Position, CompileOptions, Constructs, Options, ParseOptions, }; use pretty_assertions::assert_eq; #[test] fn character_escape() -> Result<(), String> { let danger = Options { compile: CompileOptions { allow_dangerous_html: true, allow_dangerous_protocol: true, ..Default::default() }, ..Default::default() }; assert_eq!( to_html( "\\!\\\"\\#\\$\\%\\&\\'\\(\\)\\*\\+\\,\\-\\.\\/\\:\\;\\<\\=\\>\\?\\@\\[\\\\\\]\\^\\_\\`\\{\\|\\}\\~"), "

!"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~

", "should support escaped ascii punctuation" ); assert_eq!( to_html("\\→\\A\\a\\ \\3\\φ\\«"), "

\\→\\A\\a\\ \\3\\φ\\«

", "should not support other characters after a backslash" ); assert_eq!( to_html( "\\*not emphasized*\n\\
not a tag\n\\[not a link](/foo)\n\\`not code`\n1\\. not a list\n\\* not a list\n\\# not a heading\n\\[foo]: /url \"not a reference\"\n\\ö not a character entity"), "

*not emphasized*\n<br/> not a tag\n[not a link](/foo)\n`not code`\n1. not a list\n* not a list\n# not a heading\n[foo]: /url "not a reference"\n&ouml; not a character entity

", "should escape other constructs" ); assert_eq!( to_html("foo\\\nbar"), "

foo
\nbar

", "should escape a line break" ); assert_eq!( to_html("`` \\[\\` ``"), "

\\[\\`

", "should not escape in text code" ); assert_eq!( to_html(" \\[\\]"), "
\\[\\]\n
", "should not escape in indented code" ); assert_eq!( to_html(""), "

http://example.com?find=\\*

", "should not escape in autolink" ); assert_eq!( to_html_with_options("", &danger)?, "", "should not escape in flow html" ); assert_eq!( to_html("[foo](/bar\\* \"ti\\*tle\")"), "

foo

", "should escape in resource and title" ); assert_eq!( to_html("[foo]: /bar\\* \"ti\\*tle\"\n\n[foo]"), "

foo

", "should escape in definition resource and title" ); assert_eq!( to_html("``` foo\\+bar\nfoo\n```"), "
foo\n
", "should escape in fenced code info" ); assert_eq!( to_html_with_options( "\\> a", &Options { parse: ParseOptions { constructs: Constructs { character_escape: false, ..Default::default() }, ..Default::default() }, ..Default::default() } )?, "

\\> a

", "should support turning off character escapes" ); assert_eq!( to_mdast("a \\* b", &Default::default())?, Node::Root(Root { children: vec![Node::Paragraph(Paragraph { children: vec![Node::Text(Text { value: "a * b".into(), position: Some(Position::new(1, 1, 0, 1, 7, 6)) }),], position: Some(Position::new(1, 1, 0, 1, 7, 6)) })], position: Some(Position::new(1, 1, 0, 1, 7, 6)) }), "should support character escapes as `Text`s in mdast" ); Ok(()) }