fix(import): preserve link, underline, strike and code marks

EPUB import only mapped bold and italic; a, u, s/del/strike and code
elements lost their formatting (kept as plain text). Map them to the
matching marks, consistent with what the exporters now render.

Claude-Session: https://claude.ai/code/session_01RzTWrLiy4zGuw2hZSQGamk
This commit is contained in:
pj committed 2026-06-22 13:36:07 -04:00
1 parent 0485177282
commit a54e7a42ef
1 file changed
+12 -3
+12 -3
View File
@@ -9,7 +9,7 @@ export interface RawFile {
encoding: "utf8" | "base64"; encoding: "utf8" | "base64";
} }
type Mark = { type: string }; type Mark = { type: string; attrs?: Record<string, unknown> };
const BLOCK_TAGS = new Set([ const BLOCK_TAGS = new Set([
"p", "div", "section", "article", "main", "header", "footer", "aside", "p", "div", "section", "article", "main", "header", "footer", "aside",
@@ -81,8 +81,8 @@ function hasMark(marks: Mark[], type: string): boolean {
return marks.some((m) => m.type === type); return marks.some((m) => m.type === type);
} }
function addMark(marks: Mark[], type: string): Mark[] { function addMark(marks: Mark[], type: string, attrs?: Record<string, unknown>): Mark[] {
return hasMark(marks, type) ? marks : [...marks, { type }]; return hasMark(marks, type) ? marks : [...marks, attrs ? { type, attrs } : { type }];
} }
function collectInline(node: Node, marks: Mark[], out: JSONContent[]): void { function collectInline(node: Node, marks: Mark[], out: JSONContent[]): void {
@@ -102,6 +102,15 @@ function collectInline(node: Node, marks: Mark[], out: JSONContent[]): void {
collectInline(el, addMark(marks, "bold"), out); collectInline(el, addMark(marks, "bold"), out);
} else if (tag === "em" || tag === "i") { } else if (tag === "em" || tag === "i") {
collectInline(el, addMark(marks, "italic"), out); collectInline(el, addMark(marks, "italic"), out);
} else if (tag === "u") {
collectInline(el, addMark(marks, "underline"), out);
} else if (tag === "s" || tag === "strike" || tag === "del") {
collectInline(el, addMark(marks, "strike"), out);
} else if (tag === "code") {
collectInline(el, addMark(marks, "code"), out);
} else if (tag === "a") {
const href = el.getAttribute("href");
collectInline(el, href ? addMark(marks, "link", { href }) : marks, out);
} else if (!BLOCK_TAGS.has(tag)) { } else if (!BLOCK_TAGS.has(tag)) {
collectInline(el, marks, out); collectInline(el, marks, out);
} }