Skip to content

Commit 732f0d7

Browse files
committed
fix(observablehq-compiler): update dependencies and improve file attachment handling
- Updated `@observablehq/runtime` to version 6.0.0 and added `@observablehq/inspector` and `@observablehq/stdlib` as dependencies in `markdown-it-plugins` and `observablehq-compiler`. - Enhanced `index-kit.html` and `index-preview.html` to demonstrate file attachment functionality with a CSV example. - Refactored import statements in various HTML files to align with the new dependency structure. - Modified `compiler.ts` and `writer.ts` to utilize `FileAttachments` from `@observablehq/stdlib` for better file handling. - Updated test files to reflect changes in the compilation process and ensure compatibility with the new runtime. Signed-off-by: Gordon Smith <GordonJSmith@gmail.com>
1 parent 40c9476 commit 732f0d7

10 files changed

Lines changed: 511 additions & 41 deletions

File tree

package-lock.json

Lines changed: 461 additions & 27 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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@
5656
"dependencies": {
5757
"@hpcc-js/observablehq-compiler": "^3.7.12",
5858
"@observablehq/framework": "1.13.4",
59-
"@observablehq/runtime": "5.9.9",
59+
"@observablehq/inspector": "5.0.1",
60+
"@observablehq/stdlib": "5.8.8",
61+
"@observablehq/runtime": "6.0.0",
6062
"@types/markdown-it": "14.1.2"
6163
},
6264
"peerDependencies": {

packages/observablehq-compiler/index-kit.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,28 @@
3131
import { testOmd } from "./tests/index-notebookkit.ts";
3232
testOmd("placeholder-omd");
3333
</script>
34+
<hr>
35+
<h3>FileAttachment Example</h3>
36+
<div class="placeholder" id="placeholder-fileattachment"></div>
37+
<script type="module">
38+
import { ojs2notebookKit, compileNotebook } from "./src/index.ts";
39+
import { NotebookRuntime } from "./src/kit/runtime.ts";
40+
import { registerFile } from "@observablehq/notebook-kit/runtime";
41+
42+
registerFile("AMZN.csv", { path: "./tests/AMZN.csv", mimeType: "text/csv" });
43+
44+
const ojs = `
45+
data = FileAttachment("AMZN.csv").csv({ typed: true })
46+
md\`Loaded \${data.length} rows from AMZN.csv. Columns: \${data.columns.join(", ")}\`
47+
md\`First row: Date=\${data[0].Date}, Close=\${data[0].Close}\`
48+
`;
49+
50+
const element = document.getElementById("placeholder-fileattachment");
51+
const notebook = ojs2notebookKit(ojs);
52+
const definitions = compileNotebook(notebook);
53+
const runtime = new NotebookRuntime();
54+
runtime.render(definitions, element);
55+
</script>
3456
</body>
3557

3658
</html>

packages/observablehq-compiler/index-preview.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<script type="importmap">
2727
{
2828
"imports": {
29-
"@observablehq/runtime": "https://cdn.jsdelivr.net/npm/@observablehq/runtime@5.9.9/dist/runtime.js",
29+
"@observablehq/runtime": "https://cdn.jsdelivr.net/npm/@observablehq/runtime@6.0.0/dist/runtime.js",
3030

3131
"@hpcc-js/observablehq-compiler": "../observablehq-compiler/dist/index.js"
3232
}
@@ -38,7 +38,9 @@
3838
<h1>ESM Quick Test</h1>
3939
<div id="placeholder"></div>
4040
<script type="module">
41-
import { Library, Runtime, Inspector } from "@observablehq/runtime";
41+
import { Runtime } from "@observablehq/runtime";
42+
import { Inspector } from "@observablehq/inspector";
43+
import { Library } from "@observablehq/stdlib";
4244
import { omd2notebook, compile } from "@hpcc-js/observablehq-compiler";
4345

4446
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 { Runtime } from "@observablehq/runtime";
33+
import { Inspector } from "@observablehq/inspector";
34+
import { Library } from "@observablehq/stdlib";
3335
import { omd2notebook, compile } from "./src/index.ts";
3436

3537
const md = `\

packages/observablehq-compiler/package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"ojscc": "bin/ojscc.mjs"
4040
},
4141
"scripts": {
42-
"clean": "rimraf --glob lib* types dist *.tsbuildinfo .turbo",
42+
"clean": "rimraf --glob lib* types dist dist-test *.tsbuildinfo .turbo",
4343
"bundle-node": "node ./esbuild.js",
4444
"bundle-browser": "vite build",
4545
"bundle": "run-s bundle-browser bundle-node",
@@ -63,15 +63,17 @@
6363
"update-major": "npx --yes npm-check-updates -u"
6464
},
6565
"dependencies": {
66-
"jsdom": "26.1.0",
66+
"jsdom": "29.1.1",
6767
"yargs": "18.0.0"
6868
},
6969
"devDependencies": {
7070
"@hpcc-js/esbuild-plugins": "^1.8.10",
7171
"@observablehq/notebook-kit": "1.7.9",
7272
"@observablehq/parser": "6.1.0",
73-
"@observablehq/runtime": "5.9.9",
74-
"@types/jsdom": "21.1.7"
73+
"@observablehq/inspector": "5.0.1",
74+
"@observablehq/stdlib": "5.8.8",
75+
"@observablehq/runtime": "6.0.0",
76+
"@types/jsdom": "28.0.3"
7577
},
7678
"repository": {
7779
"type": "git",

packages/observablehq-compiler/src/compiler.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
21
import { type Notebook, type Definition, compileNotebook, fixRelativeUrl, isRelativePath, obfuscatedImport } from "./kit/index.ts";
2+
import { FileAttachments } from "@observablehq/stdlib";
33
import { ohq, splitModule } from "./observable-shim.ts";
44
import { parseCell, ParsedImportCell } from "./cst.ts";
55
import { Writer } from "./writer.ts";
@@ -272,8 +272,11 @@ export function notebook(_files: ohq.File[] = [], _cells: CellFunc[] = [], { bas
272272
const cells = new Map<string | number, CellFunc>(_cells.map(c => [c.id, c]));
273273

274274
const retVal = (runtime: ohq.Runtime, inspector?: InspectorFactoryEx): ohq.Module => {
275+
if (!runtime.fileAttachments) {
276+
runtime.fileAttachments = FileAttachments;
277+
}
275278
const main = runtime.module();
276-
main.builtin("FileAttachment", runtime.fileAttachments(name => {
279+
main.builtin("FileAttachment", FileAttachments(name => {
277280
return fileAttachments.get(name) ?? { url: new URL(fixRelativeUrl(name, baseUrl)), mimeType: null };
278281
}));
279282
main.builtin("fetchEx", fetchEx);

packages/observablehq-compiler/tests/esm-import.browser.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, it, expect } from "vitest";
2-
import { Library, Runtime } from "@observablehq/runtime";
2+
import { Runtime } from "@observablehq/runtime";
3+
import { Library } from "@observablehq/stdlib";
34
import { compile, ohq } from "@hpcc-js/observablehq-compiler";
45

56
describe("observablehq-compiler", function () {

packages/observablehq-compiler/tests/index-notebookkit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ value
332332
`;
333333

334334
const notebook = omd2notebookKit(omd2);
335-
const definitions = compileKit(notebook);
335+
const definitions = compileNotebook(notebook);
336336
const runtime = new NotebookRuntime();
337337
runtime.render(definitions, element);
338338
}
@@ -358,7 +358,7 @@ mo = 38 + 40;
358358
`;
359359

360360
const notebook = ojs2notebookKit(ojs);
361-
const definitions = compileKit(notebook);
361+
const definitions = compileNotebook(notebook);
362362
const runtime = new NotebookRuntime();
363363
runtime.render(definitions, element);
364364
}

packages/observablehq-compiler/tests/simple.browser.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { Library, Runtime, Inspector } from "@observablehq/runtime";
1+
import { Runtime } from "@observablehq/runtime";
2+
import { Inspector } from "@observablehq/inspector";
3+
import { Library } from "@observablehq/stdlib";
24
import { describe, it, expect } from "vitest";
35
import { compile, download, ohq, ojs2notebook } from "@hpcc-js/observablehq-compiler";
46
import { fa } from "./File Attachments.ts";

0 commit comments

Comments
 (0)