From f210bff6bad2e02a2c50794adb5a8a45d9d16a8d Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sat, 31 Oct 2026 20:02:59 -0500 Subject: [PATCH] =?UTF-8?q?fix(id):=20.txt=20save=20keeps=20image=20alt=20?= =?UTF-8?q?text=20=E2=80=94=20image=20arm=20in=20extract=5Finline=5Ftext?= =?UTF-8?q?=20(#1491)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkgs/id/src/web/routes.rs | 81 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/pkgs/id/src/web/routes.rs b/pkgs/id/src/web/routes.rs index ca983a8bc2..98113879fc 100644 --- a/pkgs/id/src/web/routes.rs +++ b/pkgs/id/src/web/routes.rs @@ -2099,6 +2099,13 @@ fn convert_doc_to_bytes(name: &str, doc: &serde_json::Value) -> Result, /// recurses into any other inline node's `content` rather than /// dropping it. The pre-#1306 version handled only top-level /// paragraphs/headings and silently deleted everything else on save. +/// +/// Inline atoms carry no `content`, so recursion alone loses them. +/// Pinned representations (#1491): an `image` contributes its `alt` +/// text verbatim when non-empty, otherwise its `src` verbatim when +/// non-empty, otherwise nothing. Known remaining gap: `footnote_reference` +/// atoms are not yet rendered — they contribute nothing; that is +/// explicitly out of scope for the #1491 fix. fn extract_plain_text(doc: &serde_json::Value) -> String { let mut lines = Vec::new(); if let Some(content) = doc.get("content").and_then(|c| c.as_array()) { @@ -2156,6 +2163,8 @@ fn collect_plain_text_lines(nodes: &[serde_json::Value], lines: &mut Vec /// Extract the inline text of one node: `text` verbatim, `hard_break` /// as a newline, and any other inline node recursed into (its own /// inline content contributes its text instead of being dropped). +/// The `image` atom has no content — its payload is `attrs`, rendered +/// per the pinned policy on `extract_plain_text` (#1491). fn extract_inline_text(node: &serde_json::Value) -> String { let mut out = String::new(); if let Some(content) = node.get("content").and_then(|c| c.as_array()) { @@ -2168,6 +2177,20 @@ fn extract_inline_text(node: &serde_json::Value) -> String { } } "hard_break" => out.push('\n'), + "image" => { + let attrs = inline.get("attrs"); + let alt = attrs + .and_then(|a| a.get("alt")) + .and_then(|v| v.as_str()) + .unwrap_or(""); + if !alt.is_empty() { + out.push_str(alt); + } else if let Some(src) = + attrs.and_then(|a| a.get("src")).and_then(|v| v.as_str()) + { + out.push_str(src); + } + } _ => out.push_str(&extract_inline_text(inline)), } } @@ -3104,6 +3127,64 @@ mod tests { ); } + fn img(attrs: serde_json::Value) -> serde_json::Value { + serde_json::json!({"type": "image", "attrs": attrs}) + } + + #[test] + fn test_extract_plain_text_image_only_paragraph_saves_as_alt() { + // #1491: an image atom's only text is its alt. + let doc = plain_doc(vec![serde_json::json!({"type": "paragraph", "content": [ + img(serde_json::json!({"src": "blob:abc", "alt": "a red barn", "title": null})) + ]})]); + assert_eq!(extract_plain_text(&doc), "a red barn"); + } + + #[test] + fn test_extract_plain_text_image_between_text_runs() { + let doc = plain_doc(vec![serde_json::json!({"type": "paragraph", "content": [ + {"type": "text", "text": "before "}, + img(serde_json::json!({"src": "blob:abc", "alt": "middle", "title": null})), + {"type": "text", "text": " after"} + ]})]); + assert_eq!(extract_plain_text(&doc), "before middle after"); + } + + #[test] + fn test_extract_plain_text_image_in_list_item() { + // Recursive paths funnel through extract_inline_text, so the + // image policy applies inside list items too. + let doc = plain_doc(vec![serde_json::json!({"type": "bullet_list", "content": [ + {"type": "list_item", "content": [serde_json::json!({"type": "paragraph", "content": [ + img(serde_json::json!({"src": "blob:abc", "alt": "listed", "title": null})) + ]})]} + ]})]); + assert_eq!(extract_plain_text(&doc), "listed"); + } + + #[test] + fn test_extract_plain_text_image_without_alt_falls_back_to_src() { + // Missing alt and empty alt both fall back to src verbatim. + let missing = plain_doc(vec![serde_json::json!({"type": "paragraph", "content": [ + img(serde_json::json!({"src": "photo.png", "title": null})) + ]})]); + assert_eq!(extract_plain_text(&missing), "photo.png"); + let empty = plain_doc(vec![serde_json::json!({"type": "paragraph", "content": [ + img(serde_json::json!({"src": "photo.png", "alt": "", "title": null})) + ]})]); + assert_eq!(extract_plain_text(&empty), "photo.png"); + } + + #[test] + fn test_extract_plain_text_image_with_neither_alt_nor_src_is_empty() { + let doc = plain_doc(vec![serde_json::json!({"type": "paragraph", "content": [ + {"type": "text", "text": "x"}, + img(serde_json::json!({"alt": "", "title": null})), + {"type": "text", "text": "y"} + ]})]); + assert_eq!(extract_plain_text(&doc), "xy"); + } + #[test] fn test_classify_tag_primary() { let (kind, ts) = classify_tag("my-document.md");