Markdown parser fork with extended syntax for personal use.
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_setext() -> Result<(), message::Message> {
11 assert_eq!(
12 to_html("Foo *bar*\n========="),
13 "<h1>Foo <em>bar</em></h1>",
14 "should support a heading w/ an equals to (rank of 1)"
15 );
16
17 assert_eq!(
18 to_html("Foo *bar*\n---------"),
19 "<h2>Foo <em>bar</em></h2>",
20 "should support a heading w/ a dash (rank of 2)"
21 );
22
23 assert_eq!(
24 to_html("Foo *bar\nbaz*\n===="),
25 "<h1>Foo <em>bar\nbaz</em></h1>",
26 "should support line endings in setext headings"
27 );
28
29 assert_eq!(
30 to_html(" Foo *bar\nbaz*\t\n===="),
31 "<h1>Foo <em>bar\nbaz</em></h1>",
32 "should not include initial and final whitespace around content"
33 );
34
35 assert_eq!(
36 to_html("Foo\n-------------------------"),
37 "<h2>Foo</h2>",
38 "should support long underlines"
39 );
40
41 assert_eq!(
42 to_html("Foo\n="),
43 "<h1>Foo</h1>",
44 "should support short underlines"
45 );
46
47 assert_eq!(
48 to_html(" Foo\n ==="),
49 "<h1>Foo</h1>",
50 "should support indented content w/ 1 space"
51 );
52
53 assert_eq!(
54 to_html(" Foo\n---"),
55 "<h2>Foo</h2>",
56 "should support indented content w/ 2 spaces"
57 );
58
59 assert_eq!(
60 to_html(" Foo\n---"),
61 "<h2>Foo</h2>",
62 "should support indented content w/ 3 spaces"
63 );
64
65 assert_eq!(
66 to_html(" Foo\n ---"),
67 "<pre><code>Foo\n---\n</code></pre>",
68 "should not support too much indented content (1)"
69 );
70
71 assert_eq!(
72 to_html(" Foo\n---"),
73 "<pre><code>Foo\n</code></pre>\n<hr />",
74 "should not support too much indented content (2)"
75 );
76
77 assert_eq!(
78 to_html("Foo\n ---- "),
79 "<h2>Foo</h2>",
80 "should support initial and final whitespace around the underline"
81 );
82
83 assert_eq!(
84 to_html("Foo\n ="),
85 "<h1>Foo</h1>",
86 "should support whitespace before underline"
87 );
88
89 assert_eq!(
90 to_html("Foo\n ="),
91 "<p>Foo\n=</p>",
92 "should not support too much whitespace before underline (1)"
93 );
94
95 assert_eq!(
96 to_html("Foo\n\t="),
97 "<p>Foo\n=</p>",
98 "should not support too much whitespace before underline (2)"
99 );
100
101 assert_eq!(
102 to_html("Foo\n= ="),
103 "<p>Foo\n= =</p>",
104 "should not support whitespace in the underline (1)"
105 );
106
107 assert_eq!(
108 to_html("Foo\n--- -"),
109 "<p>Foo</p>\n<hr />",
110 "should not support whitespace in the underline (2)"
111 );
112
113 assert_eq!(
114 to_html("Foo \n-----"),
115 "<h2>Foo</h2>",
116 "should not support a hard break w/ spaces at the end"
117 );
118
119 assert_eq!(
120 to_html("Foo\\\n-----"),
121 "<h2>Foo\\</h2>",
122 "should not support a hard break w/ backslash at the end"
123 );
124
125 assert_eq!(
126 to_html("`Foo\n----\n`"),
127 "<h2>`Foo</h2>\n<p>`</p>",
128 "should precede over inline constructs (1)"
129 );
130
131 assert_eq!(
132 to_html("<a title=\"a lot\n---\nof dashes\"/>"),
133 "<h2><a title="a lot</h2>\n<p>of dashes"/></p>",
134 "should precede over inline constructs (2)"
135 );
136
137 assert_eq!(
138 to_html("> Foo\n---"),
139 "<blockquote>\n<p>Foo</p>\n</blockquote>\n<hr />",
140 "should not allow underline to be lazy (1)"
141 );
142
143 assert_eq!(
144 to_html("> foo\nbar\n==="),
145 "<blockquote>\n<p>foo\nbar\n===</p>\n</blockquote>",
146 "should not allow underline to be lazy (2)"
147 );
148
149 assert_eq!(
150 to_html("- Foo\n---"),
151 "<ul>\n<li>Foo</li>\n</ul>\n<hr />",
152 "should not allow underline to be lazy (3)"
153 );
154
155 assert_eq!(
156 to_html("Foo\nBar\n---"),
157 "<h2>Foo\nBar</h2>",
158 "should support line endings in setext headings"
159 );
160
161 assert_eq!(
162 to_html("---\nFoo\n---\nBar\n---\nBaz"),
163 "<hr />\n<h2>Foo</h2>\n<h2>Bar</h2>\n<p>Baz</p>",
164 "should support adjacent setext headings"
165 );
166
167 assert_eq!(
168 to_html("\n===="),
169 "<p>====</p>",
170 "should not support empty setext headings"
171 );
172
173 assert_eq!(
174 to_html("---\n---"),
175 "<hr />\n<hr />",
176 "should prefer other constructs over setext headings (1)"
177 );
178
179 assert_eq!(
180 to_html("- foo\n-----"),
181 "<ul>\n<li>foo</li>\n</ul>\n<hr />",
182 "should prefer other constructs over setext headings (2)"
183 );
184
185 assert_eq!(
186 to_html(" foo\n---"),
187 "<pre><code>foo\n</code></pre>\n<hr />",
188 "should prefer other constructs over setext headings (3)"
189 );
190
191 assert_eq!(
192 to_html("> foo\n-----"),
193 "<blockquote>\n<p>foo</p>\n</blockquote>\n<hr />",
194 "should prefer other constructs over setext headings (4)"
195 );
196
197 assert_eq!(
198 to_html("\\> foo\n------"),
199 "<h2>> foo</h2>",
200 "should support starting w/ character escapes"
201 );
202
203 assert_eq!(
204 to_html("Foo\nbar\n---\nbaz"),
205 "<h2>Foo\nbar</h2>\n<p>baz</p>",
206 "paragraph and heading interplay (1)"
207 );
208
209 assert_eq!(
210 to_html("Foo\n\nbar\n---\nbaz"),
211 "<p>Foo</p>\n<h2>bar</h2>\n<p>baz</p>",
212 "paragraph and heading interplay (2)"
213 );
214
215 assert_eq!(
216 to_html("Foo\nbar\n\n---\n\nbaz"),
217 "<p>Foo\nbar</p>\n<hr />\n<p>baz</p>",
218 "paragraph and heading interplay (3)"
219 );
220
221 assert_eq!(
222 to_html("Foo\nbar\n* * *\nbaz"),
223 "<p>Foo\nbar</p>\n<hr />\n<p>baz</p>",
224 "paragraph and heading interplay (4)"
225 );
226
227 assert_eq!(
228 to_html("Foo\nbar\n\\---\nbaz"),
229 "<p>Foo\nbar\n---\nbaz</p>",
230 "paragraph and heading interplay (5)"
231 );
232
233 // Extra:
234 assert_eq!(
235 to_html("Foo \nbar\n-----"),
236 "<h2>Foo<br />\nbar</h2>",
237 "should support a hard break w/ spaces in between"
238 );
239
240 assert_eq!(
241 to_html("Foo\\\nbar\n-----"),
242 "<h2>Foo<br />\nbar</h2>",
243 "should support a hard break w/ backslash in between"
244 );
245
246 assert_eq!(
247 to_html("a\n-\nb"),
248 "<h2>a</h2>\n<p>b</p>",
249 "should prefer a setext heading over an interrupting list"
250 );
251
252 assert_eq!(
253 to_html("[a]: b\n=\n="),
254 "<h1>=</h1>",
255 "should support a two setext heading underlines after a definition, as a setext heading"
256 );
257
258 assert_eq!(
259 to_html("> ===\na"),
260 "<blockquote>\n<p>===\na</p>\n</blockquote>",
261 "should not support lazyness (1)"
262 );
263
264 assert_eq!(
265 to_html("> a\n==="),
266 "<blockquote>\n<p>a\n===</p>\n</blockquote>",
267 "should not support lazyness (2)"
268 );
269
270 assert_eq!(
271 to_html("a\n- ==="),
272 "<p>a</p>\n<ul>\n<li>===</li>\n</ul>",
273 "should not support piercing (1)"
274 );
275
276 assert_eq!(
277 to_html("a\n* ---"),
278 "<p>a</p>\n<ul>\n<li>\n<hr />\n</li>\n</ul>",
279 "should not support piercing (2)"
280 );
281
282 assert_eq!(
283 to_html_with_options(
284 "a\n-",
285 &Options {
286 parse: ParseOptions {
287 constructs: Constructs {
288 heading_setext: false,
289 ..Default::default()
290 },
291 ..Default::default()
292 },
293 ..Default::default()
294 }
295 )?,
296 "<p>a\n-</p>",
297 "should support turning off setext underlines"
298 );
299
300 assert_eq!(
301 to_mdast("alpha\nbravo\n==", &Default::default())?,
302 Node::Root(Root {
303 children: vec![Node::Heading(Heading {
304 depth: 1,
305 children: vec![Node::Text(Text {
306 value: "alpha\nbravo".into(),
307 position: Some(Position::new(1, 1, 0, 2, 6, 11))
308 }),],
309 position: Some(Position::new(1, 1, 0, 3, 3, 14))
310 })],
311 position: Some(Position::new(1, 1, 0, 3, 3, 14))
312 }),
313 "should support heading (atx) as `Heading`s in mdast"
314 );
315
316 Ok(())
317}