Markdown parser fork with extended syntax for personal use.
1use markdown::{
2 mdast::{InlineMath, Node, Paragraph, Root, Text},
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 math_text() -> 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("$a$"),
25 "<p>$a$</p>",
26 "should not support math (text) by default"
27 );
28
29 assert_eq!(
30 to_html_with_options("$foo$ $$bar$$", &math)?,
31 "<p><code class=\"language-math math-inline\">foo</code> <code class=\"language-math math-inline\">bar</code></p>",
32 "should support math (text) if enabled"
33 );
34
35 assert_eq!(
36 to_html_with_options(
37 "$foo$ $$bar$$",
38 &Options {
39 parse: ParseOptions {
40 constructs: Constructs {
41 math_text: true,
42 math_flow: true,
43 ..Default::default()
44 },
45 math_text_single_dollar: false,
46 ..Default::default()
47 },
48 ..Default::default()
49 }
50 )?,
51 "<p>$foo$ <code class=\"language-math math-inline\">bar</code></p>",
52 "should not support math (text) w/ a single dollar, w/ `math_text_single_dollar: false`"
53 );
54
55 assert_eq!(
56 to_html_with_options("$$ foo $ bar $$", &math)?,
57 "<p><code class=\"language-math math-inline\">foo $ bar</code></p>",
58 "should support math (text) w/ more dollars"
59 );
60
61 assert_eq!(
62 to_html_with_options("$ $$ $", &math)?,
63 "<p><code class=\"language-math math-inline\">$$</code></p>",
64 "should support math (text) w/ fences inside, and padding"
65 );
66
67 assert_eq!(
68 to_html_with_options("$ $$ $", &math)?,
69 "<p><code class=\"language-math math-inline\"> $$ </code></p>",
70 "should support math (text) w/ extra padding"
71 );
72
73 assert_eq!(
74 to_html_with_options("$ a$", &math)?,
75 "<p><code class=\"language-math math-inline\"> a</code></p>",
76 "should support math (text) w/ unbalanced padding"
77 );
78
79 assert_eq!(
80 to_html_with_options("$\u{a0}b\u{a0}$", &math)?,
81 "<p><code class=\"language-math math-inline\">\u{a0}b\u{a0}</code></p>",
82 "should support math (text) w/ non-padding whitespace"
83 );
84
85 assert_eq!(
86 to_html_with_options("$ $\n$ $", &math)?,
87 "<p><code class=\"language-math math-inline\"> </code>\n<code class=\"language-math math-inline\"> </code></p>",
88 "should support math (text) w/o data"
89 );
90
91 assert_eq!(
92 to_html_with_options("$\nfoo\nbar \nbaz\n$", &math)?,
93 "<p><code class=\"language-math math-inline\">foo bar baz</code></p>",
94 "should support math (text) w/o line endings (1)"
95 );
96
97 assert_eq!(
98 to_html_with_options("$\nfoo \n$", &math)?,
99 "<p><code class=\"language-math math-inline\">foo </code></p>",
100 "should support math (text) w/o line endings (2)"
101 );
102
103 assert_eq!(
104 to_html_with_options("$foo bar \nbaz$", &math)?,
105 "<p><code class=\"language-math math-inline\">foo bar baz</code></p>",
106 "should not support whitespace collapsing"
107 );
108
109 assert_eq!(
110 to_html_with_options("$foo\\$bar$", &math)?,
111 "<p><code class=\"language-math math-inline\">foo\\</code>bar$</p>",
112 "should not support character escapes"
113 );
114
115 assert_eq!(
116 to_html_with_options("$$foo$bar$$", &math)?,
117 "<p><code class=\"language-math math-inline\">foo$bar</code></p>",
118 "should support more dollars"
119 );
120
121 assert_eq!(
122 to_html_with_options("$ foo $$ bar $", &math)?,
123 "<p><code class=\"language-math math-inline\">foo $$ bar</code></p>",
124 "should support less dollars"
125 );
126
127 assert_eq!(
128 to_html_with_options("*foo$*$", &math)?,
129 "<p>*foo<code class=\"language-math math-inline\">*</code></p>",
130 "should precede over emphasis"
131 );
132
133 assert_eq!(
134 to_html_with_options("[not a $link](/foo$)", &math)?,
135 "<p>[not a <code class=\"language-math math-inline\">link](/foo</code>)</p>",
136 "should precede over links"
137 );
138
139 assert_eq!(
140 to_html_with_options("$<a href=\"$\">$", &math)?,
141 "<p><code class=\"language-math math-inline\"><a href="</code>">$</p>",
142 "should have same precedence as HTML (1)"
143 );
144
145 assert_eq!(
146 to_html_with_options(
147 "<a href=\"$\">$",
148 &Options {
149 parse: ParseOptions {
150 constructs: Constructs {
151 math_text: true,
152 math_flow: true,
153 ..Default::default()
154 },
155 ..Default::default()
156 },
157 compile: CompileOptions {
158 allow_dangerous_html: true,
159 allow_dangerous_protocol: true,
160 ..Default::default()
161 }
162 }
163 )?,
164 "<p><a href=\"$\">$</p>",
165 "should have same precedence as HTML (2)"
166 );
167
168 assert_eq!(
169 to_html_with_options("$<http://foo.bar.$baz>$", &math)?,
170 "<p><code class=\"language-math math-inline\"><http://foo.bar.</code>baz>$</p>",
171 "should have same precedence as autolinks (1)"
172 );
173
174 assert_eq!(
175 to_html_with_options("<http://foo.bar.$baz>$", &math)?,
176 "<p><a href=\"http://foo.bar.$baz\">http://foo.bar.$baz</a>$</p>",
177 "should have same precedence as autolinks (2)"
178 );
179
180 assert_eq!(
181 to_html_with_options("$$$foo$$", &math)?,
182 "<p>$$$foo$$</p>",
183 "should not support more dollars before a fence"
184 );
185
186 assert_eq!(
187 to_html_with_options("$foo", &math)?,
188 "<p>$foo</p>",
189 "should not support no closing fence (1)"
190 );
191
192 assert_eq!(
193 to_html_with_options("$foo$$bar$$", &math)?,
194 "<p>$foo<code class=\"language-math math-inline\">bar</code></p>",
195 "should not support no closing fence (2)"
196 );
197
198 assert_eq!(
199 to_html_with_options("$foo\t\tbar$", &math)?,
200 "<p><code class=\"language-math math-inline\">foo\t\tbar</code></p>",
201 "should support tabs in code"
202 );
203
204 assert_eq!(
205 to_html_with_options("\\$$x$", &math)?,
206 "<p>$<code class=\"language-math math-inline\">x</code></p>",
207 "should support an escaped initial dollar"
208 );
209
210 assert_eq!(
211 to_mdast("a $alpha$ b.", &math.parse)?,
212 Node::Root(Root {
213 children: vec![Node::Paragraph(Paragraph {
214 children: vec![
215 Node::Text(Text {
216 value: "a ".into(),
217 position: Some(Position::new(1, 1, 0, 1, 3, 2))
218 }),
219 Node::InlineMath(InlineMath {
220 value: "alpha".into(),
221 position: Some(Position::new(1, 3, 2, 1, 10, 9))
222 }),
223 Node::Text(Text {
224 value: " b.".into(),
225 position: Some(Position::new(1, 10, 9, 1, 13, 12))
226 })
227 ],
228 position: Some(Position::new(1, 1, 0, 1, 13, 12))
229 })],
230 position: Some(Position::new(1, 1, 0, 1, 13, 12))
231 }),
232 "should support math (text) as `InlineMath`s in mdast"
233 );
234
235 Ok(())
236}