From 6e3c3c0287fd4f1acde53565787fcb2c4ac4c040 Mon Sep 17 00:00:00 2001 From: Muse Sweep Date: Mon, 26 Oct 2026 16:45:29 -0500 Subject: [PATCH] fix(id-web): preserve spaced link/image destinations in angle form on save (#1019) --- pkgs/id/src/web/markdown.rs | 105 ++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/pkgs/id/src/web/markdown.rs b/pkgs/id/src/web/markdown.rs index 942fddf0ff..e4c4f3148f 100644 --- a/pkgs/id/src/web/markdown.rs +++ b/pkgs/id/src/web/markdown.rs @@ -874,11 +874,57 @@ fn parse_linked_img_tag(html: &str) -> Option { pub fn prosemirror_to_markdown(doc: &Value) -> Result { let arena = Arena::new(); let root = json_to_ast(&arena, doc)?; + // #1019: comrak's renderer percent-encodes a space in a link/image + // destination (`my notes.md` -> `my%20notes.md`) instead of emitting + // the <...> angle form, silently breaking relative file links and + // mutating the PM href on reparse. The renderer has no angle-form + // option, so swap each spaced destination for a unique placeholder + // before rendering and restore it as afterwards. + let substitutions = substitute_spaced_destinations(root); let mut output = String::new(); format_commonmark(root, &commonmark_options(), &mut output)?; + for (placeholder, raw) in substitutions { + output = output.replace(&placeholder, &format!("<{raw}>")); + } Ok(output) } +/// Replace every link/image destination containing a space with a +/// unique placeholder token, returning (placeholder, raw destination) +/// pairs for restoration after rendering (#1019). The token is a valid +/// bare destination (no spaces or characters comrak encodes), so the +/// renderer emits it verbatim exactly where the destination belongs. +fn substitute_spaced_destinations<'a>(root: &'a AstNode<'a>) -> Vec<(String, String)> { + let mut substitutions: Vec<(String, String)> = Vec::new(); + let mut stack: Vec<&AstNode<'a>> = vec![root]; + while let Some(node) = stack.pop() { + stack.extend(node.children()); + let mut data = node.data.borrow_mut(); + let url = match &mut data.value { + NodeValue::Link(link) | NodeValue::Image(link) => &mut link.url, + _ => continue, + }; + if !url.contains(' ') { + continue; + } + if let Some(index) = substitutions + .iter() + .position(|(_, raw)| raw.as_str() == url.as_str()) + { + *url = substitutions[index].0.clone(); + } else { + let placeholder = format!( + "idweb-spaced-destination-{}-placeholder", + substitutions.len() + ); + let raw = std::mem::take(url); + *url = placeholder.clone(); + substitutions.push((placeholder, raw)); + } + } + substitutions +} + /// Convert `ProseMirror` JSON to comrak AST. fn json_to_ast<'a>(arena: &'a Arena<'a>, json: &Value) -> Result<&'a AstNode<'a>, ConversionError> { let node_type = json["type"].as_str().ok_or(ConversionError::MissingType)?; @@ -1623,6 +1669,65 @@ mod tests { } } + #[test] + fn test_spaced_link_destination_roundtrips_in_angle_form() { + // #1019: a destination containing a space must save in the + // <...> angle form byte-identically — comrak's renderer would + // otherwise percent-encode it to %20, breaking relative links + // and mutating the PM href on reparse. + let original = "[notes]()\n"; + let (first, second) = two_saves(original); + assert_eq!(first, original, "angle form retained"); + assert_eq!(first, second, "fixpoint"); + assert!(!first.contains("%20"), "no percent-encoding: {first}"); + let doc = markdown_to_prosemirror(&first); + let link = &doc["content"][0]["content"][0]["marks"][0]; + assert_eq!(link["type"], "link"); + assert_eq!(link["attrs"]["href"], "my notes.md", "PM href unmutated"); + } + + #[test] + fn test_spaced_image_destination_roundtrips_in_angle_form() { + // #1019: same preservation for image src. + let original = "![a]()\n"; + let (first, second) = two_saves(original); + assert_eq!(first, original, "angle form retained"); + assert_eq!(first, second, "fixpoint"); + let doc = markdown_to_prosemirror(&first); + let img = &doc["content"][0]["content"][0]; + assert_eq!(img["type"], "image"); + assert_eq!(img["attrs"]["src"], "my pic.png", "PM src unmutated"); + } + + #[test] + fn test_spaced_destination_with_title_and_editor_href() { + // #1019: a spaced destination with a title keeps both, and an + // editor-created PM link whose href contains a space saves in + // angle form and reparses to the identical href. + let (first, second) = two_saves("[t]( \"T\")\n"); + assert_eq!(first, "[t]( \"T\")\n"); + assert_eq!(first, second, "fixpoint"); + + let pm = json!({ + "type": "doc", + "content": [{ + "type": "paragraph", + "content": [{ + "type": "text", + "text": "t", + "marks": [{"type": "link", "attrs": {"href": "https://e.com/a b"}}] + }] + }] + }); + let md = prosemirror_to_markdown(&pm).expect("converts"); + assert_eq!(md, "[t]()\n"); + let back = markdown_to_prosemirror(&md); + assert_eq!( + back["content"][0]["content"][0]["marks"][0]["attrs"]["href"], "https://e.com/a b", + "href identical after round trip" + ); + } + #[test] fn test_em_link_roundtrip_fixpoint() { for original in [