Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add task-list-item class to task list items #468

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,18 +478,28 @@ impl<'o, 'c: 'o> HtmlFormatter<'o, 'c> {
NodeValue::List(ref nl) => {
if entering {
self.cr()?;
if nl.list_type == ListType::Bullet {
self.output.write_all(b"<ul")?;
self.render_sourcepos(node)?;
self.output.write_all(b">\n")?;
} else if nl.start == 1 {
self.output.write_all(b"<ol")?;
self.render_sourcepos(node)?;
self.output.write_all(b">\n")?;
} else {
self.output.write_all(b"<ol")?;
self.render_sourcepos(node)?;
writeln!(self.output, " start=\"{}\">", nl.start)?;

match nl.list_type {
ListType::Bullet => {
self.output.write_all(b"<ul")?;
if nl.is_task_list {
self.output.write_all(b" class=\"contains-task-list\"")?;
Copy link
Sponsor Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to advocate again for task-list. This doesn't "contain" a task list, it "is" a task list. I don't think we need to follow GH on this. https://github.com/jgm/commonmark-hs also uses 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 {
self.output.write_all(b" class=\"contains-task-list\"")?;
}
self.render_sourcepos(node)?;
if nl.start == 1 {
self.output.write_all(b">\n")?;
} else {
writeln!(self.output, " start=\"{}\">", nl.start)?;
}
}
}
} else if nl.list_type == ListType::Bullet {
self.output.write_all(b"</ul>\n")?;
Expand Down Expand Up @@ -1042,12 +1052,12 @@ impl<'o, 'c: 'o> HtmlFormatter<'o, 'c> {
NodeValue::TaskItem(symbol) => {
if entering {
self.cr()?;
self.output.write_all(b"<li")?;
self.output.write_all(b"<li class=\"task-list-item\"")?;
self.render_sourcepos(node)?;
self.output.write_all(b">")?;
write!(
self.output,
"<input type=\"checkbox\" {}disabled=\"\" /> ",
"<input type=\"checkbox\" class=\"task-list-item-checkbox\" {}disabled=\"\" /> ",
if symbol.is_some() {
"checked=\"\" "
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ pub struct NodeList {
/// Whether the list is [tight](https://github.github.com/gfm/#tight), i.e. whether the
/// paragraphs are wrapped in `<p>` tags when formatted as HTML.
pub tight: bool,

/// Whether the list contains tasks (checkbox items)
pub is_task_list: bool,
}

/// The metadata of a description list
Expand Down
20 changes: 16 additions & 4 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>\n<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Done</li>\n\
/// <li><input type=\"checkbox\" disabled=\"\" /> Not done</li>\n</ul>\n");
/// "<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");
/// ```
pub tasklist: bool,

Expand Down Expand Up @@ -2436,7 +2436,13 @@ impl<'a, 'o, 'c: 'o> Parser<'a, 'o, 'c> {
return;
}

if !node_matches!(parent.parent().unwrap(), NodeValue::Item(..)) {
let grandparent = parent.parent().unwrap();
if !node_matches!(grandparent, NodeValue::Item(..)) {
return;
}

let great_grandparent = grandparent.parent().unwrap();
if !node_matches!(great_grandparent, NodeValue::List(..)) {
return;
}

Expand All @@ -2448,8 +2454,12 @@ impl<'a, 'o, 'c: 'o> Parser<'a, 'o, 'c> {
sourcepos.start.column += end;
parent.data.borrow_mut().sourcepos.start.column += end;

parent.parent().unwrap().data.borrow_mut().value =
grandparent.data.borrow_mut().value =
NodeValue::TaskItem(if symbol == ' ' { None } else { Some(symbol) });

if let NodeValue::List(ref mut list) = &mut great_grandparent.data.borrow_mut().value {
list.is_task_list = true;
}
}

fn parse_reference_inline(&mut self, content: &[u8]) -> Option<usize> {
Expand Down Expand Up @@ -2565,6 +2575,7 @@ fn parse_list_marker(
delimiter: ListDelimType::Period,
bullet_char: c,
tight: false,
is_task_list: false,
},
));
} else if isdigit(c) {
Expand Down Expand Up @@ -2620,6 +2631,7 @@ fn parse_list_marker(
},
bullet_char: 0,
tight: false,
is_task_list: false,
},
));
}
Expand Down
2 changes: 1 addition & 1 deletion src/tests/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn tasklist() {
html_opts!(
[extension.tasklist, parse.relaxed_tasklist_matching],
"* [*]",
"<ul>\n<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> </li>\n</ul>\n",
"<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
46 changes: 23 additions & 23 deletions src/tests/tasklist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ fn tasklist() {
" * [ ] 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 class=\"contains-task-list\">\n",
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" disabled=\"\" /> Red</li>\n",
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" checked=\"\" disabled=\"\" /> Green</li>\n",
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" disabled=\"\" /> Blue</li>\n",
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-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 class=\"contains-task-list\">\n",
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" disabled=\"\" /> Bird</li>\n",
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" disabled=\"\" /> McHale</li>\n",
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-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 class=\"contains-task-list\">\n",
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" disabled=\"\" /> Red\n",
"<ul class=\"contains-task-list\">\n",
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" checked=\"\" disabled=\"\" /> Green\n",
"<ul class=\"contains-task-list\">\n",
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" disabled=\"\" /> Blue</li>\n",
"</ul>\n",
"</li>\n",
"</ul>\n",
Expand All @@ -57,8 +57,8 @@ fn tasklist_relaxed_regression() {
[extension.tasklist, parse.relaxed_tasklist_matching],
"* [!] Red\n",
concat!(
"<ul>\n",
"<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Red</li>\n",
"<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"
),
);
Expand All @@ -73,8 +73,8 @@ fn tasklist_relaxed_regression() {
[extension.tasklist, parse.relaxed_tasklist_matching],
"* [!] Red\n",
concat!(
"<ul>\n",
"<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> Red</li>\n",
"<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"
),
);
Expand All @@ -90,10 +90,10 @@ fn tasklist_32() {
"- [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 class=\"contains-task-list\">\n",
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" disabled=\"\" /> List item 1</li>\n",
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" disabled=\"\" /> This list item is <strong>bold</strong></li>\n",
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" checked=\"\" disabled=\"\" /> There is some <code>code</code> here</li>\n",
"</ul>\n"
),
);
Expand Down
24 changes: 15 additions & 9 deletions src/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,21 @@ impl<'o, 'c> XmlFormatter<'o, 'c> {
was_literal = true;
}
NodeValue::List(ref nl) => {
if nl.list_type == ListType::Bullet {
self.output.write_all(b" type=\"bullet\"")?;
} else {
write!(
self.output,
" type=\"ordered\" start=\"{}\" delim=\"{}\"",
nl.start,
nl.delimiter.xml_name()
)?;
match nl.list_type {
ListType::Bullet => {
self.output.write_all(b" type=\"bullet\"")?;
}
ListType::Ordered => {
write!(
self.output,
" type=\"ordered\" start=\"{}\" delim=\"{}\"",
nl.start,
nl.delimiter.xml_name()
)?;
}
}
if nl.is_task_list {
self.output.write_all(b" tasklist=\"true\"")?;
}
write!(self.output, " tight=\"{}\"", nl.tight)?;
}
Expand Down
Loading