From 762bdec29a8cda2ecc44c855e26065ff09ac3a54 Mon Sep 17 00:00:00 2001 From: Muse Sweep Date: Mon, 26 Oct 2026 22:52:00 -0500 Subject: [PATCH 1/2] fix(id-web): promote first row when a PM table has no header row (#1044) --- pkgs/id/src/web/markdown.rs | 85 +++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/pkgs/id/src/web/markdown.rs b/pkgs/id/src/web/markdown.rs index ee7e3c4e95..6df0db0b2c 100644 --- a/pkgs/id/src/web/markdown.rs +++ b/pkgs/id/src/web/markdown.rs @@ -1473,6 +1473,33 @@ fn json_to_ast<'a>(arena: &'a Arena<'a>, json: &Value) -> Result<&'a AstNode<'a> } } + // #1044: GFM has no headerless-table representation — comrak emits + // the delimiter row only when a row is marked as the header row, and + // without one the saved table reparses as a plain paragraph, silently + // destroying the table. The editor schema permits rows made only of + // table_cell, so when no row is a header row, promote the first row: + // a faithful rendering preserving every cell's content, and a one-time + // normalization (reparse yields table_header cells in row 0, so the + // next save is byte-identical). The table's alignments (#881) are + // stored on the table node and apply to the promoted row unchanged. + if node_type == "table" { + let mut first_row: Option<&AstNode<'a>> = None; + let mut has_header_row = false; + for child in ast_node.children() { + if let NodeValue::TableRow(is_header) = &child.data.borrow().value { + if first_row.is_none() { + first_row = Some(child); + } + if *is_header { + has_header_row = true; + } + } + } + if !has_header_row && let Some(row) = first_row { + row.data.borrow_mut().value = NodeValue::TableRow(true); + } + } + // Special case: image needs alt text as child (but NOT for HtmlInline, i.e. sized images) if node_type == "image" && matches!(ast_node.data.borrow().value, NodeValue::Image(_)) @@ -2852,6 +2879,64 @@ mod tests { assert_eq!(content[0]["type"], "table"); } + #[test] + fn test_pm_to_md_table_without_header_row_is_promoted() { + // #1044: a PM table whose rows are all table_cell (schema-legal) + // must save as a table: the first row is promoted to a header + // row so comrak emits the GFM delimiter row, instead of the + // table degrading to a plain paragraph on save. + let cell = |text: &str| { + json!({"type": "table_cell", "content": [{"type": "paragraph", "content": [{"type": "text", "text": text}]}]}) + }; + let pm_json = json!({ + "type": "doc", + "content": [{ + "type": "table", + "content": [ + {"type": "table_row", "content": [cell("a"), cell("b")]}, + {"type": "table_row", "content": [cell("c"), cell("d")]} + ] + }] + }); + + let md = prosemirror_to_markdown(&pm_json).unwrap(); + assert_eq!(md, "| a | b |\n| --- | --- |\n| c | d |\n"); + + // The saved output reparses as a table whose first row is a + // header row, and a second save is byte-identical (fixpoint). + let doc = markdown_to_prosemirror(&md); + let content = doc["content"].as_array().unwrap(); + assert_eq!(content[0]["type"], "table"); + let rows = content[0]["content"].as_array().unwrap(); + assert_eq!(rows.len(), 2); + assert_eq!(rows[0]["content"][0]["type"], "table_header"); + assert_eq!(rows[1]["content"][0]["type"], "table_cell"); + let md2 = prosemirror_to_markdown(&doc).unwrap(); + assert_eq!(md2, md); + } + + #[test] + fn test_pm_to_md_single_row_table_without_header_is_promoted() { + // #1044: single-row headerless table — the only row is promoted. + let pm_json = json!({ + "type": "doc", + "content": [{ + "type": "table", + "content": [ + {"type": "table_row", "content": [ + {"type": "table_cell", "content": [{"type": "paragraph", "content": [{"type": "text", "text": "only"}]}]} + ]} + ] + }] + }); + + let md = prosemirror_to_markdown(&pm_json).unwrap(); + assert_eq!(md, "| only |\n| --- |\n"); + let doc = markdown_to_prosemirror(&md); + assert_eq!(doc["content"][0]["type"], "table"); + assert_eq!(prosemirror_to_markdown(&doc).unwrap(), md); + } + #[test] fn test_image_basic() { let md = "![photo](http://example.com/img.png)\n"; From a33f8887cf2bd2449317cb57e0bbf4701cb10428 Mon Sep 17 00:00:00 2001 From: Muse Sweep Date: Mon, 26 Oct 2026 22:52:19 -0500 Subject: [PATCH 2/2] style(id): rustfmt headerless-table test closure (#1044) --- pkgs/id/src/web/markdown.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pkgs/id/src/web/markdown.rs b/pkgs/id/src/web/markdown.rs index 6df0db0b2c..1c51b04fe9 100644 --- a/pkgs/id/src/web/markdown.rs +++ b/pkgs/id/src/web/markdown.rs @@ -2885,9 +2885,7 @@ mod tests { // must save as a table: the first row is promoted to a header // row so comrak emits the GFM delimiter row, instead of the // table degrading to a plain paragraph on save. - let cell = |text: &str| { - json!({"type": "table_cell", "content": [{"type": "paragraph", "content": [{"type": "text", "text": text}]}]}) - }; + let cell = |text: &str| json!({"type": "table_cell", "content": [{"type": "paragraph", "content": [{"type": "text", "text": text}]}]}); let pm_json = json!({ "type": "doc", "content": [{