@hpcc-js/observablehq-compiler 1.4.0 → 1.5.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/src/compiler.ts CHANGED
@@ -1,311 +1,311 @@
1
- import { ohq, splitModule } from "@hpcc-js/observable-shim";
2
- import { parseCell, ParsedImportCell } from "./cst";
3
- import { Writer } from "./writer";
4
- import { fixRelativeUrl, isRelativePath, encodeBacktick, fetchEx, obfuscatedImport, ojs2notebook, omd2notebook } from "./util";
5
-
6
- // Inspector Factory ---
7
- export type InspectorFactoryEx = (name: string | undefined, id: string | number) => Inspector;
8
-
9
- export interface Inspector {
10
- _node?: HTMLDivElement;
11
- pending();
12
- fulfilled(value);
13
- rejected(error);
14
- }
15
-
16
- // Module ---
17
-
18
- interface ImportDefine {
19
- (runtime: ohq.Runtime, inspector?: InspectorFactoryEx): ohq.Module;
20
- delete: () => void;
21
- write: (w: Writer) => void;
22
- }
23
-
24
- async function importFile(relativePath: string, baseUrl: string) {
25
- const path = fixRelativeUrl(relativePath, baseUrl);
26
- const content = await fetchEx(path).then(r => r.text());
27
- let notebook: ohq.Notebook;
28
- if (relativePath.endsWith(".ojsnb")) {
29
- notebook = JSON.parse(content);
30
- } else if (relativePath.endsWith(".ojs")) {
31
- notebook = ojs2notebook(content);
32
- } else if (relativePath.endsWith(".omd")) {
33
- notebook = omd2notebook(content);
34
- }
35
- const retVal: ImportDefine = compile(notebook, { baseUrl }) as any;
36
- retVal.delete = () => { };
37
- retVal.write = (w: Writer) => {
38
- w.import(path);
39
- };
40
- return retVal;
41
- }
42
-
43
- // Import precompiled notebook from observable ---
44
- async function importCompiledNotebook(partial: string) {
45
- const url = `https://api.observablehq.com/${partial[0] === "@" ? partial : `d/${partial}`}.js?v=3`;
46
- let impMod = {
47
- default: function (runtime: ohq.Runtime, inspector?: InspectorFactoryEx): ohq.Module {
48
- return undefined;
49
- } as any
50
- };
51
- try {
52
- impMod = await obfuscatedImport(url);
53
- } catch (e) {
54
- }
55
- const retVal: ImportDefine = impMod.default;
56
- retVal.delete = () => { };
57
- retVal.write = (w: Writer) => {
58
- w.import(url);
59
- };
60
- return retVal;
61
- }
62
-
63
- // Recursive notebook parsing and compiling
64
- async function importNotebook(partial: string) {
65
- const url = `https://api.observablehq.com/document/${partial}`;
66
- const notebook = fetchEx(url)
67
- .then(r => {
68
- return r.json();
69
- }).catch(e => {
70
- console.error(url);
71
- console.error(e);
72
- });
73
- const retVal: ImportDefine = compile(await notebook) as any;
74
- retVal.delete = () => { };
75
- retVal.write = (w: Writer) => {
76
- w.import(url);
77
- };
78
- return retVal;
79
- }
80
-
81
- async function createModule(node: ohq.Node, parsed: ParsedImportCell, text: string, { baseUrl, importMode }: CompileOptions) {
82
- const otherModule = isRelativePath(parsed.src) ?
83
- await importFile(parsed.src, baseUrl) :
84
- importMode === "recursive" ?
85
- await importNotebook(parsed.src) :
86
- await importCompiledNotebook(parsed.src);
87
-
88
- const importVariables: ImportVariableFunc[] = [];
89
- const variables: VariableFunc[] = [];
90
- parsed.specifiers.forEach(spec => {
91
- const viewof = spec.view ? "viewof " : "";
92
- importVariables.push(createImportVariable(viewof + spec.name, viewof + spec.alias));
93
- if (spec.view) {
94
- importVariables.push(createImportVariable(spec.name, spec.alias));
95
- }
96
- });
97
-
98
- const retVal = (runtime: ohq.Runtime, main: ohq.Module, inspector?: InspectorFactoryEx) => {
99
-
100
- let mod = runtime.module(otherModule);
101
- if (parsed.injections.length) {
102
- mod = mod.derive(parsed.injections, main);
103
- }
104
- variables.forEach(v => v(main, inspector));
105
- importVariables.forEach(v => v(main, mod));
106
- return mod;
107
- };
108
- retVal.importVariables = importVariables;
109
- retVal.variables = variables;
110
- retVal.delete = () => {
111
- importVariables.forEach(v => v.delete());
112
- variables.forEach(v => v.delete());
113
- otherModule.delete();
114
- };
115
- retVal.write = (w: Writer) => {
116
- otherModule.write(w);
117
- w.importDefine(parsed);
118
- };
119
- return retVal;
120
- }
121
- type ModuleFunc = Awaited<ReturnType<typeof createModule>>;
122
-
123
- // Variable ---
124
- function createVariable(node: ohq.Node, inspect: boolean, name?: string, inputs?: string[], definition?: any, inline = false) {
125
-
126
- let i: ohq.Inspector;
127
- let v: ohq.Variable;
128
-
129
- const retVal = (module: ohq.Module, inspector?: InspectorFactoryEx) => {
130
- i = inspect ? inspector(name, node.id) : undefined;
131
- v = module.variable(i);
132
- if (arguments.length > 1) {
133
- try {
134
- v.define(name, inputs, definition);
135
- } catch (e: any) {
136
- console.error(e?.message);
137
- }
138
- }
139
- if (node.pinned) {
140
- v = module.variable(inspector(name, node.id));
141
- try {
142
- v.define(undefined, ["md"], md => {
143
- return md`\`\`\`js
144
- ${node.value}
145
- \`\`\``;
146
- });
147
- } catch (e: any) {
148
- console.error(e?.message);
149
- }
150
- }
151
- return v;
152
- };
153
- retVal.delete = () => {
154
- try {
155
- i?._node?.remove();
156
- } catch (e) {
157
- }
158
- i = undefined;
159
- try {
160
- v?.delete();
161
- } catch (e) {
162
- }
163
- v = undefined;
164
- };
165
- retVal.write = (w: Writer) => {
166
- if (inline) {
167
- w.define({ id: name, inputs, func: definition }, inspect, true);
168
- } else {
169
- const id = w.function({ id: name, func: definition });
170
- w.define({ id: name, inputs, func: definition }, inspect, false, id);
171
- }
172
- };
173
- return retVal;
174
- }
175
- type VariableFunc = ReturnType<typeof createVariable>;
176
-
177
- function createImportVariable(name?: string, alias?: string) {
178
-
179
- let v: ohq.Variable;
180
-
181
- const retVal = (main: ohq.Module, otherModule: ohq.Module) => {
182
- v = main.variable();
183
- v.import(name, alias, otherModule);
184
- };
185
-
186
- retVal.delete = () => {
187
- v?.delete();
188
- };
189
- return retVal;
190
- }
191
- type ImportVariableFunc = ReturnType<typeof createImportVariable>;
192
-
193
- // Cell ---
194
- async function createCell(node: ohq.Node, options: CompileOptions) {
195
- const modules: ModuleFunc[] = [];
196
- const variables: VariableFunc[] = [];
197
- try {
198
- const text = node.mode && node.mode !== "js" ? `${node.mode}\`${encodeBacktick(node.value)}\`` : node.value;
199
- const parsedModule = splitModule(text);
200
- for (const cell of parsedModule) {
201
- const parsed = parseCell(cell.text, options.baseUrl);
202
- switch (parsed.type) {
203
- case "import":
204
- modules.push(await createModule(node, parsed, cell.text, options));
205
- break;
206
- case "viewof":
207
- variables.push(createVariable(node, true, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
208
- variables.push(createVariable(node, false, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
209
- break;
210
- case "mutable":
211
- variables.push(createVariable(node, false, parsed.initial.id, parsed.initial.inputs, parsed.initial.func));
212
- variables.push(createVariable(node, false, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
213
- variables.push(createVariable(node, true, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
214
- break;
215
- case "variable":
216
- variables.push(createVariable(node, true, parsed.id, parsed.inputs, parsed.func));
217
- break;
218
- }
219
- }
220
- } catch (e) {
221
- variables.push(createVariable(node, true, undefined, [], e.message));
222
- }
223
-
224
- const retVal = (runtime: ohq.Runtime, main: ohq.Module, inspector?: InspectorFactoryEx) => {
225
- modules.forEach(imp => imp(runtime, main, inspector));
226
- variables.forEach(v => v(main, inspector));
227
- };
228
- retVal.id = node.id;
229
- retVal.modules = modules;
230
- retVal.variables = variables;
231
- retVal.delete = () => {
232
- variables.forEach(v => v.delete());
233
- modules.forEach(mod => mod.delete());
234
- };
235
- retVal.write = (w: Writer) => {
236
- modules.forEach(imp => imp.write(w));
237
- variables.forEach(v => v.write(w));
238
- };
239
- return retVal;
240
- }
241
- export type CellFunc = Awaited<ReturnType<typeof createCell>>;
242
-
243
- // File ---
244
- function createFile(file: ohq.File, options: CompileOptions): [string, any] {
245
- function toString() { return globalThis.url; }
246
- return [file.name, { url: new URL(fixRelativeUrl(file.url, options.baseUrl)), mimeType: file.mime_type, toString }];
247
- }
248
- type FileFunc = ReturnType<typeof createFile>;
249
-
250
- // Interpret ---
251
- export interface CompileOptions {
252
- baseUrl?: string;
253
- importMode?: "recursive" | "precompiled";
254
- }
255
- export function notebook(_files: ohq.File[] = [], _cells: CellFunc[] = [], { baseUrl = ".", importMode = "precompiled" }: CompileOptions = {}) {
256
- const files: FileFunc[] = _files.map(f => createFile(f, { baseUrl, importMode }));
257
- const fileAttachments = new Map<string, any>(files);
258
- const cells = new Map<string | number, CellFunc>(_cells.map(c => [c.id, c]));
259
-
260
- const retVal = (runtime: ohq.Runtime, inspector?: InspectorFactoryEx): ohq.Module => {
261
- const main = runtime.module();
262
- main.builtin("FileAttachment", runtime.fileAttachments(name => {
263
- return fileAttachments.get(name) ?? { url: new URL(fixRelativeUrl(name, baseUrl)), mimeType: null };
264
- }));
265
- main.builtin("fetchEx", fetchEx);
266
-
267
- cells.forEach(cell => {
268
- cell(runtime, main, inspector);
269
- });
270
- return main;
271
- };
272
- retVal.fileAttachments = fileAttachments;
273
- retVal.cells = cells;
274
- retVal.set = async (n: ohq.Node): Promise<CellFunc> => {
275
- const cell = await createCell(n, { baseUrl, importMode });
276
- retVal.delete(cell.id);
277
- cells.set(cell.id, cell);
278
- return cell;
279
- };
280
- retVal.get = (id: string | number): CellFunc => {
281
- return cells.get(id);
282
- };
283
- retVal.delete = (id: string | number): boolean => {
284
- const cell = cells.get(id);
285
- if (cell) {
286
- cell.delete();
287
- return cells.delete(id);
288
- }
289
- return false;
290
- };
291
- retVal.clear = () => {
292
- cells.forEach(cell => cell.delete());
293
- cells.clear();
294
- };
295
- retVal.write = (w: Writer) => {
296
- w.files(_files);
297
- cells.forEach(cell => cell.write(w));
298
- };
299
- retVal.toString = (w = new Writer()) => {
300
- retVal.write(w);
301
- return w.toString().trim();
302
- };
303
- return retVal;
304
- }
305
-
306
- export async function compile(notebookOrOjs: ohq.Notebook | string, { baseUrl = ".", importMode = "precompiled" }: CompileOptions = {}) {
307
- const ojsNotebook = typeof notebookOrOjs === "string" ? ojs2notebook(notebookOrOjs) : notebookOrOjs;
308
- const _cells: CellFunc[] = await Promise.all(ojsNotebook.nodes.map(n => createCell(n, { baseUrl, importMode })));
309
- return notebook(ojsNotebook.files, _cells, { baseUrl, importMode });
310
- }
311
- export type compileFunc = Awaited<ReturnType<typeof compile>>;
1
+ import { ohq, splitModule } from "@hpcc-js/observable-shim";
2
+ import { parseCell, ParsedImportCell } from "./cst";
3
+ import { Writer } from "./writer";
4
+ import { fixRelativeUrl, isRelativePath, encodeBacktick, fetchEx, obfuscatedImport, ojs2notebook, omd2notebook } from "./util";
5
+
6
+ // Inspector Factory ---
7
+ export type InspectorFactoryEx = (name: string | undefined, id: string | number) => Inspector;
8
+
9
+ export interface Inspector {
10
+ _node?: HTMLDivElement;
11
+ pending();
12
+ fulfilled(value);
13
+ rejected(error);
14
+ }
15
+
16
+ // Module ---
17
+
18
+ interface ImportDefine {
19
+ (runtime: ohq.Runtime, inspector?: InspectorFactoryEx): ohq.Module;
20
+ delete: () => void;
21
+ write: (w: Writer) => void;
22
+ }
23
+
24
+ async function importFile(relativePath: string, baseUrl: string) {
25
+ const path = fixRelativeUrl(relativePath, baseUrl);
26
+ const content = await fetchEx(path).then(r => r.text());
27
+ let notebook: ohq.Notebook;
28
+ if (relativePath.endsWith(".ojsnb")) {
29
+ notebook = JSON.parse(content);
30
+ } else if (relativePath.endsWith(".ojs")) {
31
+ notebook = ojs2notebook(content);
32
+ } else if (relativePath.endsWith(".omd")) {
33
+ notebook = omd2notebook(content);
34
+ }
35
+ const retVal: ImportDefine = compile(notebook, { baseUrl }) as any;
36
+ retVal.delete = () => { };
37
+ retVal.write = (w: Writer) => {
38
+ w.import(path);
39
+ };
40
+ return retVal;
41
+ }
42
+
43
+ // Import precompiled notebook from observable ---
44
+ async function importCompiledNotebook(partial: string) {
45
+ const url = `https://api.observablehq.com/${partial[0] === "@" ? partial : `d/${partial}`}.js?v=3`;
46
+ let impMod = {
47
+ default: function (runtime: ohq.Runtime, inspector?: InspectorFactoryEx): ohq.Module {
48
+ return undefined;
49
+ } as any
50
+ };
51
+ try {
52
+ impMod = await obfuscatedImport(url);
53
+ } catch (e) {
54
+ }
55
+ const retVal: ImportDefine = impMod.default;
56
+ retVal.delete = () => { };
57
+ retVal.write = (w: Writer) => {
58
+ w.import(url);
59
+ };
60
+ return retVal;
61
+ }
62
+
63
+ // Recursive notebook parsing and compiling
64
+ async function importNotebook(partial: string) {
65
+ const url = `https://api.observablehq.com/document/${partial}`;
66
+ const notebook = fetchEx(url)
67
+ .then(r => {
68
+ return r.json();
69
+ }).catch(e => {
70
+ console.error(url);
71
+ console.error(e);
72
+ });
73
+ const retVal: ImportDefine = compile(await notebook) as any;
74
+ retVal.delete = () => { };
75
+ retVal.write = (w: Writer) => {
76
+ w.import(url);
77
+ };
78
+ return retVal;
79
+ }
80
+
81
+ async function createModule(node: ohq.Node, parsed: ParsedImportCell, text: string, { baseUrl, importMode }: CompileOptions) {
82
+ const otherModule = isRelativePath(parsed.src) ?
83
+ await importFile(parsed.src, baseUrl) :
84
+ importMode === "recursive" ?
85
+ await importNotebook(parsed.src) :
86
+ await importCompiledNotebook(parsed.src);
87
+
88
+ const importVariables: ImportVariableFunc[] = [];
89
+ const variables: VariableFunc[] = [];
90
+ parsed.specifiers.forEach(spec => {
91
+ const viewof = spec.view ? "viewof " : "";
92
+ importVariables.push(createImportVariable(viewof + spec.name, viewof + spec.alias));
93
+ if (spec.view) {
94
+ importVariables.push(createImportVariable(spec.name, spec.alias));
95
+ }
96
+ });
97
+
98
+ const retVal = (runtime: ohq.Runtime, main: ohq.Module, inspector?: InspectorFactoryEx) => {
99
+
100
+ let mod = runtime.module(otherModule);
101
+ if (parsed.injections.length) {
102
+ mod = mod.derive(parsed.injections, main);
103
+ }
104
+ variables.forEach(v => v(main, inspector));
105
+ importVariables.forEach(v => v(main, mod));
106
+ return mod;
107
+ };
108
+ retVal.importVariables = importVariables;
109
+ retVal.variables = variables;
110
+ retVal.delete = () => {
111
+ importVariables.forEach(v => v.delete());
112
+ variables.forEach(v => v.delete());
113
+ otherModule.delete();
114
+ };
115
+ retVal.write = (w: Writer) => {
116
+ otherModule.write(w);
117
+ w.importDefine(parsed);
118
+ };
119
+ return retVal;
120
+ }
121
+ type ModuleFunc = Awaited<ReturnType<typeof createModule>>;
122
+
123
+ // Variable ---
124
+ function createVariable(node: ohq.Node, inspect: boolean, name?: string, inputs?: string[], definition?: any, inline = false) {
125
+
126
+ let i: ohq.Inspector;
127
+ let v: ohq.Variable;
128
+
129
+ const retVal = (module: ohq.Module, inspector?: InspectorFactoryEx) => {
130
+ i = inspect ? inspector(name, node.id) : undefined;
131
+ v = module.variable(i);
132
+ if (arguments.length > 1) {
133
+ try {
134
+ v.define(name, inputs, definition);
135
+ } catch (e: any) {
136
+ console.error(e?.message);
137
+ }
138
+ }
139
+ if (node.pinned) {
140
+ v = module.variable(inspector(name, node.id));
141
+ try {
142
+ v.define(undefined, ["md"], md => {
143
+ return md`\`\`\`js
144
+ ${node.value}
145
+ \`\`\``;
146
+ });
147
+ } catch (e: any) {
148
+ console.error(e?.message);
149
+ }
150
+ }
151
+ return v;
152
+ };
153
+ retVal.delete = () => {
154
+ try {
155
+ i?._node?.remove();
156
+ } catch (e) {
157
+ }
158
+ i = undefined;
159
+ try {
160
+ v?.delete();
161
+ } catch (e) {
162
+ }
163
+ v = undefined;
164
+ };
165
+ retVal.write = (w: Writer) => {
166
+ if (inline) {
167
+ w.define({ id: name, inputs, func: definition }, inspect, true);
168
+ } else {
169
+ const id = w.function({ id: name, func: definition });
170
+ w.define({ id: name, inputs, func: definition }, inspect, false, id);
171
+ }
172
+ };
173
+ return retVal;
174
+ }
175
+ type VariableFunc = ReturnType<typeof createVariable>;
176
+
177
+ function createImportVariable(name?: string, alias?: string) {
178
+
179
+ let v: ohq.Variable;
180
+
181
+ const retVal = (main: ohq.Module, otherModule: ohq.Module) => {
182
+ v = main.variable();
183
+ v.import(name, alias, otherModule);
184
+ };
185
+
186
+ retVal.delete = () => {
187
+ v?.delete();
188
+ };
189
+ return retVal;
190
+ }
191
+ type ImportVariableFunc = ReturnType<typeof createImportVariable>;
192
+
193
+ // Cell ---
194
+ async function createCell(node: ohq.Node, options: CompileOptions) {
195
+ const modules: ModuleFunc[] = [];
196
+ const variables: VariableFunc[] = [];
197
+ try {
198
+ const text = node.mode && node.mode !== "js" ? `${node.mode}\`${encodeBacktick(node.value)}\`` : node.value;
199
+ const parsedModule = splitModule(text);
200
+ for (const cell of parsedModule) {
201
+ const parsed = parseCell(cell.text, options.baseUrl);
202
+ switch (parsed.type) {
203
+ case "import":
204
+ modules.push(await createModule(node, parsed, cell.text, options));
205
+ break;
206
+ case "viewof":
207
+ variables.push(createVariable(node, true, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
208
+ variables.push(createVariable(node, false, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
209
+ break;
210
+ case "mutable":
211
+ variables.push(createVariable(node, false, parsed.initial.id, parsed.initial.inputs, parsed.initial.func));
212
+ variables.push(createVariable(node, false, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
213
+ variables.push(createVariable(node, true, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
214
+ break;
215
+ case "variable":
216
+ variables.push(createVariable(node, true, parsed.id, parsed.inputs, parsed.func));
217
+ break;
218
+ }
219
+ }
220
+ } catch (e) {
221
+ variables.push(createVariable(node, true, undefined, [], e.message));
222
+ }
223
+
224
+ const retVal = (runtime: ohq.Runtime, main: ohq.Module, inspector?: InspectorFactoryEx) => {
225
+ modules.forEach(imp => imp(runtime, main, inspector));
226
+ variables.forEach(v => v(main, inspector));
227
+ };
228
+ retVal.id = node.id;
229
+ retVal.modules = modules;
230
+ retVal.variables = variables;
231
+ retVal.delete = () => {
232
+ variables.forEach(v => v.delete());
233
+ modules.forEach(mod => mod.delete());
234
+ };
235
+ retVal.write = (w: Writer) => {
236
+ modules.forEach(imp => imp.write(w));
237
+ variables.forEach(v => v.write(w));
238
+ };
239
+ return retVal;
240
+ }
241
+ export type CellFunc = Awaited<ReturnType<typeof createCell>>;
242
+
243
+ // File ---
244
+ function createFile(file: ohq.File, options: CompileOptions): [string, any] {
245
+ function toString() { return globalThis.url; }
246
+ return [file.name, { url: new URL(fixRelativeUrl(file.url, options.baseUrl)), mimeType: file.mime_type, toString }];
247
+ }
248
+ type FileFunc = ReturnType<typeof createFile>;
249
+
250
+ // Interpret ---
251
+ export interface CompileOptions {
252
+ baseUrl?: string;
253
+ importMode?: "recursive" | "precompiled";
254
+ }
255
+ export function notebook(_files: ohq.File[] = [], _cells: CellFunc[] = [], { baseUrl = ".", importMode = "precompiled" }: CompileOptions = {}) {
256
+ const files: FileFunc[] = _files.map(f => createFile(f, { baseUrl, importMode }));
257
+ const fileAttachments = new Map<string, any>(files);
258
+ const cells = new Map<string | number, CellFunc>(_cells.map(c => [c.id, c]));
259
+
260
+ const retVal = (runtime: ohq.Runtime, inspector?: InspectorFactoryEx): ohq.Module => {
261
+ const main = runtime.module();
262
+ main.builtin("FileAttachment", runtime.fileAttachments(name => {
263
+ return fileAttachments.get(name) ?? { url: new URL(fixRelativeUrl(name, baseUrl)), mimeType: null };
264
+ }));
265
+ main.builtin("fetchEx", fetchEx);
266
+
267
+ cells.forEach(cell => {
268
+ cell(runtime, main, inspector);
269
+ });
270
+ return main;
271
+ };
272
+ retVal.fileAttachments = fileAttachments;
273
+ retVal.cells = cells;
274
+ retVal.set = async (n: ohq.Node): Promise<CellFunc> => {
275
+ const cell = await createCell(n, { baseUrl, importMode });
276
+ retVal.delete(cell.id);
277
+ cells.set(cell.id, cell);
278
+ return cell;
279
+ };
280
+ retVal.get = (id: string | number): CellFunc => {
281
+ return cells.get(id);
282
+ };
283
+ retVal.delete = (id: string | number): boolean => {
284
+ const cell = cells.get(id);
285
+ if (cell) {
286
+ cell.delete();
287
+ return cells.delete(id);
288
+ }
289
+ return false;
290
+ };
291
+ retVal.clear = () => {
292
+ cells.forEach(cell => cell.delete());
293
+ cells.clear();
294
+ };
295
+ retVal.write = (w: Writer) => {
296
+ w.files(_files);
297
+ cells.forEach(cell => cell.write(w));
298
+ };
299
+ retVal.toString = (w = new Writer()) => {
300
+ retVal.write(w);
301
+ return w.toString().trim();
302
+ };
303
+ return retVal;
304
+ }
305
+
306
+ export async function compile(notebookOrOjs: ohq.Notebook | string, { baseUrl = ".", importMode = "precompiled" }: CompileOptions = {}) {
307
+ const ojsNotebook = typeof notebookOrOjs === "string" ? ojs2notebook(notebookOrOjs) : notebookOrOjs;
308
+ const _cells: CellFunc[] = await Promise.all(ojsNotebook.nodes.map(n => createCell(n, { baseUrl, importMode })));
309
+ return notebook(ojsNotebook.files, _cells, { baseUrl, importMode });
310
+ }
311
+ export type compileFunc = Awaited<ReturnType<typeof compile>>;