Skip to content
Merged
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
77 changes: 65 additions & 12 deletions pkgs/id/src/web/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! - `bullet_list`: Unordered list with `tight` attribute
//! - `ordered_list`: Ordered list with `order` and `tight` attributes
//! - `list_item`: List item containing blocks
//! - `task_list`: Task list (GFM `- [ ]`/`- [x]` items)
//! - `task_list`: Task list (GFM `- [ ]`/`- [x]` items); ordered task lists carry an `order` attribute
//! - `task_list_item`: Task list item with `checked` attribute
//! - `image`: Inline image with `src`, `alt`, `title` attributes
//! - `hard_break`: Hard line break
Expand Down Expand Up @@ -213,10 +213,25 @@ fn convert_node<'a>(node: &'a AstNode<'a>, active_marks: &[Mark]) -> Value {

if is_task_list {
let content = collect_list_items(node);
json!({
"type": "task_list",
"content": content
})
// #997: an ordered task list carries its start in
// `attrs.order`, mirroring `ordered_list` below, or the
// ordering is lost on save. Bullet task lists keep
// emitting no attrs, preserving their current JSON shape.
match list.list_type {
ListType::Ordered => {
json!({
"type": "task_list",
"attrs": {"order": list.start},
"content": content
})
}
ListType::Bullet => {
json!({
"type": "task_list",
"content": content
})
}
}
} else {
let content = collect_list_items(node);
let tight = list.tight;
Expand Down Expand Up @@ -1030,13 +1045,25 @@ fn json_to_ast<'a>(arena: &'a Arena<'a>, json: &Value) -> Result<&'a AstNode<'a>
"list_item" => NodeValue::Item(comrak::nodes::NodeList::default()),

"task_list" => {
// Task list is a bullet list whose children are TaskItems
NodeValue::List(comrak::nodes::NodeList {
list_type: ListType::Bullet,
tight: true,
bullet_char: b'-',
..Default::default()
})
// Task list is a list whose children are TaskItems. #997: when
// the parse side stored `attrs.order`, rebuild an ordered list
// with that start; without it, a bullet list as before.
#[allow(clippy::cast_possible_truncation)] // order is typically small
let order = json["attrs"]["order"].as_u64().map(|o| o as usize);
match order {
Some(start) => NodeValue::List(comrak::nodes::NodeList {
list_type: ListType::Ordered,
start,
tight: true,
..Default::default()
}),
None => NodeValue::List(comrak::nodes::NodeList {
list_type: ListType::Bullet,
tight: true,
bullet_char: b'-',
..Default::default()
}),
}
}

"task_list_item" => {
Expand Down Expand Up @@ -2233,6 +2260,32 @@ mod tests {
assert_eq!(items[1]["attrs"]["checked"], true);
}

#[test]
fn test_ordered_task_list_roundtrips() {
// #997: an ordered task list must keep its ordering and start on
// save instead of being rewritten as a bullet task list.
let doc = markdown_to_prosemirror("1. [ ] todo\n2. [x] done\n");
let content = doc["content"].as_array().unwrap();
assert_eq!(content[0]["type"], "task_list");
assert_eq!(content[0]["attrs"]["order"], 1);

let doc5 = markdown_to_prosemirror("5. [ ] five\n6. [x] six\n");
assert_eq!(doc5["content"][0]["attrs"]["order"], 5);

// Byte-identical round-trips at start 1 and start >1; the second
// save is a stable fixpoint.
for original in ["1. [ ] todo\n2. [x] done\n", "5. [ ] five\n6. [x] six\n"] {
let (first, second) = two_saves(original);
assert_eq!(first, original, "first save for {original:?}");
assert_eq!(first, second, "no fixpoint for {original:?}");
}

// A bullet task list keeps its current PM shape: no attrs at all.
let bullet = markdown_to_prosemirror("- [ ] todo\n");
assert_eq!(bullet["content"][0]["type"], "task_list");
assert!(bullet["content"][0]["attrs"].is_null());
}

#[test]
fn test_task_list_mixed_content() {
// Task list items with text content
Expand Down
22 changes: 21 additions & 1 deletion pkgs/id/web/src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ export const richSchema = new Schema({
task_list: {
group: "block",
content: "task_list_item+",
// #997: ordered task lists carry their start in `order` (null for
// bullet task lists), mirroring the markdown codec's attrs.order.
attrs: { order: { default: null } },
parseDOM: [
{
tag: "ul",
Expand All @@ -111,8 +114,25 @@ export const richSchema = new Schema({
return false;
},
},
{
tag: "ol",
getAttrs(dom) {
Comment on lines +117 to +119

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Give the ordered task-list parser higher priority

When pasting HTML containing an ordered task list, this rule is shadowed by the unconditional ol rule on ordered_list, which addListNodes places earlier in the schema. ProseMirror uses schema order for rules with equal priority (both default to 50), so the pasted list becomes an ordinary ordered list and loses its task-item checkbox state instead of retaining the new representation. Give the task-specific rule higher priority than the generic list rule. See the parser precedence implementation and stock ordered-list rule.

Useful? React with 👍 / 👎.

// Match <ol class=contains-task-list> (ordered task list);
// its start is the list's order.
const el = dom as HTMLOListElement;
if (el.classList.contains("contains-task-list")) return { order: el.start };
const firstLi = el.querySelector("li");
if (firstLi?.querySelector('input[type="checkbox"]')) return { order: el.start };
return false;
},
},
],
toDOM() {
toDOM(node) {
if (node.attrs.order != null) {
const attrs: Record<string, string> = { class: "contains-task-list" };
if (node.attrs.order !== 1) attrs.start = String(node.attrs.order);
return ["ol", attrs, 0];
}
return ["ul", { class: "contains-task-list" }, 0];
},
},
Expand Down
Loading