@hpcc-js/observablehq-compiler 1.1.1 → 1.1.2

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.
Files changed (48) hide show
  1. package/LICENSE +43 -0
  2. package/README.md +105 -105
  3. package/bin/ojscc.mjs +74 -74
  4. package/dist/index.esm.js +554 -554
  5. package/dist/index.esm.js.map +1 -1
  6. package/dist/index.esm.min.js.map +1 -1
  7. package/dist/index.js +6700 -6696
  8. package/dist/index.js.map +1 -1
  9. package/dist/index.min.js +3 -3
  10. package/dist/index.min.js.map +1 -1
  11. package/package.json +3 -2
  12. package/src/__package__.ts +3 -3
  13. package/src/__tests__/File Attachments.ts +894 -894
  14. package/src/__tests__/Introduction to Imports.ts +748 -748
  15. package/src/__tests__/Observable TimeChart.ts +771 -771
  16. package/src/__tests__/index.ts +13 -13
  17. package/src/__tests__/node.ts +177 -177
  18. package/src/__tests__/tsconfig.json +20 -20
  19. package/src/compiler.md +234 -234
  20. package/src/compiler.ts +264 -264
  21. package/src/cst.ts +172 -172
  22. package/src/index.css +459 -459
  23. package/src/index.ts +7 -7
  24. package/src/util.md +113 -113
  25. package/src/util.ts +153 -153
  26. package/src/writer.ts +80 -80
  27. package/types/__package__.d.ts +3 -3
  28. package/types/__tests__/File Attachments.d.ts +109 -109
  29. package/types/__tests__/Introduction to Imports.d.ts +119 -119
  30. package/types/__tests__/Observable TimeChart.d.ts +110 -110
  31. package/types/__tests__/index.d.ts +1 -1
  32. package/types/__tests__/node.d.ts +1 -1
  33. package/types/compiler.d.ts +87 -87
  34. package/types/cst.d.ts +41 -41
  35. package/types/index.d.ts +5 -5
  36. package/types/util.d.ts +25 -25
  37. package/types/writer.d.ts +18 -18
  38. package/types-3.4/__package__.d.ts +4 -0
  39. package/types-3.4/__tests__/File Attachments.d.ts +110 -0
  40. package/types-3.4/__tests__/Introduction to Imports.d.ts +120 -0
  41. package/types-3.4/__tests__/Observable TimeChart.d.ts +111 -0
  42. package/types-3.4/__tests__/index.d.ts +2 -0
  43. package/types-3.4/__tests__/node.d.ts +2 -0
  44. package/types-3.4/compiler.d.ts +91 -0
  45. package/types-3.4/cst.d.ts +42 -0
  46. package/types-3.4/index.d.ts +6 -0
  47. package/types-3.4/util.d.ts +26 -0
  48. package/types-3.4/writer.d.ts +19 -0
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
- export type { ohq } from "@hpcc-js/observable-shim";
2
-
3
- export * from "./compiler";
4
- export { ojs2notebook, omd2notebook, download } from "./util";
5
- export * from "./writer";
6
-
7
- import "../src/index.css";
1
+ export type { ohq } from "@hpcc-js/observable-shim";
2
+
3
+ export * from "./compiler";
4
+ export { ojs2notebook, omd2notebook, download } from "./util";
5
+ export * from "./writer";
6
+
7
+ import "../src/index.css";
package/src/util.md CHANGED
@@ -1,113 +1,113 @@
1
- # Utilities
2
-
3
- Various utilities and helpers.
4
-
5
- ---
6
-
7
- <a name="download" href="#download">#</a> **download**(_impUrl_) => Promise\<ohq.Module\> · [<>](https://github.com/hpcc-systems/Visualization/blob/trunk/packages/observablehq/compiler/src/util.ts "Source")
8
-
9
- * _impUrl_: Full url to Obaservable HQ notebook as a string.
10
- * _returns_: Returns a Promise of a notebook (a JSON object).
11
-
12
- Downloads a notebook directly from [Observable HQ](https://observablehq.com/) as a JSON object. The following example downloads the [@observablehq/plot](https://observablehq.com/@observablehq/plot) notebook as JSON:
13
-
14
- <ClientOnly>
15
- <hpcc-vitepress style="width:100%;height:600px">
16
- <hpcc-codemirror id="placeholder" mode="json" theme="dark" style="width:100%;height:100%">
17
- </hpcc-codemirror>
18
- <script type="module">
19
- import "@hpcc-js/wc-editor";
20
- import { download } from "@hpcc-js/observablehq-compiler";
21
-
22
- const notebook = await download("https://observablehq.com/@observablehq/plot");
23
- const placeholder = document.getElementById("placeholder");
24
- placeholder.text = JSON.stringify(notebook, undefined, 4);
25
- </script>
26
- </hpcc-vitepress>
27
- </ClientOnly>
28
-
29
- ---
30
-
31
- <a name="ojs2notebook" href="#ojs2notebook">#</a> **ojs2notebook**(_ojs_) => ohq.Module · [<>](https://github.com/hpcc-systems/Visualization/blob/trunk/packages/observablehq/compiler/src/util.ts "Source")
32
-
33
- * _ojs_: String containing Observable JavaScript.
34
- * _returns_: Returns the notebook as a JSON object.
35
-
36
- Transforms Observable JavaScript to a JSON notebook.
37
-
38
- <ClientOnly>
39
- <hpcc-vitepress style="width:100%;height:600px">
40
- <hpcc-codemirror id="placeholder" mode="json" theme="dark" style="width:100%;height:100%">
41
- </hpcc-codemirror>
42
- <script type="module">
43
- import "@hpcc-js/wc-editor";
44
- import { ojs2notebook } from "@hpcc-js/observablehq-compiler";
45
-
46
- const notebook = ojs2notebook(`
47
- md\`# Simple Notebook\`
48
- a = 1
49
- b = 2
50
- c = a + b
51
- d = {
52
- yield 1;
53
- yield 2;
54
- yield 3;
55
- }
56
-
57
- viewof e = {
58
- let output = {};
59
- let listeners = [];
60
- output.value = 10;
61
- output.addEventListener = (listener) => listeners.push(listener);;
62
- output.removeEventListener = (listener) => {
63
- listeners = listeners.filter(l => l !== listener);
64
- };
65
- return output;
66
- }
67
- `);
68
- const placeholder = document.getElementById("placeholder");
69
- placeholder.text = JSON.stringify(notebook, undefined, 4);
70
- </script>
71
- </hpcc-vitepress>
72
- </ClientOnly>
73
-
74
- <a name="omd2notebook" href="#omd2notebook">#</a> **omd2notebook**(_omd_) => ohq.Module · [<>](https://github.com/hpcc-systems/Visualization/blob/trunk/packages/observablehq/compiler/src/util.ts "Source")
75
-
76
- * _omd_: String containing Observable Markdown.
77
- * _returns_: Returns the notebook as a JSON object.
78
-
79
- Transforms Observable Markdown to a JSON notebook.
80
-
81
- <ClientOnly>
82
- <hpcc-vitepress style="width:100%;height:600px">
83
- <hpcc-codemirror id="placeholder" mode="json" theme="dark" style="width:100%;height:100%">
84
- </hpcc-codemirror>
85
- <script type="module">
86
- import "@hpcc-js/wc-editor";
87
- import { omd2notebook } from "@hpcc-js/observablehq-compiler";
88
-
89
- const notebook = omd2notebook(`\
90
- # Simple Notebook
91
-
92
- * Set A
93
- \`\`\`
94
- a = 1
95
- \`\`\`
96
-
97
- * Set B
98
- \`\`\`
99
- b = 2
100
- \`\`\`
101
-
102
- * Calculate c
103
-
104
- \`\`\`
105
- c = a + b
106
- \`\`\`
107
- `);
108
- const placeholder = document.getElementById("placeholder");
109
- placeholder.text = JSON.stringify(notebook, undefined, 4);
110
- </script>
111
- </hpcc-vitepress>
112
- </ClientOnly>
113
-
1
+ # Utilities
2
+
3
+ Various utilities and helpers.
4
+
5
+ ---
6
+
7
+ <a name="download" href="#download">#</a> **download**(_impUrl_) => Promise\<ohq.Module\> · [<>](https://github.com/hpcc-systems/Visualization/blob/trunk/packages/observablehq/compiler/src/util.ts "Source")
8
+
9
+ * _impUrl_: Full url to Obaservable HQ notebook as a string.
10
+ * _returns_: Returns a Promise of a notebook (a JSON object).
11
+
12
+ Downloads a notebook directly from [Observable HQ](https://observablehq.com/) as a JSON object. The following example downloads the [@observablehq/plot](https://observablehq.com/@observablehq/plot) notebook as JSON:
13
+
14
+ <ClientOnly>
15
+ <hpcc-vitepress style="width:100%;height:600px">
16
+ <hpcc-codemirror id="placeholder" mode="json" theme="dark" style="width:100%;height:100%">
17
+ </hpcc-codemirror>
18
+ <script type="module">
19
+ import "@hpcc-js/wc-editor";
20
+ import { download } from "@hpcc-js/observablehq-compiler";
21
+
22
+ const notebook = await download("https://observablehq.com/@observablehq/plot");
23
+ const placeholder = document.getElementById("placeholder");
24
+ placeholder.text = JSON.stringify(notebook, undefined, 4);
25
+ </script>
26
+ </hpcc-vitepress>
27
+ </ClientOnly>
28
+
29
+ ---
30
+
31
+ <a name="ojs2notebook" href="#ojs2notebook">#</a> **ojs2notebook**(_ojs_) => ohq.Module · [<>](https://github.com/hpcc-systems/Visualization/blob/trunk/packages/observablehq/compiler/src/util.ts "Source")
32
+
33
+ * _ojs_: String containing Observable JavaScript.
34
+ * _returns_: Returns the notebook as a JSON object.
35
+
36
+ Transforms Observable JavaScript to a JSON notebook.
37
+
38
+ <ClientOnly>
39
+ <hpcc-vitepress style="width:100%;height:600px">
40
+ <hpcc-codemirror id="placeholder" mode="json" theme="dark" style="width:100%;height:100%">
41
+ </hpcc-codemirror>
42
+ <script type="module">
43
+ import "@hpcc-js/wc-editor";
44
+ import { ojs2notebook } from "@hpcc-js/observablehq-compiler";
45
+
46
+ const notebook = ojs2notebook(`
47
+ md\`# Simple Notebook\`
48
+ a = 1
49
+ b = 2
50
+ c = a + b
51
+ d = {
52
+ yield 1;
53
+ yield 2;
54
+ yield 3;
55
+ }
56
+
57
+ viewof e = {
58
+ let output = {};
59
+ let listeners = [];
60
+ output.value = 10;
61
+ output.addEventListener = (listener) => listeners.push(listener);;
62
+ output.removeEventListener = (listener) => {
63
+ listeners = listeners.filter(l => l !== listener);
64
+ };
65
+ return output;
66
+ }
67
+ `);
68
+ const placeholder = document.getElementById("placeholder");
69
+ placeholder.text = JSON.stringify(notebook, undefined, 4);
70
+ </script>
71
+ </hpcc-vitepress>
72
+ </ClientOnly>
73
+
74
+ <a name="omd2notebook" href="#omd2notebook">#</a> **omd2notebook**(_omd_) => ohq.Module · [<>](https://github.com/hpcc-systems/Visualization/blob/trunk/packages/observablehq/compiler/src/util.ts "Source")
75
+
76
+ * _omd_: String containing Observable Markdown.
77
+ * _returns_: Returns the notebook as a JSON object.
78
+
79
+ Transforms Observable Markdown to a JSON notebook.
80
+
81
+ <ClientOnly>
82
+ <hpcc-vitepress style="width:100%;height:600px">
83
+ <hpcc-codemirror id="placeholder" mode="json" theme="dark" style="width:100%;height:100%">
84
+ </hpcc-codemirror>
85
+ <script type="module">
86
+ import "@hpcc-js/wc-editor";
87
+ import { omd2notebook } from "@hpcc-js/observablehq-compiler";
88
+
89
+ const notebook = omd2notebook(`\
90
+ # Simple Notebook
91
+
92
+ * Set A
93
+ \`\`\`
94
+ a = 1
95
+ \`\`\`
96
+
97
+ * Set B
98
+ \`\`\`
99
+ b = 2
100
+ \`\`\`
101
+
102
+ * Calculate c
103
+
104
+ \`\`\`
105
+ c = a + b
106
+ \`\`\`
107
+ `);
108
+ const placeholder = document.getElementById("placeholder");
109
+ placeholder.text = JSON.stringify(notebook, undefined, 4);
110
+ </script>
111
+ </hpcc-vitepress>
112
+ </ClientOnly>
113
+
package/src/util.ts CHANGED
@@ -1,154 +1,154 @@
1
- import type { ohq } from "@hpcc-js/observable-shim";
2
- import { parseModule } from "@hpcc-js/observable-shim";
3
-
4
- const FuncTypes = {
5
- functionType: Object.getPrototypeOf(function () { }).constructor,
6
- asyncFunctionType: Object.getPrototypeOf(async function () { }).constructor,
7
- generatorFunctionType: Object.getPrototypeOf(function* () { }).constructor,
8
- asyncGeneratorFunctionType: Object.getPrototypeOf(async function* () { }).constructor
9
- };
10
-
11
- function funcType(async: boolean = false, generator: boolean = false) {
12
- if (!async && !generator) return FuncTypes.functionType;
13
- if (async && !generator) return FuncTypes.asyncFunctionType;
14
- if (!async && generator) return FuncTypes.generatorFunctionType;
15
- return FuncTypes.asyncGeneratorFunctionType;
16
- }
17
-
18
- interface Ref {
19
- start: number,
20
- end: number,
21
- newText: string
22
- }
23
-
24
- export interface Refs {
25
- inputs: string[];
26
- args: string[];
27
- patches: Ref[];
28
- }
29
-
30
- export function createFunction(refs: Refs, async = false, generator = false, blockStatement = false, body?: string) {
31
- if (body === undefined) {
32
- return undefined;
33
- }
34
-
35
- refs.patches.sort((l, r) => r.start - l.start);
36
- refs.patches.forEach(r => {
37
- body = body!.substring(0, r.start) + r.newText + body!.substring(r.end);
38
- });
39
- return new (funcType(async, generator))(...refs.args, blockStatement ?
40
- body.substring(1, body.length - 1).trim() :
41
- `return (\n${body}\n);`);
42
- }
43
-
44
- // Hide "import" from bundlers as they have a habit of replacing "import" with "require"
45
- export async function obfuscatedImport(url: string) {
46
- return new FuncTypes.asyncFunctionType("url", "return import(url)")(url);
47
- }
48
-
49
- interface ParsedOJS {
50
- ojs: string;
51
- offset: number;
52
- inlineMD: boolean;
53
- }
54
-
55
- export function encodeBacktick(str: string) {
56
- return str
57
- .split("`").join("\\`")
58
- ;
59
- }
60
-
61
- function createParsedOJS(ojs: string, offset: number, inlineMD: boolean): ParsedOJS {
62
- return {
63
- ojs,
64
- offset,
65
- inlineMD
66
- };
67
- }
68
-
69
- function parseOmd(_: string): ParsedOJS[] {
70
- const retVal: ParsedOJS[] = [];
71
- // Load Markdown ---
72
- const re = /(```(?:\s|\S)[\s\S]*?```)/g;
73
- let prevOffset = 0;
74
- let match = re.exec(_);
75
- while (match !== null) {
76
- if (match.index > prevOffset) {
77
- retVal.push(createParsedOJS(_.substring(prevOffset, match.index), prevOffset, true));
78
- }
79
-
80
- const outer = match[0];
81
- if (outer.indexOf("``` ") === 0 || outer.indexOf("```\n") === 0 || outer.indexOf("```\r\n") === 0) {
82
- const prefixLen = 3;
83
- const inner = outer.substring(prefixLen, outer.length - prefixLen);
84
- retVal.push(createParsedOJS(inner, match.index + prefixLen, false));
85
- } else {
86
- retVal.push(createParsedOJS(outer, match.index, true));
87
- }
88
-
89
- prevOffset = match.index + match[0].length;
90
- match = re.exec(_);
91
- }
92
- if (_.length > prevOffset) {
93
- retVal.push(createParsedOJS(_.substring(prevOffset, _.length), prevOffset, true));
94
- }
95
- return retVal;
96
- }
97
-
98
- export function notebook2ojs(_: string): ParsedOJS[] {
99
- const parsed: ohq.Notebook = JSON.parse(_);
100
- return parsed.nodes.map(node => createParsedOJS(node.value, 0, node.mode === "md"));
101
- }
102
-
103
- export function ojs2notebook(ojs: string): ohq.Notebook {
104
- const cells = parseModule(ojs);
105
- return {
106
- files: [],
107
- nodes: cells.map((cell, idx) => {
108
- return {
109
- id: idx,
110
- mode: "js",
111
- value: cell
112
- };
113
- })
114
- } as ohq.Notebook;
115
- }
116
-
117
- export function omd2notebook(omd: string): ohq.Notebook {
118
- const cells = parseOmd(omd);
119
- return {
120
- files: [],
121
- nodes: cells.map((cell, idx) => {
122
- return {
123
- id: idx,
124
- mode: cell.inlineMD ? "md" : "js",
125
- value: cell.ojs
126
- };
127
- })
128
- } as ohq.Notebook;
129
- }
130
-
131
- export function fetchEx(url: string) {
132
- return fetch(url)
133
- .then(response => {
134
- if (response.ok) return response;
135
- throw new Error("CORS?");
136
- }).catch(e => {
137
- const matches = url.match(/^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n?]+)/img);
138
- url = "https://observable-cors.glitch.me/" + url;
139
- return fetch(url, {
140
- headers: {
141
- origin: matches[0],
142
- referer: url
143
- }
144
- });
145
- });
146
- }
147
-
148
- export function download(impUrl: string): Promise<ohq.Notebook> {
149
- const isShared = impUrl.indexOf("https://observablehq.com/d") === 0;
150
- return fetchEx(impUrl.replace(`https://observablehq.com/${isShared ? "d/" : ""}`, "https://api.observablehq.com/document/"))
151
- .then(r => r.json())
152
- ;
153
-
1
+ import type { ohq } from "@hpcc-js/observable-shim";
2
+ import { parseModule } from "@hpcc-js/observable-shim";
3
+
4
+ const FuncTypes = {
5
+ functionType: Object.getPrototypeOf(function () { }).constructor,
6
+ asyncFunctionType: Object.getPrototypeOf(async function () { }).constructor,
7
+ generatorFunctionType: Object.getPrototypeOf(function* () { }).constructor,
8
+ asyncGeneratorFunctionType: Object.getPrototypeOf(async function* () { }).constructor
9
+ };
10
+
11
+ function funcType(async: boolean = false, generator: boolean = false) {
12
+ if (!async && !generator) return FuncTypes.functionType;
13
+ if (async && !generator) return FuncTypes.asyncFunctionType;
14
+ if (!async && generator) return FuncTypes.generatorFunctionType;
15
+ return FuncTypes.asyncGeneratorFunctionType;
16
+ }
17
+
18
+ interface Ref {
19
+ start: number,
20
+ end: number,
21
+ newText: string
22
+ }
23
+
24
+ export interface Refs {
25
+ inputs: string[];
26
+ args: string[];
27
+ patches: Ref[];
28
+ }
29
+
30
+ export function createFunction(refs: Refs, async = false, generator = false, blockStatement = false, body?: string) {
31
+ if (body === undefined) {
32
+ return undefined;
33
+ }
34
+
35
+ refs.patches.sort((l, r) => r.start - l.start);
36
+ refs.patches.forEach(r => {
37
+ body = body!.substring(0, r.start) + r.newText + body!.substring(r.end);
38
+ });
39
+ return new (funcType(async, generator))(...refs.args, blockStatement ?
40
+ body.substring(1, body.length - 1).trim() :
41
+ `return (\n${body}\n);`);
42
+ }
43
+
44
+ // Hide "import" from bundlers as they have a habit of replacing "import" with "require"
45
+ export async function obfuscatedImport(url: string) {
46
+ return new FuncTypes.asyncFunctionType("url", "return import(url)")(url);
47
+ }
48
+
49
+ interface ParsedOJS {
50
+ ojs: string;
51
+ offset: number;
52
+ inlineMD: boolean;
53
+ }
54
+
55
+ export function encodeBacktick(str: string) {
56
+ return str
57
+ .split("`").join("\\`")
58
+ ;
59
+ }
60
+
61
+ function createParsedOJS(ojs: string, offset: number, inlineMD: boolean): ParsedOJS {
62
+ return {
63
+ ojs,
64
+ offset,
65
+ inlineMD
66
+ };
67
+ }
68
+
69
+ function parseOmd(_: string): ParsedOJS[] {
70
+ const retVal: ParsedOJS[] = [];
71
+ // Load Markdown ---
72
+ const re = /(```(?:\s|\S)[\s\S]*?```)/g;
73
+ let prevOffset = 0;
74
+ let match = re.exec(_);
75
+ while (match !== null) {
76
+ if (match.index > prevOffset) {
77
+ retVal.push(createParsedOJS(_.substring(prevOffset, match.index), prevOffset, true));
78
+ }
79
+
80
+ const outer = match[0];
81
+ if (outer.indexOf("``` ") === 0 || outer.indexOf("```\n") === 0 || outer.indexOf("```\r\n") === 0) {
82
+ const prefixLen = 3;
83
+ const inner = outer.substring(prefixLen, outer.length - prefixLen);
84
+ retVal.push(createParsedOJS(inner, match.index + prefixLen, false));
85
+ } else {
86
+ retVal.push(createParsedOJS(outer, match.index, true));
87
+ }
88
+
89
+ prevOffset = match.index + match[0].length;
90
+ match = re.exec(_);
91
+ }
92
+ if (_.length > prevOffset) {
93
+ retVal.push(createParsedOJS(_.substring(prevOffset, _.length), prevOffset, true));
94
+ }
95
+ return retVal;
96
+ }
97
+
98
+ export function notebook2ojs(_: string): ParsedOJS[] {
99
+ const parsed: ohq.Notebook = JSON.parse(_);
100
+ return parsed.nodes.map(node => createParsedOJS(node.value, 0, node.mode === "md"));
101
+ }
102
+
103
+ export function ojs2notebook(ojs: string): ohq.Notebook {
104
+ const cells = parseModule(ojs);
105
+ return {
106
+ files: [],
107
+ nodes: cells.map((cell, idx) => {
108
+ return {
109
+ id: idx,
110
+ mode: "js",
111
+ value: cell
112
+ };
113
+ })
114
+ } as ohq.Notebook;
115
+ }
116
+
117
+ export function omd2notebook(omd: string): ohq.Notebook {
118
+ const cells = parseOmd(omd);
119
+ return {
120
+ files: [],
121
+ nodes: cells.map((cell, idx) => {
122
+ return {
123
+ id: idx,
124
+ mode: cell.inlineMD ? "md" : "js",
125
+ value: cell.ojs
126
+ };
127
+ })
128
+ } as ohq.Notebook;
129
+ }
130
+
131
+ export function fetchEx(url: string) {
132
+ return fetch(url)
133
+ .then(response => {
134
+ if (response.ok) return response;
135
+ throw new Error("CORS?");
136
+ }).catch(e => {
137
+ const matches = url.match(/^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n?]+)/img);
138
+ url = "https://observable-cors.glitch.me/" + url;
139
+ return fetch(url, {
140
+ headers: {
141
+ origin: matches[0],
142
+ referer: url
143
+ }
144
+ });
145
+ });
146
+ }
147
+
148
+ export function download(impUrl: string): Promise<ohq.Notebook> {
149
+ const isShared = impUrl.indexOf("https://observablehq.com/d") === 0;
150
+ return fetchEx(impUrl.replace(`https://observablehq.com/${isShared ? "d/" : ""}`, "https://api.observablehq.com/document/"))
151
+ .then(r => r.json())
152
+ ;
153
+
154
154
  }