From 4ad96427fa7501f50b6abe97514875b52c5ca6ff Mon Sep 17 00:00:00 2001 From: repo-worker-code Date: Mon, 26 Oct 2026 10:43:37 -0500 Subject: [PATCH 1/2] =?UTF-8?q?fix(id-web):=20canonical=20mark=20nesting?= =?UTF-8?q?=20order=20=E2=80=94=20strong/em/strikethrough+link=20round-tri?= =?UTF-8?q?p=20fixpoint=20(#944)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Parse direction appended marks outermost-first while save treated the marks array as innermost-first, so **[text](url)** and [**text**](url) converted into each other on every save. Both directions now sort marks by a shared canonical nesting order (link > strong > em > strikethrough > code): text_node_with_marks emits sorted marks so every spelling parses to the same ProseMirror doc, and create_marked_text sorts incoming marks before wrapping so the same doc always saves to the same markdown ([**text**](url)). New two-round-trip fixpoint tests for strong+link (both spellings, PM-doc convergence), em+link, strong+link with title, strikethrough+ link, and the bold-italic control. --- pkgs/id/src/web/markdown.rs | 111 ++++++++++++++++++++++++++++++++++-- 1 file changed, 106 insertions(+), 5 deletions(-) diff --git a/pkgs/id/src/web/markdown.rs b/pkgs/id/src/web/markdown.rs index 2b1514da2d..00f71d5667 100644 --- a/pkgs/id/src/web/markdown.rs +++ b/pkgs/id/src/web/markdown.rs @@ -106,6 +106,27 @@ struct Mark { attrs: Option, } +/// Canonical mark nesting order, outermost first (#944). +/// +/// Both conversion directions agree on this order, so a marked text node +/// has exactly one markdown spelling: the parse direction sorts the marks +/// it emits into this order (see [`text_node_with_marks`]) and the save +/// direction sorts incoming marks into this order before wrapping (see +/// `create_marked_text`). Without the shared order, `**[text](url)` and +/// `[**text**](url)` converted into each other on every save — the parse +/// direction appends marks outermost-first (wrapper arms push as they +/// nest) while the save direction treated the array as innermost-first. +fn mark_nesting_rank(mark_type: &str) -> u8 { + match mark_type { + "link" => 0, + "strong" => 1, + "em" => 2, + "strikethrough" => 3, + "code" => 4, + _ => 5, + } +} + /// Convert a comrak AST node to `ProseMirror` JSON. fn convert_node<'a>(node: &'a AstNode<'a>, active_marks: &[Mark]) -> Value { let data = node.data.borrow(); @@ -497,7 +518,11 @@ fn text_node_with_marks(text: &str, marks: &[Mark]) -> Value { }); if !marks.is_empty() { - let marks_json: Vec = marks + // #944: emit marks in the canonical nesting order so every + // spelling of the same marked text parses to the same PM doc. + let mut sorted: Vec = marks.to_vec(); + sorted.sort_by_key(|m| mark_nesting_rank(m.mark_type)); + let marks_json: Vec = sorted .iter() .map(|m| { if let Some(attrs) = &m.attrs { @@ -1129,10 +1154,16 @@ fn create_marked_text<'a>(arena: &'a Arena<'a>, text: &str, marks: &[Value]) -> } } - // Collect wrapper values (innermost first for building) + // Collect wrapper values in the canonical nesting order, outermost + // first (#944): sort the incoming marks — whose array order depends on + // which spelling or editor produced the doc — so the same marked text + // always saves to the same markdown. + let mut sorted_marks: Vec<&Value> = marks.iter().collect(); + sorted_marks + .sort_by_key(|mark| mark_nesting_rank(mark["type"].as_str().unwrap_or(""))); let mut wrappers: Vec = Vec::new(); - for mark in marks { + for mark in sorted_marks { let mark_type = mark["type"].as_str().unwrap_or(""); let wrapper_value = match mark_type { "em" => NodeValue::Emph, @@ -1158,8 +1189,10 @@ fn create_marked_text<'a>(arena: &'a Arena<'a>, text: &str, marks: &[Value]) -> return text_node; } - // Build wrapper chain recursively - innermost mark wraps text, outer marks wrap that - // marks array is [innermost, ..., outermost] so we build from index 0 + // Build wrapper chain recursively - innermost mark wraps text, outer + // marks wrap that. `build_mark_chain` consumes its list innermost + // first, so reverse the canonical outermost-first wrapper list. + wrappers.reverse(); build_mark_chain(arena, text_node, &wrappers, 0) } @@ -1469,6 +1502,74 @@ mod tests { assert!(result.contains("*italic*") || result.contains("_italic_")); } + /// Two consecutive open->save round-trips of `original`, returning both + /// saved spellings (#944 fixpoint assertions). + fn two_saves(original: &str) -> (String, String) { + let first = prosemirror_to_markdown(&markdown_to_prosemirror(original)).unwrap(); + let second = prosemirror_to_markdown(&markdown_to_prosemirror(&first)).unwrap(); + (first, second) + } + + #[test] + fn test_strong_link_roundtrip_fixpoint() { + // Both spellings of a bold link canonicalize to the link-outermost + // form and are byte-stable from the first save on (#944). + for original in [ + "**[text](https://example.com)**\n", + "[**text**](https://example.com)\n", + ] { + let (first, second) = two_saves(original); + assert_eq!(first, "[**text**](https://example.com)\n"); + assert_eq!(first, second, "no fixpoint for {original:?}"); + } + // The two spellings also parse to the identical PM doc. + assert_eq!( + markdown_to_prosemirror("**[text](https://example.com)**"), + markdown_to_prosemirror("[**text**](https://example.com)") + ); + } + + #[test] + fn test_em_link_roundtrip_fixpoint() { + for original in [ + "*[text](https://example.com)*\n", + "[*text*](https://example.com)\n", + ] { + let (first, second) = two_saves(original); + assert_eq!(first, "[*text*](https://example.com)\n"); + assert_eq!(first, second, "no fixpoint for {original:?}"); + } + } + + #[test] + fn test_strong_link_with_title_roundtrip_fixpoint() { + // Href and title survive every round-trip (#944 acceptance). + let (first, second) = two_saves("[**text**](https://example.com \"Title\")\n"); + assert_eq!(first, "[**text**](https://example.com \"Title\")\n"); + assert_eq!(first, second); + } + + #[test] + fn test_strikethrough_link_roundtrip_fixpoint() { + for original in [ + "~~[text](https://example.com)~~\n", + "[~~text~~](https://example.com)\n", + ] { + let (first, second) = two_saves(original); + assert_eq!(first, "[~~text~~](https://example.com)\n"); + assert_eq!(first, second, "no fixpoint for {original:?}"); + } + } + + #[test] + fn test_bold_italic_roundtrip_fixpoint() { + // Control that already round-tripped stably before #944: the + // canonical order (strong outside em) must keep it stable. + let (first, second) = two_saves("***both***\n"); + assert_eq!(first, second); + assert!(first.contains("***both***")); + } + #[test] fn test_roundtrip_code_block() { let original = "```rust\nfn main() {}\n```\n"; From ad0803476670b525a7e597f1eca5cfa8dee9565c Mon Sep 17 00:00:00 2001 From: repo-worker-code Date: Mon, 26 Oct 2026 10:56:49 -0500 Subject: [PATCH 2/2] style(id-web): rustfmt the canonical mark-order sort (#944) --- pkgs/id/src/web/markdown.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkgs/id/src/web/markdown.rs b/pkgs/id/src/web/markdown.rs index 00f71d5667..d8f3c296f9 100644 --- a/pkgs/id/src/web/markdown.rs +++ b/pkgs/id/src/web/markdown.rs @@ -1159,8 +1159,7 @@ fn create_marked_text<'a>(arena: &'a Arena<'a>, text: &str, marks: &[Value]) -> // which spelling or editor produced the doc — so the same marked text // always saves to the same markdown. let mut sorted_marks: Vec<&Value> = marks.iter().collect(); - sorted_marks - .sort_by_key(|mark| mark_nesting_rank(mark["type"].as_str().unwrap_or(""))); + sorted_marks.sort_by_key(|mark| mark_nesting_rank(mark["type"].as_str().unwrap_or(""))); let mut wrappers: Vec = Vec::new(); for mark in sorted_marks {