Skip to content

Commit 9524a97

Browse files
committed
Hide tasklist classes behind an option
1 parent 8c4312f commit 9524a97

File tree

4 files changed

+144
-17
lines changed

4 files changed

+144
-17
lines changed

src/html.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -478,19 +478,18 @@ impl<'o, 'c: 'o> HtmlFormatter<'o, 'c> {
478478
NodeValue::List(ref nl) => {
479479
if entering {
480480
self.cr()?;
481-
482481
match nl.list_type {
483482
ListType::Bullet => {
484483
self.output.write_all(b"<ul")?;
485-
if nl.is_task_list {
484+
if nl.is_task_list && self.options.render.tasklist_classes {
486485
self.output.write_all(b" class=\"contains-task-list\"")?;
487486
}
488487
self.render_sourcepos(node)?;
489488
self.output.write_all(b">\n")?;
490489
}
491490
ListType::Ordered => {
492491
self.output.write_all(b"<ol")?;
493-
if nl.is_task_list {
492+
if nl.is_task_list && self.options.render.tasklist_classes {
494493
self.output.write_all(b" class=\"contains-task-list\"")?;
495494
}
496495
self.render_sourcepos(node)?;
@@ -1052,18 +1051,20 @@ impl<'o, 'c: 'o> HtmlFormatter<'o, 'c> {
10521051
NodeValue::TaskItem(symbol) => {
10531052
if entering {
10541053
self.cr()?;
1055-
self.output.write_all(b"<li class=\"task-list-item\"")?;
1054+
self.output.write_all(b"<li")?;
1055+
if self.options.render.tasklist_classes {
1056+
self.output.write_all(b" class=\"task-list-item\"")?;
1057+
}
10561058
self.render_sourcepos(node)?;
10571059
self.output.write_all(b">")?;
1058-
write!(
1059-
self.output,
1060-
"<input type=\"checkbox\" class=\"task-list-item-checkbox\" {}disabled=\"\" /> ",
1061-
if symbol.is_some() {
1062-
"checked=\"\" "
1063-
} else {
1064-
""
1065-
}
1066-
)?;
1060+
self.output.write_all(b"<input type=\"checkbox\"")?;
1061+
if self.options.render.tasklist_classes {
1062+
self.output.write_all(b" class=\"task-list-item-checkbox\"")?;
1063+
}
1064+
if symbol.is_some() {
1065+
self.output.write_all(b" checked=\"\"")?;
1066+
}
1067+
self.output.write_all(b" disabled=\"\" /> ")?;
10671068
} else {
10681069
self.output.write_all(b"</li>\n")?;
10691070
}

src/parser/mod.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ pub struct ExtensionOptions {
223223
/// options.extension.tasklist = true;
224224
/// options.render.unsafe_ = true;
225225
/// assert_eq!(markdown_to_html("* [x] Done\n* [ ] Not done\n", &options),
226-
/// "<ul class=\"contains-task-list\">\n<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" checked=\"\" disabled=\"\" /> Done</li>\n\
227-
/// <li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" disabled=\"\" /> Not done</li>\n</ul>\n");
226+
/// "<ul>\n<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Done</li>\n\
227+
/// <li><input type=\"checkbox\" disabled=\"\" /> Not done</li>\n</ul>\n");
228228
/// ```
229229
pub tasklist: bool,
230230

@@ -892,6 +892,23 @@ pub struct RenderOptions {
892892
/// "<p><figure><img src=\"https://example.com/image.png\" alt=\"image\" title=\"this is an image\" /><figcaption>this is an image</figcaption></figure></p>\n");
893893
/// ```
894894
pub figure_with_caption: bool,
895+
896+
/// Add classes to the output of the tasklist extension. This allows tasklists to be styled.
897+
///
898+
/// ```rust
899+
/// # use comrak::{markdown_to_html, Options};
900+
/// let mut options = Options::default();
901+
/// options.extension.tasklist = true;
902+
/// let input = "- [ ] Foo";
903+
///
904+
/// assert_eq!(markdown_to_html(input, &options),
905+
/// "<ul>\n<li><input type=\"checkbox\" disabled=\"\" /> Foo</li>\n</ul>\n");
906+
///
907+
/// options.render.tasklist_classes = true;
908+
/// assert_eq!(markdown_to_html(input, &options),
909+
/// "<ul class=\"contains-task-list\">\n<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" disabled=\"\" /> Foo</li>\n</ul>\n");
910+
/// ```
911+
pub tasklist_classes: bool,
895912
}
896913

897914
#[non_exhaustive]

src/tests/fuzz.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ fn tasklist() {
1010
html_opts!(
1111
[extension.tasklist, parse.relaxed_tasklist_matching],
1212
"* [*]",
13+
"<ul>\n<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> </li>\n</ul>\n",
14+
);
15+
}
16+
17+
#[test]
18+
fn tasklist_with_classes() {
19+
html_opts!(
20+
[extension.tasklist, render.tasklist_classes, parse.relaxed_tasklist_matching],
21+
"* [*]",
1322
"<ul class=\"contains-task-list\">\n<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" checked=\"\" disabled=\"\" /> </li>\n</ul>\n",
1423
);
1524
}

src/tests/tasklist.rs

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,58 @@ fn tasklist() {
2222
" * [x] Green\n",
2323
" * [ ] Blue\n"
2424
),
25+
concat!(
26+
"<ul>\n",
27+
"<li><input type=\"checkbox\" disabled=\"\" /> Red</li>\n",
28+
"<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Green</li>\n",
29+
"<li><input type=\"checkbox\" disabled=\"\" /> Blue</li>\n",
30+
"<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Papayawhip</li>\n",
31+
"</ul>\n",
32+
"<!-- end list -->\n",
33+
"<ol>\n",
34+
"<li><input type=\"checkbox\" disabled=\"\" /> Bird</li>\n",
35+
"<li><input type=\"checkbox\" disabled=\"\" /> McHale</li>\n",
36+
"<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Parish</li>\n",
37+
"</ol>\n",
38+
"<!-- end list -->\n",
39+
"<ul>\n",
40+
"<li><input type=\"checkbox\" disabled=\"\" /> Red\n",
41+
"<ul>\n",
42+
"<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Green\n",
43+
"<ul>\n",
44+
"<li><input type=\"checkbox\" disabled=\"\" /> Blue</li>\n",
45+
"</ul>\n",
46+
"</li>\n",
47+
"</ul>\n",
48+
"</li>\n",
49+
"</ul>\n"
50+
),
51+
);
52+
}
53+
54+
#[test]
55+
fn tasklist_with_classes() {
56+
html_opts!(
57+
[
58+
render.unsafe_,
59+
extension.tasklist,
60+
render.tasklist_classes,
61+
parse.relaxed_tasklist_matching
62+
],
63+
concat!(
64+
"* [ ] Red\n",
65+
"* [x] Green\n",
66+
"* [ ] Blue\n",
67+
"* [!] Papayawhip\n",
68+
"<!-- end list -->\n",
69+
"1. [ ] Bird\n",
70+
"2. [ ] McHale\n",
71+
"3. [x] Parish\n",
72+
"<!-- end list -->\n",
73+
"* [ ] Red\n",
74+
" * [x] Green\n",
75+
" * [ ] Blue\n"
76+
),
2577
concat!(
2678
"<ul class=\"contains-task-list\">\n",
2779
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" disabled=\"\" /> Red</li>\n",
@@ -57,8 +109,8 @@ fn tasklist_relaxed_regression() {
57109
[extension.tasklist, parse.relaxed_tasklist_matching],
58110
"* [!] Red\n",
59111
concat!(
60-
"<ul class=\"contains-task-list\">\n",
61-
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" checked=\"\" disabled=\"\" /> Red</li>\n",
112+
"<ul>\n",
113+
"<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Red</li>\n",
62114
"</ul>\n"
63115
),
64116
);
@@ -72,6 +124,35 @@ fn tasklist_relaxed_regression() {
72124
html_opts!(
73125
[extension.tasklist, parse.relaxed_tasklist_matching],
74126
"* [!] Red\n",
127+
concat!(
128+
"<ul>\n",
129+
"<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Red</li>\n",
130+
"</ul>\n"
131+
),
132+
);
133+
}
134+
135+
#[test]
136+
fn tasklist_with_classes_relaxed_regression() {
137+
html_opts!(
138+
[extension.tasklist, render.tasklist_classes, parse.relaxed_tasklist_matching],
139+
"* [!] Red\n",
140+
concat!(
141+
"<ul class=\"contains-task-list\">\n",
142+
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" checked=\"\" disabled=\"\" /> Red</li>\n",
143+
"</ul>\n"
144+
),
145+
);
146+
147+
html_opts!(
148+
[extension.tasklist, render.tasklist_classes],
149+
"* [!] Red\n",
150+
concat!("<ul>\n", "<li>[!] Red</li>\n", "</ul>\n"),
151+
);
152+
153+
html_opts!(
154+
[extension.tasklist, render.tasklist_classes, parse.relaxed_tasklist_matching],
155+
"* [!] Red\n",
75156
concat!(
76157
"<ul class=\"contains-task-list\">\n",
77158
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" checked=\"\" disabled=\"\" /> Red</li>\n",
@@ -89,6 +170,25 @@ fn tasklist_32() {
89170
"- [ ] This list item is **bold**\n",
90171
"- [x] There is some `code` here\n"
91172
),
173+
concat!(
174+
"<ul>\n",
175+
"<li><input type=\"checkbox\" disabled=\"\" /> List item 1</li>\n",
176+
"<li><input type=\"checkbox\" disabled=\"\" /> This list item is <strong>bold</strong></li>\n",
177+
"<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> There is some <code>code</code> here</li>\n",
178+
"</ul>\n"
179+
),
180+
);
181+
}
182+
183+
#[test]
184+
fn tasklist_32_with_classes() {
185+
html_opts!(
186+
[render.unsafe_, extension.tasklist, render.tasklist_classes],
187+
concat!(
188+
"- [ ] List item 1\n",
189+
"- [ ] This list item is **bold**\n",
190+
"- [x] There is some `code` here\n"
191+
),
92192
concat!(
93193
"<ul class=\"contains-task-list\">\n",
94194
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" disabled=\"\" /> List item 1</li>\n",

0 commit comments

Comments
 (0)