103 lines
3.2 KiB
TypeScript
103 lines
3.2 KiB
TypeScript
import { assertEquals, assertStringIncludes } from "@std/assert";
|
|
import {
|
|
convertDocument,
|
|
EpubOutput,
|
|
type HtmlDocument,
|
|
HtmlInput,
|
|
PdfOutput,
|
|
PublicationInput,
|
|
TextOutput,
|
|
textOutputPath,
|
|
} from "./mod.ts";
|
|
import { inspectEpub } from "../epub_builder/epub_builder.ts";
|
|
|
|
Deno.test("publication input renders chapter HTML once and preserves metadata", async () => {
|
|
const root = await Deno.makeTempDir();
|
|
try {
|
|
await Deno.mkdir(`${root}/1::Column`);
|
|
await Deno.writeTextFile(
|
|
`${root}/1::Column/1::Title.md`,
|
|
"# {{ document.title }}\n\n{{ greeting }}",
|
|
);
|
|
const document = await new PublicationInput({
|
|
journal: {
|
|
from_directory: root,
|
|
template_directory: root,
|
|
build_mode: "detached",
|
|
build_js_script: false,
|
|
},
|
|
}, { greeting: "Hello" }).read();
|
|
assertEquals(document.chapters[0].id, "article-1");
|
|
assertEquals(document.chapters[0].column.name, "Column");
|
|
assertStringIncludes(document.chapters[0].html, "Hello");
|
|
assertEquals(document.chapters[0].html.includes("article-start-"), false);
|
|
} finally {
|
|
await Deno.remove(root, { recursive: true });
|
|
}
|
|
});
|
|
|
|
Deno.test("HTML input exports text and EPUB without reading publication sources", async () => {
|
|
const root = await Deno.makeTempDir();
|
|
const document: HtmlDocument = {
|
|
kind: "html",
|
|
metadata: { title: "Book", author: "Author", language: "en" },
|
|
baseDirectory: root,
|
|
assetBase: root,
|
|
chapters: [{
|
|
id: "article-1",
|
|
title: "Title",
|
|
index: 1,
|
|
sourcePath: `${root}/missing.md`,
|
|
column: { name: "Column", index: 1 },
|
|
html: "<h1>Title</h1><p>Hello <strong>world</strong></p>",
|
|
}],
|
|
layout: { cover: "", styles: "", before: "", after: "" },
|
|
};
|
|
const original = JSON.stringify(document);
|
|
const config = {
|
|
journal: {
|
|
from_directory: `${root}/does-not-exist`,
|
|
build_js_script: false as const,
|
|
},
|
|
md: { path: `${root}/book.md` },
|
|
};
|
|
try {
|
|
for (const format of ["html", "md", "txt"] as const) {
|
|
const result = await convertDocument(
|
|
new HtmlInput(document),
|
|
new TextOutput(config, format),
|
|
);
|
|
const content = await Deno.readTextFile(result.path);
|
|
assertStringIncludes(content, "Hello");
|
|
assertEquals(content.includes("article-start-"), false);
|
|
}
|
|
const result = await convertDocument(
|
|
new HtmlInput(document),
|
|
new EpubOutput(config),
|
|
);
|
|
const archive = inspectEpub(await Deno.readFile(result.path));
|
|
assertStringIncludes(
|
|
new TextDecoder().decode(archive["EPUB/text/0001-title.xhtml"]),
|
|
"Hello",
|
|
);
|
|
assertEquals(JSON.stringify(document), original);
|
|
assertEquals(
|
|
textOutputPath({ ...config, html: { path: "custom.html" } }, "html"),
|
|
"custom.html",
|
|
);
|
|
if (Deno.env.get("CHROMIUM_PATH")) {
|
|
const pdf = await convertDocument(
|
|
new HtmlInput(document),
|
|
new PdfOutput({
|
|
...config,
|
|
pdf: { generate_index: false },
|
|
playwright: { path: `${root}/book.pdf`, format: "A4" },
|
|
}),
|
|
);
|
|
assertEquals((await Deno.stat(pdf.path)).size > 0, true);
|
|
}
|
|
} finally {
|
|
await Deno.remove(root, { recursive: true });
|
|
}
|
|
});
|