asset忘了上传了,重新传一下,增加了模板引擎(见README),修复了printable的目录bug

This commit is contained in:
Nobody committed 2026-11-10 00:35:41 +08:00
1 parent 0817d33309
commit 8b88de4942
7268 files changed
+1257020

No files matched your search

+81
View File
@@ -0,0 +1,81 @@
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");
});