Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/observablehq-compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
},
"devDependencies": {
"@hpcc-js/esbuild-plugins": "^1.5.0",
"@observablehq/notebook-kit": "1.2.0",
"@observablehq/notebook-kit": "1.4.1",
"@observablehq/parser": "6.1.0",
"@observablehq/runtime": "5.9.9",
"@types/jsdom": "21.1.7"
Expand Down
20 changes: 12 additions & 8 deletions packages/observablehq-compiler/src/kit/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ import { type Notebook, type Cell, transpile } from "@observablehq/notebook-kit"
import { type Definition } from "@observablehq/notebook-kit/runtime";
import { constructFunction } from "./util.ts";

export interface CompileKitOptions {
export { Notebook, Cell };

export interface CompileCellOptions {
inline?: boolean;
resolveLocalImports?: boolean;
includePinned?: boolean;
}

export function compileCell(cell: Cell, options: CompileKitOptions = { inline: true }): Definition[] {
export function compileCell(cell: Cell, { inline = true, resolveLocalImports = false, includePinned = true }: CompileCellOptions = {}): Definition[] {
const retVal: Definition[] = [];
const sourceIDOffset = 1000000;
try {
const compiled = transpile(cell);
const compiled = transpile(cell, { resolveLocalImports });
retVal.push({
id: cell.id,
...compiled,
body: options.inline ? constructFunction(compiled.body, `cell_${cell.id}`) : compiled.body,
body: inline ? constructFunction(compiled.body, `cell_${cell.id}`) : compiled.body,
});
if (cell.pinned) {
if (includePinned && cell.pinned) {
const compiled = transpile({
...cell,
mode: "md",
Expand All @@ -29,7 +33,7 @@ ${cell.value}
retVal.push({
id: sourceIDOffset + cell.id,
...compiled,
body: options.inline ? constructFunction(compiled.body, `cell_source_${sourceIDOffset + cell.id}`) : compiled.body,
body: inline ? constructFunction(compiled.body, `cell_${sourceIDOffset + cell.id}`) : compiled.body,
Comment thread
GordonSmith marked this conversation as resolved.
});
}
} catch (error) {
Expand All @@ -38,10 +42,10 @@ ${cell.value}
return retVal;
}

export function compileNotebook(notebook: Notebook, options: CompileKitOptions = { inline: true }): Definition[] {
export function compileNotebook(notebook: Notebook, { inline = true, resolveLocalImports = false, includePinned = true }: CompileCellOptions = {}): Definition[] {
const retVal: Definition[] = [];
for (const cell of notebook.cells) {
const cellDefs = compileCell(cell, options);
const cellDefs = compileCell(cell, { inline, resolveLocalImports, includePinned });
retVal.push(...cellDefs);
}
return retVal;
Expand Down
7 changes: 3 additions & 4 deletions packages/observablehq-compiler/src/kit/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { type DefineState, NotebookRuntime as NotebookRuntimeBase } from "@observablehq/notebook-kit/runtime";
import { type Definition } from "./index.ts";
import { type Definition, type DefineState, NotebookRuntime as NotebookRuntimeBase } from "@observablehq/notebook-kit/runtime";
Comment thread
GordonSmith marked this conversation as resolved.

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

export { DefineState };
export { Definition, DefineState };

export class NotebookRuntime extends NotebookRuntimeBase {

Expand Down Expand Up @@ -84,4 +83,4 @@ export class NotebookRuntime extends NotebookRuntimeBase {
target.appendChild(observableDiv);
}
}
}
}
Loading