Skip to content

Commit 23e9e75

Browse files
committed
fix: improve runtime placeholder handling
Fixes previous issues with update and remove Signed-off-by: Gordon Smith <GordonJSmith@gmail.com>
1 parent 605b250 commit 23e9e75

2 files changed

Lines changed: 14 additions & 11 deletions

File tree

packages/observablehq-compiler/src/kit/compiler.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@ import { type Notebook, type Cell, transpile } from "@observablehq/notebook-kit"
33
import { type Definition } from "@observablehq/notebook-kit/runtime";
44
import { constructFunction } from "./util.ts";
55

6+
export { Notebook, Cell };
7+
68
export interface CompileKitOptions {
79
inline?: boolean;
10+
resolveLocalImports?: boolean;
11+
includePinned?: boolean;
812
}
913

10-
export function compileCell(cell: Cell, options: CompileKitOptions = { inline: true }): Definition[] {
14+
export function compileCell(cell: Cell, { inline = true, resolveLocalImports = false, includePinned = true }: CompileKitOptions = {}): Definition[] {
1115
const retVal: Definition[] = [];
1216
const sourceIDOffset = 1000000;
1317
try {
14-
const compiled = transpile(cell);
18+
const compiled = transpile(cell, { resolveLocalImports });
1519
retVal.push({
1620
id: cell.id,
1721
...compiled,
18-
body: options.inline ? constructFunction(compiled.body, `cell_${cell.id}`) : compiled.body,
22+
body: inline ? constructFunction(compiled.body, `cell_${cell.id}`) : compiled.body,
1923
});
20-
if (cell.pinned) {
24+
if (includePinned && cell.pinned) {
2125
const compiled = transpile({
2226
...cell,
2327
mode: "md",
@@ -29,7 +33,7 @@ ${cell.value}
2933
retVal.push({
3034
id: sourceIDOffset + cell.id,
3135
...compiled,
32-
body: options.inline ? constructFunction(compiled.body, `cell_source_${sourceIDOffset + cell.id}`) : compiled.body,
36+
body: inline ? constructFunction(compiled.body, `cell_${sourceIDOffset + cell.id}`) : compiled.body,
3337
});
3438
}
3539
} catch (error) {
@@ -38,10 +42,10 @@ ${cell.value}
3842
return retVal;
3943
}
4044

41-
export function compileNotebook(notebook: Notebook, options: CompileKitOptions = { inline: true }): Definition[] {
45+
export function compileNotebook(notebook: Notebook, { inline = true, resolveLocalImports = false }: CompileKitOptions = {}): Definition[] {
4246
const retVal: Definition[] = [];
4347
for (const cell of notebook.cells) {
44-
const cellDefs = compileCell(cell, options);
48+
const cellDefs = compileCell(cell, { inline, resolveLocalImports });
4549
retVal.push(...cellDefs);
4650
}
4751
return retVal;

packages/observablehq-compiler/src/kit/runtime.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { type DefineState, NotebookRuntime as NotebookRuntimeBase } from "@observablehq/notebook-kit/runtime";
2-
import { type Definition } from "./index.ts";
1+
import { type Definition, type DefineState, NotebookRuntime as NotebookRuntimeBase } from "@observablehq/notebook-kit/runtime";
32

43
import "@observablehq/notebook-kit/index.css";
54
import "@observablehq/notebook-kit/theme-air.css";
65

7-
export { DefineState };
6+
export { Definition, DefineState };
87

98
export class NotebookRuntime extends NotebookRuntimeBase {
109

@@ -84,4 +83,4 @@ export class NotebookRuntime extends NotebookRuntimeBase {
8483
target.appendChild(observableDiv);
8584
}
8685
}
87-
}
86+
}

0 commit comments

Comments
 (0)