@vedivad/typst-web-service 0.15.4 → 0.17.1
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/README.md +68 -74
- package/dist/index.d.ts +176 -458
- package/dist/index.js +298 -635
- package/dist/index.js.map +1 -1
- package/dist/typsten-worker.js +509 -0
- package/dist/typsten-worker.js.map +1 -0
- package/dist/typsten_bg.wasm +0 -0
- package/package.json +5 -17
- package/dist/analyzer-worker.js +0 -1045
- package/dist/analyzer-worker.js.map +0 -1
- package/dist/compiler-worker.js +0 -2207
- package/dist/compiler-worker.js.map +0 -1
- package/dist/renderer-worker.js +0 -5261
- package/dist/renderer-worker.js.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,540 +1,258 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
documentation?: string | LspMarkupContent;
|
|
29
|
-
insertText?: string;
|
|
30
|
-
/** LSP InsertTextFormat: 1 = PlainText, 2 = Snippet (TextMate syntax). */
|
|
31
|
-
insertTextFormat?: number;
|
|
32
|
-
filterText?: string;
|
|
33
|
-
sortText?: string;
|
|
34
|
-
textEdit?: {
|
|
35
|
-
range: LspRange;
|
|
36
|
-
newText: string;
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
/** LSP CompletionList. */
|
|
40
|
-
interface LspCompletionList {
|
|
41
|
-
isIncomplete: boolean;
|
|
42
|
-
items: LspCompletionItem[];
|
|
1
|
+
/** UTF-16 string offset (CodeMirror position) -> UTF-8 byte offset (typsten). */
|
|
2
|
+
declare function cmOffsetToByte(text: string, offset: number): number;
|
|
3
|
+
/** UTF-8 byte offset (typsten) -> UTF-16 string offset (CodeMirror position). */
|
|
4
|
+
declare function byteToCmOffset(text: string, byte: number): number;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Project file paths. `/main.typ` form, leading slash, forward slashes only.
|
|
8
|
+
* `@preview/...` package paths bypass this (they are pushed to the VFS verbatim).
|
|
9
|
+
*/
|
|
10
|
+
/** `/path/to/file.typ`, leading-slash, forward slashes only. */
|
|
11
|
+
type Path = string;
|
|
12
|
+
/** Ensure a path starts with a leading slash. Idempotent. */
|
|
13
|
+
declare function normalizePath(path: string): Path;
|
|
14
|
+
|
|
15
|
+
/* tslint:disable */
|
|
16
|
+
/* eslint-disable */
|
|
17
|
+
/**
|
|
18
|
+
* A highlighted source span: a half-open byte range and its CSS class.
|
|
19
|
+
*/
|
|
20
|
+
interface HlSpan {
|
|
21
|
+
from: number;
|
|
22
|
+
to: number;
|
|
23
|
+
/**
|
|
24
|
+
* Typst\'s own stable highlight class, e.g. `typ-key`, `typ-func`, `typ-str`
|
|
25
|
+
* (see `Tag::css_class`). The editor uses it verbatim as the decoration class.
|
|
26
|
+
*/
|
|
27
|
+
tag: string;
|
|
43
28
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A hover tooltip.
|
|
32
|
+
*/
|
|
33
|
+
interface Hover {
|
|
34
|
+
kind: HoverKind;
|
|
49
35
|
value: string;
|
|
50
|
-
})[];
|
|
51
|
-
/** Response shape for `textDocument/hover`. */
|
|
52
|
-
interface LspHover {
|
|
53
|
-
contents: LspHoverContents;
|
|
54
|
-
range?: LspRange;
|
|
55
36
|
}
|
|
56
37
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
38
|
+
/**
|
|
39
|
+
* A single compiler diagnostic, ready for an editor.
|
|
40
|
+
*/
|
|
41
|
+
interface Diagnostic {
|
|
42
|
+
severity: Severity;
|
|
43
|
+
message: string;
|
|
44
|
+
hints: string[];
|
|
64
45
|
/**
|
|
65
|
-
*
|
|
66
|
-
* Required — there is no default CDN URL for tinymist-web.
|
|
46
|
+
* `None` when the span is detached or cannot be resolved.
|
|
67
47
|
*/
|
|
68
|
-
|
|
48
|
+
location: Location | undefined;
|
|
69
49
|
}
|
|
50
|
+
|
|
70
51
|
/**
|
|
71
|
-
*
|
|
72
|
-
* completion and hover for Typst documents.
|
|
73
|
-
*
|
|
74
|
-
* const analyzer = await TypstAnalyzer.create({ wasmUrl: '...' });
|
|
52
|
+
* A single completion candidate.
|
|
75
53
|
*/
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
private versionCounter;
|
|
80
|
-
/**
|
|
81
|
-
* Last content pushed to the worker per URI. Presence is the source of
|
|
82
|
-
* truth for "is this URI opened on the worker?"; value drives own-RPC dedup.
|
|
83
|
-
*/
|
|
84
|
-
private readonly content;
|
|
85
|
-
private constructor();
|
|
86
|
-
static create(options: TypstAnalyzerOptions): Promise<TypstAnalyzer>;
|
|
87
|
-
didOpen(uri: string, content: string): Promise<void>;
|
|
88
|
-
didClose(uri: string): Promise<void>;
|
|
89
|
-
/**
|
|
90
|
-
* Notify the analyzer that a document has changed. Skips the RPC when the
|
|
91
|
-
* content matches what the worker last saw.
|
|
92
|
-
*/
|
|
93
|
-
didChange(uri: string, content: string): Promise<void>;
|
|
94
|
-
/**
|
|
95
|
-
* Batch document changes. Splits inputs into opens (first-time URIs) and
|
|
96
|
-
* changes (already-open URIs) and sends them in a single worker roundtrip.
|
|
97
|
-
* Skips unchanged documents; returns without an RPC if nothing is pending.
|
|
98
|
-
*/
|
|
99
|
-
didChangeMany(docs: Record<string, string>): Promise<void>;
|
|
100
|
-
/**
|
|
101
|
-
* Batch document closes. Filters to currently-open URIs and sends the set
|
|
102
|
-
* in a single worker roundtrip.
|
|
103
|
-
*/
|
|
104
|
-
didCloseMany(uris: string[]): Promise<void>;
|
|
54
|
+
interface Completion {
|
|
55
|
+
kind: CompletionKind;
|
|
56
|
+
label: string;
|
|
105
57
|
/**
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
* worker roundtrip; degrades to a plain completion request when the worker
|
|
109
|
-
* already has this exact content.
|
|
58
|
+
* Insertion text, possibly with snippet placeholders like `${body}`.
|
|
59
|
+
* Falls back to `label` when `None`.
|
|
110
60
|
*/
|
|
111
|
-
|
|
61
|
+
apply: string | undefined;
|
|
62
|
+
detail: string | undefined;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The completions at a cursor, plus where the replacement starts.
|
|
67
|
+
*/
|
|
68
|
+
interface CompletionResponse {
|
|
112
69
|
/**
|
|
113
|
-
*
|
|
114
|
-
* document state. Bundles the didChange notification with the request in one
|
|
115
|
-
* worker roundtrip; degrades to a plain hover request when the worker
|
|
116
|
-
* already has this exact content.
|
|
70
|
+
* Byte offset where the replacement begins; the editor replaces `from..cursor`.
|
|
117
71
|
*/
|
|
118
|
-
|
|
119
|
-
|
|
72
|
+
from: number;
|
|
73
|
+
completions: Completion[];
|
|
120
74
|
}
|
|
121
75
|
|
|
122
76
|
/**
|
|
123
|
-
*
|
|
124
|
-
* that knows how they relate. Keeping the vocabulary and conversions colocated
|
|
125
|
-
* means a new contributor can read one file and understand the model.
|
|
126
|
-
*
|
|
127
|
-
* 1. Path — "/main.typ". Always leading-slash, always forward slashes.
|
|
128
|
-
* Used by the compiler VFS, the `TypstProject` public API,
|
|
129
|
-
* and the `typstFilePath` facet. Addresses a file within
|
|
130
|
-
* the project.
|
|
131
|
-
*
|
|
132
|
-
* 2. AnalyzerUri — "untitled:project/main.typ". What tinymist's LSP expects.
|
|
133
|
-
* Derived from a Path plus the analyzer URI root.
|
|
134
|
-
*
|
|
135
|
-
* The types below are type aliases, not branded/opaque types. That's
|
|
136
|
-
* deliberate: callers can pass string literals where a Path is expected
|
|
137
|
-
* without wrapping, and the aliases only serve as self-documenting
|
|
138
|
-
* signatures. Ingress points still need to call `normalizePath` to defend
|
|
139
|
-
* against un-normalized input — the alias does not imply the string has
|
|
140
|
-
* been normalized.
|
|
77
|
+
* The kind of thing a completion inserts.
|
|
141
78
|
*/
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
/** `untitled:project/path/to/file.typ` — what tinymist's LSP consumes. */
|
|
145
|
-
type AnalyzerUri = string;
|
|
146
|
-
/** Ensure a path starts with a leading slash. Idempotent. */
|
|
147
|
-
declare function normalizePath(path: string): Path;
|
|
79
|
+
type CompletionKind = "syntax" | "func" | "type" | "param" | "constant" | "path" | "package" | "label" | "font" | "symbol";
|
|
80
|
+
|
|
148
81
|
/**
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
* `
|
|
82
|
+
* The outcome of a compile: one `PageInfo` per renderable page plus all
|
|
83
|
+
* diagnostics. `compile()` does NOT render, pages are drawn on demand via
|
|
84
|
+
* `render_page`/`render_pages`. Diagnostics are DATA, not errors (the editor
|
|
85
|
+
* needs them on success as warnings and on failure as errors), so this is never
|
|
86
|
+
* a `Result` and never throws. On a failed compile the `pages` describe the
|
|
87
|
+
* last successful document (the last-good preview is kept), so the presence of
|
|
88
|
+
* an error diagnostic means `pages` is stale.
|
|
152
89
|
*/
|
|
153
|
-
|
|
90
|
+
interface CompileResult {
|
|
91
|
+
pages: PageInfo[];
|
|
92
|
+
diagnostics: Diagnostic[];
|
|
93
|
+
}
|
|
94
|
+
|
|
154
95
|
/**
|
|
155
|
-
*
|
|
156
|
-
* already be normalized (see `normalizeRoot`).
|
|
157
|
-
*
|
|
158
|
-
* pathToAnalyzerUri("/main.typ", "/project") -> "untitled:project/main.typ"
|
|
96
|
+
* The width and height of a rendered page, in points.
|
|
159
97
|
*/
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
interface DiagnosticRange {
|
|
164
|
-
startLine: number;
|
|
165
|
-
startCol: number;
|
|
166
|
-
endLine: number;
|
|
167
|
-
endCol: number;
|
|
168
|
-
}
|
|
169
|
-
interface DiagnosticMessage {
|
|
170
|
-
package: string;
|
|
171
|
-
path: string;
|
|
172
|
-
severity: "Error" | "Warning" | "Info";
|
|
173
|
-
range: DiagnosticRange;
|
|
174
|
-
message: string;
|
|
98
|
+
interface PageInfo {
|
|
99
|
+
width: number;
|
|
100
|
+
height: number;
|
|
175
101
|
}
|
|
176
102
|
|
|
177
|
-
interface CompileResult {
|
|
178
|
-
diagnostics: DiagnosticMessage[];
|
|
179
|
-
/** Vector artifact bytes from the compiler, usable with TypstRenderer for SVG rendering. */
|
|
180
|
-
vector?: Uint8Array;
|
|
181
|
-
}
|
|
182
|
-
interface TypstCompilerOptions {
|
|
183
|
-
/**
|
|
184
|
-
* Explicit Worker instance. When omitted, an inlined blob worker is created automatically.
|
|
185
|
-
* Use this for Vite apps to get proper source maps:
|
|
186
|
-
* `await TypstCompiler.create({ worker: new Worker(new URL('typst-web-service/compiler-worker', import.meta.url)) })`
|
|
187
|
-
*/
|
|
188
|
-
worker?: Worker;
|
|
189
|
-
/**
|
|
190
|
-
* URL to the typst-ts-web-compiler WASM binary.
|
|
191
|
-
* Defaults to the matching version on jsDelivr CDN.
|
|
192
|
-
* Override with a local asset URL for offline support or faster load:
|
|
193
|
-
* `new URL('@myriaddreamin/typst-ts-web-compiler/pkg/typst_ts_web_compiler_bg.wasm', import.meta.url).href`
|
|
194
|
-
*/
|
|
195
|
-
wasmUrl?: string;
|
|
196
|
-
/** Font URLs to load into the Typst compiler. Defaults to Roboto from jsDelivr. */
|
|
197
|
-
fonts?: string[];
|
|
198
|
-
/**
|
|
199
|
-
* Enable fetching @preview/ packages from packages.typst.org on demand.
|
|
200
|
-
* Default: true.
|
|
201
|
-
*/
|
|
202
|
-
packages?: boolean;
|
|
203
|
-
}
|
|
204
103
|
/**
|
|
205
|
-
*
|
|
206
|
-
* all extensions (linter, autocomplete, preview, etc.).
|
|
104
|
+
* Where a diagnostic points, when its span resolves to a real location.
|
|
207
105
|
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
* await TypstCompiler.create({ worker: myWorker, fonts: [...] }) // explicit Worker + options
|
|
106
|
+
* Offsets are UTF-8 byte indices (Typst-native); `line`/`column` are 1-based
|
|
107
|
+
* (editor convention). Note these are code-point based; exact UTF-16 mapping
|
|
108
|
+
* for JS editors is a later refinement.
|
|
212
109
|
*/
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
private readonly worker;
|
|
216
|
-
private readonly encoder;
|
|
217
|
-
/** Last text content pushed per path. Drives own-RPC dedup. Invalidated by binary writes. */
|
|
218
|
-
private readonly content;
|
|
219
|
-
private constructor();
|
|
220
|
-
static create(options?: TypstCompilerOptions): Promise<TypstCompiler>;
|
|
221
|
-
/**
|
|
222
|
-
* Compile whatever is currently in the VFS (populated via
|
|
223
|
-
* setText/setBinary/setJson/setMany). Defaults to compiling "/main.typ";
|
|
224
|
-
* override with `entry`.
|
|
225
|
-
*/
|
|
226
|
-
compile(entry?: string): Promise<CompileResult>;
|
|
227
|
-
/**
|
|
228
|
-
* Compile to PDF. Operates on whatever is currently in the VFS (populated
|
|
229
|
-
* via setText/setBinary/setJson/setMany). Defaults to compiling "/main.typ";
|
|
230
|
-
* override with `entry`.
|
|
231
|
-
*/
|
|
232
|
-
compilePdf(entry?: string): Promise<Uint8Array>;
|
|
233
|
-
/**
|
|
234
|
-
* Add or overwrite a text file in the virtual compiler filesystem. Skips
|
|
235
|
-
* the worker RPC when `source` matches the last value pushed for `path`.
|
|
236
|
-
*/
|
|
237
|
-
setText(path: string, source: string): Promise<void>;
|
|
238
|
-
/** Add or overwrite a JSON file in the virtual compiler filesystem. */
|
|
239
|
-
setJson(path: string, value: unknown, replacer?: (this: unknown, key: string, value: unknown) => unknown, space?: string | number): Promise<void>;
|
|
240
|
-
/**
|
|
241
|
-
* Add or overwrite multiple files in the virtual compiler filesystem in a
|
|
242
|
-
* single worker roundtrip. Strings are UTF-8 encoded; Uint8Arrays are
|
|
243
|
-
* passed through. Text entries unchanged since their last push are skipped;
|
|
244
|
-
* binary entries always push and invalidate the text cache for their path.
|
|
245
|
-
*/
|
|
246
|
-
setMany(files: Record<string, string | Uint8Array>): Promise<void>;
|
|
247
|
-
/** Add or overwrite a binary file in the virtual compiler filesystem. */
|
|
248
|
-
setBinary(path: string, content: ArrayBuffer | ArrayBufferView): Promise<void>;
|
|
249
|
-
/** Remove a file from the virtual compiler filesystem. */
|
|
250
|
-
remove(path: string): Promise<void>;
|
|
251
|
-
/** Clear all virtual files from the compiler filesystem. */
|
|
252
|
-
clear(): Promise<void>;
|
|
253
|
-
destroy(): void;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
interface FormatConfig {
|
|
257
|
-
/** Number of spaces per indentation level. Default: 2 */
|
|
258
|
-
tab_spaces?: number;
|
|
259
|
-
/** Maximum line width. Default: 80 */
|
|
260
|
-
max_width?: number;
|
|
261
|
-
/** Maximum consecutive blank lines allowed. */
|
|
262
|
-
blank_lines_upper_bound?: number;
|
|
263
|
-
/** Collapse consecutive whitespace in markup to a single space. */
|
|
264
|
-
collapse_markup_spaces?: boolean;
|
|
265
|
-
/** Sort import items alphabetically. */
|
|
266
|
-
reorder_import_items?: boolean;
|
|
267
|
-
/** Wrap text in markup to fit within max_width. Implies collapse_markup_spaces. */
|
|
268
|
-
wrap_text?: boolean;
|
|
269
|
-
}
|
|
270
|
-
interface FormatRangeResult {
|
|
271
|
-
/** Start index (UTF-16) of the actual formatted range. */
|
|
110
|
+
interface Location {
|
|
111
|
+
file: string;
|
|
272
112
|
start: number;
|
|
273
|
-
/** End index (UTF-16) of the actual formatted range. */
|
|
274
113
|
end: number;
|
|
275
|
-
|
|
276
|
-
|
|
114
|
+
line: number;
|
|
115
|
+
column: number;
|
|
277
116
|
}
|
|
117
|
+
|
|
278
118
|
/**
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
* Runs on the main thread — typstyle is lightweight and fast.
|
|
282
|
-
* The WASM module is loaded lazily on first format call.
|
|
283
|
-
* Requires a bundler that supports WASM imports (e.g. Vite, webpack).
|
|
284
|
-
*
|
|
285
|
-
* const formatter = TypstFormatter.create({ tab_spaces: 2, max_width: 80 });
|
|
286
|
-
* const formatted = await formatter.format(source);
|
|
119
|
+
* Whether a diagnostic is a fatal error or a non-fatal warning.
|
|
287
120
|
*/
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
121
|
+
type Severity = "error" | "warning";
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Whether a hover tooltip is prose or Typst code.
|
|
125
|
+
*/
|
|
126
|
+
type HoverKind = "text" | "code";
|
|
127
|
+
|
|
128
|
+
/** A rendered page: its index, dimensions (in points), and standalone SVG. */
|
|
129
|
+
interface RenderedSvgPage {
|
|
130
|
+
index: number;
|
|
131
|
+
width: number;
|
|
132
|
+
height: number;
|
|
133
|
+
svg: string;
|
|
296
134
|
}
|
|
297
135
|
|
|
298
|
-
interface
|
|
299
|
-
compiler: TypstCompiler;
|
|
300
|
-
/**
|
|
301
|
-
* Optional analyzer. When provided, text file operations also sync with the
|
|
302
|
-
* analyzer so completions / hover reflect the current state.
|
|
303
|
-
*/
|
|
304
|
-
analyzer?: TypstAnalyzer;
|
|
136
|
+
interface TypstProjectCreateOptions {
|
|
305
137
|
/** Default entry file path. Default: "/main.typ". */
|
|
306
138
|
entry?: string;
|
|
307
|
-
/**
|
|
308
|
-
* Prefix used to build the `untitled:` URIs handed to the analyzer.
|
|
309
|
-
* Default: "/project". A path of `/main.typ` becomes
|
|
310
|
-
* `untitled:project/main.typ`. Only affects URI construction — the compiler
|
|
311
|
-
* and project VFS use the raw paths unchanged.
|
|
312
|
-
*/
|
|
313
|
-
analyzerUriRoot?: string;
|
|
314
|
-
/**
|
|
315
|
-
* Scheduling for auto-compiles after VFS mutations. Mutations debounce by
|
|
316
|
-
* `debounceMs`; `maxWaitMs` caps how long the debounce can keep deferring
|
|
317
|
-
* during sustained edits so the user still sees progress.
|
|
318
|
-
*/
|
|
139
|
+
/** Auto-compile scheduling after VFS mutations. */
|
|
319
140
|
autoCompile?: AutoCompileOptions;
|
|
320
141
|
}
|
|
321
142
|
interface AutoCompileOptions {
|
|
322
|
-
/**
|
|
323
|
-
* Idle time (ms) after the last VFS mutation before a compile fires.
|
|
324
|
-
* Default: 0 — compile fires on the next macrotask. Set higher (e.g. 150) to
|
|
325
|
-
* coalesce rapid edits.
|
|
326
|
-
*/
|
|
143
|
+
/** Idle time (ms) after the last mutation before a compile fires. Default: 0. */
|
|
327
144
|
debounceMs?: number;
|
|
328
|
-
/**
|
|
329
|
-
* Maximum time (ms) the debounce is allowed to defer a compile during
|
|
330
|
-
* sustained mutation bursts. Default: 0 (no cap — pure debounce).
|
|
331
|
-
*/
|
|
145
|
+
/** Max time (ms) the debounce may defer during sustained edits. Default: 0. */
|
|
332
146
|
maxWaitMs?: number;
|
|
333
147
|
}
|
|
148
|
+
type CompileListener = (result: CompileResult) => void;
|
|
334
149
|
/**
|
|
335
|
-
*
|
|
336
|
-
*
|
|
337
|
-
*
|
|
338
|
-
*
|
|
339
|
-
* both the compiler's shadow VFS and the analyzer's open-document set, then
|
|
340
|
-
* compiles or services LSP requests against the current state.
|
|
150
|
+
* Multi-file Typst project backed by a single typsten wasm worker. Owns the
|
|
151
|
+
* VFS; editors push `setText` edits as the user types; the project mirrors them
|
|
152
|
+
* to the worker, fetches `@preview` packages on demand, and compiles or renders
|
|
153
|
+
* against the current state.
|
|
341
154
|
*
|
|
342
|
-
* const project =
|
|
343
|
-
* await project.setMany({ "/main.typ": "..."
|
|
344
|
-
* const
|
|
155
|
+
* const project = await TypstProject.create();
|
|
156
|
+
* await project.setMany({ "/main.typ": "..." });
|
|
157
|
+
* const { pages } = await project.compile();
|
|
158
|
+
* const svg = await project.renderPage(0);
|
|
345
159
|
*/
|
|
346
|
-
type CompileListener = (result: CompileResult) => void;
|
|
347
160
|
declare class TypstProject {
|
|
348
|
-
private readonly
|
|
349
|
-
private readonly
|
|
350
|
-
private readonly analyzerUriRoot;
|
|
161
|
+
private readonly engine;
|
|
162
|
+
private readonly worker;
|
|
351
163
|
/**
|
|
352
|
-
* Tracked
|
|
353
|
-
*
|
|
354
|
-
*
|
|
355
|
-
* analyzer.
|
|
164
|
+
* Tracked project files: the text content for a text file, or `null` for a
|
|
165
|
+
* binary one. Drives dedup, `getText`, `files`, and `clear`. (Cached `@preview`
|
|
166
|
+
* package files live only in the worker VFS, never here.)
|
|
356
167
|
*/
|
|
357
|
-
private readonly
|
|
168
|
+
private readonly tracked;
|
|
358
169
|
private readonly compileListeners;
|
|
359
170
|
private readonly scheduler;
|
|
171
|
+
private readonly packageLoader;
|
|
360
172
|
private compileVersion;
|
|
361
173
|
private _lastResult;
|
|
362
174
|
private _entry;
|
|
363
175
|
private destroyed;
|
|
364
|
-
private
|
|
365
|
-
|
|
176
|
+
private constructor();
|
|
177
|
+
/** Create a project: spin up the worker, init the wasm, set the entry. */
|
|
178
|
+
static create(options?: TypstProjectCreateOptions): Promise<TypstProject>;
|
|
179
|
+
private scheduleCompile;
|
|
366
180
|
/**
|
|
367
|
-
*
|
|
368
|
-
* the
|
|
369
|
-
* listeners via a synthetic diagnostic; callers awaiting a specific compile
|
|
370
|
-
* should call `compile()` directly.
|
|
181
|
+
* Mirror text into the VFS without scheduling a compile. Returns the in-flight
|
|
182
|
+
* write to await, or `null` if the content is unchanged (deduped to a no-op).
|
|
371
183
|
*/
|
|
372
|
-
private
|
|
373
|
-
/**
|
|
184
|
+
private writeText;
|
|
185
|
+
/** Mirror binary bytes into the VFS (always writes; binaries are not deduped). */
|
|
186
|
+
private writeBinary;
|
|
374
187
|
get entry(): Path;
|
|
375
188
|
set entry(path: Path);
|
|
376
|
-
/**
|
|
377
|
-
get hasAnalyzer(): boolean;
|
|
378
|
-
/**
|
|
379
|
-
* Most recent compile result, or `undefined` before the first compile has
|
|
380
|
-
* settled. Useful for lazy-mounted UI that subscribes after boot and needs
|
|
381
|
-
* an initial value.
|
|
382
|
-
*/
|
|
189
|
+
/** Most recent compile result, or `undefined` before the first compile. */
|
|
383
190
|
get lastResult(): CompileResult | undefined;
|
|
384
|
-
/**
|
|
385
|
-
* Snapshot of tracked text file paths, in insertion order. Updated by
|
|
386
|
-
* `setText`, `setMany`, `remove`, and `clear`. Returns a fresh array — mutate
|
|
387
|
-
* freely without affecting project state.
|
|
388
|
-
*/
|
|
191
|
+
/** Tracked text file paths, in insertion order (fresh array). */
|
|
389
192
|
get files(): Path[];
|
|
390
|
-
/**
|
|
391
|
-
* Current text content for a tracked file, or `undefined` if the path was
|
|
392
|
-
* never written via `setText`/`setMany` (or was removed). Read-through to the
|
|
393
|
-
* project's sync cache — lets consumers avoid shadowing the VFS themselves.
|
|
394
|
-
*/
|
|
193
|
+
/** Current text for a tracked file, or `undefined` (binary or absent). */
|
|
395
194
|
getText(path: Path): string | undefined;
|
|
396
|
-
/**
|
|
397
|
-
* Add or overwrite a text file. Goes to the compiler's VFS and, when an
|
|
398
|
-
* analyzer is attached, to the analyzer as a document change. No-op when
|
|
399
|
-
* the tracked path already has this exact content — skips both worker RPCs
|
|
400
|
-
* and the auto-scheduled compile.
|
|
401
|
-
*/
|
|
195
|
+
/** Add or overwrite a text file. No-op if unchanged. */
|
|
402
196
|
setText(path: Path, content: string): Promise<void>;
|
|
403
|
-
/**
|
|
404
|
-
* Add or overwrite a JSON file. The analyzer does not track data files, but
|
|
405
|
-
* if `path` was previously tracked as text the analyzer document is closed
|
|
406
|
-
* and the text entry retired so the three views stay consistent.
|
|
407
|
-
*/
|
|
408
|
-
setJson(path: Path, value: unknown): Promise<void>;
|
|
409
|
-
/**
|
|
410
|
-
* Add or overwrite a binary file. If `path` was previously tracked as text,
|
|
411
|
-
* the analyzer document is closed and the text entry retired in the same
|
|
412
|
-
* call so the compiler / analyzer / project views stay consistent.
|
|
413
|
-
*/
|
|
197
|
+
/** Add or overwrite a binary file (retires any text tracking for the path). */
|
|
414
198
|
setBinary(path: Path, content: ArrayBuffer | ArrayBufferView): Promise<void>;
|
|
415
|
-
/**
|
|
416
|
-
* Batch set multiple files. Strings route to both compiler and analyzer;
|
|
417
|
-
* Uint8Array entries go to the compiler only. Strings matching the last
|
|
418
|
-
* tracked content for their path are skipped on both sinks. Binary entries
|
|
419
|
-
* always go through (no content cache, so no dedup).
|
|
420
|
-
*/
|
|
199
|
+
/** Batch set files. Strings dedup against tracked content; binaries always write. */
|
|
421
200
|
setMany(files: Record<Path, string | Uint8Array>): Promise<void>;
|
|
422
|
-
/**
|
|
423
|
-
* Remove a file. Always removed from the compiler's VFS; also closed on the
|
|
424
|
-
* analyzer when it was previously tracked as text.
|
|
425
|
-
*/
|
|
201
|
+
/** Remove a file from the VFS. */
|
|
426
202
|
remove(path: Path): Promise<void>;
|
|
427
|
-
/**
|
|
203
|
+
/** Remove all tracked project files (cached `@preview` packages are kept). */
|
|
428
204
|
clear(): Promise<void>;
|
|
429
205
|
/**
|
|
430
|
-
*
|
|
431
|
-
*
|
|
432
|
-
*
|
|
433
|
-
*
|
|
434
|
-
|
|
206
|
+
* Register a font (TTF/OTF, or TTC collection bytes) so compilation can use
|
|
207
|
+
* it, then recompile. The engine bundles default body, math, and monospace
|
|
208
|
+
* fonts; use this to add families it does not ship (e.g. CJK or a custom
|
|
209
|
+
* font). Fonts persist for the project's lifetime.
|
|
210
|
+
*/
|
|
211
|
+
addFont(bytes: Uint8Array): Promise<void>;
|
|
212
|
+
/**
|
|
213
|
+
* Subscribe to compile results. Late subscribers get `lastResult` synchronously.
|
|
214
|
+
* Returns an unsubscribe function.
|
|
435
215
|
*/
|
|
436
216
|
onCompile(listener: CompileListener): () => void;
|
|
437
217
|
/**
|
|
438
|
-
* Compile the current VFS state
|
|
439
|
-
*
|
|
440
|
-
*
|
|
441
|
-
* notified only for the most recent compile — results from an earlier call
|
|
442
|
-
* that resolves after a later one are suppressed.
|
|
443
|
-
*
|
|
444
|
-
* VFS mutations (`setText`, `remove`, etc.) auto-schedule a debounced
|
|
445
|
-
* compile; call this directly only when you need an awaitable handle on the
|
|
446
|
-
* result (e.g., to flush before rendering to PDF).
|
|
218
|
+
* Compile the current VFS state. Fetches any referenced `@preview` packages
|
|
219
|
+
* first. Errors become a synthetic diagnostic. Listeners fire only for the
|
|
220
|
+
* most recent compile (stale results are dropped).
|
|
447
221
|
*/
|
|
448
222
|
compile(): Promise<CompileResult>;
|
|
449
|
-
/**
|
|
450
|
-
|
|
451
|
-
private requireAnalyzer;
|
|
223
|
+
/** Render a single page of the last compile to SVG, or `undefined`. */
|
|
224
|
+
renderPage(index: number): Promise<string | undefined>;
|
|
452
225
|
/**
|
|
453
|
-
*
|
|
454
|
-
*
|
|
455
|
-
* nor project tracking (`files`/`getText`) is touched. Throws when no
|
|
456
|
-
* analyzer is attached.
|
|
226
|
+
* Render pages `[start, end)` as `RenderedSvgPage`s (index + dims + svg),
|
|
227
|
+
* zipping the SVG strings with the page metadata from the last compile.
|
|
457
228
|
*/
|
|
458
|
-
|
|
229
|
+
renderedPages(start: number, end: number): Promise<RenderedSvgPage[]>;
|
|
459
230
|
/**
|
|
460
|
-
*
|
|
461
|
-
*
|
|
462
|
-
* project tracking is touched. Throws when no analyzer is attached.
|
|
231
|
+
* Export the last compile as a PDF, or `undefined` if nothing has compiled
|
|
232
|
+
* yet. The bytes are a fresh `Uint8Array` (copied across the worker boundary).
|
|
463
233
|
*/
|
|
464
|
-
|
|
234
|
+
exportPdf(): Promise<Uint8Array | undefined>;
|
|
235
|
+
/** Completions at a CodeMirror `offset` in `path`, using `source` as the live buffer. */
|
|
236
|
+
completion(path: Path, source: string, offset: number, explicit?: boolean): Promise<CompletionResponse | undefined>;
|
|
237
|
+
/** Hover tooltip at a CodeMirror `offset` in `path`, using `source` as the live buffer. */
|
|
238
|
+
hover(path: Path, source: string, offset: number): Promise<Hover | undefined>;
|
|
239
|
+
/** Format `source` (the live buffer for `path`); returns the formatted text or `undefined`. */
|
|
240
|
+
format(path: Path, source: string): Promise<string | undefined>;
|
|
465
241
|
/**
|
|
466
|
-
*
|
|
467
|
-
*
|
|
468
|
-
*
|
|
469
|
-
*
|
|
470
|
-
*
|
|
471
|
-
* If you need to share a compiler or analyzer across projects, destroy them
|
|
472
|
-
* yourself and don't call this method — the project does not provide an
|
|
473
|
-
* ownership toggle.
|
|
242
|
+
* Syntax-highlight `source` over the CodeMirror window `[from, to)` (UTF-16
|
|
243
|
+
* offsets; defaults to the whole string). Returns spans whose `from`/`to` are
|
|
244
|
+
* CodeMirror offsets, ready to drive decorations. Stateless: the worker parses
|
|
245
|
+
* `source` directly via typst-syntax, so this neither reads nor mutates the
|
|
246
|
+
* VFS and works for any text (the live buffer, a hover code snippet).
|
|
474
247
|
*/
|
|
475
|
-
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
interface TypstRendererOptions {
|
|
248
|
+
highlight(source: string, from?: number, to?: number): Promise<HlSpan[]>;
|
|
479
249
|
/**
|
|
480
|
-
*
|
|
481
|
-
*
|
|
482
|
-
* `TypstRenderer.create({ worker: new Worker(new URL('typst-web-service/renderer-worker', import.meta.url)) })`
|
|
250
|
+
* Syntax-highlight `source` to nested `<span class="typ-*">` HTML for a static
|
|
251
|
+
* context (e.g. a hover tooltip). Stateless, like `highlight`.
|
|
483
252
|
*/
|
|
484
|
-
|
|
485
|
-
/**
|
|
486
|
-
wasmUrl?: string;
|
|
487
|
-
}
|
|
488
|
-
interface RenderedSvgPage {
|
|
489
|
-
/** Zero-based page index within the document. */
|
|
490
|
-
index: number;
|
|
491
|
-
/** Page width in typographic points. */
|
|
492
|
-
width: number;
|
|
493
|
-
/** Page height in typographic points. */
|
|
494
|
-
height: number;
|
|
495
|
-
/** Standalone SVG string for just this page. */
|
|
496
|
-
svg: string;
|
|
497
|
-
}
|
|
498
|
-
/**
|
|
499
|
-
* Converts Typst vector artifacts to SVG strings, off the main thread.
|
|
500
|
-
*
|
|
501
|
-
* Wraps `@myriaddreamin/typst.ts`'s renderer hosted in a Web Worker — the
|
|
502
|
-
* WASM init, vector → SVG conversion, and base64 image-embedding all happen
|
|
503
|
-
* off-thread. Returned SVG strings cross via Comlink; page splitting runs on
|
|
504
|
-
* the main thread (Workers don't have `DOMParser`).
|
|
505
|
-
*
|
|
506
|
-
* const renderer = TypstRenderer.create();
|
|
507
|
-
* const svg = await renderer.renderSvg(vector);
|
|
508
|
-
*/
|
|
509
|
-
declare class TypstRenderer {
|
|
510
|
-
#private;
|
|
511
|
-
private constructor();
|
|
512
|
-
static create(options?: TypstRendererOptions): TypstRenderer;
|
|
513
|
-
/** Terminate the worker. The instance is unusable afterwards. */
|
|
253
|
+
highlightHtml(source: string): Promise<string>;
|
|
254
|
+
/** Tear down the worker and drop all state. Idempotent. */
|
|
514
255
|
destroy(): void;
|
|
515
|
-
/**
|
|
516
|
-
* Render a Typst vector artifact to a single merged SVG string.
|
|
517
|
-
*
|
|
518
|
-
* Ownership of `vector.buffer` transfers to the worker (zero-copy).
|
|
519
|
-
* Don't reuse the vector after passing it in — accessing its bytes from
|
|
520
|
-
* the main thread after this call is undefined behavior.
|
|
521
|
-
*
|
|
522
|
-
* Concurrent calls are serialized in the worker: while a render is in
|
|
523
|
-
* flight, the most recent `vector` becomes the next render and any
|
|
524
|
-
* intermediate calls are dropped. All overlapping callers share one
|
|
525
|
-
* returned promise that resolves with the LAST rendered SVG — your
|
|
526
|
-
* specific vector may have been superseded.
|
|
527
|
-
*/
|
|
528
|
-
renderSvg(vector: Uint8Array): Promise<string>;
|
|
529
|
-
/**
|
|
530
|
-
* Render a Typst vector artifact into one self-contained SVG string per
|
|
531
|
-
* physical page. The merged SVG is split by `<g class="typst-page">`
|
|
532
|
-
* children; each group's `data-page-width` / `data-page-height` give the
|
|
533
|
-
* page-local viewBox. Shared `<defs>` / `<style>` are duplicated into each
|
|
534
|
-
* page so the output SVGs render independently. Returns an empty array if
|
|
535
|
-
* the document has no page groups.
|
|
536
|
-
*/
|
|
537
|
-
renderSvgPages(vector: Uint8Array): Promise<RenderedSvgPage[]>;
|
|
538
256
|
}
|
|
539
257
|
|
|
540
|
-
export { type
|
|
258
|
+
export { type AutoCompileOptions, type CompileListener, type CompileResult, type Completion, type CompletionKind, type CompletionResponse, type Diagnostic, type HlSpan, type Hover, type HoverKind, type Location, type PageInfo, type Path, type RenderedSvgPage, type Severity, TypstProject, type TypstProjectCreateOptions, byteToCmOffset, cmOffsetToByte, normalizePath };
|