@vedivad/typst-web-service 0.6.0 → 0.7.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 +98 -0
- package/dist/analyzer-worker.js +12 -0
- package/dist/analyzer-worker.js.map +1 -1
- package/dist/index.d.ts +90 -91
- package/dist/index.js +105 -126
- package/dist/index.js.map +1 -1
- package/dist/worker.js +6 -7
- package/dist/worker.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -20,7 +20,7 @@ interface TypstAnalyzerOptions {
|
|
|
20
20
|
/**
|
|
21
21
|
* Explicit Worker instance. When omitted, an inlined blob worker is created automatically.
|
|
22
22
|
* Use this for Vite apps:
|
|
23
|
-
* `
|
|
23
|
+
* `await TypstAnalyzer.create({ worker: new Worker(new URL('typst-web-service/analyzer-worker', import.meta.url), { type: 'module' }) })`
|
|
24
24
|
*/
|
|
25
25
|
worker?: Worker;
|
|
26
26
|
/**
|
|
@@ -37,27 +37,25 @@ interface TypstAnalyzerOptions {
|
|
|
37
37
|
* content changes, and receive diagnostics via `onDiagnostics()` listeners
|
|
38
38
|
* whenever tinymist publishes them.
|
|
39
39
|
*
|
|
40
|
-
* const analyzer =
|
|
40
|
+
* const analyzer = await TypstAnalyzer.create({ wasmUrl: '...' });
|
|
41
41
|
* analyzer.onDiagnostics((uri, diags) => { ... });
|
|
42
42
|
*/
|
|
43
43
|
declare class TypstAnalyzer {
|
|
44
|
-
readonly ready: Promise<void>;
|
|
45
44
|
private idCounter;
|
|
46
45
|
private versionCounter;
|
|
47
46
|
private worker;
|
|
48
47
|
private openedUris;
|
|
49
48
|
private diagnosticsListeners;
|
|
50
|
-
private
|
|
51
|
-
|
|
49
|
+
private constructor();
|
|
50
|
+
static create(options: TypstAnalyzerOptions): Promise<TypstAnalyzer>;
|
|
52
51
|
/**
|
|
53
52
|
* Register a listener for push-based diagnostics.
|
|
54
53
|
* Returns an unsubscribe function.
|
|
55
54
|
*/
|
|
56
55
|
onDiagnostics(listener: DiagnosticsListener): () => void;
|
|
57
|
-
/** Returns the latest pushed diagnostics for a URI, if available. */
|
|
58
|
-
getLatestDiagnostics(uri: string): LspDiagnostic[] | undefined;
|
|
59
56
|
private rpc;
|
|
60
57
|
didOpen(uri: string, content: string): Promise<void>;
|
|
58
|
+
didClose(uri: string): Promise<void>;
|
|
61
59
|
/**
|
|
62
60
|
* Notify the analyzer that a document has changed.
|
|
63
61
|
* Diagnostics will arrive asynchronously via `onDiagnostics()` listeners.
|
|
@@ -68,6 +66,78 @@ declare class TypstAnalyzer {
|
|
|
68
66
|
destroy(): void;
|
|
69
67
|
}
|
|
70
68
|
|
|
69
|
+
type DiagnosticsSubscriber = (diagnostics: LspDiagnostic[]) => void;
|
|
70
|
+
interface AnalyzerSessionOptions {
|
|
71
|
+
analyzer: Pick<TypstAnalyzer, "didOpen" | "didChange" | "completion" | "hover" | "onDiagnostics">;
|
|
72
|
+
/** Project root used to build stable in-memory analyzer URIs. Default: "/project". */
|
|
73
|
+
rootPath?: string;
|
|
74
|
+
/** Entry file path within the project. Synced last to ensure dependencies load first. Default: "/main.typ". */
|
|
75
|
+
entryPath?: string;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Synchronizes an in-memory Typst project with a TypstAnalyzer.
|
|
79
|
+
* Handles multi-file ordering, request queueing, and diagnostic subscriptions.
|
|
80
|
+
*
|
|
81
|
+
* Diagnostics arrive via the analyzer's push mechanism and are forwarded
|
|
82
|
+
* to subscribers registered with `subscribe()`.
|
|
83
|
+
*
|
|
84
|
+
* const session = new AnalyzerSession({ analyzer });
|
|
85
|
+
* session.subscribe("/main.typ", (diags) => { ... });
|
|
86
|
+
* await session.sync("/main.typ", files);
|
|
87
|
+
*/
|
|
88
|
+
declare class AnalyzerSession {
|
|
89
|
+
private readonly analyzer;
|
|
90
|
+
private readonly rootPath;
|
|
91
|
+
private readonly entryPath;
|
|
92
|
+
private readonly syncedFiles;
|
|
93
|
+
private queue;
|
|
94
|
+
private readonly listenersByUri;
|
|
95
|
+
/** Last push received per URI. Replayed on subscribe() so tab-back shows correct diagnostics instantly. */
|
|
96
|
+
private readonly diagnosticsCache;
|
|
97
|
+
private readonly unsubscribeAnalyzer;
|
|
98
|
+
constructor(options: AnalyzerSessionOptions);
|
|
99
|
+
/** Build a tinymist URI from a project-relative path. */
|
|
100
|
+
toUri(path: string): string;
|
|
101
|
+
/**
|
|
102
|
+
* Subscribe to push-based diagnostics for a file path.
|
|
103
|
+
* Returns an unsubscribe function.
|
|
104
|
+
*/
|
|
105
|
+
subscribe(path: string, listener: DiagnosticsSubscriber): () => void;
|
|
106
|
+
/**
|
|
107
|
+
* Sync all project files with the analyzer.
|
|
108
|
+
* `files` must include the active file's current content under `path`.
|
|
109
|
+
*
|
|
110
|
+
* If the active file's content hasn't changed since the last sync, a
|
|
111
|
+
* lightweight hover is triggered to ensure the analyzer re-analyzes with
|
|
112
|
+
* the current project state and publishes fresh diagnostics.
|
|
113
|
+
*/
|
|
114
|
+
sync(path: string, files: Record<string, string>): Promise<void>;
|
|
115
|
+
/**
|
|
116
|
+
* Sync files and request completions at the given position.
|
|
117
|
+
* Returns the raw LSP CompletionList/CompletionItem[] from tinymist.
|
|
118
|
+
*/
|
|
119
|
+
completion(path: string, files: Record<string, string>, line: number, character: number): Promise<unknown>;
|
|
120
|
+
/**
|
|
121
|
+
* Sync files and request hover info at the given position.
|
|
122
|
+
* Returns the raw LSP Hover result from tinymist.
|
|
123
|
+
*/
|
|
124
|
+
hover(path: string, files: Record<string, string>, line: number, character: number): Promise<unknown>;
|
|
125
|
+
destroy(): void;
|
|
126
|
+
/**
|
|
127
|
+
* Sync all project files: dependencies first, active file last.
|
|
128
|
+
* Returns whether the active file's content was actually sent to the analyzer.
|
|
129
|
+
*/
|
|
130
|
+
private syncFiles;
|
|
131
|
+
/** Sync a single file. Returns true if content was sent to the analyzer. */
|
|
132
|
+
private syncFile;
|
|
133
|
+
private orderedPaths;
|
|
134
|
+
private enqueue;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
declare function normalizeUntitledUri(uri: string): string;
|
|
138
|
+
declare function normalizePath(path: string): string;
|
|
139
|
+
declare function normalizeRoot(rootPath: string): string;
|
|
140
|
+
|
|
71
141
|
/** Source range for a diagnostic. All values are 0-indexed. */
|
|
72
142
|
interface DiagnosticRange {
|
|
73
143
|
startLine: number;
|
|
@@ -92,7 +162,7 @@ interface TypstCompilerOptions {
|
|
|
92
162
|
/**
|
|
93
163
|
* Explicit Worker instance. When omitted, an inlined blob worker is created automatically.
|
|
94
164
|
* Use this for Vite apps to get proper source maps:
|
|
95
|
-
* `
|
|
165
|
+
* `await TypstCompiler.create({ worker: new Worker(new URL('typst-web-service/worker', import.meta.url)) })`
|
|
96
166
|
*/
|
|
97
167
|
worker?: Worker;
|
|
98
168
|
/**
|
|
@@ -114,18 +184,18 @@ interface TypstCompilerOptions {
|
|
|
114
184
|
* Manages a Typst compiler worker. Create one instance and share it across
|
|
115
185
|
* all extensions (linter, autocomplete, preview, etc.).
|
|
116
186
|
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
187
|
+
* await TypstCompiler.create() // blob worker, defaults
|
|
188
|
+
* await TypstCompiler.create({ wasmUrl: '...' }) // blob worker, custom WASM
|
|
189
|
+
* await TypstCompiler.create({ worker: myWorker }) // explicit Worker (Vite)
|
|
190
|
+
* await TypstCompiler.create({ worker: myWorker, fonts: [...] }) // explicit Worker + options
|
|
121
191
|
*/
|
|
122
192
|
declare class TypstCompiler {
|
|
123
|
-
readonly ready: Promise<void>;
|
|
124
193
|
private idCounter;
|
|
125
194
|
private worker;
|
|
126
195
|
/** The most recent vector artifact from a compile, if any. */
|
|
127
196
|
lastVector?: Uint8Array;
|
|
128
|
-
constructor(
|
|
197
|
+
private constructor();
|
|
198
|
+
static create(options?: TypstCompilerOptions): Promise<TypstCompiler>;
|
|
129
199
|
/** Compile a single source string (treated as /main.typ) or a map of files. */
|
|
130
200
|
compile(source: string | Record<string, string>): Promise<CompileResult>;
|
|
131
201
|
/** Compile to PDF from a single source string (treated as /main.typ) or a map of files. */
|
|
@@ -133,79 +203,6 @@ declare class TypstCompiler {
|
|
|
133
203
|
destroy(): void;
|
|
134
204
|
}
|
|
135
205
|
|
|
136
|
-
type DiagnosticsSubscriber = (diagnostics: LspDiagnostic[]) => void;
|
|
137
|
-
interface AnalyzerSessionOptions {
|
|
138
|
-
analyzer: Pick<TypstAnalyzer, "ready" | "didOpen" | "didChange" | "completion" | "hover" | "onDiagnostics">;
|
|
139
|
-
/** Project root used to build stable in-memory analyzer URIs. Default: "/project". */
|
|
140
|
-
rootPath?: string;
|
|
141
|
-
/** Entry file path within the project. Synced last to ensure dependencies load first. Default: "/main.typ". */
|
|
142
|
-
entryPath?: string;
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* Synchronizes an in-memory Typst project with a TypstAnalyzer.
|
|
146
|
-
* Handles multi-file ordering, request queueing, and diagnostic subscriptions.
|
|
147
|
-
*
|
|
148
|
-
* Diagnostics arrive via the analyzer's push mechanism and are forwarded
|
|
149
|
-
* to subscribers registered with `subscribe()`.
|
|
150
|
-
*
|
|
151
|
-
* const session = new AnalyzerSession({ analyzer });
|
|
152
|
-
* session.subscribe("/main.typ", (diags) => { ... });
|
|
153
|
-
* await session.sync("/main.typ", source, files);
|
|
154
|
-
*/
|
|
155
|
-
declare class AnalyzerSession {
|
|
156
|
-
readonly ready: Promise<void>;
|
|
157
|
-
private readonly analyzer;
|
|
158
|
-
private readonly rootPath;
|
|
159
|
-
private readonly entryPath;
|
|
160
|
-
private readonly syncedFiles;
|
|
161
|
-
private queue;
|
|
162
|
-
private syncRevision;
|
|
163
|
-
private readonly listenersByUri;
|
|
164
|
-
private readonly diagnosticsByUri;
|
|
165
|
-
private readonly diagnosticsHashByUri;
|
|
166
|
-
private readonly unsubscribeAnalyzer;
|
|
167
|
-
constructor(options: AnalyzerSessionOptions);
|
|
168
|
-
/** Build a tinymist URI from a project-relative path. */
|
|
169
|
-
toUri(path: string): string;
|
|
170
|
-
/**
|
|
171
|
-
* Subscribe to push-based diagnostics for a file path.
|
|
172
|
-
* Returns an unsubscribe function. Replays cached diagnostics immediately.
|
|
173
|
-
*/
|
|
174
|
-
subscribe(path: string, listener: DiagnosticsSubscriber): () => void;
|
|
175
|
-
/**
|
|
176
|
-
* Sync all project files with the analyzer, then notify it of the active file change.
|
|
177
|
-
* Diagnostics will arrive asynchronously via subscribers registered with `subscribe()`.
|
|
178
|
-
*/
|
|
179
|
-
sync(path: string, content: string, files: Record<string, string>): Promise<void>;
|
|
180
|
-
/**
|
|
181
|
-
* Sync files, then compile with the provided compiler.
|
|
182
|
-
* Diagnostics come from the analyzer (push-based); the compiler provides preview artifacts.
|
|
183
|
-
*/
|
|
184
|
-
syncAndCompile(path: string, content: string, files: Record<string, string>, compiler: TypstCompiler, onCompile?: (result: CompileResult) => void, signal?: AbortSignal): Promise<void>;
|
|
185
|
-
/**
|
|
186
|
-
* Sync files and request completions at the given position.
|
|
187
|
-
* Returns the raw LSP CompletionList/CompletionItem[] from tinymist.
|
|
188
|
-
*/
|
|
189
|
-
completion(path: string, content: string, files: Record<string, string>, line: number, character: number): Promise<unknown>;
|
|
190
|
-
/**
|
|
191
|
-
* Sync files and request hover info at the given position.
|
|
192
|
-
* Returns the raw LSP Hover result from tinymist.
|
|
193
|
-
*/
|
|
194
|
-
hover(path: string, content: string, files: Record<string, string>, line: number, character: number): Promise<unknown>;
|
|
195
|
-
destroy(): void;
|
|
196
|
-
/**
|
|
197
|
-
* Sync a single file with the analyzer.
|
|
198
|
-
* @param forceOpen - Always use didOpen (triggers tinymist diagnostics for the active file).
|
|
199
|
-
*/
|
|
200
|
-
private syncFile;
|
|
201
|
-
private orderedPaths;
|
|
202
|
-
private enqueue;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
declare function normalizeUntitledUri(uri: string): string;
|
|
206
|
-
declare function normalizePath(path: string): string;
|
|
207
|
-
declare function normalizeRoot(rootPath: string): string;
|
|
208
|
-
|
|
209
206
|
interface FormatConfig {
|
|
210
207
|
/** Number of spaces per indentation level. Default: 2 */
|
|
211
208
|
tab_spaces?: number;
|
|
@@ -235,12 +232,13 @@ interface FormatRangeResult {
|
|
|
235
232
|
* The WASM module is loaded lazily on first format call.
|
|
236
233
|
* Requires a bundler that supports WASM imports (e.g. Vite, webpack).
|
|
237
234
|
*
|
|
238
|
-
* const formatter =
|
|
235
|
+
* const formatter = TypstFormatter.create({ tab_spaces: 2, max_width: 80 });
|
|
239
236
|
* const formatted = await formatter.format(source);
|
|
240
237
|
*/
|
|
241
238
|
declare class TypstFormatter {
|
|
242
239
|
private config;
|
|
243
|
-
constructor(
|
|
240
|
+
private constructor();
|
|
241
|
+
static create(config?: FormatConfig): TypstFormatter;
|
|
244
242
|
/** Format an entire Typst source string. */
|
|
245
243
|
format(source: string): Promise<string>;
|
|
246
244
|
/** Format a range within a Typst source string. Indices are UTF-16 code units. */
|
|
@@ -256,14 +254,15 @@ interface TypstRendererOptions {
|
|
|
256
254
|
*
|
|
257
255
|
* The renderer WASM module is loaded lazily on first use.
|
|
258
256
|
*
|
|
259
|
-
* const renderer =
|
|
257
|
+
* const renderer = TypstRenderer.create();
|
|
260
258
|
* const svg = await renderer.renderSvg(vector);
|
|
261
259
|
*/
|
|
262
260
|
declare class TypstRenderer {
|
|
263
261
|
#private;
|
|
264
262
|
private wasmUrl;
|
|
265
263
|
private instance;
|
|
266
|
-
constructor(
|
|
264
|
+
private constructor();
|
|
265
|
+
static create(options?: TypstRendererOptions): TypstRenderer;
|
|
267
266
|
private getInstance;
|
|
268
267
|
/** Free the underlying WASM renderer instance. */
|
|
269
268
|
destroy(): Promise<void>;
|