@hpcc-js/observablehq-compiler 1.1.4 → 1.2.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/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@hpcc-js/util')) :
3
- typeof define === 'function' && define.amd ? define(['exports', '@hpcc-js/util'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@hpcc-js/observablehq-compiler"] = {}, global["@hpcc-js/util"]));
5
- })(this, (function (exports, util) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@hpcc-js/observablehq-compiler"] = {}));
5
+ })(this, (function (exports) { 'use strict';
6
6
 
7
7
  // This file was generated. Do not modify manually!
8
8
  var astralIdentifierCodes = [509, 0, 227, 0, 150, 4, 294, 9, 1368, 2, 2, 1, 6, 3, 41, 2, 5, 0, 166, 1, 574, 3, 9, 9, 370, 1, 154, 10, 50, 3, 123, 2, 54, 14, 32, 10, 3, 1, 11, 3, 46, 10, 8, 0, 46, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 2, 11, 83, 11, 7, 0, 161, 11, 6, 9, 7, 3, 56, 1, 2, 6, 3, 1, 3, 2, 10, 0, 11, 1, 3, 6, 4, 4, 193, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 84, 14, 5, 9, 243, 14, 166, 9, 71, 5, 2, 1, 3, 3, 2, 0, 2, 1, 13, 9, 120, 6, 3, 6, 4, 0, 29, 9, 41, 6, 2, 3, 9, 0, 10, 10, 47, 15, 406, 7, 2, 7, 17, 9, 57, 21, 2, 13, 123, 5, 4, 0, 2, 1, 2, 6, 2, 0, 9, 9, 49, 4, 2, 1, 2, 4, 9, 9, 330, 3, 19306, 9, 87, 9, 39, 4, 60, 6, 26, 9, 1014, 0, 2, 54, 8, 3, 82, 0, 12, 1, 19628, 1, 4706, 45, 3, 22, 543, 4, 4, 5, 9, 7, 3, 6, 31, 3, 149, 2, 1418, 49, 513, 54, 5, 49, 9, 0, 15, 0, 23, 4, 2, 14, 1361, 6, 2, 16, 3, 6, 2, 1, 2, 4, 262, 6, 10, 9, 357, 0, 62, 13, 1495, 6, 110, 6, 6, 9, 4759, 9, 787719, 239];
@@ -6622,8 +6622,13 @@
6622
6622
  }
6623
6623
  }
6624
6624
  function splitModule(input) {
6625
- // @ts-ignore
6626
- return ModuleParser.parse(input, { ecmaVersion: "latest" }).cells.map(cell => input.substring(cell.start, cell.end));
6625
+ return ModuleParser
6626
+ .parse(input, { ecmaVersion: "latest" })
6627
+ .cells.map((cell) => ({
6628
+ text: input.substring(cell.start, cell.end),
6629
+ start: cell.start,
6630
+ end: cell.end
6631
+ }));
6627
6632
  }
6628
6633
  function parseCell$2(input) {
6629
6634
  return parseCell$1(input);
@@ -6656,6 +6661,18 @@
6656
6661
  body.substring(1, body.length - 1).trim() :
6657
6662
  `return (\n${body}\n);`);
6658
6663
  }
6664
+ function join(baseURL, relativeURL) {
6665
+ return relativeURL
6666
+ ? baseURL.replace(/\/+$/, "") + "/" + relativeURL.replace(/^\/+/, "")
6667
+ : baseURL;
6668
+ }
6669
+ const isRelativePath = (path) => path[0] === ".";
6670
+ const fixRelativeUrl = (path, basePath) => {
6671
+ if (isRelativePath(path)) {
6672
+ return join(basePath, path);
6673
+ }
6674
+ return path;
6675
+ };
6659
6676
  // Hide "import" from bundlers as they have a habit of replacing "import" with "require"
6660
6677
  async function obfuscatedImport(url) {
6661
6678
  return new FuncTypes.asyncFunctionType("url", "return import(url)")(url);
@@ -6681,7 +6698,7 @@
6681
6698
  error
6682
6699
  };
6683
6700
  }
6684
- function parseOmd(_) {
6701
+ function splitOmd(_) {
6685
6702
  const retVal = [];
6686
6703
  // Load Markdown ---
6687
6704
  const re = /(```(?:\s|\S)[\s\S]*?```)/g;
@@ -6716,20 +6733,24 @@
6716
6733
  return {
6717
6734
  id: idx,
6718
6735
  mode: "js",
6719
- value: cell
6736
+ value: cell.text,
6737
+ start: cell.start,
6738
+ end: cell.end
6720
6739
  };
6721
6740
  })
