Markdown parser fork with extended syntax for personal use.
at main 240 lines 7.5 kB view raw
1use markdown::{ 2 mdast::{Blockquote, Node, Paragraph, 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 block_quote() -> Result<(), message::Message> { 11 assert_eq!( 12 to_html("> # a\n> b\n> c"), 13 "<blockquote>\n<h1>a</h1>\n<p>b\nc</p>\n</blockquote>", 14 "should support block quotes" 15 ); 16 17 assert_eq!( 18 to_html("># a\n>b\n> c"), 19 "<blockquote>\n<h1>a</h1>\n<p>b\nc</p>\n</blockquote>", 20 "should support block quotes w/o space" 21 ); 22 23 assert_eq!( 24 to_html(" > # a\n > b\n > c"), 25 "<blockquote>\n<h1>a</h1>\n<p>b\nc</p>\n</blockquote>", 26 "should support prefixing block quotes w/ spaces" 27 ); 28 29 assert_eq!( 30 to_html(" > # a\n > b\n > c"), 31 "<pre><code>&gt; # a\n&gt; b\n&gt; c\n</code></pre>", 32 "should not support block quotes w/ 4 spaces" 33 ); 34 35 assert_eq!( 36 to_html("> # a\n> b\nc"), 37 "<blockquote>\n<h1>a</h1>\n<p>b\nc</p>\n</blockquote>", 38 "should support lazy content lines" 39 ); 40 41 assert_eq!( 42 to_html("> a\nb\n> c"), 43 "<blockquote>\n<p>a\nb\nc</p>\n</blockquote>", 44 "should support lazy content lines inside block quotes" 45 ); 46 47 assert_eq!( 48 to_html("> a\n> ---"), 49 "<blockquote>\n<h2>a</h2>\n</blockquote>", 50 "should support setext headings underlines in block quotes" 51 ); 52 53 assert_eq!( 54 to_html("> a\n---"), 55 "<blockquote>\n<p>a</p>\n</blockquote>\n<hr />", 56 "should not support lazy setext headings underlines in block quotes" 57 ); 58 59 assert_eq!( 60 to_html("> - a\n> - b"), 61 "<blockquote>\n<ul>\n<li>a</li>\n<li>b</li>\n</ul>\n</blockquote>", 62 "should support lists in block quotes" 63 ); 64 65 assert_eq!( 66 to_html("> - a\n- b"), 67 "<blockquote>\n<ul>\n<li>a</li>\n</ul>\n</blockquote>\n<ul>\n<li>b</li>\n</ul>", 68 "should not support lazy lists in block quotes" 69 ); 70 71 assert_eq!( 72 to_html("> a\n b"), 73 "<blockquote>\n<pre><code>a\n</code></pre>\n</blockquote>\n<pre><code>b\n</code></pre>", 74 "should not support lazy indented code in block quotes" 75 ); 76 77 assert_eq!( 78 to_html("> ```\na\n```"), 79 "<blockquote>\n<pre><code></code></pre>\n</blockquote>\n<p>a</p>\n<pre><code></code></pre>\n", 80 "should not support lazy fenced code in block quotes (1)" 81 ); 82 83 assert_eq!( 84 to_html("> a\n```\nb"), 85 "<blockquote>\n<p>a</p>\n</blockquote>\n<pre><code>b\n</code></pre>\n", 86 "should not support lazy fenced code in block quotes (2)" 87 ); 88 89 assert_eq!( 90 to_html("> a\n - b"), 91 "<blockquote>\n<p>a\n- b</p>\n</blockquote>", 92 "should not support lazy indented code (or lazy list) in block quotes" 93 ); 94 95 assert_eq!( 96 to_html("> [\na"), 97 "<blockquote>\n<p>[\na</p>\n</blockquote>", 98 "should support lazy, definition-like lines" 99 ); 100 101 assert_eq!( 102 to_html("> [a]: b\nc"), 103 "<blockquote>\n<p>c</p>\n</blockquote>", 104 "should support a definition, followed by a lazy paragraph" 105 ); 106 107 assert_eq!( 108 to_html(">"), 109 "<blockquote>\n</blockquote>", 110 "should support empty block quotes (1)" 111 ); 112 113 assert_eq!( 114 to_html(">\n> \n> "), 115 "<blockquote>\n</blockquote>", 116 "should support empty block quotes (2)" 117 ); 118 119 assert_eq!( 120 to_html(">\n> a\n> "), 121 "<blockquote>\n<p>a</p>\n</blockquote>", 122 "should support initial or final lazy empty block quote lines" 123 ); 124 125 assert_eq!( 126 to_html("> a\n\n> b"), 127 "<blockquote>\n<p>a</p>\n</blockquote>\n<blockquote>\n<p>b</p>\n</blockquote>", 128 "should support adjacent block quotes" 129 ); 130 131 assert_eq!( 132 to_html("> a\n> b"), 133 "<blockquote>\n<p>a\nb</p>\n</blockquote>", 134 "should support a paragraph in a block quote" 135 ); 136 137 assert_eq!( 138 to_html("> a\n>\n> b"), 139 "<blockquote>\n<p>a</p>\n<p>b</p>\n</blockquote>", 140 "should support adjacent paragraphs in block quotes" 141 ); 142 143 assert_eq!( 144 to_html("[a]\n\n> [a]: b"), 145 "<p><a href=\"b\">a</a></p>\n<blockquote>\n</blockquote>", 146 "should support a definition in a block quote (1)" 147 ); 148 149 assert_eq!( 150 to_html("> [a]: b\n\n[a]"), 151 "<blockquote>\n</blockquote>\n<p><a href=\"b\">a</a></p>", 152 "should support a definition in a block quote (2)" 153 ); 154 155 assert_eq!( 156 to_html("a\n> b"), 157 "<p>a</p>\n<blockquote>\n<p>b</p>\n</blockquote>", 158 "should support interrupting paragraphs w/ block quotes" 159 ); 160 161 assert_eq!( 162 to_html("> a\n***\n> b"), 163 "<blockquote>\n<p>a</p>\n</blockquote>\n<hr />\n<blockquote>\n<p>b</p>\n</blockquote>", 164 "should support interrupting block quotes w/ thematic breaks" 165 ); 166 167 assert_eq!( 168 to_html("> a\nb"), 169 "<blockquote>\n<p>a\nb</p>\n</blockquote>", 170 "should not support interrupting block quotes w/ paragraphs" 171 ); 172 173 assert_eq!( 174 to_html("> a\n\nb"), 175 "<blockquote>\n<p>a</p>\n</blockquote>\n<p>b</p>", 176 "should support interrupting block quotes w/ blank lines" 177 ); 178 179 assert_eq!( 180 to_html("> a\n>\nb"), 181 "<blockquote>\n<p>a</p>\n</blockquote>\n<p>b</p>", 182 "should not support interrupting a blank line in a block quotes w/ paragraphs" 183 ); 184 185 assert_eq!( 186 to_html("> > > a\nb"), 187 "<blockquote>\n<blockquote>\n<blockquote>\n<p>a\nb</p>\n</blockquote>\n</blockquote>\n</blockquote>", 188 "should not support interrupting many block quotes w/ paragraphs (1)" 189 ); 190 191 assert_eq!( 192 to_html(">>> a\n> b\n>>c"), 193 "<blockquote>\n<blockquote>\n<blockquote>\n<p>a\nb\nc</p>\n</blockquote>\n</blockquote>\n</blockquote>", 194 "should not support interrupting many block quotes w/ paragraphs (2)" 195 ); 196 197 assert_eq!( 198 to_html("> a\n\n> b"), 199 "<blockquote>\n<pre><code>a\n</code></pre>\n</blockquote>\n<blockquote>\n<p>b</p>\n</blockquote>", 200 "should support 5 spaces for indented code, not 4" 201 ); 202 203 assert_eq!( 204 to_html_with_options( 205 "> # a\n> b\n> c", 206 &Options { 207 parse: ParseOptions { 208 constructs: Constructs { 209 block_quote: false, 210 ..Default::default() 211 }, 212 ..Default::default() 213 }, 214 ..Default::default() 215 } 216 )?, 217 "<p>&gt; # a\n&gt; b\n&gt; c</p>", 218 "should support turning off block quotes" 219 ); 220 221 assert_eq!( 222 to_mdast("> a", &Default::default())?, 223 Node::Root(Root { 224 children: vec![Node::Blockquote(Blockquote { 225 children: vec![Node::Paragraph(Paragraph { 226 children: vec![Node::Text(Text { 227 value: "a".into(), 228 position: Some(Position::new(1, 3, 2, 1, 4, 3)) 229 }),], 230 position: Some(Position::new(1, 3, 2, 1, 4, 3)) 231 })], 232 position: Some(Position::new(1, 1, 0, 1, 4, 3)) 233 })], 234 position: Some(Position::new(1, 1, 0, 1, 4, 3)) 235 }), 236 "should support block quotes as `BlockQuote`s in mdast" 237 ); 238 239 Ok(()) 240}