This commit is contained in:
Nobody committed 2026-11-12 18:34:33 +08:00
1 parent 5977b1b272
commit 09af3bd8c0
18 files changed
+57 -30

No files matched your search

+385
View File
@@ -0,0 +1,385 @@
import { strToU8, unzipSync, type Zippable, zipSync } from "fflate";
import { dirname, extname, relative, resolve, sep } from "node:path";
import { PFiles } from "../publication/file.ts";
import { md } from "../mdit/md.ts";
import { renderTemplate, type TemplateVariables } from "../template/engine.ts";
type EpubConfig = {
journal: {
build_mode?: "detached" | "flat";
from_directory: string;
template_directory: string;
title?: string;
author?: string;
language?: string;
identifier?: string;
};
vars?: Record<string, unknown>;
epub?: { path?: string; title?: string; author?: string; language?: string };
md?: { path?: string };
markdown_output?: string;
asset_base?: string;
};
type EpubChapter = {
id: string;
href: string;
title: string;
html: string;
sourcePath: string;
column: string;
};
function mediaType(extension: string): string {
return ({
".avif": "image/avif",
".gif": "image/gif",
".jpeg": "image/jpeg",
".jpg": "image/jpeg",
".png": "image/png",
".svg": "image/svg+xml",
".webp": "image/webp",
".css": "text/css",
".woff": "font/woff",
".woff2": "font/woff2",
".ttf": "font/ttf",
".otf": "font/otf",
".html": "application/xhtml+xml",
".htm": "application/xhtml+xml",
".js": "text/javascript",
".mp3": "audio/mpeg",
".mp4": "video/mp4",
".pdf": "application/pdf",
} as Record<string, string>)[extension] ?? "application/octet-stream";
}
function escapeXml(value: string): string {
return value.replace(/[&<>"']/g, (char) => ({
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&apos;",
}[char]!));
}
function normalizeHeadingText(value: string): string {
return value
.replace(/<[^>]*>/g, "")
.replace(
/&(#x[\da-f]+|#\d+|amp|lt|gt|quot|apos);/gi,
(entity, code: string) => {
if (code[0] === "#") {
const hex = code[1]?.toLowerCase() === "x";
const number = Number.parseInt(
code.slice(hex ? 2 : 1),
hex ? 16 : 10,
);
return Number.isFinite(number)
? String.fromCodePoint(number)
: entity;
}
return ({ amp: "&", lt: "<", gt: ">", quot: '"', apos: "'" } as Record<
string,
string
>)[code.toLowerCase()] ?? entity;
},
)
.normalize("NFKC")
.replace(/\s+/g, " ")
.trim();
}
function removeRepeatedLeadingTitle(html: string, title: string): string {
const heading = html.match(/^\s*<h1\b[^>]*>([\s\S]*?)<\/h1>\s*/i);
if (
!heading || normalizeHeadingText(heading[1]) !== normalizeHeadingText(title)
) {
return html;
}
return html.slice(heading[0].length);
}
function slug(value: string, index: number): string {
const normalized = value.normalize("NFKD").toLowerCase()
.replace(/[^\p{L}\p{N}]+/gu, "-").replace(/^-|-$/g, "");
return `${String(index).padStart(4, "0")}-${normalized || "chapter"}`;
}
function navXhtml(chapters: EpubChapter[], title: string): string {
let previousColumn = "";
let links = "";
for (const chapter of chapters) {
if (chapter.column !== previousColumn) {
if (previousColumn) links += "</ol></li>";
previousColumn = chapter.column;
links += `<li><span>${escapeXml(chapter.column)}</span><ol>`;
}
links += `<li><a href="${chapter.href}">${
escapeXml(chapter.title)
}</a></li>`;
}
if (previousColumn) links += "</ol></li>";
return `<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" lang="zh" xml:lang="zh">
<head><title>${escapeXml(title)}</title><meta charset="utf-8" /></head>
<body><nav epub:type="toc" id="toc"><h1>目录</h1><ol>${links}</ol></nav></body>
</html>`;
}
function packageOpf(
chapters: EpubChapter[],
media: Map<string, string>,
title: string,
author: string,
language: string,
identifier: string,
): string {
const manifest = [
`<item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/>`,
...chapters.map((chapter) =>
`<item id="${chapter.id}" href="${chapter.href}" media-type="application/xhtml+xml"/>`
),
...[...media.entries()].map(([href, mime], index) =>
`<item id="media-${index + 1}" href="${href}" media-type="${mime}"/>`
),
].join("\n ");
const spine = chapters.map((chapter) => `<itemref idref="${chapter.id}"/>`)
.join("\n ");
return `<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" unique-identifier="book-id" xml:lang="${
escapeXml(language)
}">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:identifier id="book-id">${escapeXml(identifier)}</dc:identifier>
<dc:title>${escapeXml(title)}</dc:title>
<dc:language>${escapeXml(language)}</dc:language>
<dc:creator>${escapeXml(author)}</dc:creator>
<meta property="dcterms:modified">${
new Date().toISOString().replace(/\.\d{3}Z$/, "Z")
}</meta>
</metadata>
<manifest>
${manifest}
</manifest>
<spine>
${spine}
</spine>
</package>`;
}
export class EpubBuilder {
private readonly config: EpubConfig;
private readonly variables: TemplateVariables;
private pfiles?: PFiles;
constructor(config: EpubConfig, variables: TemplateVariables = {}) {
this.config = config;
this.variables = variables;
}
with_files_sync(directory = this.config.journal.from_directory): this {
this.pfiles = new PFiles(
this.config.journal.build_mode ?? "flat",
directory,
this.config,
);
return this;
}
async build(path = this.outputPath()): Promise<void> {
if (!this.pfiles) this.with_files_sync();
const chapters: EpubChapter[] = [];
const mediaManifest = new Map<string, string>();
const epubMedia = new Map<string, Uint8Array>();
const allFiles = this.pfiles!;
let index = 0;
const sourceToHref = new Map<string, string>();
for (const column of allFiles.pcolumes) {
for (const pfile of column.pfiles) {
index += 1;
sourceToHref.set(
resolve(pfile.full_name),
`text/${slug(pfile.meta.title, index)}.xhtml`,
);
}
}
index = 0;
for (const column of allFiles.pcolumes) {
for (const pfile of column.pfiles) {
index += 1;
const id = `chapter-${index}`;
const href = `text/${slug(pfile.meta.title, index)}.xhtml`;
let html = md.render(renderTemplate(pfile.content, {
...this.variables,
document: {
title: pfile.meta.title,
index: pfile.meta.index,
file: pfile.full_name,
},
column: { name: column.name, index: column.index },
}));
html = removeRepeatedLeadingTitle(html, pfile.meta.title);
const targetDir = dirname(resolve(href));
const sourceDir = dirname(resolve(pfile.full_name));
const media: Array<{ href: string; bytes: Uint8Array; mime: string }> =
[];
const mediaBySource = new Map<string, string>();
const knownTargets = new Set(sourceToHref.keys());
const assetReferences = [
...html.matchAll(/\b(src|href)=(['"])([^'"]+)\2/gi),
];
for (const reference of assetReferences) {
const sourceUrl = reference[3];
if (sourceUrl.split(/[?#]/, 1)[0].toLowerCase().endsWith(".md")) {
continue;
}
if (/^(?:[a-z][a-z0-9+.-]*:|#|\/\/)/i.test(sourceUrl)) continue;
const sourceAsset = resolve(
sourceDir,
decodeURIComponent(sourceUrl.split(/[?#]/, 1)[0]),
);
const assetMarker = sourceUrl.match(/(?:^|\/)assets\/(.+)$/i);
const sharedAsset = assetMarker && this.config.asset_base
? resolve(this.config.asset_base, assetMarker[1])
: undefined;
const resolvedAsset = sharedAsset && await Deno.stat(sharedAsset)
.then((info) => info.isFile ? sharedAsset : undefined)
.catch(() => undefined) || sourceAsset;
if (knownTargets.has(sourceAsset)) continue;
const localAsset = resolvedAsset.startsWith(
`${resolve(this.config.journal.from_directory)}${sep}`,
) || (sharedAsset != null && resolvedAsset === sharedAsset);
if (!localAsset) continue;
try {
const info = await Deno.stat(resolvedAsset);
if (!info.isFile) continue;
} catch {
continue;
}
const destination = mediaBySource.get(resolvedAsset) ??
`media/${index}/${media.length + 1}${
extname(resolvedAsset).toLowerCase()
}`;
if (!mediaBySource.has(resolvedAsset)) {
mediaBySource.set(resolvedAsset, destination);
const ext = extname(resolvedAsset).toLowerCase();
media.push({
href: destination,
bytes: await Deno.readFile(resolvedAsset),
mime: mediaType(ext),
});
}
const escapedUrl = reference[0];
const replacement = `${reference[1]}=${reference[2]}${
relative(targetDir, resolve(destination)).split(sep).join("/")
}${reference[2]}`;
html = html.replace(escapedUrl, replacement);
}
// Resolve authored links that point to another Markdown chapter inside this EPUB.
html = html.replace(
/\b(href)=(['"])([^'"]+\.md(?:#[^'"]*)?)\2/gi,
(match, attr: string, quote: string, target: string) => {
const [filePart, fragment] = target.split("#", 2);
const targetPath = resolve(sourceDir, decodeURIComponent(filePart));
const chapterHref = sourceToHref.get(targetPath);
return chapterHref
? `${attr}=${quote}${
relative(targetDir, resolve(chapterHref)).split(sep).join("/")
}${fragment ? `#${fragment}` : ""}${quote}`
: match;
},
);
chapters.push({
id,
href,
title: pfile.meta.title,
html,
sourcePath: pfile.full_name,
column: column.name,
});
for (const asset of media) {
epubMedia.set(asset.href, asset.bytes);
mediaManifest.set(asset.href, asset.mime);
}
}
}
if (chapters.length === 0) {
throw new Error("No Markdown articles found for EPUB");
}
const epub = this.config.epub ?? {};
const title = epub.title ?? this.config.journal.title ?? "未命名出版物";
const author = epub.author ?? this.config.journal.author ?? "未知作者";
const language = epub.language ?? this.config.journal.language ?? "zh-CN";
const identifier = this.config.journal.identifier ??
`urn:uuid:${await this.contentIdentifier(chapters)}`;
const files: Record<string, Uint8Array> = {
mimetype: strToU8("application/epub+zip"),
"META-INF/container.xml": strToU8(
`<?xml version="1.0" encoding="UTF-8"?>\n<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container"><rootfiles><rootfile full-path="EPUB/content.opf" media-type="application/oebps-package+xml"/></rootfiles></container>`,
),
"EPUB/nav.xhtml": strToU8(navXhtml(chapters, title)),
"EPUB/content.opf": strToU8(
packageOpf(
chapters,
mediaManifest,
title,
author,
language,
identifier,
),
),
};
for (const [href, bytes] of epubMedia) files[`EPUB/${href}`] = bytes;
chapters.forEach((chapter) => {
files[`EPUB/${chapter.href}`] = strToU8(
`<?xml version="1.0" encoding="utf-8"?>\n<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" lang="${
escapeXml(language)
}" xml:lang="${
escapeXml(language)
}"><head><meta charset="utf-8"/><title>${
escapeXml(chapter.title)
}</title></head><body><h1>${
escapeXml(chapter.title)
}</h1>${chapter.html}</body></html>`,
);
});
const orderedEntries: Zippable = {
mimetype: [files.mimetype, { level: 0 }],
};
for (const [name, bytes] of Object.entries(files)) {
if (name !== "mimetype") orderedEntries[name] = bytes;
}
const archive = zipSync(orderedEntries, {
level: 6,
mtime: new Date("2020-01-01T00:00:00Z"),
});
await Deno.mkdir(path.replace(/[/\\][^/\\]*$/, ""), { recursive: true });
await Deno.writeFile(path, archive);
}
outputPath(): string {
if (this.config.epub?.path) return this.config.epub.path;
const sourcePath = this.config.md?.path ?? this.config.markdown_output;
if (!sourcePath) return "./outputs/book.epub";
return sourcePath.replace(/\.md$/i, ".epub");
}
private async contentIdentifier(chapters: EpubChapter[]): Promise<string> {
const digestInput = new TextEncoder().encode(
`${this.config.journal.from_directory}\n${
chapters.map((chapter) => chapter.sourcePath).join("\n")
}`,
);
const digest = await crypto.subtle.digest("SHA-256", digestInput);
return [...new Uint8Array(digest)].map((byte) =>
byte.toString(16).padStart(2, "0")
).join("");
}
}
export function inspectEpub(bytes: Uint8Array): Record<string, Uint8Array> {
return unzipSync(bytes);
}
+165
View File
@@ -0,0 +1,165 @@
import { assertEquals, assertStringIncludes } from "@std/assert";
import { EpubBuilder, inspectEpub } from "./epub_builder.ts";
Deno.test("generates EPUB 3 with a linked table of contents and spine", async () => {
const root = await Deno.makeTempDir();
const publication = `${root}/publication`;
const templates = `${root}/templates`;
const output = `${root}/book.epub`;
await Deno.mkdir(`${publication}/1::专栏`, { recursive: true });
await Deno.mkdir(templates, { recursive: true });
await Deno.writeTextFile(
`${publication}/1::专栏/1::第一篇.md`,
"正文甲",
);
await Deno.writeTextFile(
`${publication}/1::专栏/2::第二篇.md`,
"# 第二篇\n\n第二段\n\n![封面](assets/pixel.png)\n\n[转到第一篇](1::第一篇.md)",
);
await Deno.mkdir(`${publication}/1::专栏/assets`, { recursive: true });
await Deno.writeFile(
`${publication}/1::专栏/assets/pixel.png`,
new Uint8Array([1, 2, 3]),
);
const builder = new EpubBuilder({
journal: {
build_mode: "detached",
from_directory: publication,
template_directory: templates,
identifier: "urn:test:book",
},
epub: { path: output, title: "测试书", author: "作者甲" },
}).with_files_sync();
try {
await builder.build();
const archiveBytes = await Deno.readFile(output);
const zip = inspectEpub(archiveBytes);
const mimetype = new TextDecoder().decode(zip.mimetype);
assertEquals(mimetype, "application/epub+zip");
const archiveView = new DataView(
archiveBytes.buffer,
archiveBytes.byteOffset,
archiveBytes.byteLength,
);
// Local-file-header fields: compression method is the uint16 at offset 8.
assertEquals(archiveView.getUint16(8, true), 0);
assertEquals(
new TextDecoder().decode(archiveBytes.slice(30, 38)),
"mimetype",
);
const nav = new TextDecoder().decode(zip["EPUB/nav.xhtml"]);
assertStringIncludes(nav, 'epub:type="toc"');
assertStringIncludes(nav, "text/0001-第一篇.xhtml");
assertStringIncludes(nav, "text/0002-第二篇.xhtml");
const opf = new TextDecoder().decode(zip["EPUB/content.opf"]);
assertStringIncludes(opf, "<spine>");
assertStringIncludes(opf, '<itemref idref="chapter-1"/>');
assertStringIncludes(
new TextDecoder().decode(zip["EPUB/text/0001-第一篇.xhtml"]),
"正文甲",
);
const secondChapter = new TextDecoder().decode(
zip["EPUB/text/0002-第二篇.xhtml"],
);
assertEquals((secondChapter.match(/<h1\b/g) ?? []).length, 1);
assertStringIncludes(secondChapter, "第二篇</h1>");
assertStringIncludes(secondChapter, "../media/2/1.png");
assertStringIncludes(secondChapter, "0001-第一篇.xhtml");
assertEquals(zip["EPUB/media/2/1.png"].length, 3);
} finally {
await Deno.remove(root, { recursive: true });
}
});
Deno.test("uses EPUB output path or a stable default", () => {
const builder = new EpubBuilder({
journal: { from_directory: "publication", template_directory: "templates" },
});
assertEquals(builder.outputPath(), "./outputs/book.epub");
});
Deno.test("bundles shared assets referenced by publication articles", async () => {
const root = await Deno.makeTempDir();
const publication = `${root}/publication`;
const templates = `${root}/templates`;
const assets = `${root}/assets`;
const output = `${root}/shared-assets.epub`;
await Deno.mkdir(`${publication}/1::专栏`, { recursive: true });
await Deno.mkdir(templates, { recursive: true });
await Deno.mkdir(`${assets}/pictures`, { recursive: true });
await Deno.writeTextFile(
`${publication}/1::专栏/1::文章.md`,
"![共享图片](../../../assets/pictures/shared.png)",
);
const imageBytes = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]);
await Deno.writeFile(`${assets}/pictures/shared.png`, imageBytes);
const builder = new EpubBuilder({
asset_base: assets,
journal: {
build_mode: "detached",
from_directory: publication,
template_directory: templates,
},
epub: { path: output },
}).with_files_sync();
try {
await builder.build();
const archive = inspectEpub(await Deno.readFile(output));
const chapter = new TextDecoder().decode(
archive["EPUB/text/0001-文章.xhtml"],
);
assertStringIncludes(chapter, "../media/1/1.png");
assertEquals(
[...archive["EPUB/media/1/1.png"]],
[...imageBytes],
);
} finally {
await Deno.remove(root, { recursive: true });
}
});
Deno.test("packages shared assets directory images as EPUB resources", async () => {
const root = await Deno.makeTempDir();
const publication = `${root}/publication`;
const templates = `${root}/templates`;
const assets = `${root}/assets`;
const output = `${root}/shared-assets.epub`;
await Deno.mkdir(`${publication}/1::专栏`, { recursive: true });
await Deno.mkdir(templates, { recursive: true });
await Deno.mkdir(`${assets}/pictures`, { recursive: true });
await Deno.writeTextFile(
`${publication}/1::专栏/1::文章.md`,
"![共享图片](../../../assets/pictures/shared.png)",
);
const imageBytes = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]);
await Deno.writeFile(`${assets}/pictures/shared.png`, imageBytes);
const builder = new EpubBuilder({
asset_base: assets,
journal: {
build_mode: "detached",
from_directory: publication,
template_directory: templates,
identifier: "urn:test:shared-assets",
},
epub: { path: output },
}).with_files_sync();
try {
await builder.build();
const zip = inspectEpub(await Deno.readFile(output));
const chapter = new TextDecoder().decode(
zip["EPUB/text/0001-文章.xhtml"],
);
assertStringIncludes(chapter, "../media/1/1.png");
assertEquals(zip["EPUB/media/1/1.png"], imageBytes);
assertStringIncludes(
new TextDecoder().decode(zip["EPUB/content.opf"]),
'media/1/1.png" media-type="image/png"',
);
} finally {
await Deno.remove(root, { recursive: true });
}
});