Skip to content

Commit 3757b4f

Browse files
committed
feat: add support for notebook-kit
Signed-off-by: Gordon Smith <[email protected]>
1 parent b9c9c06 commit 3757b4f

File tree

15 files changed

+528
-37
lines changed

15 files changed

+528
-37
lines changed

package-lock.json

Lines changed: 379 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/markdown-it-plugins/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@
5454
"dependencies": {
5555
"@hpcc-js/observablehq-compiler": "^3.3.9",
5656
"@observablehq/framework": "1.13.3",
57-
"@observablehq/runtime": "5.9.9",
57+
"@observablehq/parser": "6.1.0",
58+
"@observablehq/inspector": "5.0.1",
59+
"@observablehq/stdlib": "5.8.8",
60+
"@observablehq/runtime": "6.0.0",
5861
"@types/markdown-it": "14.1.2"
5962
},
6063
"peerDependencies": {

packages/observablehq-compiler/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ The interpreter is 100% compatible with:
8080
It also supports fetching notebooks directly from the ObservableHQ repository.
8181

8282
```js
83-
import { Library, Runtime, Inspector } from "@observablehq/runtime";
83+
import { Inspector } from "@observablehq/inspector";
84+
import { Library } from "@observablehq/stdlib";
85+
import { Runtime } from "@observablehq/runtime";
8486
import { download, compile } from "@hpcc-js/observablehq-compiler";
8587

8688
const placeholder = document.getElementById("placeholder");

packages/observablehq-compiler/esbuild.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

packages/observablehq-compiler/index-preview.html

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,14 @@
2626
<script type="importmap">
2727
{
2828
"imports": {
29-
"@observablehq/runtime": "https://cdn.jsdelivr.net/npm/@observablehq/[email protected]/dist/runtime.js",
29+
"@observablehq/stdlib": "https://cdn.jsdelivr.net/npm/@observablehq/[email protected]/src/index.js",
30+
"@observablehq/inspector": "https://cdn.jsdelivr.net/npm/@observablehq/[email protected]/src/index.js",
31+
"@observablehq/runtime": "https://cdn.jsdelivr.net/npm/@observablehq/[email protected]/src/index.js",
32+
"isoformat": "https://cdn.jsdelivr.net/npm/[email protected]/src/index.js",
33+
"d3-dsv": "https://cdn.jsdelivr.net/npm/[email protected]/src/index.js",
34+
"d3-require": "https://cdn.jsdelivr.net/npm/[email protected]/src/index.mjs",
35+
"d3-array": "https://cdn.jsdelivr.net/npm/[email protected]/src/index.js",
36+
"internmap": "https://cdn.jsdelivr.net/npm/[email protected]/src/index.js",
3037

3138
"@hpcc-js/observablehq-compiler": "../observablehq-compiler/dist/index.js"
3239
}
@@ -38,7 +45,9 @@
3845
<h1>ESM Quick Test</h1>
3946
<div id="placeholder"></div>
4047
<script type="module">
41-
import { Library, Runtime, Inspector } from "@observablehq/runtime";
48+
import { Inspector } from "@observablehq/inspector";
49+
import { Library } from "@observablehq/stdlib";
50+
import { Runtime } from "@observablehq/runtime";
4251
import { omd2notebook, compile } from "@hpcc-js/observablehq-compiler";
4352

4453
const md = `\

packages/observablehq-compiler/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
<h1>ESM Quick Test</h1>
3030
<div id="placeholder"></div>
3131
<script type="module">
32-
import { Library, Runtime, Inspector } from "@observablehq/runtime";
32+
import { Inspector } from "@observablehq/inspector";
33+
import { Library } from "@observablehq/stdlib";
34+
import { Runtime } from "@observablehq/runtime";
3335
import { omd2notebook, compile } from "./src/index.ts";
3436

3537
const md = `\

packages/observablehq-compiler/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@
4848
"devDependencies": {
4949
"@hpcc-js/esbuild-plugins": "^1.4.9",
5050
"@observablehq/parser": "6.1.0",
51-
"@observablehq/runtime": "5.9.9"
51+
"@observablehq/inspector": "5.0.1",
52+
"@observablehq/stdlib": "5.8.8",
53+
"@observablehq/notebook-kit": "1.0.1"
5254
},
5355
"repository": {
5456
"type": "git",

packages/observablehq-compiler/src/compiler.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ To render it to a web page, simply follow the same steps as if you had downloade
7171

7272
```js
7373

74-
import { Library, Runtime, Inspector } from "https://cdn.skypack.dev/@observablehq/runtime";
74+
import { Inspector } from "https://cdn.skypack.dev/@observablehq/inspector";
75+
import { Library } from "https://cdn.skypack.dev/@observablehq/stdlib";
76+
import { Runtime } from "https://cdn.skypack.dev/@observablehq/runtime";
7577

7678
const placeholder = document.getElementById("placeholder");
7779

@@ -93,7 +95,9 @@ Putting it all together:
9395
<div id="placeholder" style="height:100%;overflow-y:scroll">
9496
</div>
9597
<script type="module">
96-
import { Library, Runtime, Inspector } from "https://cdn.skypack.dev/@observablehq/runtime";
98+
import { Inspector } from "https://cdn.skypack.dev/@observablehq/inspector";
99+
import { Library } from "https://cdn.skypack.dev/@observablehq/stdlib";
100+
import { Runtime } from "https://cdn.skypack.dev/@observablehq/runtime";
97101
import { compile } from "@hpcc-js/observablehq-compiler";
98102

99103
const placeholder = document.getElementById("placeholder");
@@ -183,7 +187,9 @@ To output the generated code simply call `toString` on the compiled function:
183187
<div id="placeholder" style="height:400px;overflow-y:scroll">
184188
</div>
185189
<script type="module">
186-
import { Library, Runtime, Inspector } from "https://cdn.skypack.dev/@observablehq/runtime";
190+
import { Inspector } from "https://cdn.skypack.dev/@observablehq/inspector";
191+
import { Library } from "https://cdn.skypack.dev/@observablehq/stdlib";
192+
import { Runtime } from "https://cdn.skypack.dev/@observablehq/runtime";
187193
import { download, compile } from "@hpcc-js/observablehq-compiler";
188194

189195
const notebookUrl = "https://observablehq.com/@observablehq/plot";
@@ -212,7 +218,10 @@ To output the generated code simply call `toString` on the compiled function:
212218
<div id="placeholder" style="height:400px;overflow-y:scroll">
213219
</div>
214220
<script type="module">
215-
import { Library, Runtime, Inspector } from "https://cdn.skypack.dev/@observablehq/runtime";
221+
import { Inspector } from "https://cdn.skypack.dev/@observablehq/inspector";
222+
import { Library } from "https://cdn.skypack.dev/@observablehq/stdlib";
223+
import { Runtime } from "https://cdn.skypack.dev/@observablehq/runtime";
224+
216225
import { download, compile } from "@hpcc-js/observablehq-compiler";
217226

218227
const notebookUrl = "https://observablehq.com/@mbostock/fullscreen-canvas";

packages/observablehq-compiler/src/compiler.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { FileAttachments } from "@observablehq/stdlib";
12
import { ohq, splitModule } from "./observable-shim.ts";
23
import { parseCell, ParsedImportCell } from "./cst.ts";
34
import { Writer } from "./writer.ts";
@@ -266,13 +267,16 @@ export interface CompileOptions {
266267
}
267268
export function notebook(_files: ohq.File[] = [], _cells: CellFunc[] = [], { baseUrl = ".", importMode = "precompiled" }: CompileOptions = {}) {
268269
const files: FileFunc[] = _files.map(f => createFile(f, { baseUrl, importMode }));
269-
const fileAttachments = new Map<string, any>(files);
270+
const fileAttachmentsMap = new Map<string, any>(files);
270271
const cells = new Map<string | number, CellFunc>(_cells.map(c => [c.id, c]));
271272

272273
const retVal = (runtime: ohq.Runtime, inspector?: InspectorFactoryEx): ohq.Module => {
274+
if (runtime.fileAttachments === undefined) {
275+
Object.defineProperties(Object.getPrototypeOf(runtime), { fileAttachments: { value: FileAttachments, writable: true, configurable: true } });
276+
}
273277
const main = runtime.module();
274278
main.builtin("FileAttachment", runtime.fileAttachments(name => {
275-
return fileAttachments.get(name) ?? { url: new URL(fixRelativeUrl(name, baseUrl)), mimeType: null };
279+
return fileAttachmentsMap.get(name) ?? { url: new URL(fixRelativeUrl(name, baseUrl)), mimeType: null };
276280
}));
277281
main.builtin("fetchEx", fetchEx);
278282

@@ -281,7 +285,7 @@ export function notebook(_files: ohq.File[] = [], _cells: CellFunc[] = [], { bas
281285
});
282286
return main;
283287
};
284-
retVal.fileAttachments = fileAttachments;
288+
retVal.fileAttachments = fileAttachmentsMap;
285289
retVal.cells = cells;
286290
retVal.set = async (n: ohq.Node): Promise<CellFunc> => {
287291
const cell = await createCell(n, { baseUrl, importMode });

packages/observablehq-compiler/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ export { ojs2notebook, omd2notebook, download } from "./util.ts";
55
export * from "./writer.ts";
66

77
import "../src/index.css";
8+
9+
// export { ohqnk } from "./ohqnk/index.ts";

0 commit comments

Comments
 (0)