Use article link markers for PDF indexing

This commit is contained in:
Nobody committed 2026-11-17 16:26:01 +08:00
1 parent 09af3bd8c0
commit 33f15d9ce0
20 files changed
+1137 -293

No files matched your search

+102
View File
@@ -0,0 +1,102 @@
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 });
}
});
+77
View File
@@ -0,0 +1,77 @@
import { resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { PFiles } from "../publication/file.ts";
import { md } from "../mdit/md.ts";
import { renderTemplate, type TemplateVariables } from "../template/engine.ts";
import type { DocumentConfig, DocumentInput, HtmlDocument } from "./types.ts";
export function publicationToHtml(
files: PFiles,
config: DocumentConfig,
variables: TemplateVariables = {},
): HtmlDocument {
const render = (source: string) => renderTemplate(source, variables);
let id = 0;
return {
kind: "html",
metadata: {
title: config.journal.title ?? "未命名出版物",
author: config.journal.author ?? "未知作者",
language: config.journal.language ?? "zh-CN",
identifier: config.journal.identifier,
},
baseDirectory: resolve(config.journal.from_directory),
assetBase: /^file:\/\//i.test(config.asset_base ?? "")
? fileURLToPath(config.asset_base!)
: resolve(config.asset_base ?? "../../assets"),
chapters: files.pcolumes.flatMap((column) =>
column.pfiles.map((file) => ({
id: `article-${++id}`,
title: file.meta.title,
sourcePath: resolve(file.full_name),
index: file.meta.index,
column: { name: column.name, index: column.index },
html: md.render(renderTemplate(file.content, {
...variables,
document: {
title: file.meta.title,
index: file.meta.index,
file: file.full_name,
},
column: { name: column.name, index: column.index },
})),
}))
),
layout: {
cover: render(files.cover_html ?? ""),
styles: render(files.style_html ?? ""),
before: render(files.other_htmls.join("\n")),
after: render(files.end_html ?? ""),
},
};
}
export class PublicationInput implements DocumentInput {
constructor(
private readonly config: DocumentConfig,
private readonly variables: TemplateVariables = {},
) {}
async read(): Promise<HtmlDocument> {
const files = new PFiles(
this.config.journal.build_mode ?? "flat",
this.config.journal.from_directory,
this.config,
);
return publicationToHtml(files, this.config, this.variables);
}
}
/** Allows callers and future parsers to supply HTML without publication files. */
export class HtmlInput implements DocumentInput {
constructor(private readonly document: HtmlDocument) {}
async read(): Promise<HtmlDocument> {
return this.document;
}
}
+8
View File
@@ -0,0 +1,8 @@
export * from "./types.ts";
export { HtmlInput, PublicationInput } from "./inputs.ts";
export {
EpubOutput,
PdfOutput,
TextOutput,
textOutputPath,
} from "./outputs.ts";
+96
View File
@@ -0,0 +1,96 @@
import { dirname } from "node:path";
import { DocuBuilder } from "../pdf_builder/docu_builder.ts";
import { EpubBuilder } from "../epub_builder/epub_builder.ts";
import {
type GPConfig,
type PDFPipelineOptions,
processPdfConfig,
} from "../pdf_builder/pipeline.ts";
import type {
DocumentConfig,
DocumentOutput,
HtmlDocument,
OutputResult,
} from "./types.ts";
export type TextFormat = "html" | "md" | "txt";
export function textOutputPath(
config: DocumentConfig,
format: TextFormat,
): string {
const explicit = format === "html"
? config.html?.path
: format === "txt"
? config.txt?.path
: config.md?.path;
if (explicit) return explicit;
const source = config.md?.path ?? config.markdown_output;
if (!source) {
throw new Error(
`Missing ${format} output path. Use ${format}.path or md.path.`,
);
}
return format === "md"
? source
: /\.md$/i.test(source)
? source.replace(/\.md$/i, `.${format}`)
: `${source}.${format}`;
}
export class TextOutput implements DocumentOutput {
constructor(
private readonly config: DocumentConfig,
private readonly format: TextFormat,
private readonly path = textOutputPath(config, format),
) {}
async write(document: HtmlDocument): Promise<OutputResult> {
const builder = new DocuBuilder({
...this.config,
asset_base: document.assetBase,
});
let content: string;
if (this.format === "html") {
content = await builder.as_html_page(document, { articleMarkers: false });
} else {
if (this.format === "md") await builder.as_markdown(document);
else await builder.as_txt(true, document);
content = builder.pages.join("\n");
}
await Deno.mkdir(dirname(this.path), { recursive: true });
await Deno.writeTextFile(this.path, content);
return {
path: this.path,
mediaType: this.format === "html"
? "text/html"
: this.format === "md"
? "text/markdown"
: "text/plain",
};
}
}
export class EpubOutput implements DocumentOutput {
constructor(
private readonly config: DocumentConfig,
private readonly path?: string,
) {}
async write(document: HtmlDocument): Promise<OutputResult> {
const builder = new EpubBuilder(this.config);
const path = this.path ?? builder.outputPath();
await builder.build(path, document);
return { path, mediaType: "application/epub+zip" };
}
}
export class PdfOutput implements DocumentOutput {
constructor(
private readonly config: GPConfig,
private readonly options: PDFPipelineOptions = {},
) {}
async write(document: HtmlDocument): Promise<OutputResult> {
await processPdfConfig(this.config, this.options, document);
return { path: this.config.playwright.path, mediaType: "application/pdf" };
}
}
+73
View File
@@ -0,0 +1,73 @@
import type { TemplateVariables } from "../template/engine.ts";
/** Configuration shared by publication inputs and output adapters. */
export interface DocumentConfig {
journal: {
from_directory: string;
template_directory?: string;
build_mode?: "detached" | "flat";
build_js_script?: string | false;
title?: string;
author?: string;
language?: string;
identifier?: string;
};
asset_base?: string;
vars?: TemplateVariables;
md?: { path?: string };
html?: { path?: string };
txt?: { path?: string };
epub?: { path?: string; title?: string; author?: string; language?: string };
markdown_output?: string;
}
/** An HTML fragment with stable identity and its original resource base. */
export interface HtmlChapter {
readonly id: string;
readonly title: string;
readonly html: string;
readonly sourcePath: string;
readonly index: number;
readonly column: { readonly name: string; readonly index: number };
}
/** Portable intermediate representation. HTML fragments contain no PDF markers. */
export interface HtmlDocument {
readonly kind: "html";
readonly metadata: {
readonly title: string;
readonly author: string;
readonly language: string;
readonly identifier?: string;
};
readonly baseDirectory: string;
readonly assetBase: string;
readonly chapters: readonly HtmlChapter[];
readonly layout: {
readonly cover: string;
readonly styles: string;
readonly before: string;
readonly after: string;
};
}
export interface DocumentInput {
read(): Promise<HtmlDocument>;
}
export interface OutputResult {
readonly path: string;
readonly mediaType: string;
}
export interface DocumentOutput {
write(document: HtmlDocument): Promise<OutputResult>;
}
/** The pipeline has no knowledge of publication files or output formats. */
export async function convertDocument(
input: DocumentInput,
output: DocumentOutput,
): Promise<OutputResult> {
return await output.write(await input.read());
}