@hpcc-js/observablehq-compiler 1.3.0 → 3.1.0
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/bin/ojscc.mjs +13 -19
- package/dist/index.css +2 -1
- package/dist/index.css.map +7 -0
- package/dist/index.js +24 -8011
- package/dist/index.js.map +7 -1
- package/package.json +39 -50
- package/src/__package__.ts +2 -2
- package/src/compiler.ts +33 -21
- package/src/cst.ts +27 -23
- package/src/index.ts +3 -2
- package/src/util.ts +4 -1
- package/src/writer.ts +8 -5
- package/types/compiler.d.ts +6 -7
- package/types/cst.d.ts +0 -1
- package/types/index.d.ts +3 -3
- package/types/util.d.ts +1 -2
- package/types/writer.d.ts +1 -2
- package/dist/index.esm.css +0 -1
- package/dist/index.esm.js +0 -8011
- package/dist/index.esm.js.map +0 -1
- package/dist/index.esm.min.js +0 -4
- package/dist/index.esm.min.js.map +0 -1
- package/dist/index.min.js +0 -4
- package/dist/index.min.js.map +0 -1
- package/src/__tests__/File Attachments.ts +0 -895
- package/src/__tests__/Introduction to Imports.ts +0 -749
- package/src/__tests__/Observable TimeChart.ts +0 -772
- package/src/__tests__/index.ts +0 -13
- package/src/__tests__/m1.mjs +0 -3
- package/src/__tests__/node.ts +0 -199
- package/types/__package__.d.ts +0 -4
- package/types/__package__.d.ts.map +0 -1
- package/types/__tests__/File Attachments.d.ts +0 -110
- package/types/__tests__/File Attachments.d.ts.map +0 -1
- package/types/__tests__/Introduction to Imports.d.ts +0 -120
- package/types/__tests__/Introduction to Imports.d.ts.map +0 -1
- package/types/__tests__/Observable TimeChart.d.ts +0 -111
- package/types/__tests__/Observable TimeChart.d.ts.map +0 -1
- package/types/__tests__/index.d.ts +0 -2
- package/types/__tests__/index.d.ts.map +0 -1
- package/types/__tests__/node.d.ts +0 -2
- package/types/__tests__/node.d.ts.map +0 -1
- package/types/compiler.d.ts.map +0 -1
- package/types/cst.d.ts.map +0 -1
- package/types/index.d.ts.map +0 -1
- package/types/util.d.ts.map +0 -1
- package/types/writer.d.ts.map +0 -1
- package/types-3.4/__package__.d.ts +0 -4
- package/types-3.4/__tests__/File Attachments.d.ts +0 -110
- package/types-3.4/__tests__/Introduction to Imports.d.ts +0 -120
- package/types-3.4/__tests__/Observable TimeChart.d.ts +0 -111
- package/types-3.4/__tests__/index.d.ts +0 -2
- package/types-3.4/__tests__/node.d.ts +0 -2
- package/types-3.4/compiler.d.ts +0 -112
- package/types-3.4/cst.d.ts +0 -42
- package/types-3.4/index.d.ts +0 -5
- package/types-3.4/util.d.ts +0 -30
- package/types-3.4/writer.d.ts +0 -19
package/package.json
CHANGED
|
@@ -1,74 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hpcc-js/observablehq-compiler",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "hpcc-js - ObservableHQ Compiler (unoffical)",
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
"interpreter",
|
|
11
|
-
"renderer"
|
|
12
|
-
],
|
|
13
|
-
"main": "dist/index.js",
|
|
14
|
-
"module": "dist/index.esm.js",
|
|
15
|
-
"browser": "dist/index.js",
|
|
16
|
-
"unpkg": "dist/index.min.js",
|
|
17
|
-
"jsdelivr": "dist/index.min.js",
|
|
18
|
-
"types": "types/index.d.ts",
|
|
19
|
-
"typesVersions": {
|
|
20
|
-
"<3.8": {
|
|
21
|
-
"*": [
|
|
22
|
-
"types-3.4/index.d.ts"
|
|
23
|
-
]
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./types/index.d.ts",
|
|
9
|
+
"default": "./dist/index.js"
|
|
24
10
|
}
|
|
25
11
|
},
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
},
|
|
12
|
+
"main": "./dist/index.js",
|
|
13
|
+
"types": "./types/index.d.ts",
|
|
29
14
|
"files": [
|
|
30
15
|
"dist/*",
|
|
31
|
-
"
|
|
32
|
-
"types
|
|
33
|
-
"src/*"
|
|
16
|
+
"src/*",
|
|
17
|
+
"types/*"
|
|
34
18
|
],
|
|
19
|
+
"bin": {
|
|
20
|
+
"ojscc": "bin/ojscc.mjs"
|
|
21
|
+
},
|
|
35
22
|
"scripts": {
|
|
36
|
-
"clean": "rimraf --glob lib* dist
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"bundle-watch": "npm run bundle -- -w",
|
|
43
|
-
"minimize-index": "terser dist/index.js -c -m --source-map \"content='dist/index.js.map',url='index.min.js.map'\" -o dist/index.min.js",
|
|
44
|
-
"minimize-index-es6": "terser dist/index.esm.js -c -m --source-map \"content='dist/index.esm.js.map',url='index.esm.min.js.map'\" -o dist/index.esm.min.js",
|
|
45
|
-
"minimize": "run-p minimize-index minimize-index-es6",
|
|
46
|
-
"gen-legacy-types": "downlevel-dts ./types ./types-3.4",
|
|
47
|
-
"build": "npm run compile-es6 && npm run bundle",
|
|
48
|
-
"watch": "npm-run-all compile-es6 -p compile-es6-watch bundle-watch",
|
|
23
|
+
"clean": "rimraf --glob lib* types dist dist-test *.tsbuildinfo .turbo",
|
|
24
|
+
"bundle": "node esbuild.js",
|
|
25
|
+
"bundle-watch": "npm run bundle -- --development --watch",
|
|
26
|
+
"gen-types": "tsc --project tsconfig.json",
|
|
27
|
+
"gen-types-watch": "npm run gen-types -- --watch",
|
|
28
|
+
"build": "run-p gen-types bundle",
|
|
49
29
|
"stamp": "node ../../node_modules/@hpcc-js/bundle/src/stamp.js",
|
|
50
30
|
"lint": "eslint ./src",
|
|
51
31
|
"docs": "typedoc --options tdoptions.json .",
|
|
52
|
-
"
|
|
53
|
-
"test-
|
|
54
|
-
"test-
|
|
55
|
-
"test": "
|
|
56
|
-
"
|
|
32
|
+
"test-browser": "vitest run --project browser",
|
|
33
|
+
"test-node": "vitest run --project node",
|
|
34
|
+
"test-both": "vitest run",
|
|
35
|
+
"test-bin": "node ./bin/ojscc.mjs --version",
|
|
36
|
+
"test": "run-p test-both test-bin",
|
|
37
|
+
"coverage": "vitest run --coverage",
|
|
38
|
+
"update": "npx -y npm-check-updates -u -t minor",
|
|
39
|
+
"update-major": "npx -y npm-check-updates -u"
|
|
57
40
|
},
|
|
58
41
|
"dependencies": {
|
|
59
|
-
"@hpcc-js/observable-shim": "^2.6.0",
|
|
60
|
-
"node-fetch": "3.3.2",
|
|
61
42
|
"yargs": "17.7.2"
|
|
62
43
|
},
|
|
63
44
|
"devDependencies": {
|
|
64
|
-
"@hpcc-js/
|
|
65
|
-
"@
|
|
66
|
-
"
|
|
45
|
+
"@hpcc-js/esbuild-plugins": "^1.2.0",
|
|
46
|
+
"@hpcc-js/observable-shim": "^3.1.0",
|
|
47
|
+
"@observablehq/runtime": "5.9.9"
|
|
67
48
|
},
|
|
68
49
|
"repository": {
|
|
69
50
|
"type": "git",
|
|
70
51
|
"url": "git+https://github.com/hpcc-systems/Visualization.git"
|
|
71
52
|
},
|
|
53
|
+
"keywords": [
|
|
54
|
+
"observablehq",
|
|
55
|
+
"markdown",
|
|
56
|
+
"observable",
|
|
57
|
+
"compiler",
|
|
58
|
+
"interpreter",
|
|
59
|
+
"renderer"
|
|
60
|
+
],
|
|
72
61
|
"author": "Gordon Smith <gordonjsmith@gmail.com>",
|
|
73
62
|
"contributors": [],
|
|
74
63
|
"license": "Apache-2.0",
|
|
@@ -76,5 +65,5 @@
|
|
|
76
65
|
"url": "https://github.com/hpcc-systems/Visualization/issues"
|
|
77
66
|
},
|
|
78
67
|
"homepage": "https://github.com/hpcc-systems/Visualization/tree/trunk/packages/observablehq-compiler",
|
|
79
|
-
"gitHead": "
|
|
68
|
+
"gitHead": "1fc4d0ad31fe8e30ae5348fdee67284df4c928ed"
|
|
80
69
|
}
|
package/src/__package__.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export const PKG_NAME = "@hpcc-js/observablehq-compiler";
|
|
2
|
-
export const PKG_VERSION = "1.
|
|
3
|
-
export const BUILD_VERSION = "2.
|
|
2
|
+
export const PKG_VERSION = "3.1.0";
|
|
3
|
+
export const BUILD_VERSION = "2.107.0";
|
package/src/compiler.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { ohq, splitModule } from "@hpcc-js/observable-shim";
|
|
2
|
-
import { parseCell, ParsedImportCell } from "./cst";
|
|
3
|
-
import { Writer } from "./writer";
|
|
4
|
-
import { fixRelativeUrl, isRelativePath, encodeBacktick, fetchEx, obfuscatedImport, ojs2notebook, omd2notebook } from "./util";
|
|
2
|
+
import { parseCell, ParsedImportCell } from "./cst.ts";
|
|
3
|
+
import { Writer } from "./writer.ts";
|
|
4
|
+
import { fixRelativeUrl, isRelativePath, encodeBacktick, fetchEx, obfuscatedImport, ojs2notebook, omd2notebook } from "./util.ts";
|
|
5
5
|
|
|
6
6
|
// Inspector Factory ---
|
|
7
7
|
export type InspectorFactoryEx = (name: string | undefined, id: string | number) => Inspector;
|
|
8
8
|
|
|
9
9
|
export interface Inspector {
|
|
10
10
|
_node?: HTMLDivElement;
|
|
11
|
-
pending();
|
|
12
|
-
fulfilled(value);
|
|
13
|
-
rejected(error);
|
|
11
|
+
pending(): void;
|
|
12
|
+
fulfilled(value: any): void;
|
|
13
|
+
rejected(error: Error): void;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
// Module ---
|
|
@@ -31,6 +31,9 @@ async function importFile(relativePath: string, baseUrl: string) {
|
|
|
31
31
|
notebook = ojs2notebook(content);
|
|
32
32
|
} else if (relativePath.endsWith(".omd")) {
|
|
33
33
|
notebook = omd2notebook(content);
|
|
34
|
+
} else {
|
|
35
|
+
console.warn(`Unknown file type: ${relativePath}, assuming .ojsnb`);
|
|
36
|
+
notebook = JSON.parse(content);
|
|
34
37
|
}
|
|
35
38
|
const retVal: ImportDefine = compile(notebook, { baseUrl }) as any;
|
|
36
39
|
retVal.delete = () => { };
|
|
@@ -44,7 +47,7 @@ async function importFile(relativePath: string, baseUrl: string) {
|
|
|
44
47
|
async function importCompiledNotebook(partial: string) {
|
|
45
48
|
const url = `https://api.observablehq.com/${partial[0] === "@" ? partial : `d/${partial}`}.js?v=3`;
|
|
46
49
|
let impMod = {
|
|
47
|
-
default: function (runtime: ohq.Runtime, inspector?: InspectorFactoryEx): ohq.Module {
|
|
50
|
+
default: function (runtime: ohq.Runtime, inspector?: InspectorFactoryEx): ohq.Module | undefined {
|
|
48
51
|
return undefined;
|
|
49
52
|
} as any
|
|
50
53
|
};
|
|
@@ -80,7 +83,7 @@ async function importNotebook(partial: string) {
|
|
|
80
83
|
|
|
81
84
|
async function createModule(node: ohq.Node, parsed: ParsedImportCell, text: string, { baseUrl, importMode }: CompileOptions) {
|
|
82
85
|
const otherModule = isRelativePath(parsed.src) ?
|
|
83
|
-
await importFile(parsed.src, baseUrl) :
|
|
86
|
+
await importFile(parsed.src, baseUrl ?? "") :
|
|
84
87
|
importMode === "recursive" ?
|
|
85
88
|
await importNotebook(parsed.src) :
|
|
86
89
|
await importCompiledNotebook(parsed.src);
|
|
@@ -123,11 +126,13 @@ type ModuleFunc = Awaited<ReturnType<typeof createModule>>;
|
|
|
123
126
|
// Variable ---
|
|
124
127
|
function createVariable(node: ohq.Node, inspect: boolean, name?: string, inputs?: string[], definition?: any, inline = false) {
|
|
125
128
|
|
|
126
|
-
let i: ohq.Inspector;
|
|
127
|
-
let v: ohq.Variable;
|
|
129
|
+
let i: ohq.Inspector | undefined;
|
|
130
|
+
let v: ohq.Variable | undefined;
|
|
128
131
|
|
|
129
132
|
const retVal = (module: ohq.Module, inspector?: InspectorFactoryEx) => {
|
|
130
|
-
|
|
133
|
+
if (inspect && inspector) {
|
|
134
|
+
i = inspector(name, node.id);
|
|
135
|
+
}
|
|
131
136
|
v = module.variable(i);
|
|
132
137
|
if (arguments.length > 1) {
|
|
133
138
|
try {
|
|
@@ -137,9 +142,9 @@ function createVariable(node: ohq.Node, inspect: boolean, name?: string, inputs?
|
|
|
137
142
|
}
|
|
138
143
|
}
|
|
139
144
|
if (node.pinned) {
|
|
140
|
-
v = module.variable(inspector(name, node.id));
|
|
145
|
+
v = inspector ? module.variable(inspector(name, node.id)) : module.variable();
|
|
141
146
|
try {
|
|
142
|
-
v.define(undefined, ["md"], md => {
|
|
147
|
+
v.define(undefined, ["md"], (md: any) => {
|
|
143
148
|
return md`\`\`\`js
|
|
144
149
|
${node.value}
|
|
145
150
|
\`\`\``;
|
|
@@ -174,13 +179,17 @@ ${node.value}
|
|
|
174
179
|
}
|
|
175
180
|
type VariableFunc = ReturnType<typeof createVariable>;
|
|
176
181
|
|
|
177
|
-
function createImportVariable(name
|
|
182
|
+
function createImportVariable(name: string, alias?: string) {
|
|
178
183
|
|
|
179
184
|
let v: ohq.Variable;
|
|
180
185
|
|
|
181
186
|
const retVal = (main: ohq.Module, otherModule: ohq.Module) => {
|
|
182
187
|
v = main.variable();
|
|
183
|
-
|
|
188
|
+
if (alias === undefined) {
|
|
189
|
+
v.import(name, otherModule);
|
|
190
|
+
} else {
|
|
191
|
+
v.import(name, alias, otherModule);
|
|
192
|
+
}
|
|
184
193
|
};
|
|
185
194
|
|
|
186
195
|
retVal.delete = () => {
|
|
@@ -198,7 +207,7 @@ async function createCell(node: ohq.Node, options: CompileOptions) {
|
|
|
198
207
|
const text = node.mode && node.mode !== "js" ? `${node.mode}\`${encodeBacktick(node.value)}\`` : node.value;
|
|
199
208
|
const parsedModule = splitModule(text);
|
|
200
209
|
for (const cell of parsedModule) {
|
|
201
|
-
const parsed = parseCell(cell.text, options.baseUrl);
|
|
210
|
+
const parsed = parseCell(cell.text, options.baseUrl ?? "");
|
|
202
211
|
switch (parsed.type) {
|
|
203
212
|
case "import":
|
|
204
213
|
modules.push(await createModule(node, parsed, cell.text, options));
|
|
@@ -217,8 +226,8 @@ async function createCell(node: ohq.Node, options: CompileOptions) {
|
|
|
217
226
|
break;
|
|
218
227
|
}
|
|
219
228
|
}
|
|
220
|
-
} catch (e) {
|
|
221
|
-
variables.push(createVariable(node, true, undefined, [], e.message));
|
|
229
|
+
} catch (e: any) {
|
|
230
|
+
variables.push(createVariable(node, true, undefined, [], e.message ?? "Unkown error"));
|
|
222
231
|
}
|
|
223
232
|
|
|
224
233
|
const retVal = (runtime: ohq.Runtime, main: ohq.Module, inspector?: InspectorFactoryEx) => {
|
|
@@ -242,8 +251,11 @@ export type CellFunc = Awaited<ReturnType<typeof createCell>>;
|
|
|
242
251
|
|
|
243
252
|
// File ---
|
|
244
253
|
function createFile(file: ohq.File, options: CompileOptions): [string, any] {
|
|
245
|
-
function toString() {
|
|
246
|
-
|
|
254
|
+
function toString() {
|
|
255
|
+
// TODO Double check url should not be URL?
|
|
256
|
+
return (globalThis as any).url ?? "";
|
|
257
|
+
}
|
|
258
|
+
return [file.name, { url: new URL(fixRelativeUrl(file.url, options.baseUrl ?? "")), mimeType: file.mime_type, toString }];
|
|
247
259
|
}
|
|
248
260
|
type FileFunc = ReturnType<typeof createFile>;
|
|
249
261
|
|
|
@@ -277,7 +289,7 @@ export function notebook(_files: ohq.File[] = [], _cells: CellFunc[] = [], { bas
|
|
|
277
289
|
cells.set(cell.id, cell);
|
|
278
290
|
return cell;
|
|
279
291
|
};
|
|
280
|
-
retVal.get = (id: string | number): CellFunc => {
|
|
292
|
+
retVal.get = (id: string | number): CellFunc | undefined => {
|
|
281
293
|
return cells.get(id);
|
|
282
294
|
};
|
|
283
295
|
retVal.delete = (id: string | number): boolean => {
|
package/src/cst.ts
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
|
-
import { parseCell as ohqParseCell } from "@hpcc-js/observable-shim";
|
|
2
|
-
import { acorn, walk } from "@observablehq/parser";
|
|
3
|
-
import { ancestor } from "acorn-walk";
|
|
1
|
+
import { ancestor, parseCell as ohqParseCell, Cell, Node, walk, AncestorVisitors } from "@hpcc-js/observable-shim";
|
|
4
2
|
|
|
5
|
-
import { fixRelativeUrl, createFunction, Refs } from "./util";
|
|
3
|
+
import { fixRelativeUrl, createFunction, Refs } from "./util.ts";
|
|
6
4
|
|
|
7
|
-
function calcRefs(cellAst, cellStr): Refs {
|
|
5
|
+
function calcRefs(cellAst: Cell, cellStr: string): Refs {
|
|
8
6
|
if (cellAst.references === undefined) return { inputs: [], args: [], patches: [] };
|
|
9
7
|
|
|
10
|
-
const dedup = {};
|
|
11
|
-
cellAst.references.forEach(r => dedup[cellStr.substring(r.start, r.end)] = true);
|
|
8
|
+
const dedup: { [id: string]: boolean } = {};
|
|
9
|
+
cellAst.references.forEach((r: any) => dedup[cellStr.substring(r.start, r.end)] = true);
|
|
12
10
|
const retVal: Refs = {
|
|
13
11
|
inputs: Object.keys(dedup),
|
|
14
12
|
args: Object.keys(dedup).map(r => r.split(" ").join("_")),
|
|
15
13
|
patches: []
|
|
16
14
|
};
|
|
17
|
-
const pushPatch = (node, newText) => retVal.patches.push({ start: node.start - cellAst.body
|
|
15
|
+
const pushPatch = (node: Node, newText: string) => retVal.patches.push({ start: node.start - (cellAst.body?.start ?? 0), end: node.end - (cellAst.body?.start ?? 0), newText });
|
|
18
16
|
if (cellAst.body) {
|
|
19
17
|
ancestor(cellAst.body, {
|
|
20
18
|
Identifier(node) {
|
|
@@ -22,23 +20,23 @@ function calcRefs(cellAst, cellStr): Refs {
|
|
|
22
20
|
if (dedup[value]) {
|
|
23
21
|
}
|
|
24
22
|
},
|
|
25
|
-
MutableExpression(node) {
|
|
23
|
+
MutableExpression(node: Node) {
|
|
26
24
|
const value = cellStr.substring(node.start, node.end);
|
|
27
25
|
const newText = value.split(" ").join("_") + ".value";
|
|
28
26
|
pushPatch(node, newText);
|
|
29
27
|
},
|
|
30
|
-
ViewExpression(node) {
|
|
28
|
+
ViewExpression(node: Node) {
|
|
31
29
|
const value = cellStr.substring(node.start, node.end);
|
|
32
30
|
const newText = value.split(" ").join("_");
|
|
33
31
|
pushPatch(node, newText);
|
|
34
32
|
},
|
|
35
|
-
ThisExpression(node, ancestors:
|
|
33
|
+
ThisExpression(node: Node, ancestors: Node[]) {
|
|
36
34
|
const value = cellStr.substring(node.start, node.end);
|
|
37
35
|
if (value === "this" && !ancestors.find(n => n.type === "FunctionExpression")) {
|
|
38
36
|
pushPatch(node, "((this === globalThis || this === globalThis.window)? undefined : this?.valueOf())");
|
|
39
37
|
}
|
|
40
38
|
}
|
|
41
|
-
} as
|
|
39
|
+
} as AncestorVisitors<any>, walk);
|
|
42
40
|
}
|
|
43
41
|
return retVal;
|
|
44
42
|
}
|
|
@@ -54,18 +52,18 @@ export interface ParsedImportCell extends ParsedCell {
|
|
|
54
52
|
injections: { name: string, alias: string }[];
|
|
55
53
|
}
|
|
56
54
|
|
|
57
|
-
function parseImportDeclaration(cellAst): ParsedImportCell {
|
|
55
|
+
function parseImportDeclaration(cellAst: any): ParsedImportCell {
|
|
58
56
|
return {
|
|
59
57
|
type: "import",
|
|
60
58
|
src: cellAst.body.source.value,
|
|
61
|
-
specifiers: cellAst.body.specifiers?.map(spec => {
|
|
59
|
+
specifiers: cellAst.body.specifiers?.map((spec: any) => {
|
|
62
60
|
return {
|
|
63
61
|
view: spec.view,
|
|
64
62
|
name: spec.imported.name,
|
|
65
63
|
alias: (spec.local?.name && spec.imported.name !== spec.local.name) ? spec.local.name : spec.imported.name
|
|
66
64
|
};
|
|
67
65
|
}) ?? [],
|
|
68
|
-
injections: cellAst.body.injections?.map(inj => {
|
|
66
|
+
injections: cellAst.body.injections?.map((inj: any) => {
|
|
69
67
|
return {
|
|
70
68
|
name: inj.imported.name,
|
|
71
69
|
alias: inj.local?.name ?? inj.imported.name
|
|
@@ -87,7 +85,7 @@ export interface ParsedViewCell extends ParsedCell {
|
|
|
87
85
|
variableValue: ParsedVariable;
|
|
88
86
|
}
|
|
89
87
|
|
|
90
|
-
function parseViewExpression(cellStr: string, cellAst, refs: Refs, bodyStr?: string): ParsedViewCell {
|
|
88
|
+
function parseViewExpression(cellStr: string, cellAst: any, refs: Refs, bodyStr?: string): ParsedViewCell {
|
|
91
89
|
const id = cellAst.id && cellStr.substring(cellAst.id.start, cellAst.id.end);
|
|
92
90
|
return {
|
|
93
91
|
type: "viewof",
|
|
@@ -101,7 +99,7 @@ function parseViewExpression(cellStr: string, cellAst, refs: Refs, bodyStr?: str
|
|
|
101
99
|
type: "variable",
|
|
102
100
|
id: cellAst?.id?.id?.name,
|
|
103
101
|
inputs: ["Generators", id],
|
|
104
|
-
func: (G, _) => G.input(_)
|
|
102
|
+
func: (G: any, _: any) => G.input(_)
|
|
105
103
|
}
|
|
106
104
|
};
|
|
107
105
|
}
|
|
@@ -113,7 +111,7 @@ interface ParsedMutableCell extends ParsedCell {
|
|
|
113
111
|
variableValue: ParsedVariable;
|
|
114
112
|
}
|
|
115
113
|
|
|
116
|
-
function parseMutableExpression(cellStr: string, cellAst, refs: Refs, bodyStr?: string): ParsedMutableCell {
|
|
114
|
+
function parseMutableExpression(cellStr: string, cellAst: any, refs: Refs, bodyStr?: string): ParsedMutableCell {
|
|
117
115
|
const id = cellAst.id && cellStr.substring(cellAst.id.start, cellAst.id.end);
|
|
118
116
|
const initialValueId = cellAst?.id?.id?.name;
|
|
119
117
|
const initialId = `initial ${initialValueId}`;
|
|
@@ -129,13 +127,13 @@ function parseMutableExpression(cellStr: string, cellAst, refs: Refs, bodyStr?:
|
|
|
129
127
|
type: "variable",
|
|
130
128
|
id,
|
|
131
129
|
inputs: ["Mutable", initialId],
|
|
132
|
-
func: (M, _) => new M(_)
|
|
130
|
+
func: (M: any, _: any) => new M(_)
|
|
133
131
|
},
|
|
134
132
|
variableValue: {
|
|
135
133
|
type: "variable",
|
|
136
134
|
id: initialValueId,
|
|
137
135
|
inputs: [id],
|
|
138
|
-
func: _ => _.generator
|
|
136
|
+
func: (_: any) => _.generator
|
|
139
137
|
}
|
|
140
138
|
};
|
|
141
139
|
}
|
|
@@ -147,7 +145,7 @@ interface ParsedVariableCell extends ParsedCell {
|
|
|
147
145
|
func: any,
|
|
148
146
|
}
|
|
149
147
|
|
|
150
|
-
function parseVariableExpression(cellStr: string, cellAst, refs: Refs, bodyStr?: string): ParsedVariableCell {
|
|
148
|
+
function parseVariableExpression(cellStr: string, cellAst: any, refs: Refs, bodyStr?: string): ParsedVariableCell {
|
|
151
149
|
return {
|
|
152
150
|
type: "variable",
|
|
153
151
|
id: cellAst.id && cellStr.substring(cellAst.id?.start, cellAst.id?.end),
|
|
@@ -163,10 +161,15 @@ export function parseCell(cellStr: string, baseUrl: string): ParsedImportCell |
|
|
|
163
161
|
case "ImportDeclaration":
|
|
164
162
|
return parseImportDeclaration(cellAst);
|
|
165
163
|
case "ImportExpression":
|
|
166
|
-
|
|
164
|
+
switch (cellAst.body.source.type) {
|
|
165
|
+
case "Literal":
|
|
166
|
+
bodyStr = `import("${fixRelativeUrl("" + cellAst.body.source.value, baseUrl)}")`;
|
|
167
|
+
break;
|
|
168
|
+
default:
|
|
169
|
+
console.error("Unexpected import value");
|
|
170
|
+
}
|
|
167
171
|
}
|
|
168
172
|
const refs = calcRefs(cellAst, cellStr);
|
|
169
|
-
|
|
170
173
|
switch (cellAst.id?.type) {
|
|
171
174
|
case "ViewExpression":
|
|
172
175
|
return parseViewExpression(cellStr, cellAst, refs, bodyStr);
|
|
@@ -176,3 +179,4 @@ export function parseCell(cellStr: string, baseUrl: string): ParsedImportCell |
|
|
|
176
179
|
return parseVariableExpression(cellStr, cellAst, refs, bodyStr);
|
|
177
180
|
}
|
|
178
181
|
}
|
|
182
|
+
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export type { ohq } from "@hpcc-js/observable-shim";
|
|
2
2
|
|
|
3
|
-
export * from "./compiler";
|
|
4
|
-
export { ojs2notebook, omd2notebook, download } from "./util";
|
|
3
|
+
export * from "./compiler.ts";
|
|
4
|
+
export { ojs2notebook, omd2notebook, download } from "./util.ts";
|
|
5
|
+
export * from "./writer.ts";
|
|
5
6
|
|
|
6
7
|
import "../src/index.css";
|
package/src/util.ts
CHANGED
|
@@ -41,7 +41,7 @@ export function createFunction(refs: Refs, async = false, generator = false, blo
|
|
|
41
41
|
`return (\n${body}\n);`);
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
function join(baseURL, relativeURL) {
|
|
44
|
+
function join(baseURL: string, relativeURL: string) {
|
|
45
45
|
return relativeURL
|
|
46
46
|
? baseURL.replace(/\/+$/, "") + "/" + relativeURL.replace(/^\/+/, "")
|
|
47
47
|
: baseURL;
|
|
@@ -159,6 +159,9 @@ export function omd2notebook(omd: string): ohq.Notebook {
|
|
|
159
159
|
|
|
160
160
|
export function fetchEx(url: string, proxyPrefix = "https://api.codetabs.com/v1/proxy/?quest=", proxyPostfix = "") {
|
|
161
161
|
const matches = url.match(/^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n?]+)/img);
|
|
162
|
+
if (!matches || matches.length === 0) {
|
|
163
|
+
throw new Error(`Invalid URL: ${url}`);
|
|
164
|
+
}
|
|
162
165
|
return fetch(url, { headers: { origin: matches[0], referer: url } }).then(response => {
|
|
163
166
|
if (response.ok) return response;
|
|
164
167
|
throw new Error("CORS?");
|
package/src/writer.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ohq } from "@hpcc-js/observable-shim";
|
|
2
|
-
import { ParsedImportCell, ParsedVariable } from "./cst";
|
|
2
|
+
import { ParsedImportCell, ParsedVariable } from "./cst.ts";
|
|
3
3
|
|
|
4
4
|
export class Writer {
|
|
5
5
|
|
|
@@ -43,14 +43,16 @@ export default function define(runtime, observer) {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
importDefine(imp: Partial<ParsedImportCell>) {
|
|
46
|
-
const
|
|
46
|
+
const impInjections = imp.injections ?? [];
|
|
47
|
+
const injections = impInjections.map(inj => {
|
|
47
48
|
return inj.name === inj.alias ?
|
|
48
49
|
`"${inj.name}"` :
|
|
49
50
|
`{name: "${inj.name}", alias: "${inj.alias}"}`;
|
|
50
51
|
});
|
|
51
|
-
const derive =
|
|
52
|
+
const derive = impInjections.length ? `.derive([${injections.join(", ")}], main)` : "";
|
|
52
53
|
this._defines.push(`const child${this._defineUid} = runtime.module(define${this._defineUid})${derive};`);
|
|
53
|
-
imp.specifiers
|
|
54
|
+
const impSpecifiers = imp.specifiers ?? [];
|
|
55
|
+
impSpecifiers.forEach(s => {
|
|
54
56
|
this._defines.push(`main.import("${s.name}"${s.alias && s.alias !== s.name ? `, "${s.alias}"` : ""}, child${this._defineUid}); `);
|
|
55
57
|
});
|
|
56
58
|
}
|
|
@@ -67,7 +69,8 @@ export default function define(runtime, observer) {
|
|
|
67
69
|
funcId = funcId ?? variable.id;
|
|
68
70
|
const observe = observable ? `.variable(observer(${variable.id ? JSON.stringify(variable.id) : ""}))` : "";
|
|
69
71
|
const id = variable.id ? `${JSON.stringify(variable.id)}, ` : "";
|
|
70
|
-
const
|
|
72
|
+
const variableInputs = variable.inputs ?? [];
|
|
73
|
+
const inputs = variableInputs.length ? `[${variableInputs.map(i => JSON.stringify(i)).join(", ")}], ` : "";
|
|
71
74
|
const func = inlineFunc ?
|
|
72
75
|
variable.func?.toString() :
|
|
73
76
|
funcId;
|
package/types/compiler.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { ohq } from "@hpcc-js/observable-shim";
|
|
2
|
-
import { Writer } from "./writer";
|
|
2
|
+
import { Writer } from "./writer.ts";
|
|
3
3
|
export type InspectorFactoryEx = (name: string | undefined, id: string | number) => Inspector;
|
|
4
4
|
export interface Inspector {
|
|
5
5
|
_node?: HTMLDivElement;
|
|
6
|
-
pending():
|
|
7
|
-
fulfilled(value: any):
|
|
8
|
-
rejected(error:
|
|
6
|
+
pending(): void;
|
|
7
|
+
fulfilled(value: any): void;
|
|
8
|
+
rejected(error: Error): void;
|
|
9
9
|
}
|
|
10
10
|
declare function createCell(node: ohq.Node, options: CompileOptions): Promise<{
|
|
11
11
|
(runtime: ohq.Runtime, main: ohq.Module, inspector?: InspectorFactoryEx): void;
|
|
@@ -66,7 +66,7 @@ export declare function notebook(_files?: ohq.File[], _cells?: CellFunc[], { bas
|
|
|
66
66
|
write(w: Writer): void;
|
|
67
67
|
}>;
|
|
68
68
|
set(n: ohq.Node): Promise<CellFunc>;
|
|
69
|
-
get(id: string | number): CellFunc;
|
|
69
|
+
get(id: string | number): CellFunc | undefined;
|
|
70
70
|
delete(id: string | number): boolean;
|
|
71
71
|
clear(): void;
|
|
72
72
|
write(w: Writer): void;
|
|
@@ -101,7 +101,7 @@ export declare function compile(notebookOrOjs: ohq.Notebook | string, { baseUrl,
|
|
|
101
101
|
write(w: Writer): void;
|
|
102
102
|
}>;
|
|
103
103
|
set(n: ohq.Node): Promise<CellFunc>;
|
|
104
|
-
get(id: string | number): CellFunc;
|
|
104
|
+
get(id: string | number): CellFunc | undefined;
|
|
105
105
|
delete(id: string | number): boolean;
|
|
106
106
|
clear(): void;
|
|
107
107
|
write(w: Writer): void;
|
|
@@ -109,4 +109,3 @@ export declare function compile(notebookOrOjs: ohq.Notebook | string, { baseUrl,
|
|
|
109
109
|
}>;
|
|
110
110
|
export type compileFunc = Awaited<ReturnType<typeof compile>>;
|
|
111
111
|
export {};
|
|
112
|
-
//# sourceMappingURL=compiler.d.ts.map
|
package/types/cst.d.ts
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export type { ohq } from "@hpcc-js/observable-shim";
|
|
2
|
-
export * from "./compiler";
|
|
3
|
-
export { ojs2notebook, omd2notebook, download } from "./util";
|
|
2
|
+
export * from "./compiler.ts";
|
|
3
|
+
export { ojs2notebook, omd2notebook, download } from "./util.ts";
|
|
4
|
+
export * from "./writer.ts";
|
|
4
5
|
import "../src/index.css";
|
|
5
|
-
//# sourceMappingURL=index.d.ts.map
|
package/types/util.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ export interface Refs {
|
|
|
11
11
|
}
|
|
12
12
|
export declare function createFunction(refs: Refs, async?: boolean, generator?: boolean, blockStatement?: boolean, body?: string): any;
|
|
13
13
|
export declare const isRelativePath: (path: string) => boolean;
|
|
14
|
-
export declare const fixRelativeUrl: (path: string, basePath: string) =>
|
|
14
|
+
export declare const fixRelativeUrl: (path: string, basePath: string) => string;
|
|
15
15
|
export declare function obfuscatedImport(url: string): Promise<any>;
|
|
16
16
|
interface ParsedOJS {
|
|
17
17
|
ojs: string;
|
|
@@ -27,4 +27,3 @@ export declare function omd2notebook(omd: string): ohq.Notebook;
|
|
|
27
27
|
export declare function fetchEx(url: string, proxyPrefix?: string, proxyPostfix?: string): Promise<Response>;
|
|
28
28
|
export declare function download(impUrl: string, proxyPrefix?: string, proxyPostfix?: string): Promise<ohq.Notebook>;
|
|
29
29
|
export {};
|
|
30
|
-
//# sourceMappingURL=util.d.ts.map
|
package/types/writer.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ohq } from "@hpcc-js/observable-shim";
|
|
2
|
-
import { ParsedImportCell, ParsedVariable } from "./cst";
|
|
2
|
+
import { ParsedImportCell, ParsedVariable } from "./cst.ts";
|
|
3
3
|
export declare class Writer {
|
|
4
4
|
protected _files: ohq.File[];
|
|
5
5
|
protected _imports: string[];
|
|
@@ -16,4 +16,3 @@ export declare class Writer {
|
|
|
16
16
|
define(variable: Partial<ParsedVariable>, observable?: boolean, inlineFunc?: boolean, funcId?: string): void;
|
|
17
17
|
error(msg: string): void;
|
|
18
18
|
}
|
|
19
|
-
//# sourceMappingURL=writer.d.ts.map
|
package/dist/index.esm.css
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
@import url("https://fonts.googleapis.com/css2?family=Source+Serif+Pro:ital,wght@0,400;0,600;0,700;1,400;1,600;1,700&display=swap");:root{--syntax-normal:#1b1e23;--syntax-comment:#828282;--syntax-number:#20a5ba;--syntax-keyword:#c30771;--syntax-atom:#10a778;--syntax-string:#008ec4;--syntax-error:#ffbedc;--syntax-unknown-variable:#838383;--syntax-known-variable:#005f87;--syntax-matchbracket:#20bbfc;--syntax-key:#6636b4;--mono-fonts:82%/1.5 Menlo,Consolas,monospace}.observablehq--collapsed,.observablehq--expanded,.observablehq--function,.observablehq--gray,.observablehq--import,.observablehq--string:after,.observablehq--string:before{color:var(--syntax-normal)}.observablehq--collapsed,.observablehq--expanded.observablehq--inspect a{cursor:pointer}.observablehq--field{margin-left:1em;text-indent:-1em}.observablehq--empty{color:var(--syntax_comment)}.observablehq--blue,.observablehq--keyword,a[href]{color:#3182bd}.hljs-deletion,.hljs-variable,.observablehq--forbidden,.observablehq--pink{color:#e377c2}.observablehq--orange{color:#e6550d}.hljs-literal,.observablehq--boolean,.observablehq--null,.observablehq--undefined{color:var(--syntax-atom)}.hljs-bullet,.hljs-link,.hljs-number,.hljs-regexp,.observablehq--bigint,.observablehq--date,.observablehq--green,.observablehq--number,.observablehq--regexp,.observablehq--symbol{color:var(--syntax-number)}.observablehq--index,.observablehq--key{color:var(--syntax-key)}.observablehq--prototype-key{color:#aaa}.observablehq--empty{font-style:oblique}.hljs-addition,.hljs-meta,.hljs-string,.hljs-symbol,.hljs-template-tag,.hljs-template-variable,.observablehq--purple,.observablehq--string{color:var(--syntax-string)}.observablehq--error,.observablehq--red{color:#e7040f}.observablehq--inspect,.observablehq:empty:after,.observablehq>link:only-child,.observablehq>style:only-child{display:block;font:var(--monospace-font);overflow-x:auto;padding:4px 0;white-space:pre}.observablehq--error .observablehq--inspect{white-space:pre-wrap;word-break:break-all}:root{--syntax-diff:#24292e;--syntax-diff-bg:#fff;--hr:rgba(0,0,0,.05);--monospace:Menlo,Consolas,monospace;--monospace-font:14px/1.5 var(--monospace);--serif:"Source Serif Pro","Iowan Old Style","Apple Garamond","Palatino Linotype","Times New Roman","Droid Serif",Times,serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";--sans-serif:-apple-system,BlinkMacSystemFont,"avenir next",avenir,helvetica,"helvetica neue",ubuntu,roboto,noto,"segoe ui",arial,sans-serif}html{-webkit-text-size-adjust:100%;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;color:#1b1e23;font:17px/1.5 var(--serif)}body{margin:0 14px}body.fullscreen{margin:0}h1,h2,h3,h4,h5,h6{color:#333;font-weight:700;line-height:1.15;margin-bottom:.25rem;margin-top:0}h2~p,h2~table,h3~p,h3~table,h4~p,h4~table{margin-top:0}.observablehq:first-of-type h1+h2{font-size:20px;font-style:italic;font-weight:400;margin-bottom:1rem}a[href]{text-decoration:none}a[href]:hover{text-decoration:underline}h1 code,h2 code,h3 code,h4 code,h5 code,h6 code{font-size:90%}code,pre,tt{font-family:var(--monospace);font-size:14px;line-height:1.5}img{max-width:calc(100vw - 28px)}.katex-display,figcaption,figure,h1,h2,h3,h4,h5,h6,p,table{max-width:640px}blockquote,ol,ul{max-width:600px}blockquote{margin:1rem 1.5rem}ol,ul{padding-left:28px}hr{background:no-repeat 50%/100% 1px linear-gradient(to right,var(--hr),var(--hr));border:none;height:1px;margin:1rem 0;padding:1rem 0}pre{padding:2px 0}.observablehq--md-pre{overflow-x:auto}input:not([type]),input[type=email],input[type=number],input[type=password],input[type=range],input[type=search],input[type=tel],input[type=text],input[type=url]{width:240px}button,canvas,input{vertical-align:middle}button,input,textarea{accent-color:#3b5fc0}table{border-collapse:collapse;font:13px/1.2 var(--sans-serif);width:100%}table code,table pre,table tt{font-size:inherit;line-height:inherit}td>pre:only-child,th>pre:only-child{margin:0;padding:0}th{color:#111;text-align:left;vertical-align:bottom}td{color:#444;vertical-align:top}td,th{padding:3px 6.5px 3px 0}td:last-child,th:last-child{padding-right:0}tr:not(:last-child){border-bottom:1px solid #eee}thead tr{border-bottom:1px solid #ccc}figure,table{margin:1rem 0}figure img{max-width:100%}figcaption{color:var(--syntax-unknown-variable);font:small var(--sans-serif)}.observablehq--caret{margin-right:4px;vertical-align:baseline}.observablehq--field{margin-left:1rem;text-indent:-1rem}.hljs-comment,.observablehq--empty,.observablehq--prototype-key{color:var(--syntax-comment)}.hljs-built_in{color:var(--syntax-known-variable)}.observablehq--unknown{color:var(--syntax-unknown-variable)}.hljs-doctag,.hljs-keyword,.hljs-name,.hljs-section,.hljs-selector-class,.hljs-selector-id,.hljs-selector-tag,.hljs-strong,.hljs-tag,.hljs-type{color:var(--syntax-keyword)}.observablehq{margin:17px 0;min-height:1.5rem;position:relative}.observablehq:before{bottom:1px;content:"";left:-14px;position:absolute;top:0;transition:background-color .25s linear;width:4px}.observablehq--changed:before,.observablehq--running:before{background-color:#a9b0bc;transition:none}.observablehq--error:before{background-color:#e7040f}.observablehq:not(.observablehq--running):empty:after{color:var(--syntax-comment);content:"<detached>";font-style:oblique}.observablehq>link:only-child,.observablehq>style:only-child{color:var(--syntax-keyword);visibility:hidden;white-space:nowrap}.observablehq>link:only-child:before{content:"<link>";pointer-events:none;text-decoration:none;visibility:visible}.observablehq>style:only-child:before{content:"<style>";visibility:visible}.observablehq--inspect.observablehq--import{white-space:normal}.observablehq--inspect::-webkit-scrollbar{display:none}.observablehq--string-expand{background:#eee;border-radius:2px;color:var(--syntax-normal);cursor:pointer;font-size:80%;margin-left:6px;padding:2px 6px;position:sticky;right:0;vertical-align:middle}.observablehq--string-expand:active,.observablehq--string-expand:hover{background:#ddd}
|