Skip to content

Commit

Permalink
Use separate property for tracking tasklists
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Sep 12, 2024
1 parent 00e9822 commit 2c22915
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/cm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ impl<'a, 'o, 'c> CommonMarkFormatter<'a, 'o, 'c> {

let mut listmarker = vec![];

let marker_width = if matches!(parent.list_type, ListType::Bullet | ListType::Task) {
let marker_width = if parent.list_type == ListType::Bullet {
2
} else {
let last_stack = self.ol_stack.last_mut().unwrap();
Expand All @@ -487,7 +487,7 @@ impl<'a, 'o, 'c> CommonMarkFormatter<'a, 'o, 'c> {
};

if entering {
if matches!(parent.list_type, ListType::Bullet | ListType::Task) {
if parent.list_type == ListType::Bullet {
let bullet = char::from(self.options.render.list_style as u8);
write!(self, "{} ", bullet).unwrap();
} else {
Expand Down
13 changes: 7 additions & 6 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,16 +482,17 @@ impl<'o, 'c: 'o> HtmlFormatter<'o, 'c> {
match nl.list_type {
ListType::Bullet => {
self.output.write_all(b"<ul")?;
self.render_sourcepos(node)?;
self.output.write_all(b">\n")?;
}
ListType::Task => {
self.output.write_all(b"<ul class=\"contains-task-list\"")?;
if nl.is_task_list {
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 {
self.output.write_all(b" class=\"contains-task-list\"")?;
}
self.render_sourcepos(node)?;
if nl.start == 1 {
self.output.write_all(b">\n")?;
Expand All @@ -500,7 +501,7 @@ impl<'o, 'c: 'o> HtmlFormatter<'o, 'c> {
}
}
}
} else if matches!(nl.list_type, ListType::Bullet | ListType::Task) {
} else if nl.list_type == ListType::Bullet {
self.output.write_all(b"</ul>\n")?;
} else {
self.output.write_all(b"</ol>\n")?;
Expand Down
6 changes: 3 additions & 3 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 All @@ -318,9 +321,6 @@ pub enum ListType {

/// An ordered list.
Ordered,

/// A task list i.e. a checkbox list.
Task,
}

/// The delimiter for ordered lists, i.e. the character which appears after each number.
Expand Down
6 changes: 4 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ 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 class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" checked=\"\" disabled=\"\" /> Done</li>\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 @@ -2458,7 +2458,7 @@ impl<'a, 'o, 'c: 'o> Parser<'a, 'o, 'c> {
NodeValue::TaskItem(if symbol == ' ' { None } else { Some(symbol) });

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

Expand Down Expand Up @@ -2575,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 @@ -2630,6 +2631,7 @@ fn parse_list_marker(
},
bullet_char: 0,
tight: false,
is_task_list: false,
},
));
}
Expand Down
1 change: 0 additions & 1 deletion src/tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ fn exercise_full_api() {
match nl.list_type {
nodes::ListType::Bullet => {}
nodes::ListType::Ordered => {}
nodes::ListType::Task => {}
}
let _: usize = nl.start;
match nl.delimiter {
Expand Down
4 changes: 2 additions & 2 deletions src/tests/tasklist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ fn tasklist() {
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" checked=\"\" disabled=\"\" /> Papayawhip</li>\n",
"</ul>\n",
"<!-- end list -->\n",
"<ul class=\"contains-task-list\">\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",
"</ul>\n",
"</ol>\n",
"<!-- end list -->\n",
"<ul class=\"contains-task-list\">\n",
"<li class=\"task-list-item\"><input type=\"checkbox\" class=\"task-list-item-checkbox\" disabled=\"\" /> Red\n",
Expand Down
6 changes: 3 additions & 3 deletions src/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ impl<'o, 'c> XmlFormatter<'o, 'c> {
nl.delimiter.xml_name()
)?;
}
ListType::Task => {
self.output.write_all(b" type=\"task\"")?;
}
}
if nl.is_task_list {
self.output.write_all(b" tasklist=\"true\"")?;
}
write!(self.output, " tight=\"{}\"", nl.tight)?;
}
Expand Down

0 comments on commit 2c22915

Please sign in to comment.