diff --git a/pkgs/id/src/web/markdown.rs b/pkgs/id/src/web/markdown.rs index 39e9c06f0a..102791439a 100644 --- a/pkgs/id/src/web/markdown.rs +++ b/pkgs/id/src/web/markdown.rs @@ -379,8 +379,14 @@ fn convert_node<'a>(node: &'a AstNode<'a>, active_marks: &[Mark]) -> Value { if let Some(img_attrs) = parse_linked_img_tag(html).or_else(|| parse_img_tag(html)) { return img_attrs; } - // Other inline HTML becomes plain text - text_node_with_marks(html, active_marks) + // #943: other inline HTML passes through as an html_inline + // atom node carrying the raw tag, so the save direction emits + // it verbatim (NodeValue::HtmlInline) instead of the comrak + // text renderer backslash-escaping it as literal text. + json!({ + "type": "html_inline", + "attrs": {"html": html} + }) } NodeValue::Table(table) => { @@ -1059,6 +1065,12 @@ fn json_to_ast<'a>(arena: &'a Arena<'a>, json: &Value) -> Result<&'a AstNode<'a> } } + // #943: raw inline HTML (e.g. , , , ) carried + // verbatim; comrak renders HtmlInline without escaping. + "html_inline" => { + NodeValue::HtmlInline(json["attrs"]["html"].as_str().unwrap_or("").to_owned()) + } + "hard_break" => NodeValue::LineBreak, "text" => { @@ -2226,4 +2238,63 @@ mod tests { assert!(md.contains("fn a() {}"), "first node kept: {md}"); assert!(md.contains("fn b() {}"), "second node kept: {md}"); } + + #[test] + fn test_inline_html_sub_sup_u_roundtrip_unescaped() { + // #943: inline HTML must round-trip open->save as HTML, not as + // backslash-escaped literal text. + for original in [ + "H2O\n", + "x2 + y\n", + "some underlined text\n", + ] { + let (first, second) = two_saves(original); + assert!( + !first.contains("\\<"), + "no backslash-escaping in save of {original:?}: {first}" + ); + for tag in ["", "", "", "", "", ""] { + if original.contains(tag) { + assert!( + first.contains(tag), + "{tag} survives save of {original:?}: {first}" + ); + } + } + assert_eq!(first, second, "fixpoint for {original:?}"); + } + } + + #[test] + fn test_inline_html_parse_shape_is_html_inline_node() { + // #943: the parse direction represents a tag as one html_inline + // atom node carrying the raw tag, not as a text node. + let doc = markdown_to_prosemirror("H2O\n"); + let content = &doc["content"][0]["content"]; + let html_nodes: Vec<&Value> = content + .as_array() + .unwrap() + .iter() + .filter(|n| n["type"] == "html_inline") + .collect(); + assert_eq!(html_nodes.len(), 2, "open+close tags: {content}"); + assert_eq!(html_nodes[0]["attrs"]["html"], ""); + assert_eq!(html_nodes[1]["attrs"]["html"], ""); + } + + #[test] + fn test_inline_html_generality_nonformatting_tag_roundtrips() { + // #943 generality: passthrough is not a sub/sup/u whitelist — any + // inline tag (here and a classed ) round-trips. + let (first, second) = two_saves("press Ctrl then go\n"); + assert!(first.contains(""), "kbd tag survives: {first}"); + assert!(first.contains(""), "kbd close survives: {first}"); + assert!( + first.contains(""), + "span with attrs survives verbatim: {first}" + ); + assert!(first.contains(""), "span close survives: {first}"); + assert!(!first.contains("\\<"), "no escaping: {first}"); + assert_eq!(first, second, "fixpoint"); + } } diff --git a/pkgs/id/web/src/editor.ts b/pkgs/id/web/src/editor.ts index 28ca11c626..9bd4bd987f 100644 --- a/pkgs/id/web/src/editor.ts +++ b/pkgs/id/web/src/editor.ts @@ -186,6 +186,28 @@ export const richSchema = new Schema({ return ["th", 0]; }, }, + // #943: raw inline HTML (e.g. , , , ) as an atom + // node carrying the tag verbatim in `html`; the markdown codec + // reads/writes it. Display shows the tag as inert text — the attr + // is never rendered via innerHTML or as live markup. + html_inline: { + inline: true, + group: "inline", + atom: true, + selectable: true, + attrs: { html: { default: "" } }, + parseDOM: [ + { + tag: "span[data-html-inline]", + getAttrs(dom) { + return { html: (dom as HTMLElement).dataset.htmlInline ?? "" }; + }, + }, + ], + toDOM(node) { + return ["span", { "data-html-inline": node.attrs.html as string }, node.attrs.html as string]; + }, + }, }), marks: basicSchema.spec.marks.append({ strikethrough: {