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
94 changes: 89 additions & 5 deletions pkgs/id/src/web/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
//! - `link`: Hyperlink with `href` and `title` attributes
//! - `strikethrough`: Strikethrough text (GFM `~~text~~`)

use comrak::nodes::{AstNode, ListType, NodeValue};
use comrak::nodes::{AstNode, ListDelimType, ListType, NodeValue};
use comrak::{Arena, Options, format_commonmark, parse_document};
use serde_json::{Value, json};

Expand Down Expand Up @@ -3190,9 +3190,29 @@ fn is_end_list_separator<'a>(node: &'a AstNode<'a>) -> bool {
/// so numbering renders unchanged; `tight` is carried from the source
/// list to every run. Placement validation and the editor schema are
/// untouched: every emitted list is pure.
/// #1380: the PM `delimiter` attr spelling for a comrak ordered-list
/// delimiter (`.` vs `)`). The editor schema mirrors these spellings.
fn delimiter_attr(delimiter: ListDelimType) -> &'static str {
match delimiter {
ListDelimType::Period => "period",
ListDelimType::Paren => "paren",
}
}

/// #1380: read the PM `delimiter` attr back to a comrak delimiter.
/// Absent or unknown (legacy PM docs stored by the old codec, which
/// dropped the delimiter) defaults to Period - exactly how the old
/// codec saved every ordered list.
fn delimiter_from_attrs(json: &Value) -> ListDelimType {
match json["attrs"]["delimiter"].as_str() {
Some("paren") => ListDelimType::Paren,
_ => ListDelimType::Period,
}
}

