Markdown parser fork with extended syntax for personal use.
1use markdown::{
2 mdast::{Node, Root, ThematicBreak},
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 thematic_break() -> Result<(), message::Message> {
11 assert_eq!(
12 to_html("***\n---\n___"),
13 "<hr />\n<hr />\n<hr />",
14 "should support thematic breaks w/ asterisks, dashes, and underscores"
15 );
16
17 assert_eq!(
18 to_html("+++"),
19 "<p>+++</p>",
20 "should not support thematic breaks w/ plusses"
21 );
22
23 assert_eq!(
24 to_html("==="),
25 "<p>===</p>",
26 "should not support thematic breaks w/ equals"
27 );
28
29 assert_eq!(
30 to_html("--"),
31 "<p>--</p>",
32 "should not support thematic breaks w/ two dashes"
33 );
34
35 assert_eq!(
36 to_html("**"),
37 "<p>**</p>",
38 "should not support thematic breaks w/ two asterisks"
39 );
40
41 assert_eq!(
42 to_html("__"),
43 "<p>__</p>",
44 "should not support thematic breaks w/ two underscores"
45 );
46
47 assert_eq!(
48 to_html(" ***"),
49 "<hr />",
50 "should support thematic breaks w/ 1 space"
51 );
52
53 assert_eq!(
54 to_html(" ***"),
55 "<hr />",
56 "should support thematic breaks w/ 2 spaces"
57 );
58
59 assert_eq!(
60 to_html(" ***"),
61 "<hr />",
62 "should support thematic breaks w/ 3 spaces"
63 );
64
65 assert_eq!(
66 to_html(" ***"),
67 "<pre><code>***\n</code></pre>",
68 "should not support thematic breaks w/ 4 spaces"
69 );
70
71 assert_eq!(
72 to_html("Foo\n ***"),
73 "<p>Foo\n***</p>",
74 "should not support thematic breaks w/ 4 spaces as paragraph continuation"
75 );
76
77 assert_eq!(
78 to_html("_____________________________________"),
79 "<hr />",
80 "should support thematic breaks w/ many markers"
81 );
82
83 assert_eq!(
84 to_html(" - - -"),
85 "<hr />",
86 "should support thematic breaks w/ spaces (1)"
87 );
88
89 assert_eq!(
90 to_html(" ** * ** * ** * **"),
91 "<hr />",
92 "should support thematic breaks w/ spaces (2)"
93 );
94
95 assert_eq!(
96 to_html("- - - -"),
97 "<hr />",
98 "should support thematic breaks w/ spaces (3)"
99 );
100
101 assert_eq!(
102 to_html("- - - - "),
103 "<hr />",
104 "should support thematic breaks w/ trailing spaces"
105 );
106
107 assert_eq!(
108 to_html("_ _ _ _ a"),
109 "<p>_ _ _ _ a</p>",
110 "should not support thematic breaks w/ other characters (1)"
111 );
112
113 assert_eq!(
114 to_html("a------"),
115 "<p>a------</p>",
116 "should not support thematic breaks w/ other characters (2)"
117 );
118
119 assert_eq!(
120 to_html("---a---"),
121 "<p>---a---</p>",
122 "should not support thematic breaks w/ other characters (3)"
123 );
124
125 assert_eq!(
126 to_html(" *-*"),
127 "<p><em>-</em></p>",
128 "should not support thematic breaks w/ mixed markers"
129 );
130
131 assert_eq!(
132 to_html("- foo\n***\n- bar"),
133 "<ul>\n<li>foo</li>\n</ul>\n<hr />\n<ul>\n<li>bar</li>\n</ul>",
134 "should support thematic breaks mixed w/ lists (1)"
135 );
136
137 assert_eq!(
138 to_html("* Foo\n* * *\n* Bar"),
139 "<ul>\n<li>Foo</li>\n</ul>\n<hr />\n<ul>\n<li>Bar</li>\n</ul>",
140 "should support thematic breaks mixed w/ lists (2)"
141 );
142
143 assert_eq!(
144 to_html("Foo\n***\nbar"),
145 "<p>Foo</p>\n<hr />\n<p>bar</p>",
146 "should support thematic breaks interrupting paragraphs"
147 );
148
149 assert_eq!(
150 to_html("Foo\n---\nbar"),
151 "<h2>Foo</h2>\n<p>bar</p>",
152 "should not support thematic breaks w/ dashes interrupting paragraphs (setext heading)"
153 );
154
155 assert_eq!(
156 to_html("- Foo\n- * * *"),
157 "<ul>\n<li>Foo</li>\n<li>\n<hr />\n</li>\n</ul>",
158 "should support thematic breaks in lists"
159 );
160
161 assert_eq!(
162 to_html("> ---\na"),
163 "<blockquote>\n<hr />\n</blockquote>\n<p>a</p>",
164 "should not support lazyness (1)"
165 );
166
167 assert_eq!(
168 to_html("> a\n---"),
169 "<blockquote>\n<p>a</p>\n</blockquote>\n<hr />",
170 "should not support lazyness (2)"
171 );
172
173 assert_eq!(
174 to_html_with_options(
175 "***",
176 &Options {
177 parse: ParseOptions {
178 constructs: Constructs {
179 thematic_break: false,
180 ..Default::default()
181 },
182 ..Default::default()
183 },
184 ..Default::default()
185 }
186 )?,
187 "<p>***</p>",
188 "should support turning off thematic breaks"
189 );
190
191 assert_eq!(
192 to_mdast("***", &Default::default())?,
193 Node::Root(Root {
194 children: vec![Node::ThematicBreak(ThematicBreak {
195 position: Some(Position::new(1, 1, 0, 1, 4, 3))
196 })],
197 position: Some(Position::new(1, 1, 0, 1, 4, 3))
198 }),
199 "should support thematic breaks as `ThematicBreak`s in mdast"
200 );
201
202 Ok(())
203}