Skip to content
Merged
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
44 changes: 42 additions & 2 deletions pkgs/id/src/web/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Preserve empty links without emitting forbidden empty text nodes

When a Markdown file contains an empty link anywhere, this emits a text node that ProseMirror rejects: its TextNode constructor throws RangeError("Empty text nodes are not allowed"). In the actual editor load path, pkgs/id/web/src/editor.ts:352–356 calls Node.fromJSON and handles that exception by replacing the entire document with an empty document. Thus before [](https://e.com) after now opens completely blank, rather than merely losing the empty link. Use an editor-compatible representation and validate it through the frontend deserializer; codec-only round-trip tests miss this regression.

Useful? React with 👍 / 👎.

}
json!(children)
}

NodeValue::Image(image) => {
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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 = &para_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```");
Expand Down
Loading