Markdown parser fork with extended syntax for personal use.
at main 223 lines 6.6 kB view raw
1use markdown::{ 2 mdast::{Code, Node, Root}, 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_indented() -> Result<(), message::Message> { 11 assert_eq!( 12 to_html(" a simple\n indented code block"), 13 "<pre><code>a simple\n indented code block\n</code></pre>", 14 "should support indented code" 15 ); 16 17 assert_eq!( 18 to_html(" - foo\n\n bar"), 19 "<ul>\n<li>\n<p>foo</p>\n<p>bar</p>\n</li>\n</ul>", 20 "should prefer list item content over indented code (1)" 21 ); 22 23 assert_eq!( 24 to_html("1. foo\n\n - bar"), 25 "<ol>\n<li>\n<p>foo</p>\n<ul>\n<li>bar</li>\n</ul>\n</li>\n</ol>", 26 "should prefer list item content over indented code (2)" 27 ); 28 29 assert_eq!( 30 to_html(" <a/>\n *hi*\n\n - one"), 31 "<pre><code>&lt;a/&gt;\n*hi*\n\n- one\n</code></pre>", 32 "should support blank lines in indented code (1)" 33 ); 34 35 assert_eq!( 36 to_html(" chunk1\n\n chunk2\n \n \n \n chunk3"), 37 "<pre><code>chunk1\n\nchunk2\n\n\n\nchunk3\n</code></pre>", 38 "should support blank lines in indented code (2)" 39 ); 40 41 assert_eq!( 42 to_html(" chunk1\n \n chunk2"), 43 "<pre><code>chunk1\n \n chunk2\n</code></pre>", 44 "should support blank lines in indented code (3)" 45 ); 46 47 assert_eq!( 48 to_html("Foo\n bar"), 49 "<p>Foo\nbar</p>", 50 "should not support interrupting paragraphs" 51 ); 52 53 assert_eq!( 54 to_html(" foo\nbar"), 55 "<pre><code>foo\n</code></pre>\n<p>bar</p>", 56 "should support paragraphs directly after indented code" 57 ); 58 59 assert_eq!( 60 to_html("# Heading\n foo\nHeading\n------\n foo\n----"), 61 "<h1>Heading</h1>\n<pre><code>foo\n</code></pre>\n<h2>Heading</h2>\n<pre><code>foo\n</code></pre>\n<hr />", 62 "should mix w/ other content" 63 ); 64 65 assert_eq!( 66 to_html(" foo\n bar"), 67 "<pre><code> foo\nbar\n</code></pre>", 68 "should support extra whitespace on the first line" 69 ); 70 71 assert_eq!( 72 to_html("\n \n foo\n "), 73 "<pre><code>foo\n</code></pre>", 74 "should not support initial blank lines" 75 ); 76 77 assert_eq!( 78 to_html(" foo "), 79 "<pre><code>foo \n</code></pre>", 80 "should support trailing whitespace" 81 ); 82 83 assert_eq!( 84 to_html("> a\nb"), 85 "<blockquote>\n<pre><code>a\n</code></pre>\n</blockquote>\n<p>b</p>", 86 "should not support lazyness (1)" 87 ); 88 89 assert_eq!( 90 to_html("> a\n b"), 91 "<blockquote>\n<p>a\nb</p>\n</blockquote>", 92 "should not support lazyness (2)" 93 ); 94 95 assert_eq!( 96 to_html("> a\n b"), 97 "<blockquote>\n<p>a\nb</p>\n</blockquote>", 98 "should not support lazyness (3)" 99 ); 100 101 assert_eq!( 102 to_html("> a\n b"), 103 "<blockquote>\n<p>a\nb</p>\n</blockquote>", 104 "should not support lazyness (4)" 105 ); 106 107 assert_eq!( 108 to_html("> a\n b"), 109 "<blockquote>\n<pre><code>a\n</code></pre>\n</blockquote>\n<pre><code>b\n</code></pre>", 110 "should not support lazyness (5)" 111 ); 112 113 assert_eq!( 114 to_html("> a\n b"), 115 "<blockquote>\n<pre><code>a\n</code></pre>\n</blockquote>\n<pre><code> b\n</code></pre>", 116 "should not support lazyness (6)" 117 ); 118 119 assert_eq!( 120 to_html("> a\n b"), 121 "<blockquote>\n<pre><code>a\n</code></pre>\n</blockquote>\n<pre><code> b\n</code></pre>", 122 "should not support lazyness (7)" 123 ); 124 125 let off = Options { 126 parse: ParseOptions { 127 constructs: Constructs { 128 code_indented: false, 129 ..Default::default() 130 }, 131 ..Default::default() 132 }, 133 ..Default::default() 134 }; 135 136 assert_eq!( 137 to_html_with_options(" a", &off)?, 138 "<p>a</p>", 139 "should support turning off code (indented, 1)" 140 ); 141 142 assert_eq!( 143 to_html_with_options("> a\n b", &off)?, 144 "<blockquote>\n<p>a\nb</p>\n</blockquote>", 145 "should support turning off code (indented, 2)" 146 ); 147 148 assert_eq!( 149 to_html_with_options("- a\n b", &off)?, 150 "<ul>\n<li>a\nb</li>\n</ul>", 151 "should support turning off code (indented, 3)" 152 ); 153 154 assert_eq!( 155 to_html_with_options("- a\n - b", &off)?, 156 "<ul>\n<li>a\n<ul>\n<li>b</li>\n</ul>\n</li>\n</ul>", 157 "should support turning off code (indented, 4)" 158 ); 159 160 assert_eq!( 161 to_html_with_options("- a\n - b", &off)?, 162 "<ul>\n<li>a\n<ul>\n<li>b</li>\n</ul>\n</li>\n</ul>", 163 "should support turning off code (indented, 5)" 164 ); 165 166 assert_eq!( 167 to_html_with_options("```\na\n ```", &off)?, 168 "<pre><code>a\n</code></pre>", 169 "should support turning off code (indented, 6)" 170 ); 171 172 assert_eq!( 173 to_html_with_options( 174 "a <?\n ?>", 175 &Options { 176 parse: ParseOptions { 177 constructs: Constructs { 178 code_indented: false, 179 ..Default::default() 180 }, 181 ..Default::default() 182 }, 183 compile: CompileOptions { 184 allow_dangerous_html: true, 185 ..Default::default() 186 } 187 } 188 )?, 189 "<p>a <?\n?></p>", 190 "should support turning off code (indented, 7)" 191 ); 192 193 assert_eq!( 194 to_html_with_options("- Foo\n---", &off)?, 195 "<ul>\n<li>Foo</li>\n</ul>\n<hr />", 196 "should support turning off code (indented, 8)" 197 ); 198 199 assert_eq!( 200 to_html_with_options("- Foo\n ---", &off)?, 201 "<ul>\n<li>\n<h2>Foo</h2>\n</li>\n</ul>", 202 "should support turning off code (indented, 9)" 203 ); 204 205 assert_eq!( 206 to_mdast( 207 "\tconsole.log(1)\n console.log(2)\n", 208 &Default::default() 209 )?, 210 Node::Root(Root { 211 children: vec![Node::Code(Code { 212 lang: None, 213 meta: None, 214 value: "console.log(1)\nconsole.log(2)".into(), 215 position: Some(Position::new(1, 1, 0, 2, 19, 34)) 216 })], 217 position: Some(Position::new(1, 1, 0, 3, 1, 35)) 218 }), 219 "should support code (indented) as `Code`s in mdast" 220 ); 221 222 Ok(()) 223}