1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
use markdown::{
mdast::{InlineCode, Node, Paragraph, Root, Text},
to_html, to_html_with_options, to_mdast,
unist::Position,
CompileOptions, Constructs, Options, ParseOptions,
};
use pretty_assertions::assert_eq;
#[test]
fn code_text() -> Result<(), String> {
let danger = Options {
compile: CompileOptions {
allow_dangerous_html: true,
allow_dangerous_protocol: true,
..Default::default()
},
..Default::default()
};
assert_eq!(
to_html("`foo`"),
"<p><code>foo</code></p>",
"should support code"
);
assert_eq!(
to_html("`` foo ` bar ``"),
"<p><code>foo ` bar</code></p>",
"should support code w/ more accents"
);
assert_eq!(
to_html("` `` `"),
"<p><code>``</code></p>",
"should support code w/ fences inside, and padding"
);
assert_eq!(
to_html("` `` `"),
"<p><code> `` </code></p>",
"should support code w/ extra padding"
);
assert_eq!(
to_html("` a`"),
"<p><code> a</code></p>",
"should support code w/ unbalanced padding"
);
assert_eq!(
to_html("`\u{a0}b\u{a0}`"),
"<p><code>\u{a0}b\u{a0}</code></p>",
"should support code w/ non-padding whitespace"
);
assert_eq!(
to_html("` `\n` `"),
"<p><code> </code>\n<code> </code></p>",
"should support code w/o data"
);
assert_eq!(
to_html("``\nfoo\nbar \nbaz\n``"),
"<p><code>foo bar baz</code></p>",
"should support code w/o line endings (1)"
);
assert_eq!(
to_html("``\nfoo \n``"),
"<p><code>foo </code></p>",
"should support code w/o line endings (2)"
);
assert_eq!(
to_html("`foo bar \nbaz`"),
"<p><code>foo bar baz</code></p>",
"should not support whitespace collapsing"
);
assert_eq!(
to_html("`foo\\`bar`"),
"<p><code>foo\\</code>bar`</p>",
"should not support character escapes"
);
assert_eq!(
to_html("``foo`bar``"),
"<p><code>foo`bar</code></p>",
"should support more accents"
);
assert_eq!(
to_html("` foo `` bar `"),
"<p><code>foo `` bar</code></p>",
"should support less accents"
);
assert_eq!(
to_html("*foo`*`"),
"<p>*foo<code>*</code></p>",
"should precede over emphasis"
);
assert_eq!(
to_html("[not a `link](/foo`)"),
"<p>[not a <code>link](/foo</code>)</p>",
"should precede over links"
);
assert_eq!(
to_html("`<a href=\"`\">`"),
"<p><code><a href="</code>">`</p>",
"should have same precedence as HTML (1)"
);
assert_eq!(
to_html_with_options("<a href=\"`\">`", &danger)?,
"<p><a href=\"`\">`</p>",
"should have same precedence as HTML (2)"
);
assert_eq!(
to_html("`<http://foo.bar.`baz>`"),
"<p><code><http://foo.bar.</code>baz>`</p>",
"should have same precedence as autolinks (1)"
);
assert_eq!(
to_html("<http://foo.bar.`baz>`"),
"<p><a href=\"http://foo.bar.%60baz\">http://foo.bar.`baz</a>`</p>",
"should have same precedence as autolinks (2)"
);
assert_eq!(
to_html("```foo``"),
"<p>```foo``</p>",
"should not support more accents before a fence"
);
assert_eq!(
to_html("`foo"),
"<p>`foo</p>",
"should not support no closing fence (1)"
);
assert_eq!(
to_html("`foo``bar``"),
"<p>`foo<code>bar</code></p>",
"should not support no closing fence (2)"
);
// Extra:
assert_eq!(
to_html("`foo\t\tbar`"),
"<p><code>foo\t\tbar</code></p>",
"should support tabs in code"
);
assert_eq!(
to_html("\\``x`"),
"<p>`<code>x</code></p>",
"should support an escaped initial grave accent"
);
assert_eq!(
to_html_with_options(
"`a`",
&Options {
parse: ParseOptions {
constructs: Constructs {
code_text: false,
..Default::default()
},
..Default::default()
},
..Default::default()
}
)?,
"<p>`a`</p>",
"should support turning off code (text)"
);
assert_eq!(
to_mdast("a `alpha` b.", &Default::default())?,
Node::Root(Root {
children: vec![Node::Paragraph(Paragraph {
children: vec![
Node::Text(Text {
value: "a ".into(),
position: Some(Position::new(1, 1, 0, 1, 3, 2))
}),
Node::InlineCode(InlineCode {
value: "alpha".into(),
position: Some(Position::new(1, 3, 2, 1, 10, 9))
}),
Node::Text(Text {
value: " b.".into(),
position: Some(Position::new(1, 10, 9, 1, 13, 12))
})
],
position: Some(Position::new(1, 1, 0, 1, 13, 12))
})],
position: Some(Position::new(1, 1, 0, 1, 13, 12))
}),
"should support code (text) as `InlineCode`s in mdast"
);
Ok(())
}
|