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

+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" };
}
}