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

+104 -127
View File
@@ -1,25 +1,11 @@
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";
import type { TemplateVariables } from "../template/engine.ts";
import { publicationToHtml } from "../document/inputs.ts";
import type { DocumentConfig, HtmlDocument } from "../document/types.ts";
type EpubConfig = {
journal: {
build_mode?: "detached" | "flat";
from_directory: string;
template_directory: string;
title?: string;
author?: string;
language?: string;
identifier?: string;
};
vars?: Record<string, unknown>;
epub?: { path?: string; title?: string; author?: string; language?: string };
md?: { path?: string };
markdown_output?: string;
asset_base?: string;
};
type EpubConfig = DocumentConfig;
type EpubChapter = {
id: string;
@@ -188,121 +174,112 @@ export class EpubBuilder {
return this;
}
async build(path = this.outputPath()): Promise<void> {
if (!this.pfiles) this.with_files_sync();
async build(
path = this.outputPath(),
document?: HtmlDocument,
): Promise<void> {
if (!document && !this.pfiles) this.with_files_sync();
const input = document ??
publicationToHtml(this.pfiles!, this.config, this.variables);
const chapters: EpubChapter[] = [];
const mediaManifest = new Map<string, string>();
const epubMedia = new Map<string, Uint8Array>();
const allFiles = this.pfiles!;
let index = 0;
const sourceToHref = new Map<string, string>();
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`,
);
}
for (const chapter of input.chapters) {
index += 1;
sourceToHref.set(
resolve(chapter.sourcePath),
`text/${slug(chapter.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<string, string>();
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);
for (const chapter of input.chapters) {
index += 1;
const id = `chapter-${index}`;
const href = `text/${slug(chapter.title, index)}.xhtml`;
let html = removeRepeatedLeadingTitle(chapter.html, chapter.title);
const targetDir = dirname(resolve(href));
const sourceDir = dirname(resolve(chapter.sourcePath));
const media: Array<{ href: string; bytes: Uint8Array; mime: string }> =
[];
const mediaBySource = new Map<string, string>();
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;
}
// 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;
},
if (/^(?:[a-z][a-z0-9+.-]*:|#|\/\/)/i.test(sourceUrl)) continue;
const sourceAsset = resolve(
sourceDir,
decodeURIComponent(sourceUrl.split(/[?#]/, 1)[0]),
);
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);
const assetMarker = sourceUrl.match(/(?:^|\/)assets\/(.+)$/i);
const sharedAsset = assetMarker && input.assetBase
? resolve(input.assetBase, 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(
`${input.baseDirectory}${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: chapter.title,
html,
sourcePath: chapter.sourcePath,
column: chapter.column.name,
});
for (const asset of media) {
epubMedia.set(asset.href, asset.bytes);
mediaManifest.set(asset.href, asset.mime);
}
}
if (chapters.length === 0) {
@@ -310,10 +287,10 @@ export class EpubBuilder {
}
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 ??
const title = epub.title ?? input.metadata.title;
const author = epub.author ?? input.metadata.author;
const language = epub.language ?? input.metadata.language;
const identifier = input.metadata.identifier ??
`urn:uuid:${await this.contentIdentifier(chapters)}`;
const files: Record<string, Uint8Array> = {
mimetype: strToU8("application/epub+zip"),