@junejs/pdf 0.1.0-dev.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/package.json +4 -28
- package/src/index.ts +5 -2
- package/src/markdown-typst.ts +1 -1
- package/src/renderer.ts +1 -1
- package/src/tools.ts +2 -2
- package/dist/typst.d.ts +0 -13
- package/dist/typst.js +0 -58
- package/src/typst.ts +0 -104
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { PdfDocument, PdfPaper, PdfRenderError, PdfRenderer } from "./renderer.js";
|
|
2
2
|
import { PdfToolsOptions, RenderPdfInput, RenderPdfResult, pdfFilename, pdfTools } from "./tools.js";
|
|
3
|
-
|
|
3
|
+
import { TypstSourceOptions, markdownToTypst, typstSource } from "./markdown-typst.js";
|
|
4
|
+
export { type PdfDocument, type PdfPaper, PdfRenderError, type PdfRenderer, type PdfToolsOptions, type RenderPdfInput, type RenderPdfResult, type TypstSourceOptions, markdownToTypst, pdfFilename, pdfTools, typstSource };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { pdfFilename, pdfTools } from "./tools.js";
|
|
2
2
|
import { PdfRenderError } from "./renderer.js";
|
|
3
|
-
|
|
3
|
+
import { markdownToTypst, typstSource } from "./markdown-typst.js";
|
|
4
|
+
export { PdfRenderError, markdownToTypst, pdfFilename, pdfTools, typstSource };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@junejs/pdf",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "June — PDF output as an agent capability: a render_pdf tool over a replaceable PdfRenderer (
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "June — PDF output as an agent capability: a render_pdf tool over a replaceable PdfRenderer (@junejs/pdf-typst-wasm is June's).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://june.build",
|
|
@@ -11,12 +11,6 @@
|
|
|
11
11
|
"bun": "./src/index.ts",
|
|
12
12
|
"types": "./dist/index.d.ts",
|
|
13
13
|
"default": "./dist/index.js"
|
|
14
|
-
},
|
|
15
|
-
"./typst": {
|
|
16
|
-
"source": "./src/typst.ts",
|
|
17
|
-
"bun": "./src/typst.ts",
|
|
18
|
-
"types": "./dist/typst.d.ts",
|
|
19
|
-
"default": "./dist/typst.js"
|
|
20
14
|
}
|
|
21
15
|
},
|
|
22
16
|
"files": [
|
|
@@ -30,32 +24,14 @@
|
|
|
30
24
|
"build": "tsdown"
|
|
31
25
|
},
|
|
32
26
|
"dependencies": {
|
|
33
|
-
"@junejs/core": "
|
|
27
|
+
"@junejs/core": "^0.2.0",
|
|
34
28
|
"marked": "^18.0.14"
|
|
35
29
|
},
|
|
36
|
-
"peerDependencies": {
|
|
37
|
-
"@myriaddreamin/typst.ts": "^0.7.0",
|
|
38
|
-
"@myriaddreamin/typst-ts-web-compiler": "^0.7.0"
|
|
39
|
-
},
|
|
40
|
-
"peerDependenciesMeta": {
|
|
41
|
-
"@myriaddreamin/typst.ts": {
|
|
42
|
-
"optional": true
|
|
43
|
-
},
|
|
44
|
-
"@myriaddreamin/typst-ts-web-compiler": {
|
|
45
|
-
"optional": true
|
|
46
|
-
}
|
|
47
|
-
},
|
|
48
|
-
"devDependencies": {
|
|
49
|
-
"@junejs/server": ">=1.0.0-dev.47",
|
|
50
|
-
"@myriaddreamin/typst.ts": "^0.7.0",
|
|
51
|
-
"@myriaddreamin/typst-ts-web-compiler": "^0.7.0"
|
|
52
|
-
},
|
|
53
30
|
"keywords": [
|
|
54
31
|
"june",
|
|
55
32
|
"agent",
|
|
56
33
|
"pdf",
|
|
57
|
-
"typst"
|
|
58
|
-
"wasm"
|
|
34
|
+
"typst"
|
|
59
35
|
],
|
|
60
36
|
"publishConfig": {
|
|
61
37
|
"access": "public"
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
// @junejs/pdf — PDF output as an agent capability (docs/rfc-agent-files.md §4): the
|
|
2
|
-
// render_pdf tool (pdfTools) over a replaceable PdfRenderer.
|
|
3
|
-
// WebAssembly, is its own
|
|
2
|
+
// render_pdf tool (pdfTools) over a replaceable PdfRenderer. June's renderer, Typst on
|
|
3
|
+
// WebAssembly, is its own package: `import { typstWasmRenderer } from "@junejs/pdf-typst-wasm"`.
|
|
4
4
|
|
|
5
5
|
export { pdfTools, pdfFilename, type PdfToolsOptions, type RenderPdfInput, type RenderPdfResult } from "./tools";
|
|
6
6
|
export { PdfRenderError, type PdfDocument, type PdfPaper, type PdfRenderer } from "./renderer";
|
|
7
|
+
// The Markdown → Typst source conversion, for any renderer built on a Typst compiler
|
|
8
|
+
// (@junejs/pdf-typst-wasm, or one an app writes).
|
|
9
|
+
export { markdownToTypst, typstSource, type TypstSourceOptions } from "./markdown-typst";
|
package/src/markdown-typst.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Markdown → Typst source, for
|
|
1
|
+
// Markdown → Typst source, for a renderer on a Typst compiler (@junejs/pdf-typst-wasm). Pure (no WASM), so it is tested on its own.
|
|
2
2
|
//
|
|
3
3
|
// Every run of text is emitted as a Typst STRING LITERAL in code mode — `#"text";` — never as
|
|
4
4
|
// markup. Typst markup gives meaning to dozens of characters (`#`, `*`, `_`, `$`, `@`, `<`,
|
package/src/renderer.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// process but costs a 28 MB module and tens of MB of memory per document; a headless
|
|
4
4
|
// browser renders HTML/CSS faithfully but needs one; a remote service needs none of it
|
|
5
5
|
// locally. So the tool depends on this interface only, and every
|
|
6
|
-
// engine —
|
|
6
|
+
// engine — @junejs/pdf-typst-wasm's, or one an app writes — implements it.
|
|
7
7
|
|
|
8
8
|
// What a renderer is asked to produce. Markdown is the input because it is what a model
|
|
9
9
|
// writes best and what a skill can teach in a page; layout stays the renderer's job.
|
package/src/tools.ts
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
//
|
|
3
3
|
// // agent/tools/pdf.ts
|
|
4
4
|
// import { pdfTools } from "@junejs/pdf";
|
|
5
|
-
// import {
|
|
6
|
-
// export default pdfTools({ renderer:
|
|
5
|
+
// import { typstWasmRenderer } from "@junejs/pdf-typst-wasm";
|
|
6
|
+
// export default pdfTools({ renderer: typstWasmRenderer() });
|
|
7
7
|
//
|
|
8
8
|
// One tool, render_pdf: Markdown in, a FileRef out. The bytes go to ctx.files (the host's
|
|
9
9
|
// FileStore), the ref goes back to the model and into the durable log, and the engine's
|
package/dist/typst.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { PdfRenderer } from "./renderer.js";
|
|
2
|
-
//#region src/typst.d.ts
|
|
3
|
-
export type TypstFontAsset = "text" | "cjk" | "emoji";
|
|
4
|
-
export type TypstRendererOptions = {
|
|
5
|
-
fonts?: (Uint8Array | string)[];
|
|
6
|
-
fontAssets?: TypstFontAsset[] | false;
|
|
7
|
-
fontAssetUrl?: string;
|
|
8
|
-
fontFamilies?: string[];
|
|
9
|
-
wasm?: Uint8Array | ArrayBuffer | WebAssembly.Module | (() => Promise<Uint8Array | ArrayBuffer | WebAssembly.Module>);
|
|
10
|
-
fetcher?: typeof fetch;
|
|
11
|
-
};
|
|
12
|
-
export declare function typstRenderer(opts?: TypstRendererOptions): PdfRenderer;
|
|
13
|
-
//#endregion
|
package/dist/typst.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { PdfRenderError } from "./renderer.js";
|
|
2
|
-
import { typstSource } from "./markdown-typst.js";
|
|
3
|
-
//#region src/typst.ts
|
|
4
|
-
const MAIN = "/main.typ";
|
|
5
|
-
const PDF_FORMAT = 1;
|
|
6
|
-
function typstRenderer(opts = {}) {
|
|
7
|
-
let compiler;
|
|
8
|
-
let queue = Promise.resolve();
|
|
9
|
-
const ready = () => compiler ??= init(opts).catch((err) => {
|
|
10
|
-
compiler = void 0;
|
|
11
|
-
throw err;
|
|
12
|
-
});
|
|
13
|
-
async function compile(doc) {
|
|
14
|
-
const c = await ready();
|
|
15
|
-
c.addSource(MAIN, typstSource(doc, { fonts: opts.fontFamilies }));
|
|
16
|
-
const out = await c.compile({
|
|
17
|
-
mainFilePath: MAIN,
|
|
18
|
-
format: PDF_FORMAT,
|
|
19
|
-
diagnostics: "full"
|
|
20
|
-
});
|
|
21
|
-
if (!out.result?.byteLength) {
|
|
22
|
-
const detail = (out.diagnostics ?? []).filter((d) => d.severity !== "warning").map((d) => `${d.range ? `${d.range}: ` : ""}${d.message ?? "unknown error"}`).join("; ");
|
|
23
|
-
throw new PdfRenderError(`the document could not be typeset${detail ? `: ${detail}` : ""}`);
|
|
24
|
-
}
|
|
25
|
-
return out.result;
|
|
26
|
-
}
|
|
27
|
-
return {
|
|
28
|
-
name: "typst",
|
|
29
|
-
render(doc) {
|
|
30
|
-
const run = queue.then(() => compile(doc));
|
|
31
|
-
queue = run.catch(() => {});
|
|
32
|
-
return run;
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
async function init(opts) {
|
|
37
|
-
const { createTypstCompiler, initOptions } = await import("@myriaddreamin/typst.ts");
|
|
38
|
-
const compiler = createTypstCompiler();
|
|
39
|
-
const fonts = initOptions.loadFonts(opts.fonts ?? [], {
|
|
40
|
-
assets: opts.fontAssets === false ? false : opts.fontAssets ?? ["text", "cjk"],
|
|
41
|
-
...opts.fontAssetUrl ? { assetUrlPrefix: opts.fontAssetUrl } : {},
|
|
42
|
-
...opts.fetcher ? { fetcher: opts.fetcher } : {}
|
|
43
|
-
});
|
|
44
|
-
await compiler.init({
|
|
45
|
-
getModule: () => loadWasm(opts.wasm),
|
|
46
|
-
beforeBuild: [fonts]
|
|
47
|
-
});
|
|
48
|
-
return compiler;
|
|
49
|
-
}
|
|
50
|
-
async function loadWasm(wasm) {
|
|
51
|
-
if (typeof wasm === "function") return wasm();
|
|
52
|
-
if (wasm) return wasm;
|
|
53
|
-
const url = import.meta.resolve("@myriaddreamin/typst-ts-web-compiler/wasm");
|
|
54
|
-
const [{ readFile }, { fileURLToPath }] = await Promise.all([import("node:fs/promises"), import("node:url")]);
|
|
55
|
-
return new Uint8Array(await readFile(fileURLToPath(url)));
|
|
56
|
-
}
|
|
57
|
-
//#endregion
|
|
58
|
-
export { typstRenderer };
|
package/src/typst.ts
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
// typstRenderer — a PdfRenderer on the Typst compiler built for WebAssembly
|
|
2
|
-
// (@myriaddreamin/typst.ts). Markdown → Typst source (markdown-typst.ts) → PDF, in process,
|
|
3
|
-
// with no browser and no native binary. Verified on Bun and Node. Cloudflare Workers allow
|
|
4
|
-
// 64 MiB uncompressed per Worker, so the 28 MB module fits, but this renderer is not yet
|
|
5
|
-
// verified on workerd: there, pass the compiled module as `wasm` (the default loader reads
|
|
6
|
-
// it with node:fs), mind the 128 MB of memory an isolate's requests share, and expect the
|
|
7
|
-
// Free plan's 10 ms of CPU to be too little. Measured 2026-10-12 on Bun 1.3.14: ~550 ms to
|
|
8
|
-
// initialize once per process (fonts included), then ~30 ms per one-page document.
|
|
9
|
-
//
|
|
10
|
-
// A separate entry (@junejs/pdf/typst) so an app that renders with something else never
|
|
11
|
-
// references the compiler, and the compiler itself is imported on the first render, so an
|
|
12
|
-
// agent that never produces a PDF never loads it.
|
|
13
|
-
//
|
|
14
|
-
// Fonts are the renderer's job. typst.ts's hosted assets (Libertinus Serif, New Computer
|
|
15
|
-
// Modern, DejaVu Sans Mono; "cjk" adds a Simplified Chinese Noto Serif subset) are fetched
|
|
16
|
-
// from jsdelivr at init unless disabled. Traditional Chinese, Japanese and Korean text needs
|
|
17
|
-
// a font that covers it — pass its bytes or URL in `fonts`, or the missing glyphs render
|
|
18
|
-
// blank.
|
|
19
|
-
|
|
20
|
-
import type { TypstCompiler } from "@myriaddreamin/typst.ts/compiler";
|
|
21
|
-
|
|
22
|
-
import { typstSource } from "./markdown-typst";
|
|
23
|
-
import { PdfRenderError, type PdfDocument, type PdfRenderer } from "./renderer";
|
|
24
|
-
|
|
25
|
-
export type TypstFontAsset = "text" | "cjk" | "emoji";
|
|
26
|
-
|
|
27
|
-
export type TypstRendererOptions = {
|
|
28
|
-
// Extra font files: bytes, or URLs fetched at init.
|
|
29
|
-
fonts?: (Uint8Array | string)[];
|
|
30
|
-
// typst.ts's hosted font sets to load at init (default ["text", "cjk"]); false loads none,
|
|
31
|
-
// for a deployment that supplies every font through `fonts` and makes no network call.
|
|
32
|
-
fontAssets?: TypstFontAsset[] | false;
|
|
33
|
-
// Where the hosted font sets are fetched from (default: jsdelivr) — a mirror you control.
|
|
34
|
-
fontAssetUrl?: string;
|
|
35
|
-
// Font families in order of preference, e.g. ["Noto Sans TC"]. Typst still falls back to
|
|
36
|
-
// any loaded font that has a glyph.
|
|
37
|
-
fontFamilies?: string[];
|
|
38
|
-
// The compiler's WASM module: its bytes, a compiled module, or a loader. Default: the file
|
|
39
|
-
// @myriaddreamin/typst-ts-web-compiler ships, read from disk. typst.ts instantiates the
|
|
40
|
-
// module once per process, so the first renderer to initialize decides it.
|
|
41
|
-
wasm?: Uint8Array | ArrayBuffer | WebAssembly.Module | (() => Promise<Uint8Array | ArrayBuffer | WebAssembly.Module>);
|
|
42
|
-
// How URLs in `fonts` and the hosted font sets are fetched (default: global fetch).
|
|
43
|
-
fetcher?: typeof fetch;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
const MAIN = "/main.typ";
|
|
47
|
-
const PDF_FORMAT = 1; // CompileFormatEnum.pdf — a const enum value, inlined so no runtime import is needed
|
|
48
|
-
|
|
49
|
-
type Diagnostic = { path?: string; range?: string; severity?: string; message?: string };
|
|
50
|
-
|
|
51
|
-
export function typstRenderer(opts: TypstRendererOptions = {}): PdfRenderer {
|
|
52
|
-
let compiler: Promise<TypstCompiler> | undefined;
|
|
53
|
-
// One compiler holds one main file: renders run one at a time, in call order.
|
|
54
|
-
let queue: Promise<unknown> = Promise.resolve();
|
|
55
|
-
|
|
56
|
-
const ready = (): Promise<TypstCompiler> =>
|
|
57
|
-
(compiler ??= init(opts).catch((err: unknown) => {
|
|
58
|
-
compiler = undefined; // a failed init (offline font fetch, missing WASM) is retried next render
|
|
59
|
-
throw err;
|
|
60
|
-
}));
|
|
61
|
-
|
|
62
|
-
async function compile(doc: PdfDocument): Promise<Uint8Array> {
|
|
63
|
-
const c = await ready();
|
|
64
|
-
c.addSource(MAIN, typstSource(doc, { fonts: opts.fontFamilies }));
|
|
65
|
-
const out = (await c.compile({ mainFilePath: MAIN, format: PDF_FORMAT, diagnostics: "full" })) as { result?: Uint8Array; diagnostics?: Diagnostic[] };
|
|
66
|
-
if (!out.result?.byteLength) {
|
|
67
|
-
const errors = (out.diagnostics ?? []).filter((d) => d.severity !== "warning");
|
|
68
|
-
const detail = errors.map((d) => `${d.range ? `${d.range}: ` : ""}${d.message ?? "unknown error"}`).join("; ");
|
|
69
|
-
throw new PdfRenderError(`the document could not be typeset${detail ? `: ${detail}` : ""}`);
|
|
70
|
-
}
|
|
71
|
-
return out.result;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return {
|
|
75
|
-
name: "typst",
|
|
76
|
-
render(doc) {
|
|
77
|
-
const run = queue.then(() => compile(doc));
|
|
78
|
-
queue = run.catch(() => {});
|
|
79
|
-
return run;
|
|
80
|
-
},
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
async function init(opts: TypstRendererOptions): Promise<TypstCompiler> {
|
|
85
|
-
const { createTypstCompiler, initOptions } = await import("@myriaddreamin/typst.ts");
|
|
86
|
-
const compiler = createTypstCompiler();
|
|
87
|
-
const fonts = initOptions.loadFonts(opts.fonts ?? [], {
|
|
88
|
-
assets: opts.fontAssets === false ? false : (opts.fontAssets ?? ["text", "cjk"]),
|
|
89
|
-
...(opts.fontAssetUrl ? { assetUrlPrefix: opts.fontAssetUrl } : {}),
|
|
90
|
-
...(opts.fetcher ? { fetcher: opts.fetcher } : {}),
|
|
91
|
-
});
|
|
92
|
-
await compiler.init({ getModule: () => loadWasm(opts.wasm), beforeBuild: [fonts] });
|
|
93
|
-
return compiler;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
async function loadWasm(wasm: TypstRendererOptions["wasm"]): Promise<Uint8Array | ArrayBuffer | WebAssembly.Module> {
|
|
97
|
-
if (typeof wasm === "function") return wasm();
|
|
98
|
-
if (wasm) return wasm;
|
|
99
|
-
// The package's own file, from disk: the hosts this renderer targets all have node:fs, and
|
|
100
|
-
// fetch() of a file: URL works on Bun but not on Node.
|
|
101
|
-
const url = import.meta.resolve("@myriaddreamin/typst-ts-web-compiler/wasm");
|
|
102
|
-
const [{ readFile }, { fileURLToPath }] = await Promise.all([import("node:fs/promises"), import("node:url")]);
|
|
103
|
-
return new Uint8Array(await readFile(fileURLToPath(url)));
|
|
104
|
-
}
|