mirror of
https://github.com/priyanshujain/margin.git
synced 2026-10-04 20:17:03 +00:00
feat(export): surface typst warnings and glyph-coverage notice
Typst compile warnings were discarded. Capture them via the Warned result and emit a pdf-warnings event (only from the export path, not the live preview) that App shows as a non-fatal toast; the export still succeeds. Also scan book text before reporting success and warn when it contains scripts the embedded Latin fonts cannot render (CJK, Arabic, Hebrew, Devanagari, Thai, emoji). Claude-Session: https://claude.ai/code/session_01RzTWrLiy4zGuw2hZSQGamk
This commit is contained in:
1 parent
5c0040955e
commit
9eabef9041
6 files changed
+69
-10
No files matched your search
@@ -23,8 +23,12 @@ function App() {
|
||||
else if (event.payload === "export-epub") runExport("epub");
|
||||
else if (event.payload === "check-updates") checkForUpdates(false);
|
||||
});
|
||||
const unlistenWarn = listen<string>("pdf-warnings", (event) => {
|
||||
useBook.getState().setNotice(`PDF exported with warnings:\n${event.payload}`);
|
||||
});
|
||||
return () => {
|
||||
unlisten.then((stop) => stop());
|
||||
unlistenWarn.then((stop) => stop());
|
||||
};
|
||||
}, []);
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ const EPUB = [{ name: "EPUB", extensions: ["epub"] }];
|
||||
|
||||
export async function exportPdf(book: Book): Promise<void> {
|
||||
const { source, images } = bookToPdfInputs(book);
|
||||
const bytes = await compilePdf(source, images);
|
||||
const bytes = await compilePdf(source, images, true);
|
||||
await saveBytes(bytes, `${book.metadata.title || "Untitled"}.pdf`, PDF);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { Book } from "../model/book";
|
||||
import { exportEpub, exportPdf } from "./exporters";
|
||||
import { unsupportedScripts } from "./typst";
|
||||
import { useBook } from "../store/useBook";
|
||||
import { isDesktop } from "../ipc";
|
||||
|
||||
@@ -29,6 +30,11 @@ export async function runExport(format: "pdf" | "epub"): Promise<void> {
|
||||
await nextPaint();
|
||||
try {
|
||||
await run(book);
|
||||
if (format === "pdf") {
|
||||
const scripts = unsupportedScripts(book);
|
||||
if (scripts.length)
|
||||
setNotice(`Some ${scripts.join(", ")} characters may not render — the PDF fonts cover Latin scripts only.`);
|
||||
}
|
||||
} catch (e) {
|
||||
setNotice(`${label} export failed: ${e}`);
|
||||
} finally {
|
||||
|
||||
@@ -256,6 +256,34 @@ export function extractImages(book: Book): { images: ImageInput[]; paths: Map<st
|
||||
return collectImages(book.chapters.map((chapter) => chapter.content));
|
||||
}
|
||||
|
||||
const SCRIPT_RANGES: { label: string; test: RegExp }[] = [
|
||||
{ label: "CJK", test: /[-鿿가-豈-]/u },
|
||||
{ label: "Arabic", test: /[-ۿݐ-ݿ]/u },
|
||||
{ label: "Hebrew", test: /[-]/u },
|
||||
{ label: "Devanagari", test: /[ऀ-ॿ]/u },
|
||||
{ label: "Thai", test: /[-]/u },
|
||||
{ label: "emoji", test: /[☀-➿]|[\u{1f000}-\u{1faff}]/u },
|
||||
];
|
||||
|
||||
function collectText(book: Book): string {
|
||||
const parts = [book.metadata.title, book.metadata.subtitle, book.metadata.author];
|
||||
const visit = (node: JSONContent) => {
|
||||
if (node.type === "text" && node.text) parts.push(node.text);
|
||||
if (node.attrs?.caption) parts.push(String(node.attrs.caption));
|
||||
(node.content ?? []).forEach(visit);
|
||||
};
|
||||
book.chapters.forEach((chapter) => {
|
||||
parts.push(chapter.title);
|
||||
visit(chapter.content);
|
||||
});
|
||||
return parts.join("\n");
|
||||
}
|
||||
|
||||
export function unsupportedScripts(book: Book): string[] {
|
||||
const text = collectText(book);
|
||||
return SCRIPT_RANGES.filter((s) => s.test.test(text)).map((s) => s.label);
|
||||
}
|
||||
|
||||
function cleanTitle(title: string): string {
|
||||
return (title || "")
|
||||
.replace(/[\u0000-\u001f\u007f-\u009f\u2028\u2029]+/g, " ")
|
||||
|
||||
+6
-2
@@ -7,7 +7,11 @@ export interface ImageInput {
|
||||
data: string;
|
||||
}
|
||||
|
||||
export async function compilePdf(source: string, images: ImageInput[] = []): Promise<Uint8Array> {
|
||||
const buffer = await invoke<ArrayBuffer>("compile_pdf", { source, images });
|
||||
export async function compilePdf(
|
||||
source: string,
|
||||
images: ImageInput[] = [],
|
||||
emitWarnings = false,
|
||||
): Promise<Uint8Array> {
|
||||
const buffer = await invoke<ArrayBuffer>("compile_pdf", { source, images, emitWarnings });
|
||||
return new Uint8Array(buffer);
|
||||
}
|
||||
Reference in new issue
Block a user