fn convert_list_runs<'a>(node: &'a AstNode<'a>) -> Vec<Value> {
let (list_type, start, tight) = match &node.data.borrow().value {
NodeValue::List(list) => (list.list_type, list.start, list.tight),
let (list_type, start, tight, delimiter) = match &node.data.borrow().value {
NodeValue::List(list) => (list.list_type, list.start, list.tight, list.delimiter),
_ => return Vec::new(),
};
// (is_task, items before this run, converted items)
Expand All @@ -3214,7 +3234,7 @@ fn convert_list_runs<'a>(node: &'a AstNode<'a>) -> Vec<Value> {
.map(|(is_task, offset, content)| match (is_task, list_type) {
(true, ListType::Ordered) => json!({
"type": "task_list",
"attrs": {"order": start + offset, "tight": tight},
"attrs": {"order": start + offset, "tight": tight, "delimiter": delimiter_attr(delimiter)},
"content": content
}),
(true, ListType::Bullet) => json!({
Expand All @@ -3224,7 +3244,7 @@ fn convert_list_runs<'a>(node: &'a AstNode<'a>) -> Vec<Value> {
}),
(false, ListType::Ordered) => json!({
"type": "ordered_list",
"attrs": {"order": start + offset, "tight": tight},
"attrs": {"order": start + offset, "tight": tight, "delimiter": delimiter_attr(delimiter)},
"content": content
}),
(false, ListType::Bullet) => json!({
Expand Down Expand Up @@ -4481,10 +4501,13 @@ fn json_to_ast<'a>(arena: &'a Arena<'a>, json: &Value) -> Result<&'a AstNode<'a>
#[allow(clippy::cast_possible_truncation)] // order is typically small
let order = json["attrs"]["order"].as_u64().unwrap_or(1) as usize;
let tight = json["attrs"]["tight"].as_bool().unwrap_or(false);
// #1380: carry the delimiter style; absent (legacy docs)
// defaults to Period in delimiter_from_attrs.
NodeValue::List(comrak::nodes::NodeList {
list_type: ListType::Ordered,
start: order,
tight,
delimiter: delimiter_from_attrs(json),
..Default::default()
})
}
Expand Down Expand Up @@ -4516,6 +4539,8 @@ fn json_to_ast<'a>(arena: &'a Arena<'a>, json: &Value) -> Result<&'a AstNode<'a>
list_type: ListType::Ordered,
start,
tight,
// #1380: same delimiter carry as ordered_list.
delimiter: delimiter_from_attrs(json),
..Default::default()
}),
None => NodeValue::List(comrak::nodes::NodeList {
Expand Down Expand Up @@ -8702,6 +8727,65 @@ Some text
assert_eq!(bullet["content"][0]["attrs"]["tight"], true);
}

#[test]
fn test_ordered_list_paren_delimiter_roundtrips() {
// #1380: the ordered-list delimiter style (`1)` vs `1.`) must
// survive the codec in both directions instead of every list
// being rewritten with periods.
let doc = markdown_to_prosemirror("1) one\n2) two\n");
let content = doc["content"].as_array().unwrap();
assert_eq!(content[0]["type"], "ordered_list");
assert_eq!(content[0]["attrs"]["delimiter"], "paren");

let (first, second) = two_saves("1) one\n2) two\n");
assert_eq!(first, "1) one\n2) two\n", "paren round-trip changed bytes");
assert_eq!(first, second, "no fixpoint for paren list");

// Period lists are unchanged and now carry the attr explicitly.
let doc = markdown_to_prosemirror("1. one\n2. two\n");
assert_eq!(doc["content"][0]["attrs"]["delimiter"], "period");
let (first, second) = two_saves("1. one\n2. two\n");
assert_eq!(first, "1. one\n2. two\n");
assert_eq!(first, second, "no fixpoint for period list");
}

#[test]
fn test_ordered_task_list_paren_delimiter_roundtrips() {
// #1380: ordered task lists keep their delimiter too.
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);
assert_eq!(content[0]["attrs"]["delimiter"], "paren");

let (first, second) = two_saves("1) [ ] todo\n2) [x] done\n");
assert_eq!(first, "1) [ ] todo\n2) [x] done\n");
assert_eq!(first, second, "no fixpoint for paren task list");
}

#[test]
fn test_legacy_ordered_list_without_delimiter_attr_saves_period() {
// #1380: legacy PM docs stored by the old codec carry no
// delimiter attr; they must save exactly as today (period).
let doc = json!({
"type": "doc",
"content": [{
"type": "ordered_list",
"attrs": {"order": 1, "tight": true},
"content": [
{"type": "list_item", "content": [
{"type": "paragraph", "content": [{"type": "text", "text": "one"}]}
]},
{"type": "list_item", "content": [
{"type": "paragraph", "content": [{"type": "text", "text": "two"}]}
]}
]
}]
});
let md = prosemirror_to_markdown(&doc).unwrap();
assert_eq!(md, "1. one\n2. two\n", "legacy doc changed: {md:?}");
}

#[test]
fn test_task_list_mixed_content() {
// Task list items with text content
Expand Down
4 changes: 3 additions & 1 deletion pkgs/id/web/src/editor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ describe("richSchema list tight attrs (#1469)", () => {
content: [{ type: "ordered_list", attrs: { order: 3, tight }, content: [item("a"), item("b")] }],
};
const out = richSchema.nodeFromJSON(json).toJSON();
expect(out.content[0].attrs).toEqual({ order: 3, tight });
// #1380: the schema fills the delimiter default ("period") when the
// input omits it, so the round-tripped attrs carry it explicitly.
expect(out.content[0].attrs).toEqual({ order: 3, tight, delimiter: "period" });
expect(out.content[0].content).toEqual([item("a"), item("b")]);
});
});
Expand Down
12 changes: 11 additions & 1 deletion pkgs/id/web/src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ export const richSchema = new Schema({
attrs: {
order: { default: 1, validate: "number" },
tight: { default: true },
// #1380: mirror the codec's delimiter attr ("period"/"paren",
// default period) so paren-style lists are not rewritten on
// save; undeclared, it was dropped on editor load.
delimiter: { default: "period" },

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 Update the existing ordered-list round-trip expectations

Adding this default attribute makes richSchema.nodeFromJSON(...).toJSON() include delimiter: "period" even when the input omits it. Both parameterizations of the existing ordered-list round-trip test in pkgs/id/web/src/editor.test.ts:242–250 still compare the complete attrs object against { order: 3, tight }, so they now fail. Update those expectations to include the delimiter default; otherwise the test-web-unit check listed in .github/scripts/id-pr-checks.txt fails for this change.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in ecb7a49 (task 6504, lane H resume): the ordered-list round-trip expectations in pkgs/id/web/src/editor.test.ts now include delimiter: "period", matching the schema default filled on nodeFromJSON. Verified on the laptop: without the change both parameterizations fail (2 failed); with it, the full editor.test.ts suite passes (117 passed).

},
})
.update("image", {
Expand Down Expand Up @@ -211,7 +215,13 @@ export const richSchema = new Schema({
// depends on it so multi-block task items do not fuse on save.
// #997: ordered task lists carry their start in `order` (null for
// bullet task lists), mirroring the markdown codec's attrs.order.
attrs: { tight: { default: true }, order: { default: null } },
// #1380: ordered task lists also carry `delimiter`, mirroring
// the codec (default period, as in ordered_list above).
attrs: {
tight: { default: true },
order: { default: null },
delimiter: { default: "period" },
},
parseDOM: [
{
tag: "ul",
Expand Down