diff --git a/pkgs/id/src/web/markdown.rs b/pkgs/id/src/web/markdown.rs index 2b1514da2d..d8f3c296f9 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,15 @@ 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 +1188,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 +1501,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";