perf(preview): add per-chapter Typst compile inputs

This commit is contained in:
pj committed 2026-06-20 19:03:46 -04:00
1 parent 8b9a7081f7
commit 29e8842847
1 file changed
+26 -2
+26 -2
View File
@@ -143,7 +143,7 @@ function imageExtension(dataUrl: string): string {
return kind === "jpeg" ? "jpg" : kind;
}
export function extractImages(book: Book): { images: ImageInput[]; paths: Map<string, string> } {
function collectImages(contents: JSONContent[]): { images: ImageInput[]; paths: Map<string, string> } {
const paths = new Map<string, string>();
const images: ImageInput[] = [];
@@ -159,10 +159,14 @@ export function extractImages(book: Book): { images: ImageInput[]; paths: Map<st
(node.content ?? []).forEach(visit);
};
book.chapters.forEach((chapter) => visit(chapter.content));
contents.forEach(visit);
return { images, paths };
}
export function extractImages(book: Book): { images: ImageInput[]; paths: Map<string, string> } {
return collectImages(book.chapters.map((chapter) => chapter.content));
}
export function bookToTypst(book: Book, paths: Map<string, string> = new Map()): string {
const meta = book.metadata;
const front = `#set page(numbering: none)
@@ -186,3 +190,23 @@ export function bookToPdfInputs(book: Book): { source: string; images: ImageInpu
const { images, paths } = extractImages(book);
return { source: bookToTypst(book, paths), images };
}
function chapterToTypst(book: Book, index: number, paths: Map<string, string>): string {
const chapter = book.chapters[index];
const opener = `#chapteropener(${str(String(index + 1))}, [${esc(chapter.title || "Untitled")}])`;
return `${preamble(book)}
#set page(numbering: "1")
#counter(page).update(1)
${opener}
${chapterBody(chapter.content, paths)}
`;
}
export function chapterToPdfInputs(book: Book, index: number): { source: string; images: ImageInput[] } {
const chapter = book.chapters[index];
const { images, paths } = collectImages([chapter.content]);
return { source: chapterToTypst(book, index, paths), images };
}