增加了可打印版本的src

This commit is contained in:
hydrogen committed 2026-05-04 11:38:56 +08:00
1 parent 0161480fa2
commit d3e7503676
9 files changed
+288 -197

No files matched your search

+28 -7
View File
@@ -48,14 +48,14 @@ export class DocuBuilder {
this.pfiles = new PFiles(dir);
}
as_markdown() {
async as_markdown() {
const turndownService = new TurndownService();
const md_page = turndownService.turndown(this.as_html_page());
const md_page = turndownService.turndown(await this.as_html_page());
this.pages = [md_page];
}
as_html_page(): string {
async as_html_page(): Promise<string> {
this.pages = [];
const md = new MarkdownIt({
html: true,
@@ -86,7 +86,9 @@ export class DocuBuilder {
if (this.cfg.journal.build_js_script != false) {
const f = Deno.readTextFileSync(`${this.cfg.journal.build_js_script}`);
const html = this.pages.join("\n");
const domParserCtor = (globalThis as {
// Ensure a DOMParser is available; if not, try to load deno_dom dynamically.
let domParserCtor = (globalThis as {
DOMParser?: new () => {
parseFromString: (
html: string,
@@ -94,9 +96,28 @@ export class DocuBuilder {
) => { documentElement?: { outerHTML: string } } | null;
};
}).DOMParser;
if (domParserCtor == null) {
throw new Error("DOMParser is not available in this runtime.");
try {
const denoDom = await import(
"https://deno.land/x/deno_dom/deno-dom-wasm.ts"
);
if (denoDom && denoDom.DOMParser) {
(globalThis as any).DOMParser = denoDom.DOMParser;
domParserCtor = denoDom.DOMParser;
}
} catch (e) {
// ignore; will throw below with helpful message
}
}
if (domParserCtor == null) {
throw new Error(
"DOMParser is not available in this runtime. Install deno_dom or run in a runtime that provides DOMParser.\n" +
"Suggested: import 'https://deno.land/x/deno_dom/deno-dom-wasm.ts' or run with a JS DOM-enabled environment.",
);
}
const document = new domParserCtor().parseFromString(html, "text/html");
const runBuildScript = new Function(
"html",
@@ -135,7 +156,7 @@ return document?.documentElement?.outerHTML ?? html;`,
throw new Error("Browser is not initialized. Call with_browser() first.");
}
if (this.pfiles != null && pages.length === 0) {
this.as_html_page();
await this.as_html_page();
}
const page = await this.browser.newPage();
try {
@@ -269,7 +290,7 @@ export class IndexBuilder {
const builder = new DocuBuilder(cfg);
try {
await builder.with_browser();
builder.pages = [this.as_html_page()];
builder.pages = [await this.as_html_page()];
await builder.build_as_pdf(cfg);
} finally {
await builder.close_browser();