Markdown parser fork with extended syntax for personal use.
at main 239 lines 6.8 kB view raw
1use markdown::{ 2 mdast::{InlineCode, 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 code_text() -> 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("`foo`"), 22 "<p><code>foo</code></p>", 23 "should support code" 24 ); 25 26 assert_eq!( 27 to_html("`` foo ` bar ``"), 28 "<p><code>foo ` bar</code></p>", 29 "should support code w/ more accents" 30 ); 31 32 assert_eq!( 33 to_html("` `` `"), 34 "<p><code>``</code></p>", 35 "should support code w/ fences inside, and padding" 36 ); 37 38 assert_eq!( 39 to_html("` `` `"), 40 "<p><code> `` </code></p>", 41 "should support code w/ extra padding" 42 ); 43 44 assert_eq!( 45 to_html("` a`"), 46 "<p><code> a</code></p>", 47 "should support code w/ unbalanced padding" 48 ); 49 50 assert_eq!( 51 to_html("`\u{a0}b\u{a0}`"), 52 "<p><code>\u{a0}b\u{a0}</code></p>", 53 "should support code w/ non-padding whitespace" 54 ); 55 56 assert_eq!( 57 to_html("` `\n` `"), 58 "<p><code> </code>\n<code> </code></p>", 59 "should support code w/o data" 60 ); 61 62 assert_eq!( 63 to_html("``\nfoo\nbar \nbaz\n``"), 64 "<p><code>foo bar baz</code></p>", 65 "should support code w/o line endings (1)" 66 ); 67 68 assert_eq!( 69 to_html("``\nfoo \n``"), 70 "<p><code>foo </code></p>", 71 "should support code w/o line endings (2)" 72 ); 73 74 assert_eq!( 75 to_html("`foo bar \nbaz`"), 76 "<p><code>foo bar baz</code></p>", 77 "should not support whitespace collapsing" 78 ); 79 80 assert_eq!( 81 to_html("`foo\\`bar`"), 82 "<p><code>foo\\</code>bar`</p>", 83 "should not support character escapes" 84 ); 85 86 assert_eq!( 87 to_html("``foo`bar``"), 88 "<p><code>foo`bar</code></p>", 89 "should support more accents" 90 ); 91 92 assert_eq!( 93 to_html("` foo `` bar `"), 94 "<p><code>foo `` bar</code></p>", 95 "should support less accents" 96 ); 97 98 assert_eq!( 99 to_html("*foo`*`"), 100 "<p>*foo<code>*</code></p>", 101 "should precede over emphasis" 102 ); 103 104 assert_eq!( 105 to_html("[not a `link](/foo`)"), 106 "<p>[not a <code>link](/foo</code>)</p>", 107 "should precede over links" 108 ); 109 110 assert_eq!( 111 to_html("`<a href=\"`\">`"), 112 "<p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>", 113 "should have same precedence as HTML (1)" 114 ); 115 116 assert_eq!( 117 to_html_with_options("<a href=\"`\">`", &danger)?, 118 "<p><a href=\"`\">`</p>", 119 "should have same precedence as HTML (2)" 120 ); 121 122 assert_eq!( 123 to_html("`<http://foo.bar.`baz>`"), 124 "<p><code>&lt;http://foo.bar.</code>baz&gt;`</p>", 125 "should have same precedence as autolinks (1)" 126 ); 127 128 assert_eq!( 129 to_html("<http://foo.bar.`baz>`"), 130 "<p><a href=\"http://foo.bar.%60baz\">http://foo.bar.`baz</a>`</p>", 131 "should have same precedence as autolinks (2)" 132 ); 133 134 assert_eq!( 135 to_html("```foo``"), 136 "<p>```foo``</p>", 137 "should not support more accents before a fence" 138 ); 139 140 assert_eq!( 141 to_html("`foo"), 142 "<p>`foo</p>", 143 "should not support no closing fence (1)" 144 ); 145 146 assert_eq!( 147 to_html("`foo``bar``"), 148 "<p>`foo<code>bar</code></p>", 149 "should not support no closing fence (2)" 150 ); 151 152 // Extra: 153 assert_eq!( 154 to_html("`foo\t\tbar`"), 155 "<p><code>foo\t\tbar</code></p>", 156 "should support tabs in code" 157 ); 158 159 assert_eq!( 160 to_html("\\``x`"), 161 "<p>`<code>x</code></p>", 162 "should support an escaped initial grave accent" 163 ); 164 165 assert_eq!( 166 to_html_with_options( 167 "`a`", 168 &Options { 169 parse: ParseOptions { 170 constructs: Constructs { 171 code_text: false, 172 ..Default::default() 173 }, 174 ..Default::default() 175 }, 176 ..Default::default() 177 } 178 )?, 179 "<p>`a`</p>", 180 "should support turning off code (text)" 181 ); 182 183 assert_eq!( 184 to_mdast("a `alpha` b.", &Default::default())?, 185 Node::Root(Root { 186 children: vec![Node::Paragraph(Paragraph { 187 children: vec![ 188 Node::Text(Text { 189 value: "a ".into(), 190 position: Some(Position::new(1, 1, 0, 1, 3, 2)) 191 }), 192 Node::InlineCode(InlineCode { 193 value: "alpha".into(), 194 position: Some(Position::new(1, 3, 2, 1, 10, 9)) 195 }), 196 Node::Text(Text { 197 value: " b.".into(), 198 position: Some(Position::new(1, 10, 9, 1, 13, 12)) 199 }) 200 ], 201 position: Some(Position::new(1, 1, 0, 1, 13, 12)) 202 })], 203 position: Some(Position::new(1, 1, 0, 1, 13, 12)) 204 }), 205 "should support code (text) as `InlineCode`s in mdast" 206 ); 207 208 assert_eq!( 209 to_mdast("` alpha `", &Default::default())?, 210 Node::Root(Root { 211 children: vec![Node::Paragraph(Paragraph { 212 children: vec![Node::InlineCode(InlineCode { 213 value: " alpha".into(), 214 position: Some(Position::new(1, 1, 0, 1, 11, 10)) 215 }),], 216 position: Some(Position::new(1, 1, 0, 1, 11, 10)) 217 })], 218 position: Some(Position::new(1, 1, 0, 1, 11, 10)) 219 }), 220 "should strip one space from each side of `InlineCode` if the value starts and ends with space" 221 ); 222 223 assert_eq!( 224 to_mdast("` `", &Default::default())?, 225 Node::Root(Root { 226 children: vec![Node::Paragraph(Paragraph { 227 children: vec![Node::InlineCode(InlineCode { 228 value: " ".into(), 229 position: Some(Position::new(1, 1, 0, 1, 6, 5)) 230 }),], 231 position: Some(Position::new(1, 1, 0, 1, 6, 5)) 232 })], 233 position: Some(Position::new(1, 1, 0, 1, 6, 5)) 234 }), 235 "should not strip any whitespace if `InlineCode` is all whitespace" 236 ); 237 238 Ok(()) 239}