6722
6741
  };
6723
6742
  }
6724
6743
  function omd2notebook(omd) {
6725
- const cells = parseOmd(omd);
6744
+ const cells = splitOmd(omd);
6726
6745
  return {
6727
6746
  files: [],
6728
6747
  nodes: cells.map((cell, idx) => {
6729
6748
  return {
6730
6749
  id: idx,
6731
6750
  mode: cell.inlineMD ? "md" : "js",
6732
- value: cell.ojs
6751
+ value: cell.ojs,
6752
+ start: cell.offset,
6753
+ end: cell.offset + cell.ojs.length
6733
6754
  };
6734
6755
  })
6735
6756
  };
@@ -6794,7 +6815,7 @@
6794
6815
  }
6795
6816
  return retVal;
6796
6817
  }
6797
- function parseImportExpression(cellAst) {
6818
+ function parseImportDeclaration(cellAst) {
6798
6819
  var _a, _b, _c, _d;
6799
6820
  return {
6800
6821
  type: "import",
@@ -6871,14 +6892,17 @@
6871
6892
  func: createFunction(refs, cellAst.async, cellAst.generator, cellAst.body.type === "BlockStatement", bodyStr)
6872
6893
  };
6873
6894
  }
6874
- function parseCell(cellStr) {
6895
+ function parseCell(cellStr, baseUrl) {
6875
6896
  var _a, _b;
6876
6897
  const cellAst = parseCell$2(cellStr);
6877
- if (((_a = (cellAst.body)) === null || _a === void 0 ? void 0 : _a.type) == "ImportDeclaration") {
6878
- return parseImportExpression(cellAst);
6898
+ let bodyStr = cellAst.body && cellStr.substring(cellAst.body.start, cellAst.body.end);
6899
+ switch ((_a = (cellAst.body)) === null || _a === void 0 ? void 0 : _a.type) {
6900
+ case "ImportDeclaration":
6901
+ return parseImportDeclaration(cellAst);
6902
+ case "ImportExpression":
6903
+ bodyStr = `import("${fixRelativeUrl(cellAst.body.source.value, baseUrl)}")`;
6879
6904
  }
6880
6905
  const refs = calcRefs(cellAst, cellStr);
6881
- const bodyStr = cellAst.body && cellStr.substring(cellAst.body.start, cellAst.body.end);
6882
6906
  switch ((_b = cellAst.id) === null || _b === void 0 ? void 0 : _b.type) {
6883
6907
  case "ViewExpression":
6884
6908
  return parseViewExpression(cellStr, cellAst, refs, bodyStr);
@@ -6959,29 +6983,27 @@ export default function define(runtime, observer) {
6959
6983
  }
6960
6984
  }
6961
6985
 
6962
- const isRelativePath = (path) => path[0] === ".";
6963
- const fullUrl = (path, basePath) => isRelativePath(path) ? util.join(basePath, path) : path;
6964
6986
  async function importFile(relativePath, baseUrl) {
6965
- const path = fullUrl(relativePath, baseUrl);
6987
+ const path = fixRelativeUrl(relativePath, baseUrl);
6966
6988
  const content = await fetchEx(path).then(r => r.text());
6967
6989
  let notebook;
6968
- if (util.endsWith(relativePath, ".ojsnb")) {
6990
+ if (relativePath.endsWith(".ojsnb")) {
6969
6991
  notebook = JSON.parse(content);
6970
6992
  }
6971
- else if (util.endsWith(relativePath, ".ojs")) {
6993
+ else if (relativePath.endsWith(".ojs")) {
6972
6994
  notebook = ojs2notebook(content);
6973
6995
  }
6974
- else if (util.endsWith(relativePath, ".omd")) {
6996
+ else if (relativePath.endsWith(".omd")) {
6975
6997
  notebook = omd2notebook(content);
6976
6998
  }
6977
- const retVal = compile(notebook, baseUrl);
6978
- retVal.dispose = () => { };
6999
+ const retVal = compile(notebook, { baseUrl });
7000
+ retVal.delete = () => { };
6979
7001
  retVal.write = (w) => {
6980
7002
  w.import(path);
6981
7003
  };
6982
7004
  return retVal;
6983
7005
  }
6984
- // @ts-ignore - use precompiled notebook from observable
7006
+ // Import precompiled notebook from observable ---
6985
7007
  async function importCompiledNotebook(partial) {
6986
7008
  const url = `https://api.observablehq.com/${partial[0] === "@" ? partial : `d/${partial}`}.js?v=3`;
6987
7009
  let impMod = {
@@ -6995,17 +7017,72 @@ export default function define(runtime, observer) {
6995
7017
  catch (e) {
6996
7018
  }
6997
7019
  const retVal = impMod.default;
6998
- retVal.dispose = () => { };
7020
+ retVal.delete = () => { };
6999
7021
  retVal.write = (w) => {
7000
7022
  w.import(url);
7001
7023
  };
7002
7024
  return retVal;
7003
7025
  }
7004
- function createVariable(inspect, name, inputs, definition, inline = false) {
7026
+ // Recursive notebook parsing and compiling
7027
+ async function importNotebook(partial) {
7028
+ const url = `https://api.observablehq.com/document/${partial}`;
7029
+ const notebook = fetchEx(url)
7030
+ .then(r => {
7031
+ return r.json();
7032
+ }).catch(e => {
7033
+ console.error(url);
7034
+ console.error(e);
7035
+ });
7036
+ const retVal = compile(await notebook);
7037
+ retVal.delete = () => { };
7038
+ retVal.write = (w) => {
7039
+ w.import(url);
7040
+ };
7041
+ return retVal;
7042
+ }
7043
+ async function createModule(node, parsed, text, { baseUrl, importMode }) {
7044
+ const otherModule = isRelativePath(parsed.src) ?
7045
+ await importFile(parsed.src, baseUrl) :
7046
+ importMode === "recursive" ?
7047
+ await importNotebook(parsed.src) :
7048
+ await importCompiledNotebook(parsed.src);
7049
+ const importVariables = [];
7050
+ const variables = [];
7051
+ parsed.specifiers.forEach(spec => {
7052
+ const viewof = spec.view ? "viewof " : "";
7053
+ importVariables.push(createImportVariable(viewof + spec.name, viewof + spec.alias));
7054
+ if (spec.view) {
7055
+ importVariables.push(createImportVariable(spec.name, spec.alias));
7056
+ }
7057
+ });
7058
+ const retVal = (runtime, main, inspector) => {
7059
+ let mod = runtime.module(otherModule);
7060
+ if (parsed.injections.length) {
7061
+ mod = mod.derive(parsed.injections, main);
7062
+ }
7063
+ variables.forEach(v => v(main, inspector));
7064
+ importVariables.forEach(v => v(main, mod));
7065
+ return mod;
7066
+ };
7067
+ retVal.importVariables = importVariables;
7068
+ retVal.variables = variables;
7069
+ retVal.delete = () => {
7070
+ importVariables.forEach(v => v.delete());
7071
+ variables.forEach(v => v.delete());
7072
+ otherModule.delete();
7073
+ };
7074
+ retVal.write = (w) => {
7075
+ otherModule.write(w);
7076
+ w.importDefine(parsed);
7077
+ };
7078
+ return retVal;
7079
+ }
7080
+ // Variable ---
7081
+ function createVariable(node, inspect, name, inputs, definition, inline = false) {
7005
7082
  let i;
7006
7083
  let v;
7007
7084
  const retVal = (module, inspector) => {
7008
- i = inspect ? inspector(name) : undefined;
7085
+ i = inspect ? inspector(name, node.id) : undefined;
7009
7086
  v = module.variable(i);
7010
7087
  if (arguments.length > 1) {
7011
7088
  try {
@@ -7015,9 +7092,22 @@ export default function define(runtime, observer) {
7015
7092
  console.error(e === null || e === void 0 ? void 0 : e.message);
7016
7093
  }
7017
7094
  }
7095
+ if (node.pinned) {
7096
+ v = module.variable(inspector(name, node.id));
7097
+ try {
7098
+ v.define(undefined, ["md"], md => {
7099
+ return md `\`\`\`js
7100
+ ${node.value}
7101
+ \`\`\``;
7102
+ });
7103
+ }
7104
+ catch (e) {
7105
+ console.error(e === null || e === void 0 ? void 0 : e.message);
7106
+ }
7107
+ }
7018
7108
  return v;
7019
7109
  };
7020
- retVal.dispose = () => {
7110
+ retVal.delete = () => {
7021
7111
  var _a;
7022
7112
  try {
7023
7113
  (_a = i === null || i === void 0 ? void 0 : i._node) === null || _a === void 0 ? void 0 : _a.remove();
@@ -7049,91 +7139,52 @@ export default function define(runtime, observer) {
7049
7139
  v = main.variable();
7050
7140
  v.import(name, alias, otherModule);
7051
7141
  };
7052
- retVal.dispose = () => {
7142
+ retVal.delete = () => {
7053
7143
  v === null || v === void 0 ? void 0 : v.delete();
7054
7144
  };
7055
7145
  return retVal;
7056
7146
  }
7057
- async function createModule(parsed, text, baseUrl) {
7058
- const otherModule = isRelativePath(parsed.src) ?
7059
- await importFile(parsed.src, baseUrl) :
7060
- await importCompiledNotebook(parsed.src);
7061
- const importVariables = [];
7062
- const variables = [];
7063
- parsed.specifiers.forEach(spec => {
7064
- const viewof = spec.view ? "viewof " : "";
7065
- importVariables.push(createImportVariable(viewof + spec.name, viewof + spec.alias));
7066
- if (spec.view) {
7067
- importVariables.push(createImportVariable(spec.name, spec.alias));
7068
- }
7069
- });
7070
- variables.push(createVariable(true, undefined, ["md"], md => {
7071
- return md `\`\`\`JavaScript
7072
- ${text}
7073
- \`\`\``;
7074
- }));
7075
- const retVal = (runtime, main, inspector) => {
7076
- let mod = runtime.module(otherModule);
7077
- if (parsed.injections.length) {
7078
- mod = mod.derive(parsed.injections, main);
7079
- }
7080
- variables.forEach(v => v(main, inspector));
7081
- importVariables.forEach(v => v(main, mod));
7082
- return mod;
7083
- };
7084
- retVal.importVariables = importVariables;
7085
- retVal.variables = variables;
7086
- retVal.dispose = () => {
7087
- importVariables.forEach(v => v.dispose());
7088
- variables.forEach(v => v.dispose());
7089
- otherModule.dispose();
7090
- };
7091
- retVal.write = (w) => {
7092
- otherModule.write(w);
7093
- w.importDefine(parsed);
7094
- };
7095
- return retVal;
7096
- }
7097
- async function createCell(node, baseUrl) {
7147
+ // Cell ---
7148
+ async function createCell(node, options) {
7098
7149
  const modules = [];
7099
7150
  const variables = [];
7100
7151
  try {
7101
7152
  const text = node.mode && node.mode !== "js" ? `${node.mode}\`${encodeBacktick(node.value)}\`` : node.value;
7102
7153
  const parsedModule = splitModule(text);
7103
- for (const text of parsedModule) {
7104
- const parsed = parseCell(text);
7154
+ for (const cell of parsedModule) {
7155
+ const parsed = parseCell(cell.text, options.baseUrl);
7105
7156
  switch (parsed.type) {
7106
7157
  case "import":
7107
- modules.push(await createModule(parsed, text, baseUrl));
7158
+ modules.push(await createModule(node, parsed, cell.text, options));
7108
7159
  break;
7109
7160
  case "viewof":
7110
- variables.push(createVariable(true, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
7111
- variables.push(createVariable(false, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
7161
+ variables.push(createVariable(node, true, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
7162
+ variables.push(createVariable(node, false, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
7112
7163
  break;
7113
7164
  case "mutable":
7114
- variables.push(createVariable(false, parsed.initial.id, parsed.initial.inputs, parsed.initial.func));
7115
- variables.push(createVariable(false, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
7116
- variables.push(createVariable(true, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
7165
+ variables.push(createVariable(node, false, parsed.initial.id, parsed.initial.inputs, parsed.initial.func));
7166
+ variables.push(createVariable(node, false, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
7167
+ variables.push(createVariable(node, true, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
7117
7168
  break;
7118
7169
  case "variable":
7119
- variables.push(createVariable(true, parsed.id, parsed.inputs, parsed.func));
7170
+ variables.push(createVariable(node, true, parsed.id, parsed.inputs, parsed.func));
7120
7171
  break;
7121
7172
  }
7122
7173
  }
7123
7174
  }
7124
7175
  catch (e) {
7125
- variables.push(createVariable(true, undefined, [], e.message));
7176
+ variables.push(createVariable(node, true, undefined, [], e.message));
7126
7177
  }
7127
7178
  const retVal = (runtime, main, inspector) => {
7128
7179
  modules.forEach(imp => imp(runtime, main, inspector));
7129
7180
  variables.forEach(v => v(main, inspector));
7130
7181
  };
7131
- retVal.id = "" + node.id;
7182
+ retVal.id = node.id;
7132
7183
  retVal.modules = modules;
7133
7184
  retVal.variables = variables;
7134
- retVal.dispose = () => {
7135
- variables.forEach(v => v.dispose());
7136
- modules.forEach(mod => mod.dispose());
7185
+ retVal.delete = () => {
7186
+ variables.forEach(v => v.delete());
7187
+ modules.forEach(mod => mod.delete());
7137
7188
  };
7138
7189
  retVal.write = (w) => {
7139
7190
  modules.forEach(imp => imp.write(w));
@@ -7141,20 +7192,20 @@ ${text}
7141
7192
  };
7142
7193
  return retVal;
7143
7194
  }
7144
- function createFile(file, baseUrl) {
7195
+ // File ---
7196
+ function createFile(file, options) {
7145
7197
  function toString() { return globalThis.url; }
7146
- return [file.name, { url: new URL(fullUrl(file.url, baseUrl)), mimeType: file.mime_type, toString }];
7198
+ return [file.name, { url: new URL(fixRelativeUrl(file.url, options.baseUrl)), mimeType: file.mime_type, toString }];
7147
7199
  }
7148
- async function compile(notebook, baseUrl = ".") {
7149
- const files = notebook.files.map(f => createFile(f, baseUrl));
7200
+ function notebook(_files = [], _cells = [], { baseUrl = ".", importMode = "precompiled" } = {}) {
7201
+ const files = _files.map(f => createFile(f, { baseUrl, importMode }));
7150
7202
  const fileAttachments = new Map(files);
7151
- const _cells = await Promise.all(notebook.nodes.map(n => createCell(n, baseUrl)));
7152
7203
  const cells = new Map(_cells.map(c => [c.id, c]));
7153
7204
  const retVal = (runtime, inspector) => {
7154
7205
  const main = runtime.module();
7155
7206
  main.builtin("FileAttachment", runtime.fileAttachments(name => {
7156
7207
  var _a;
7157
- return (_a = fileAttachments.get(name)) !== null && _a !== void 0 ? _a : { url: new URL(fullUrl(name, baseUrl)), mimeType: null };
7208
+ return (_a = fileAttachments.get(name)) !== null && _a !== void 0 ? _a : { url: new URL(fixRelativeUrl(name, baseUrl)), mimeType: null };
7158
7209
  }));
7159
7210
  main.builtin("fetchEx", fetchEx);
7160
7211
  cells.forEach(cell => {
@@ -7164,25 +7215,29 @@ ${text}
7164
7215
  };
7165
7216
  retVal.fileAttachments = fileAttachments;
7166
7217
  retVal.cells = cells;
7167
- retVal.appendCell = async (n, baseUrl) => {
7168
- const cell = await createCell(n, baseUrl);
7169
- retVal.disposeCell(cell.id);
7218
+ retVal.set = async (n) => {
7219
+ const cell = await createCell(n, { baseUrl, importMode });
7220
+ retVal.delete(cell.id);
7170
7221
  cells.set(cell.id, cell);
7171
7222
  return cell;
7172
7223
  };
7173
- retVal.disposeCell = async (id) => {
7224
+ retVal.get = (id) => {
7225
+ return cells.get(id);
7226
+ };
7227
+ retVal.delete = (id) => {
7174
7228
  const cell = cells.get(id);
7175
7229
  if (cell) {
7176
- cells.delete(id);
7177
- cell.dispose();
7230
+ cell.delete();
7231
+ return cells.delete(id);
7178
7232
  }
7233
+ return false;
7179
7234
  };
7180
- retVal.dispose = () => {
7181
- cells.forEach(cell => cell.dispose());
7235
+ retVal.clear = () => {
7236
+ cells.forEach(cell => cell.delete());
7182
7237
  cells.clear();
7183
7238
  };
7184
7239
  retVal.write = (w) => {
7185
- w.files(notebook.files);
7240
+ w.files(_files);
7186
7241
  cells.forEach(cell => cell.write(w));
7187
7242
  };
7188
7243
  retVal.toString = (w = new Writer()) => {
@@ -7191,12 +7246,17 @@ ${text}
7191
7246
  };
7192
7247
  return retVal;
7193
7248
  }
7249
+ async function compile(notebookOrOjs, { baseUrl = ".", importMode = "precompiled" } = {}) {
7250
+ const ojsNotebook = typeof notebookOrOjs === "string" ? ojs2notebook(notebookOrOjs) : notebookOrOjs;
7251
+ const _cells = await Promise.all(ojsNotebook.nodes.map(n => createCell(n, { baseUrl, importMode })));
7252
+ return notebook(ojsNotebook.files, _cells, { baseUrl, importMode });
7253
+ }
7194
7254
 
7195
7255
  exports.compile = compile;
7196
7256
  exports.download = download;
7257
+ exports.notebook = notebook;
7197
7258
  exports.ojs2notebook = ojs2notebook;
7198
7259
  exports.omd2notebook = omd2notebook;
7199
- exports.parseOmd = parseOmd;
7200
7260
 
7201
7261
  Object.defineProperty(exports, '__esModule', { value: true });
7202
7262