From a06b506de936ba0021ee6b0a08ca02268244ec0b Mon Sep 17 00:00:00 2001 From: Muse Sweep Date: Mon, 26 Oct 2026 18:23:24 -0500 Subject: [PATCH] fix(id-web): preserve empty-text links in markdown codec (#996) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Link arm represented a link only as a mark on its child text nodes; an empty-text link has no children, so the mark and href vanished on open and save emitted nothing. - convert_node Link arm: when the link has no inline children, synthesize an empty text node carrying the link mark. - text_node_with_marks: drop empty text only when it carries no marks — an empty marked node is the representation of an empty-text link. - Tests: PM shape for [](https://e.com) plus byte-identical round-trips alone, mid-paragraph, and with a title, each a stable second-save fixpoint. Queue task 1190. --- pkgs/id/src/web/markdown.rs | 44 +++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/pkgs/id/src/web/markdown.rs b/pkgs/id/src/web/markdown.rs index 031be4aad0..d52e1fd192 100644 --- a/pkgs/id/src/web/markdown.rs +++ b/pkgs/id/src/web/markdown.rs @@ -308,7 +308,17 @@ fn convert_node<'a>(node: &'a AstNode<'a>, active_marks: &[Mark]) -> Value { mark_type: "link", attrs: Some(attrs), }); - json!(collect_inline_children(node, &marks)) + let children = collect_inline_children(node, &marks); + if children.is_empty() { + // #996: an empty-text link has no inline children to + // carry the link mark, so without this the mark — and + // the href — vanish and save emits nothing. Synthesize + // an empty text node carrying the mark; the save side + // wraps it back into a Link node (create_marked_text), + // so `[](url)` round-trips. + return json!(vec![text_node_with_marks("", &marks)]); + } + json!(children) } NodeValue::Image(image) => { @@ -535,7 +545,10 @@ fn convert_node<'a>(node: &'a AstNode<'a>, active_marks: &[Mark]) -> Value { /// Create a text node with the given marks. fn text_node_with_marks(text: &str, marks: &[Mark]) -> Value { - if text.is_empty() { + // An empty text node with no marks carries nothing and is dropped, + // but an empty text node WITH marks is information-bearing: it is + // the only representation of an empty-text link (#996). + if text.is_empty() && marks.is_empty() { return json!(null); } @@ -1640,6 +1653,33 @@ mod tests { assert_eq!(link_mark["attrs"]["href"], "https://example.com"); } + #[test] + fn test_empty_link_roundtrips() { + // #996: an empty-text link has no child text node to carry the + // link mark; the codec synthesizes an empty marked text node so + // the href is present in the PM doc and survives save. + let doc = markdown_to_prosemirror("[](https://e.com)"); + let para_content = doc["content"][0]["content"].as_array().unwrap(); + assert_eq!(para_content.len(), 1); + assert_eq!(para_content[0]["type"], "text"); + assert_eq!(para_content[0]["text"], ""); + let mark = ¶_content[0]["marks"][0]; + assert_eq!(mark["type"], "link"); + assert_eq!(mark["attrs"]["href"], "https://e.com"); + + // Byte-identical round-trips, alone, mid-paragraph, and with a + // title; the second save is a stable fixpoint. + for original in [ + "[](https://e.com)\n", + "before [](https://e.com) after\n", + "[](https://e.com \"T\")\n", + ] { + let (first, second) = two_saves(original); + assert_eq!(first, original, "first save for {original:?}"); + assert_eq!(first, second, "no fixpoint for {original:?}"); + } + } + #[test] fn test_code_block() { let doc = markdown_to_prosemirror("```rust\nfn main() {}\n```");