@hpcc-js/observablehq-compiler 1.5.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/LICENSE +43 -43
- package/README.md +105 -105
- package/bin/ojscc.mjs +73 -73
- package/dist/index.esm.js +49 -49
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js.map +1 -1
- package/dist/index.js +49 -49
- package/dist/index.js.map +1 -1
- package/dist/index.min.js.map +1 -1
- package/package.json +3 -3
- package/src/__package__.ts +3 -3
- package/src/__tests__/File Attachments.ts +894 -894
- package/src/__tests__/Introduction to Imports.ts +748 -748
- package/src/__tests__/Observable TimeChart.ts +771 -771
- package/src/__tests__/index.ts +13 -13
- package/src/__tests__/m1.mjs +3 -3
- package/src/__tests__/node.ts +199 -199
- package/src/compiler.md +234 -234
- package/src/compiler.ts +311 -311
- package/src/cst.ts +178 -178
- package/src/index.css +459 -459
- package/src/index.ts +6 -6
- package/src/util.md +113 -113
- package/src/util.ts +175 -175
- package/src/writer.ts +80 -80
package/src/compiler.md
CHANGED
|
@@ -1,234 +1,234 @@
|
|
|
1
|
-
# Compiler
|
|
2
|
-
|
|
3
|
-
Observable HQ Notebook Compiler and Interpreter
|
|
4
|
-
|
|
5
|
-
## Minimal JSON Notebook
|
|
6
|
-
|
|
7
|
-
While the compiler supports and persists the entire Observable HQ Notebook (v3), it only needs a minimal subset to actually function:
|
|
8
|
-
|
|
9
|
-
```ts
|
|
10
|
-
interface Notebook {
|
|
11
|
-
files: File[];
|
|
12
|
-
nodes: Node[];
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
interface Node {
|
|
16
|
-
id: number | string;
|
|
17
|
-
mode: "js" | "md";
|
|
18
|
-
value: string;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
interface File {
|
|
22
|
-
name: string;
|
|
23
|
-
url: string;
|
|
24
|
-
mime_type?: string;
|
|
25
|
-
}
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
---
|
|
29
|
-
|
|
30
|
-
<a name="compile" href="#compile">#</a> **compile**(_notebook_) => Promise\<function\> · [<>](https://github.com/hpcc-systems/Visualization/blob/trunk/packages/observablehq/compiler/src/compiler.ts "Source")
|
|
31
|
-
|
|
32
|
-
* _notebook_: JSON Notebook, see [utilities](./util) for examples on how to fetch or create a notebook.
|
|
33
|
-
* _returns_: Returns a Promise to a "define" function that is compatible with the ["@observablehq/runtime"](https://github.com/observablehq/runtime) and ["@observablehq/inspector"](https://github.com/observablehq/inspector)
|
|
34
|
-
|
|
35
|
-
## Hello World Example
|
|
36
|
-
|
|
37
|
-
First take a simple hello world notebook
|
|
38
|
-
|
|
39
|
-
```js
|
|
40
|
-
const notebook = {
|
|
41
|
-
files: [],
|
|
42
|
-
nodes: [
|
|
43
|
-
{
|
|
44
|
-
id: 1,
|
|
45
|
-
value: "md`# ${h} ${w}`",
|
|
46
|
-
mode: "js"
|
|
47
|
-
},
|
|
48
|
-
{
|
|
49
|
-
id: 2,
|
|
50
|
-
value: "h = \"Hello\"",
|
|
51
|
-
mode: "js"
|
|
52
|
-
},
|
|
53
|
-
{
|
|
54
|
-
id: 3,
|
|
55
|
-
value: "w = \"World\"",
|
|
56
|
-
mode: "js"
|
|
57
|
-
}
|
|
58
|
-
]
|
|
59
|
-
}
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
Then import the compiler and invoke it:
|
|
63
|
-
|
|
64
|
-
```js
|
|
65
|
-
import { compile } from "@hpcc-js/observablehq-compiler";
|
|
66
|
-
|
|
67
|
-
const compiledNotebook = await compile(notebook);
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
To render it to a web page, simply follow the same steps as if you had downloaded a compiled version from ObservableHQ site:
|
|
71
|
-
|
|
72
|
-
```js
|
|
73
|
-
|
|
74
|
-
import { Library, Runtime, Inspector } from "https://cdn.skypack.dev/@observablehq/runtime";
|
|
75
|
-
|
|
76
|
-
const placeholder = document.getElementById("placeholder");
|
|
77
|
-
|
|
78
|
-
const library = new Library();
|
|
79
|
-
const runtime = new Runtime(library);
|
|
80
|
-
|
|
81
|
-
compiledNotebook(runtime, name => {
|
|
82
|
-
const div = document.createElement("div");
|
|
83
|
-
placeholder.appendChild(div);
|
|
84
|
-
return new Inspector(div);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
Putting it all together:
|
|
90
|
-
|
|
91
|
-
<ClientOnly>
|
|
92
|
-
<hpcc-vitepress preview_height_ratio=0.4 style="width:100%;height:400px">
|
|
93
|
-
<div id="placeholder" style="height:100%;overflow-y:scroll">
|
|
94
|
-
</div>
|
|
95
|
-
<script type="module">
|
|
96
|
-
import { Library, Runtime, Inspector } from "https://cdn.skypack.dev/@observablehq/runtime";
|
|
97
|
-
import { compile } from "@hpcc-js/observablehq-compiler";
|
|
98
|
-
|
|
99
|
-
const placeholder = document.getElementById("placeholder");
|
|
100
|
-
|
|
101
|
-
const notebook = {
|
|
102
|
-
files: [],
|
|
103
|
-
nodes: [
|
|
104
|
-
{
|
|
105
|
-
id: 1,
|
|
106
|
-
value: "md`# ${h} ${w}`",
|
|
107
|
-
mode: "js"
|
|
108
|
-
},
|
|
109
|
-
{
|
|
110
|
-
id: 2,
|
|
111
|
-
value: "h = \"Hello\"",
|
|
112
|
-
mode: "js"
|
|
113
|
-
},
|
|
114
|
-
{
|
|
115
|
-
id: 3,
|
|
116
|
-
value: "w = \"World\"",
|
|
117
|
-
mode: "js"
|
|
118
|
-
}
|
|
119
|
-
]
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const compiledNotebook = await compile(notebook);
|
|
123
|
-
|
|
124
|
-
const library = new Library();
|
|
125
|
-
const runtime = new Runtime(library);
|
|
126
|
-
compiledNotebook(runtime, name => {
|
|
127
|
-
const div = document.createElement("div");
|
|
128
|
-
placeholder.appendChild(div);
|
|
129
|
-
return new Inspector(div);
|
|
130
|
-
});
|
|
131
|
-
</script>
|
|
132
|
-
</hpcc-vitepress>
|
|
133
|
-
</ClientOnly>
|
|
134
|
-
|
|
135
|
-
---
|
|
136
|
-
|
|
137
|
-
To output the generated code simply call `toString` on the compiled function:
|
|
138
|
-
|
|
139
|
-
<ClientOnly>
|
|
140
|
-
<hpcc-vitepress style="width:100%;height:600px">
|
|
141
|
-
<hpcc-codemirror id="placeholder" mode="javascript" theme="dark" style="width:100%;height:100%">
|
|
142
|
-
</hpcc-codemirror>
|
|
143
|
-
<script type="module">
|
|
144
|
-
import "@hpcc-js/wc-editor";
|
|
145
|
-
import { compile } from "@hpcc-js/observablehq-compiler";
|
|
146
|
-
|
|
147
|
-
const notebook = {
|
|
148
|
-
files: [],
|
|
149
|
-
nodes: [
|
|
150
|
-
{
|
|
151
|
-
id: 1,
|
|
152
|
-
value: "md`# ${h} ${w}`",
|
|
153
|
-
mode: "js"
|
|
154
|
-
},
|
|
155
|
-
{
|
|
156
|
-
id: 2,
|
|
157
|
-
value: "h = \"Hello\"",
|
|
158
|
-
mode: "js"
|
|
159
|
-
},
|
|
160
|
-
{
|
|
161
|
-
id: 3,
|
|
162
|
-
value: "w = \"World\"",
|
|
163
|
-
mode: "js"
|
|
164
|
-
}
|
|
165
|
-
]
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
const compiledNotebook = await compile(notebook);
|
|
169
|
-
const placeholder = document.getElementById("placeholder");
|
|
170
|
-
placeholder.text = compiledNotebook.toString();
|
|
171
|
-
</script>
|
|
172
|
-
</hpcc-vitepress>
|
|
173
|
-
</ClientOnly>
|
|
174
|
-
|
|
175
|
-
---
|
|
176
|
-
|
|
177
|
-
## MoreExamples
|
|
178
|
-
|
|
179
|
-
* [@observablehq/plot](https://observablehq.com/@observablehq/plot)
|
|
180
|
-
|
|
181
|
-
<ClientOnly>
|
|
182
|
-
<hpcc-vitepress style="width:100%;height:600px">
|
|
183
|
-
<div id="placeholder" style="height:400px;overflow-y:scroll">
|
|
184
|
-
</div>
|
|
185
|
-
<script type="module">
|
|
186
|
-
import { Library, Runtime, Inspector } from "https://cdn.skypack.dev/@observablehq/runtime";
|
|
187
|
-
import { download, compile } from "@hpcc-js/observablehq-compiler";
|
|
188
|
-
|
|
189
|
-
const notebookUrl = "https://observablehq.com/@observablehq/plot";
|
|
190
|
-
const placeholder = document.getElementById("placeholder");
|
|
191
|
-
|
|
192
|
-
const notebook = await download(notebookUrl);
|
|
193
|
-
const compiledNB = await compile(notebook);
|
|
194
|
-
|
|
195
|
-
const library = new Library();
|
|
196
|
-
const runtime = new Runtime(library);
|
|
197
|
-
compiledNB(runtime, name => {
|
|
198
|
-
const div = document.createElement("div");
|
|
199
|
-
placeholder.appendChild(div);
|
|
200
|
-
return new Inspector(div);
|
|
201
|
-
});
|
|
202
|
-
</script>
|
|
203
|
-
</hpcc-vitepress>
|
|
204
|
-
</ClientOnly>
|
|
205
|
-
|
|
206
|
-
---
|
|
207
|
-
|
|
208
|
-
* [@mbostock/fullscreen-canvas](https://observablehq.com/@mbostock/fullscreen-canvas)
|
|
209
|
-
|
|
210
|
-
<ClientOnly>
|
|
211
|
-
<hpcc-vitepress style="width:100%;height:600px">
|
|
212
|
-
<div id="placeholder" style="height:400px;overflow-y:scroll">
|
|
213
|
-
</div>
|
|
214
|
-
<script type="module">
|
|
215
|
-
import { Library, Runtime, Inspector } from "https://cdn.skypack.dev/@observablehq/runtime";
|
|
216
|
-
import { download, compile } from "@hpcc-js/observablehq-compiler";
|
|
217
|
-
|
|
218
|
-
const notebookUrl = "https://observablehq.com/@mbostock/fullscreen-canvas";
|
|
219
|
-
const placeholder = document.getElementById("placeholder");
|
|
220
|
-
|
|
221
|
-
const notebook = await download(notebookUrl);
|
|
222
|
-
const compiledNB = await compile(notebook);
|
|
223
|
-
|
|
224
|
-
const library = new Library();
|
|
225
|
-
const runtime = new Runtime(library);
|
|
226
|
-
compiledNB(runtime, name => {
|
|
227
|
-
const div = document.createElement("div");
|
|
228
|
-
placeholder.appendChild(div);
|
|
229
|
-
return new Inspector(div);
|
|
230
|
-
});
|
|
231
|
-
</script>
|
|
232
|
-
</hpcc-vitepress>
|
|
233
|
-
</ClientOnly>
|
|
234
|
-
|
|
1
|
+
# Compiler
|
|
2
|
+
|
|
3
|
+
Observable HQ Notebook Compiler and Interpreter
|
|
4
|
+
|
|
5
|
+
## Minimal JSON Notebook
|
|
6
|
+
|
|
7
|
+
While the compiler supports and persists the entire Observable HQ Notebook (v3), it only needs a minimal subset to actually function:
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
interface Notebook {
|
|
11
|
+
files: File[];
|
|
12
|
+
nodes: Node[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface Node {
|
|
16
|
+
id: number | string;
|
|
17
|
+
mode: "js" | "md";
|
|
18
|
+
value: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface File {
|
|
22
|
+
name: string;
|
|
23
|
+
url: string;
|
|
24
|
+
mime_type?: string;
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
<a name="compile" href="#compile">#</a> **compile**(_notebook_) => Promise\<function\> · [<>](https://github.com/hpcc-systems/Visualization/blob/trunk/packages/observablehq/compiler/src/compiler.ts "Source")
|
|
31
|
+
|
|
32
|
+
* _notebook_: JSON Notebook, see [utilities](./util) for examples on how to fetch or create a notebook.
|
|
33
|
+
* _returns_: Returns a Promise to a "define" function that is compatible with the ["@observablehq/runtime"](https://github.com/observablehq/runtime) and ["@observablehq/inspector"](https://github.com/observablehq/inspector)
|
|
34
|
+
|
|
35
|
+
## Hello World Example
|
|
36
|
+
|
|
37
|
+
First take a simple hello world notebook
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
const notebook = {
|
|
41
|
+
files: [],
|
|
42
|
+
nodes: [
|
|
43
|
+
{
|
|
44
|
+
id: 1,
|
|
45
|
+
value: "md`# ${h} ${w}`",
|
|
46
|
+
mode: "js"
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
id: 2,
|
|
50
|
+
value: "h = \"Hello\"",
|
|
51
|
+
mode: "js"
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
id: 3,
|
|
55
|
+
value: "w = \"World\"",
|
|
56
|
+
mode: "js"
|
|
57
|
+
}
|
|
58
|
+
]
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Then import the compiler and invoke it:
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
import { compile } from "@hpcc-js/observablehq-compiler";
|
|
66
|
+
|
|
67
|
+
const compiledNotebook = await compile(notebook);
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
To render it to a web page, simply follow the same steps as if you had downloaded a compiled version from ObservableHQ site:
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
|
|
74
|
+
import { Library, Runtime, Inspector } from "https://cdn.skypack.dev/@observablehq/runtime";
|
|
75
|
+
|
|
76
|
+
const placeholder = document.getElementById("placeholder");
|
|
77
|
+
|
|
78
|
+
const library = new Library();
|
|
79
|
+
const runtime = new Runtime(library);
|
|
80
|
+
|
|
81
|
+
compiledNotebook(runtime, name => {
|
|
82
|
+
const div = document.createElement("div");
|
|
83
|
+
placeholder.appendChild(div);
|
|
84
|
+
return new Inspector(div);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Putting it all together:
|
|
90
|
+
|
|
91
|
+
<ClientOnly>
|
|
92
|
+
<hpcc-vitepress preview_height_ratio=0.4 style="width:100%;height:400px">
|
|
93
|
+
<div id="placeholder" style="height:100%;overflow-y:scroll">
|
|
94
|
+
</div>
|
|
95
|
+
<script type="module">
|
|
96
|
+
import { Library, Runtime, Inspector } from "https://cdn.skypack.dev/@observablehq/runtime";
|
|
97
|
+
import { compile } from "@hpcc-js/observablehq-compiler";
|
|
98
|
+
|
|
99
|
+
const placeholder = document.getElementById("placeholder");
|
|
100
|
+
|
|
101
|
+
const notebook = {
|
|
102
|
+
files: [],
|
|
103
|
+
nodes: [
|
|
104
|
+
{
|
|
105
|
+
id: 1,
|
|
106
|
+
value: "md`# ${h} ${w}`",
|
|
107
|
+
mode: "js"
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
id: 2,
|
|
111
|
+
value: "h = \"Hello\"",
|
|
112
|
+
mode: "js"
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
id: 3,
|
|
116
|
+
value: "w = \"World\"",
|
|
117
|
+
mode: "js"
|
|
118
|
+
}
|
|
119
|
+
]
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const compiledNotebook = await compile(notebook);
|
|
123
|
+
|
|
124
|
+
const library = new Library();
|
|
125
|
+
const runtime = new Runtime(library);
|
|
126
|
+
compiledNotebook(runtime, name => {
|
|
127
|
+
const div = document.createElement("div");
|
|
128
|
+
placeholder.appendChild(div);
|
|
129
|
+
return new Inspector(div);
|
|
130
|
+
});
|
|
131
|
+
</script>
|
|
132
|
+
</hpcc-vitepress>
|
|
133
|
+
</ClientOnly>
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
To output the generated code simply call `toString` on the compiled function:
|
|
138
|
+
|
|
139
|
+
<ClientOnly>
|
|
140
|
+
<hpcc-vitepress style="width:100%;height:600px">
|
|
141
|
+
<hpcc-codemirror id="placeholder" mode="javascript" theme="dark" style="width:100%;height:100%">
|
|
142
|
+
</hpcc-codemirror>
|
|
143
|
+
<script type="module">
|
|
144
|
+
import "@hpcc-js/wc-editor";
|
|
145
|
+
import { compile } from "@hpcc-js/observablehq-compiler";
|
|
146
|
+
|
|
147
|
+
const notebook = {
|
|
148
|
+
files: [],
|
|
149
|
+
nodes: [
|
|
150
|
+
{
|
|
151
|
+
id: 1,
|
|
152
|
+
value: "md`# ${h} ${w}`",
|
|
153
|
+
mode: "js"
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
id: 2,
|
|
157
|
+
value: "h = \"Hello\"",
|
|
158
|
+
mode: "js"
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
id: 3,
|
|
162
|
+
value: "w = \"World\"",
|
|
163
|
+
mode: "js"
|
|
164
|
+
}
|
|
165
|
+
]
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const compiledNotebook = await compile(notebook);
|
|
169
|
+
const placeholder = document.getElementById("placeholder");
|
|
170
|
+
placeholder.text = compiledNotebook.toString();
|
|
171
|
+
</script>
|
|
172
|
+
</hpcc-vitepress>
|
|
173
|
+
</ClientOnly>
|
|
174
|
+
|
|
175
|
+
---
|
|
176
|
+
|
|
177
|
+
## MoreExamples
|
|
178
|
+
|
|
179
|
+
* [@observablehq/plot](https://observablehq.com/@observablehq/plot)
|
|
180
|
+
|
|
181
|
+
<ClientOnly>
|
|
182
|
+
<hpcc-vitepress style="width:100%;height:600px">
|
|
183
|
+
<div id="placeholder" style="height:400px;overflow-y:scroll">
|
|
184
|
+
</div>
|
|
185
|
+
<script type="module">
|
|
186
|
+
import { Library, Runtime, Inspector } from "https://cdn.skypack.dev/@observablehq/runtime";
|
|
187
|
+
import { download, compile } from "@hpcc-js/observablehq-compiler";
|
|
188
|
+
|
|
189
|
+
const notebookUrl = "https://observablehq.com/@observablehq/plot";
|
|
190
|
+
const placeholder = document.getElementById("placeholder");
|
|
191
|
+
|
|
192
|
+
const notebook = await download(notebookUrl);
|
|
193
|
+
const compiledNB = await compile(notebook);
|
|
194
|
+
|
|
195
|
+
const library = new Library();
|
|
196
|
+
const runtime = new Runtime(library);
|
|
197
|
+
compiledNB(runtime, name => {
|
|
198
|
+
const div = document.createElement("div");
|
|
199
|
+
placeholder.appendChild(div);
|
|
200
|
+
return new Inspector(div);
|
|
201
|
+
});
|
|
202
|
+
</script>
|
|
203
|
+
</hpcc-vitepress>
|
|
204
|
+
</ClientOnly>
|
|
205
|
+
|
|
206
|
+
---
|
|
207
|
+
|
|
208
|
+
* [@mbostock/fullscreen-canvas](https://observablehq.com/@mbostock/fullscreen-canvas)
|
|
209
|
+
|
|
210
|
+
<ClientOnly>
|
|
211
|
+
<hpcc-vitepress style="width:100%;height:600px">
|
|
212
|
+
<div id="placeholder" style="height:400px;overflow-y:scroll">
|
|
213
|
+
</div>
|
|
214
|
+
<script type="module">
|
|
215
|
+
import { Library, Runtime, Inspector } from "https://cdn.skypack.dev/@observablehq/runtime";
|
|
216
|
+
import { download, compile } from "@hpcc-js/observablehq-compiler";
|
|
217
|
+
|
|
218
|
+
const notebookUrl = "https://observablehq.com/@mbostock/fullscreen-canvas";
|
|
219
|
+
const placeholder = document.getElementById("placeholder");
|
|
220
|
+
|
|
221
|
+
const notebook = await download(notebookUrl);
|
|
222
|
+
const compiledNB = await compile(notebook);
|
|
223
|
+
|
|
224
|
+
const library = new Library();
|
|
225
|
+
const runtime = new Runtime(library);
|
|
226
|
+
compiledNB(runtime, name => {
|
|
227
|
+
const div = document.createElement("div");
|
|
228
|
+
placeholder.appendChild(div);
|
|
229
|
+
return new Inspector(div);
|
|
230
|
+
});
|
|
231
|
+
</script>
|
|
232
|
+
</hpcc-vitepress>
|
|
233
|
+
</ClientOnly>
|
|
234
|
+
|