Skip to content

Commit

Permalink
Hide tasklist classes behind an option
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Oct 13, 2024
1 parent 8c4312f commit f0d3a3b
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 17 deletions.
28 changes: 15 additions & 13 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,19 +478,18 @@ impl<'o, 'c: 'o> HtmlFormatter<'o, 'c> {
NodeValue::List(ref nl) => {
if entering {
self.cr()?;

match nl.list_type {
ListType::Bullet => {
self.output.write_all(b"<ul")?;
if nl.is_task_list {
if nl.is_task_list && self.options.render.tasklist_classes {
self.output.write_all(b" class=\"contains-task-list\"")?;
}
self.render_sourcepos(node)?;
self.output.write_all(b">\n")?;
}
ListType::Ordered => {
self.output.write_all(b"<ol")?;
if nl.is_task_list {
if nl.is_task_list && self.options.render.tasklist_classes {
self.output.write_all(b" class=\"contains-task-list\"")?;
}
self.render_sourcepos(node)?;
Expand Down Expand Up @@ -1052,18 +1051,21 @@ impl<'o, 'c: 'o> HtmlFormatter<'o, 'c> {
NodeValue::TaskItem(symbol) => {
if entering {
self.cr()?;
self.output.write_all(b"<li class=\"task-list-item\"")?;
self.output.write_all(b"<li")?;
if self.options.render.tasklist_classes {
self.output.write_all(b" class=\"task-list-item\"")?;
}
self.render_sourcepos(node)?;
self.output.write_all(b">")?;
write!(
self.output,
"<input type=\"checkbox\" class=\"task-list-item-checkbox\" {}disabled=\"\" /> ",
if symbol.is_some() {
"checked=\"\" "
} else {
""
}
)?;
self.output.write_all(b"<input type=\"checkbox\"")?;
if self.options.render.tasklist_classes {
self.output
.write_all(b" class=\"task-list-item-checkbox\"")?;
}
if symbol.is_some() {
self.output.write_all(b" checked=\"\"")?;
}
self.output.write_all(b" disabled=\"\" /> ")?;
} else {
self.output.write_all(b"</li>\n")?;
}
Expand Down
21 changes: 19 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ pub struct ExtensionOptions {
/// options.extension.tasklist = true;
/// options.render.unsafe_ = true;
/// assert_eq!(markdown_to_html("* [x] Done\n* [ ] Not done\n", &options),
/// "<ul class=\"contains-task-list\">\n<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" checked=\"\" disabled=\"\" /> Done</li>\n\
/// <li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" disabled=\"\" /> Not done</li>\n</ul>\n");
/// "<ul>\n<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Done</li>\n\
/// <li><input type=\"checkbox\" disabled=\"\" /> Not done</li>\n</ul>\n");
/// ```
pub tasklist: bool,

Expand Down Expand Up @@ -892,6 +892,23 @@ pub struct RenderOptions {
/// "<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");
/// ```
pub figure_with_caption: bool,

/// Add classes to the output of the tasklist extension. This allows tasklists to be styled.
///
/// ```rust
/// # use comrak::{markdown_to_html, Options};
/// let mut options = Options::default();
/// options.extension.tasklist = true;
/// let input = "- [ ] Foo";
///
/// assert_eq!(markdown_to_html(input, &options),
/// "<ul>\n<li><input type=\"checkbox\" disabled=\"\" /> Foo</li>\n</ul>\n");
///
/// options.render.tasklist_classes = true;
/// assert_eq!(markdown_to_html(input, &options),
/// "<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");
/// ```
pub tasklist_classes: bool,
}

#[non_exhaustive]
Expand Down
9 changes: 9 additions & 0 deletions src/tests/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ fn tasklist() {
html_opts!(
[extension.tasklist, parse.relaxed_tasklist_matching],
"* [*]",
"<ul>\n<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> </li>\n</ul>\n",
);
}

#[test]
fn tasklist_with_classes() {
html_opts!(
[extension.tasklist, render.tasklist_classes, parse.relaxed_tasklist_matching],
"* [*]",
"<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",
);
}
Expand Down
104 changes: 102 additions & 2 deletions src/tests/tasklist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,58 @@ fn tasklist() {
" * [x] Green\n",
" * [ ] Blue\n"
),
concat!(
"<ul>\n",
"<li><input type=\"checkbox\" disabled=\"\" /> Red</li>\n",
"<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Green</li>\n",
"<li><input type=\"checkbox\" disabled=\"\" /> Blue</li>\n",
"<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Papayawhip</li>\n",
"</ul>\n",
"<!-- end list -->\n",
"<ol>\n",
"<li><input type=\"checkbox\" disabled=\"\" /> Bird</li>\n",
"<li><input type=\"checkbox\" disabled=\"\" /> McHale</li>\n",
"<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Parish</li>\n",
"</ol>\n",
"<!-- end list -->\n",
"<ul>\n",
"<li><input type=\"checkbox\" disabled=\"\" /> Red\n",
"<ul>\n",
"<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Green\n",
"<ul>\n",
"<li><input type=\"checkbox\" disabled=\"\" /> Blue</li>\n",
"</ul>\n",
"</li>\n",
"</ul>\n",
"</li>\n",
"</ul>\n"
),
);
}

#[test]
fn tasklist_with_classes() {
html_opts!(
[
render.unsafe_,
extension.tasklist,
render.tasklist_classes,
parse.relaxed_tasklist_matching
],
concat!(
"* [ ] Red\n",
"* [x] Green\n",
"* [ ] Blue\n",
"* [!] Papayawhip\n",
"<!-- end list -->\n",
"1. [ ] Bird\n",
"2. [ ] McHale\n",
"3. [x] Parish\n",
"<!-- end list -->\n",
"* [ ] Red\n",
" * [x] Green\n",
" * [ ] Blue\n"
),
concat!(
"<ul class=\"contains-task-list\">\n",
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" disabled=\"\" /> Red</li>\n",
Expand Down Expand Up @@ -57,8 +109,8 @@ fn tasklist_relaxed_regression() {
[extension.tasklist, parse.relaxed_tasklist_matching],
"* [!] Red\n",
concat!(
"<ul class=\"contains-task-list\">\n",
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" checked=\"\" disabled=\"\" /> Red</li>\n",
"<ul>\n",
"<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Red</li>\n",
"</ul>\n"
),
);
Expand All @@ -72,6 +124,35 @@ fn tasklist_relaxed_regression() {
html_opts!(
[extension.tasklist, parse.relaxed_tasklist_matching],
"* [!] Red\n",
concat!(
"<ul>\n",
"<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Red</li>\n",
"</ul>\n"
),
);
}

#[test]
fn tasklist_with_classes_relaxed_regression() {
html_opts!(
[extension.tasklist, render.tasklist_classes, parse.relaxed_tasklist_matching],
"* [!] Red\n",
concat!(
"<ul class=\"contains-task-list\">\n",
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" checked=\"\" disabled=\"\" /> Red</li>\n",
"</ul>\n"
),
);

html_opts!(
[extension.tasklist, render.tasklist_classes],
"* [!] Red\n",
concat!("<ul>\n", "<li>[!] Red</li>\n", "</ul>\n"),
);

html_opts!(
[extension.tasklist, render.tasklist_classes, parse.relaxed_tasklist_matching],
"* [!] Red\n",
concat!(
"<ul class=\"contains-task-list\">\n",
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" checked=\"\" disabled=\"\" /> Red</li>\n",
Expand All @@ -89,6 +170,25 @@ fn tasklist_32() {
"- [ ] This list item is **bold**\n",
"- [x] There is some `code` here\n"
),
concat!(
"<ul>\n",
"<li><input type=\"checkbox\" disabled=\"\" /> List item 1</li>\n",
"<li><input type=\"checkbox\" disabled=\"\" /> This list item is <strong>bold</strong></li>\n",
"<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> There is some <code>code</code> here</li>\n",
"</ul>\n"
),
);
}

#[test]
fn tasklist_32_with_classes() {
html_opts!(
[render.unsafe_, extension.tasklist, render.tasklist_classes],
concat!(
"- [ ] List item 1\n",
"- [ ] This list item is **bold**\n",
"- [x] There is some `code` here\n"
),
concat!(
"<ul class=\"contains-task-list\">\n",
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" disabled=\"\" /> List item 1</li>\n",
Expand Down

0 comments on commit f0d3a3b

Please sign in to comment.