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
87 changes: 85 additions & 2 deletions pkgs/id/src/web/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,14 +360,19 @@ fn convert_node<'a>(node: &'a AstNode<'a>, active_marks: &[Mark]) -> Value {
"content": [img_node]
});
}
// Convert HTML blocks to code blocks (safe fallback)
// Convert HTML blocks to code blocks (safe fallback), with
// a provenance attr (#982, cf. the #880 linkHref precedent):
// in PM shape a converted block is otherwise identical to an
// authored ```html fence, so the save direction uses
// `htmlBlock` to tell them apart and emit a real HtmlBlock
// for converted blocks while authored fences stay fenced.
let text = html.literal.strip_suffix('\n').unwrap_or(&html.literal);
if text.is_empty() {
json!({"type": "paragraph"})
} else {
json!({
"type": "code_block",
"attrs": {"params": "html"},
"attrs": {"params": "html", "htmlBlock": true},

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve the new provenance attributes in the editor schema

On the actual browser load/save path, this provenance is always lost: richSchema in pkgs/id/web/src/editor.ts inherits the basic code_block without declaring either htmlBlock or params, so Node.fromJSON(editorSchema, initialDoc) discards both attributes. getEditorState() then serializes that normalized document, which main.ts posts to /api/save, leaving the new HTML-emission branch unreachable for browser saves. Opening a Markdown file containing <!-- comment --> and saving it therefore still replaces the comment with a code fence. Although the commit mentions attribute stripping as a possible limitation, the existing schema makes it definitive. Declare both attributes on the editor's code-block schema and test a round trip through that schema rather than only through the Rust codec.

Useful? React with 👍 / 👎.

"content": [{"type": "text", "text": text}]
})
}
Expand Down Expand Up @@ -933,6 +938,20 @@ fn json_to_ast<'a>(arena: &'a Arena<'a>, json: &Value) -> Result<&'a AstNode<'a>
let content = content.as_str();
let info = json["attrs"]["params"].as_str().unwrap_or("");

// #982: a code_block carrying the htmlBlock provenance attr
// (set by the parse direction for blocks converted from raw
// HTML) emits as a real HTML block again, not a ```html
// fence. Authored fences and already-saved docs have no
// attr and keep the fenced emit below — no retro-guessing.
if info == "html" && json["attrs"]["htmlBlock"].as_bool() == Some(true) {
return Ok(arena.alloc(AstNode::from(NodeValue::HtmlBlock(
comrak::nodes::NodeHtmlBlock {
literal: format!("{content}\n"),
..Default::default()
},
))));
}

NodeValue::CodeBlock(Box::new(comrak::nodes::NodeCodeBlock {
fenced: true,
fence_char: b'`',
Expand Down Expand Up @@ -1836,6 +1855,70 @@ mod tests {
assert_eq!(reference["attrs"]["label"], "1");
}

#[test]
fn test_html_block_roundtrip_fixpoint() {
// #982: raw HTML blocks (comment, details, div) round-trip
// instead of being rewritten as ```html fences on save, and
// the second save is a fixpoint. Single-segment blocks are
// byte-identical; the multi-segment <details> case takes
// comrak's own block separation (a blank line before the
// closing tag) — verified as comrak-direct behaviour
// (parse + format_commonmark of the input, no codec involved,
// produces the same string), so byte-identity is not
// achievable for it at this layer.
for (original, expected) in [
("<!-- comment -->\n", "<!-- comment -->\n"),
(
"<details>\n<summary>Title</summary>\n\nBody text.\n</details>\n",
"<details>\n<summary>Title</summary>\n\nBody text.\n\n</details>\n",
),
(
"<div>\nSome content\n</div>\n",
"<div>\nSome content\n</div>\n",
),
] {
let (first, second) = two_saves(original);
assert_eq!(first, expected, "HTML block preserved: {original:?}");
assert_eq!(first, second, "fixpoint for {original:?}");
assert!(!first.contains("```"), "no fence rewrite: {first:?}");
}
}

#[test]
fn test_html_block_parse_shape_has_provenance_attr() {
// #982: the parse direction marks converted HTML blocks with
// the htmlBlock provenance attr so save can tell them apart
// from authored fences.
let doc = markdown_to_prosemirror("<!-- comment -->\n");
let block = &doc["content"][0];
assert_eq!(block["type"], "code_block");
assert_eq!(block["attrs"]["params"], "html");
assert_eq!(block["attrs"]["htmlBlock"], true);
}

#[test]
fn test_authored_html_fence_stays_fenced() {
// #982: a user-authored ```html fence round-trips as a fence —
// it has no provenance attr, so save keeps the fenced emit.
let original = "```html\n<div>x</div>\n```\n";
let (first, second) = two_saves(original);
assert_eq!(first, original, "authored fence preserved");
assert_eq!(first, second, "fixpoint");

// Already-saved docs (params=html, no htmlBlock attr) also
// stay fenced — no retro-guessing about their origin.
let pm = json!({
"type": "doc",
"content": [{
"type": "code_block",
"attrs": {"params": "html"},
"content": [{"type": "text", "text": "<!-- old -->"}]
}]
});
let md = prosemirror_to_markdown(&pm).expect("converts");
assert_eq!(md, "```html\n<!-- old -->\n```\n");
}

#[test]
fn test_em_link_roundtrip_fixpoint() {
for original in [
Expand Down
Loading