mirror of
https://github.com/priyanshujain/margin.git
synced 2026-10-02 19:17:03 +00:00
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:
1 parent
a54e7a42ef
commit
f4079a6f54
1 file changed
+19
-7
+19
-7
@@ -146,13 +146,23 @@ function meaningful(inline: JSONContent[]): boolean {
|
||||
|
||||
function imgFigure(img: Element, caption = ""): JSONContent[] {
|
||||
const src = img.getAttribute("src");
|
||||
if (!src || !src.startsWith("data:")) return [];
|
||||
return [
|
||||
{
|
||||
type: "figure",
|
||||
attrs: { src, alt: img.getAttribute("alt") ?? "", caption, placement: "full-width" },
|
||||
},
|
||||
];
|
||||
if (src && src.startsWith("data:")) {
|
||||
return [
|
||||
{
|
||||
type: "figure",
|
||||
attrs: { src, alt: img.getAttribute("alt") ?? "", caption, placement: "full-width" },
|
||||
},
|
||||
];
|
||||
}
|
||||
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[] {
|
||||
@@ -218,6 +228,8 @@ function blockFromElement(el: Element): JSONContent[] {
|
||||
return figureFrom(el);
|
||||
case "img":
|
||||
return imgFigure(el);
|
||||
case "svg":
|
||||
return svgFigure(el);
|
||||
case "script":
|
||||
case "style":
|
||||
return [];
|
||||
|
||||
Reference in new issue
Block a user