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