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; 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)[extension] ?? "application/octet-stream"; } function escapeXml(value: string): string { return value.replace(/[&<>"']/g, (char) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'", }[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*]*>([\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 += ""; previousColumn = chapter.column; links += `
  • ${escapeXml(chapter.column)}
      `; } links += `
    1. ${ escapeXml(chapter.title) }
    2. `; } if (previousColumn) links += "
  • "; return ` ${escapeXml(title)} `; } function packageOpf( chapters: EpubChapter[], media: Map, title: string, author: string, language: string, identifier: string, ): string { const manifest = [ ``, ...chapters.map((chapter) => `` ), ...[...media.entries()].map(([href, mime], index) => `` ), ].join("\n "); const spine = chapters.map((chapter) => ``) .join("\n "); return ` ${escapeXml(identifier)} ${escapeXml(title)} ${escapeXml(language)} ${escapeXml(author)} ${ new Date().toISOString().replace(/\.\d{3}Z$/, "Z") } ${manifest} ${spine} `; } 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 { if (!this.pfiles) this.with_files_sync(); const chapters: EpubChapter[] = []; const mediaManifest = new Map(); const epubMedia = new Map(); const allFiles = this.pfiles!; let index = 0; const sourceToHref = new Map(); 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(); 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 = { mimetype: strToU8("application/epub+zip"), "META-INF/container.xml": strToU8( `\n`, ), "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( `\n${ escapeXml(chapter.title) }

    ${ escapeXml(chapter.title) }

    ${chapter.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 { 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 { return unzipSync(bytes); }