fix(import): keep inline svg and unresolved image alt text

Inline svg elements were dropped entirely and an img whose source could
not be resolved was discarded. Serialize a standalone svg to a
data:image/svg+xml figure, and fall back to an unresolved image's alt
text as a paragraph instead of losing it.

Claude-Session: https://claude.ai/code/session_01RzTWrLiy4zGuw2hZSQGamk
This commit is contained in:
pj committed 2026-06-22 13:36:50 -04:00
1 parent a54e7a42ef
commit f4079a6f54
1 file changed
+13 -1
+13 -1
View File
@@ -146,7 +146,7 @@ function meaningful(inline: JSONContent[]): boolean {
function imgFigure(img: Element, caption = ""): JSONContent[] { function imgFigure(img: Element, caption = ""): JSONContent[] {
const src = img.getAttribute("src"); const src = img.getAttribute("src");
if (!src || !src.startsWith("data:")) return []; if (src && src.startsWith("data:")) {
return [ return [
{ {
type: "figure", type: "figure",
@@ -154,6 +154,16 @@ function imgFigure(img: Element, caption = ""): JSONContent[] {
}, },
]; ];
} }
const alt = (img.getAttribute("alt") ?? "").trim();
return alt ? [{ type: "paragraph", content: [{ type: "text", text: alt }] }] : [];
}
function svgFigure(el: Element): JSONContent[] {
const markup = el.outerHTML;
if (!markup) return [];
const src = `data:image/svg+xml;base64,${btoa(unescape(encodeURIComponent(markup)))}`;
return [{ type: "figure", attrs: { src, alt: "", caption: "", placement: "full-width" } }];
}
function figureFrom(el: Element): JSONContent[] { function figureFrom(el: Element): JSONContent[] {
const img = el.querySelector("img"); const img = el.querySelector("img");
@@ -218,6 +228,8 @@ function blockFromElement(el: Element): JSONContent[] {
return figureFrom(el); return figureFrom(el);
case "img": case "img":
return imgFigure(el); return imgFigure(el);
case "svg":
return svgFigure(el);
case "script": case "script":
case "style": case "style":
return []; return [];