Markdown parser fork with extended syntax for personal use.
at hack 244 lines 6.2 kB view raw
1use markdown::{ 2 mdast::{Heading, Node, Root, Text}, 3 message, to_html, to_html_with_options, to_mdast, 4 unist::Position, 5 Constructs, Options, ParseOptions, 6}; 7use pretty_assertions::assert_eq; 8 9#[test] 10fn heading_atx() -> Result<(), message::Message> { 11 assert_eq!( 12 to_html("# foo"), 13 "<h1>foo</h1>", 14 "should support a heading w/ rank 1" 15 ); 16 17 assert_eq!( 18 to_html("## foo"), 19 "<h2>foo</h2>", 20 "should support a heading w/ rank 2" 21 ); 22 23 assert_eq!( 24 to_html("### foo"), 25 "<h3>foo</h3>", 26 "should support a heading w/ rank 3" 27 ); 28 29 assert_eq!( 30 to_html("#### foo"), 31 "<h4>foo</h4>", 32 "should support a heading w/ rank 4" 33 ); 34 35 assert_eq!( 36 to_html("##### foo"), 37 "<h5>foo</h5>", 38 "should support a heading w/ rank 5" 39 ); 40 41 assert_eq!( 42 to_html("###### foo"), 43 "<h6>foo</h6>", 44 "should support a heading w/ rank 6" 45 ); 46 47 assert_eq!( 48 to_html("####### foo"), 49 "<p>####### foo</p>", 50 "should not support a heading w/ rank 7" 51 ); 52 53 assert_eq!( 54 to_html("#5 bolt"), 55 "<p>#5 bolt</p>", 56 "should not support a heading for a number sign not followed by whitespace (1)" 57 ); 58 59 assert_eq!( 60 to_html("#hashtag"), 61 "<p>#hashtag</p>", 62 "should not support a heading for a number sign not followed by whitespace (2)" 63 ); 64 65 assert_eq!( 66 to_html("\\## foo"), 67 "<p>## foo</p>", 68 "should not support a heading for an escaped number sign" 69 ); 70 71 assert_eq!( 72 to_html("# foo *bar* \\*baz\\*"), 73 "<h1>foo <em>bar</em> *baz*</h1>", 74 "should support text content in headings" 75 ); 76 77 assert_eq!( 78 to_html("# foo "), 79 "<h1>foo</h1>", 80 "should support arbitrary initial and final whitespace" 81 ); 82 83 assert_eq!( 84 to_html(" ### foo"), 85 "<h3>foo</h3>", 86 "should support an initial space" 87 ); 88 89 assert_eq!( 90 to_html(" ## foo"), 91 "<h2>foo</h2>", 92 "should support two initial spaces" 93 ); 94 95 assert_eq!( 96 to_html(" # foo"), 97 "<h1>foo</h1>", 98 "should support three initial spaces" 99 ); 100 101 assert_eq!( 102 to_html(" # foo"), 103 "<pre><code># foo\n</code></pre>", 104 "should not support four initial spaces" 105 ); 106 107 assert_eq!( 108 to_html("foo\n # bar"), 109 "<p>foo\n# bar</p>", 110 "should not support four initial spaces when interrupting" 111 ); 112 113 assert_eq!( 114 to_html("## foo ##"), 115 "<h2>foo</h2>", 116 "should support a closing sequence (1)" 117 ); 118 119 assert_eq!( 120 to_html(" ### bar ###"), 121 "<h3>bar</h3>", 122 "should support a closing sequence (2)" 123 ); 124 125 assert_eq!( 126 to_html("# foo ##################################"), 127 "<h1>foo</h1>", 128 "should support a closing sequence w/ an arbitrary number of number signs (1)" 129 ); 130 131 assert_eq!( 132 to_html("##### foo ##"), 133 "<h5>foo</h5>", 134 "should support a closing sequence w/ an arbitrary number of number signs (2)" 135 ); 136 137 assert_eq!( 138 to_html("### foo ### "), 139 "<h3>foo</h3>", 140 "should support trailing whitespace after a closing sequence" 141 ); 142 143 assert_eq!( 144 to_html("### foo ### b"), 145 "<h3>foo ### b</h3>", 146 "should not support other content after a closing sequence" 147 ); 148 149 assert_eq!( 150 to_html("# foo#"), 151 "<h1>foo#</h1>", 152 "should not support a closing sequence w/o whitespace before it" 153 ); 154 155 assert_eq!( 156 to_html("### foo \\###"), 157 "<h3>foo ###</h3>", 158 "should not support an “escaped” closing sequence (1)" 159 ); 160 161 assert_eq!( 162 to_html("## foo #\\##"), 163 "<h2>foo ###</h2>", 164 "should not support an “escaped” closing sequence (2)" 165 ); 166 167 assert_eq!( 168 to_html("# foo \\#"), 169 "<h1>foo #</h1>", 170 "should not support an “escaped” closing sequence (3)" 171 ); 172 173 assert_eq!( 174 to_html("****\n## foo\n****"), 175 "<hr />\n<h2>foo</h2>\n<hr />", 176 "should support atx headings when not surrounded by blank lines" 177 ); 178 179 assert_eq!( 180 to_html("Foo bar\n# baz\nBar foo"), 181 "<p>Foo bar</p>\n<h1>baz</h1>\n<p>Bar foo</p>", 182 "should support atx headings interrupting paragraphs" 183 ); 184 185 assert_eq!( 186 to_html("## \n#\n### ###"), 187 "<h2></h2>\n<h1></h1>\n<h3></h3>", 188 "should support empty atx headings (1)" 189 ); 190 191 assert_eq!( 192 to_html("#\na\n# b"), 193 "<h1></h1>\n<p>a</p>\n<h1>b</h1>", 194 "should support empty atx headings (2)" 195 ); 196 197 assert_eq!( 198 to_html("> #\na"), 199 "<blockquote>\n<h1></h1>\n</blockquote>\n<p>a</p>", 200 "should not support lazyness (1)" 201 ); 202 203 assert_eq!( 204 to_html("> a\n#"), 205 "<blockquote>\n<p>a</p>\n</blockquote>\n<h1></h1>", 206 "should not support lazyness (2)" 207 ); 208 209 assert_eq!( 210 to_html_with_options( 211 "# a", 212 &Options { 213 parse: ParseOptions { 214 constructs: Constructs { 215 heading_atx: false, 216 ..Default::default() 217 }, 218 ..Default::default() 219 }, 220 ..Default::default() 221 } 222 )?, 223 "<p># a</p>", 224 "should support turning off heading (atx)" 225 ); 226 227 assert_eq!( 228 to_mdast("## alpha #", &Default::default())?, 229 Node::Root(Root { 230 children: vec![Node::Heading(Heading { 231 depth: 2, 232 children: vec![Node::Text(Text { 233 value: "alpha".into(), 234 position: Some(Position::new(1, 4, 3, 1, 9, 8)) 235 }),], 236 position: Some(Position::new(1, 1, 0, 1, 11, 10)) 237 })], 238 position: Some(Position::new(1, 1, 0, 1, 11, 10)) 239 }), 240 "should support heading (atx) as `Heading`s in mdast" 241 ); 242 243 Ok(()) 244}