Markdown parser fork with extended syntax for personal use.
1use markdown::{
2 mdast::{Math, Node, Root},
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 math_flow() -> Result<(), message::Message> {
11 let math = Options {
12 parse: ParseOptions {
13 constructs: Constructs {
14 math_text: true,
15 math_flow: true,
16 ..Default::default()
17 },
18 ..Default::default()
19 },
20 ..Default::default()
21 };
22
23 assert_eq!(
24 to_html("$$\na\n$$"),
25 "<p>$$\na\n$$</p>",
26 "should not support math (flow) by default"
27 );
28
29 assert_eq!(
30 to_html_with_options("$$\na\n$$", &math)?,
31 "<pre><code class=\"language-math math-display\">a\n</code></pre>",
32 "should support math (flow) if enabled"
33 );
34
35 assert_eq!(
36 to_html_with_options("$$\n<\n >\n$$", &math)?,
37 "<pre><code class=\"language-math math-display\"><\n >\n</code></pre>",
38 "should support math (flow)"
39 );
40
41 assert_eq!(
42 to_html_with_options("$\nfoo\n$", &math)?,
43 "<p><code class=\"language-math math-inline\">foo</code></p>",
44 "should not support math (flow) w/ less than two markers"
45 );
46
47 assert_eq!(
48 to_html_with_options("$$$\naaa\n$$\n$$$$", &math)?,
49 "<pre><code class=\"language-math math-display\">aaa\n$$\n</code></pre>",
50 "should support a closing sequence longer, but not shorter than, the opening"
51 );
52
53 assert_eq!(
54 to_html_with_options("$$", &math)?,
55 "<pre><code class=\"language-math math-display\"></code></pre>\n",
56 "should support an eof right after an opening sequence"
57 );
58
59 assert_eq!(
60 to_html_with_options("$$$\n\n$$\naaa\n", &math)?,
61 "<pre><code class=\"language-math math-display\">\n$$\naaa\n</code></pre>\n",
62 "should support an eof somewhere in content"
63 );
64
65 assert_eq!(
66 to_html_with_options("> $$\n> aaa\n\nbbb", &math)?,
67 "<blockquote>\n<pre><code class=\"language-math math-display\">aaa\n</code></pre>\n</blockquote>\n<p>bbb</p>",
68 "should support no closing sequence in a block quote"
69 );
70
71 assert_eq!(
72 to_html_with_options("$$\n\n \n$$", &math)?,
73 "<pre><code class=\"language-math math-display\">\n \n</code></pre>",
74 "should support blank lines in math (flow)"
75 );
76
77 assert_eq!(
78 to_html_with_options("$$\n$$", &math)?,
79 "<pre><code class=\"language-math math-display\"></code></pre>",
80 "should support empty math (flow)"
81 );
82
83 assert_eq!(
84 to_html_with_options(" $$\n aaa\naaa\n$$", &math)?,
85 "<pre><code class=\"language-math math-display\">aaa\naaa\n</code></pre>",
86 "should remove up to one space from the content if the opening sequence is indented w/ 1 space"
87 );
88
89 assert_eq!(
90 to_html_with_options(" $$\naaa\n aaa\naaa\n $$", &math)?,
91 "<pre><code class=\"language-math math-display\">aaa\naaa\naaa\n</code></pre>",
92 "should remove up to two space from the content if the opening sequence is indented w/ 2 spaces"
93 );
94
95 assert_eq!(
96 to_html_with_options(" $$\n aaa\n aaa\n aaa\n $$", &math)?,
97 "<pre><code class=\"language-math math-display\">aaa\n aaa\naaa\n</code></pre>",
98 "should remove up to three space from the content if the opening sequence is indented w/ 3 spaces"
99 );
100
101 assert_eq!(
102 to_html_with_options(" $$\n aaa\n $$", &math)?,
103 "<pre><code>$$\naaa\n$$\n</code></pre>",
104 "should not support indenteding the opening sequence w/ 4 spaces"
105 );
106
107 assert_eq!(
108 to_html_with_options("$$\naaa\n $$", &math)?,
109 "<pre><code class=\"language-math math-display\">aaa\n</code></pre>",
110 "should support an indented closing sequence"
111 );
112
113 assert_eq!(
114 to_html_with_options(" $$\naaa\n $$", &math)?,
115 "<pre><code class=\"language-math math-display\">aaa\n</code></pre>",
116 "should support a differently indented closing sequence than the opening sequence"
117 );
118
119 assert_eq!(
120 to_html_with_options("$$\naaa\n $$\n", &math)?,
121 "<pre><code class=\"language-math math-display\">aaa\n $$\n</code></pre>\n",
122 "should not support an indented closing sequence w/ 4 spaces"
123 );
124
125 assert_eq!(
126 to_html_with_options("$$ $$\naaa", &math)?,
127 "<p><code class=\"language-math math-inline\"> </code>\naaa</p>",
128 "should not support dollars in the opening fence after the opening sequence"
129 );
130
131 assert_eq!(
132 to_html_with_options("$$$\naaa\n$$$ $$\n", &math)?,
133 "<pre><code class=\"language-math math-display\">aaa\n$$$ $$\n</code></pre>\n",
134 "should not support spaces in the closing sequence"
135 );
136
137 assert_eq!(
138 to_html_with_options("foo\n$$\nbar\n$$\nbaz", &math)?,
139 "<p>foo</p>\n<pre><code class=\"language-math math-display\">bar\n</code></pre>\n<p>baz</p>",
140 "should support interrupting paragraphs"
141 );
142
143 assert_eq!(
144 to_html_with_options("foo\n---\n$$\nbar\n$$\n# baz", &math)?,
145 "<h2>foo</h2>\n<pre><code class=\"language-math math-display\">bar\n</code></pre>\n<h1>baz</h1>",
146 "should support interrupting other content"
147 );
148
149 assert_eq!(
150 to_html_with_options("$$ruby\ndef foo(x)\n return 3\nend\n$$", &math)?,
151 "<pre><code class=\"language-math math-display\">def foo(x)\n return 3\nend\n</code></pre>",
152 "should not support an “info” string (1)"
153 );
154
155 assert_eq!(
156 to_html_with_options("$$$;\n$$$", &math)?,
157 "<pre><code class=\"language-math math-display\"></code></pre>",
158 "should not support an “info” string (2)"
159 );
160
161 assert_eq!(
162 to_html_with_options("$$ ruby startline=3 `%@#`\ndef foo(x)\n return 3\nend\n$$$$", &math)?,
163 "<pre><code class=\"language-math math-display\">def foo(x)\n return 3\nend\n</code></pre>",
164 "should not support an “info” string (3)"
165 );
166
167 assert_eq!(
168 to_html_with_options("$$ aa $$\nfoo", &math)?,
169 "<p><code class=\"language-math math-inline\">aa</code>\nfoo</p>",
170 "should not support dollars in the meta string"
171 );
172
173 assert_eq!(
174 to_html_with_options("$$\n$$ aaa\n$$", &math)?,
175 "<pre><code class=\"language-math math-display\">$$ aaa\n</code></pre>",
176 "should not support meta string on closing sequences"
177 );
178
179 // Our own:
180 assert_eq!(
181 to_html_with_options("$$ ", &math)?,
182 "<pre><code class=\"language-math math-display\"></code></pre>\n",
183 "should support an eof after whitespace, after the start fence sequence"
184 );
185
186 assert_eq!(
187 to_html_with_options("$$ js\nalert(1)\n$$", &math)?,
188 "<pre><code class=\"language-math math-display\">alert(1)\n</code></pre>",
189 "should support whitespace between the sequence and the meta string"
190 );
191
192 assert_eq!(
193 to_html_with_options("$$js", &math)?,
194 "<pre><code class=\"language-math math-display\"></code></pre>\n",
195 "should support an eof after the meta string"
196 );
197
198 assert_eq!(
199 to_html_with_options("$$ js \nalert(1)\n$$", &math)?,
200 "<pre><code class=\"language-math math-display\">alert(1)\n</code></pre>",
201 "should support whitespace after the meta string"
202 );
203
204 assert_eq!(
205 to_html_with_options("$$\n ", &math)?,
206 "<pre><code class=\"language-math math-display\"> \n</code></pre>\n",
207 "should support an eof after whitespace in content"
208 );
209
210 assert_eq!(
211 to_html_with_options(" $$\n ", &math)?,
212 "<pre><code class=\"language-math math-display\"></code></pre>\n",
213 "should support an eof in the prefix, in content"
214 );
215
216 assert_eq!(
217 to_html_with_options("$$j\\+s©", &math)?,
218 "<pre><code class=\"language-math math-display\"></code></pre>\n",
219 "should support character escapes and character references in meta strings"
220 );
221
222 assert_eq!(
223 to_html_with_options("$$a\\&b\0c", &math)?,
224 "<pre><code class=\"language-math math-display\"></code></pre>\n",
225 "should support dangerous characters in meta strings"
226 );
227
228 assert_eq!(
229 to_html_with_options(" $$\naaa\n $$", &math)?,
230 "<pre><code class=\"language-math math-display\">aaa\n $$\n</code></pre>\n",
231 "should not support a closing sequence w/ too much indent, regardless of opening sequence (1)"
232 );
233
234 assert_eq!(
235 to_html_with_options("> $$\n>\n>\n>\n\na", &math)?,
236 "<blockquote>\n<pre><code class=\"language-math math-display\">\n\n\n</code></pre>\n</blockquote>\n<p>a</p>",
237 "should not support a closing sequence w/ too much indent, regardless of opening sequence (2)"
238 );
239
240 assert_eq!(
241 to_html_with_options("> $$a\nb", &math)?,
242 "<blockquote>\n<pre><code class=\"language-math math-display\"></code></pre>\n</blockquote>\n<p>b</p>",
243 "should not support lazyness (1)"
244 );
245
246 assert_eq!(
247 to_html_with_options("> a\n$$b", &math)?,
248 "<blockquote>\n<p>a</p>\n</blockquote>\n<pre><code class=\"language-math math-display\"></code></pre>\n",
249 "should not support lazyness (2)"
250 );
251
252 assert_eq!(
253 to_html_with_options("> $$a\n$$", &math)?,
254 "<blockquote>\n<pre><code class=\"language-math math-display\"></code></pre>\n</blockquote>\n<pre><code class=\"language-math math-display\"></code></pre>\n",
255 "should not support lazyness (3)"
256 );
257
258 assert_eq!(
259 to_mdast("$$extra\nabc\ndef\n$$", &math.parse)?,
260 Node::Root(Root {
261 children: vec![Node::Math(Math {
262 meta: Some("extra".into()),
263 value: "abc\ndef".into(),
264 position: Some(Position::new(1, 1, 0, 4, 3, 18))
265 })],
266 position: Some(Position::new(1, 1, 0, 4, 3, 18))
267 }),
268 "should support math (flow) as `Math`s in mdast"
269 );
270
271 Ok(())
272}