166 lines
5.9 KiB
TypeScript
166 lines
5.9 KiB
TypeScript
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\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`,
|
|
"",
|
|
);
|
|
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`,
|
|
"",
|
|
);
|
|
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 });
|
|
}
|
|
});
|