@hpcc-js/observablehq-compiler 1.1.3 → 1.2.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/dist/index.js CHANGED
@@ -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);
@@ -6681,7 +6686,7 @@
6681
6686
  error
6682
6687
  };
6683
6688
  }
6684
- function parseOmd(_) {
6689
+ function splitOmd(_) {
6685
6690
  const retVal = [];
6686
6691
  // Load Markdown ---
6687
6692
  const re = /(```(?:\s|\S)[\s\S]*?```)/g;
@@ -6716,20 +6721,24 @@
6716
6721
  return {
6717
6722
  id: idx,
6718
6723
  mode: "js",
6719
- value: cell
6724
+ value: cell.text,
6725
+ start: cell.start,
6726
+ end: cell.end
6720
6727
  };
6721
6728
  })
6722
6729
  };
6723
6730
  }
6724
6731
  function omd2notebook(omd) {
6725
- const cells = parseOmd(omd);
6732
+ const cells = splitOmd(omd);
6726
6733
  return {
6727
6734
  files: [],
6728
6735
  nodes: cells.map((cell, idx) => {
6729
6736
  return {
6730
6737
  id: idx,
6731
6738
  mode: cell.inlineMD ? "md" : "js",
6732
- value: cell.ojs
6739
+ value: cell.ojs,
6740
+ start: cell.offset,
6741
+ end: cell.offset + cell.ojs.length
6733
6742
  };
6734
6743
  })
6735
6744
  };
@@ -6959,6 +6968,7 @@ export default function define(runtime, observer) {
6959
6968
  }
6960
6969
  }
6961
6970
 
6971
+ // Module ---
6962
6972
  const isRelativePath = (path) => path[0] === ".";
6963
6973
  const fullUrl = (path, basePath) => isRelativePath(path) ? util.join(basePath, path) : path;
6964
6974
  async function importFile(relativePath, baseUrl) {
@@ -6974,14 +6984,14 @@ export default function define(runtime, observer) {
6974
6984
  else if (util.endsWith(relativePath, ".omd")) {
6975
6985
  notebook = omd2notebook(content);
6976
6986
  }
6977
- const retVal = compile(notebook, baseUrl);
6978
- retVal.dispose = () => { };
6987
+ const retVal = compile(notebook, { baseUrl });
6988
+ retVal.delete = () => { };
6979
6989
  retVal.write = (w) => {
6980
6990
  w.import(path);
6981
6991
  };
6982
6992
  return retVal;
6983
6993
  }
6984
- // @ts-ignore - use precompiled notebook from observable
6994
+ // Import precompiled notebook from observable ---
6985
6995
  async function importCompiledNotebook(partial) {
6986
6996
  const url = `https://api.observablehq.com/${partial[0] === "@" ? partial : `d/${partial}`}.js?v=3`;
6987
6997
  let impMod = {
@@ -6995,17 +7005,72 @@ export default function define(runtime, observer) {
6995
7005
  catch (e) {
6996
7006
  }
6997
7007
  const retVal = impMod.default;
6998
- retVal.dispose = () => { };
7008
+ retVal.delete = () => { };
7009
+ retVal.write = (w) => {
7010
+ w.import(url);
7011
+ };
7012
+ return retVal;
7013
+ }
7014
+ // Recursive notebook parsing and compiling
7015
+ async function importNotebook(partial) {
7016
+ const url = `https://api.observablehq.com/document/${partial}`;
7017
+ const notebook = fetchEx(url)
7018
+ .then(r => {
7019
+ return r.json();
7020
+ }).catch(e => {
7021
+ console.error(url);
7022
+ console.error(e);
7023
+ });
7024
+ const retVal = compile(await notebook);
7025
+ retVal.delete = () => { };
6999
7026
  retVal.write = (w) => {
7000
7027
  w.import(url);
7001
7028
  };
7002
7029
  return retVal;
7003
7030
  }
7004
- function createVariable(inspect, name, inputs, definition, inline = false) {
7031
+ async function createModule(node, parsed, text, { baseUrl, importMode }) {
7032
+ const otherModule = isRelativePath(parsed.src) ?
7033
+ await importFile(parsed.src, baseUrl) :
7034
+ importMode === "recursive" ?
7035
+ await importNotebook(parsed.src) :
7036
+ await importCompiledNotebook(parsed.src);
7037
+ const importVariables = [];
7038
+ const variables = [];
7039
+ parsed.specifiers.forEach(spec => {
7040
+ const viewof = spec.view ? "viewof " : "";
7041
+ importVariables.push(createImportVariable(viewof + spec.name, viewof + spec.alias));
7042
+ if (spec.view) {
7043
+ importVariables.push(createImportVariable(spec.name, spec.alias));
7044
+ }
7045
+ });
7046
+ const retVal = (runtime, main, inspector) => {
7047
+ let mod = runtime.module(otherModule);
7048
+ if (parsed.injections.length) {
7049
+ mod = mod.derive(parsed.injections, main);
7050
+ }
7051
+ variables.forEach(v => v(main, inspector));
7052
+ importVariables.forEach(v => v(main, mod));
7053
+ return mod;
7054
+ };
7055
+ retVal.importVariables = importVariables;
7056
+ retVal.variables = variables;
7057
+ retVal.delete = () => {
7058
+ importVariables.forEach(v => v.delete());
7059
+ variables.forEach(v => v.delete());
7060
+ otherModule.delete();
7061
+ };
7062
+ retVal.write = (w) => {
7063
+ otherModule.write(w);
7064
+ w.importDefine(parsed);
7065
+ };
7066
+ return retVal;
7067
+ }
7068
+ // Variable ---
7069
+ function createVariable(node, inspect, name, inputs, definition, inline = false) {
7005
7070
  let i;
7006
7071
  let v;
7007
7072
  const retVal = (module, inspector) => {
7008
- i = inspect ? inspector(name) : undefined;
7073
+ i = inspect ? inspector(name, node.id) : undefined;
7009
7074
  v = module.variable(i);
7010
7075
  if (arguments.length > 1) {
7011
7076
  try {
@@ -7015,9 +7080,22 @@ export default function define(runtime, observer) {
7015
7080
  console.error(e === null || e === void 0 ? void 0 : e.message);
7016
7081
  }
7017
7082
  }
7083
+ if (node.pinned) {
7084
+ v = module.variable(inspector(name, node.id));
7085
+ try {
7086
+ v.define(undefined, ["md"], md => {
7087
+ return md `\`\`\`js
7088
+ ${node.value}
7089
+ \`\`\``;
7090
+ });
7091
+ }
7092
+ catch (e) {
7093
+ console.error(e === null || e === void 0 ? void 0 : e.message);
7094
+ }
7095
+ }
7018
7096
  return v;
7019
7097
  };
7020
- retVal.dispose = () => {
7098
+ retVal.delete = () => {
7021
7099
  var _a;
7022
7100
  try {
7023
7101
  (_a = i === null || i === void 0 ? void 0 : i._node) === null || _a === void 0 ? void 0 : _a.remove();
@@ -7049,91 +7127,52 @@ export default function define(runtime, observer) {
7049
7127
  v = main.variable();
7050
7128
  v.import(name, alias, otherModule);
7051
7129
  };
7052
- retVal.dispose = () => {
7130
+ retVal.delete = () => {
7053
7131
  v === null || v === void 0 ? void 0 : v.delete();
7054
7132
  };
7055
7133
  return retVal;
7056
7134
  }
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) {
7135
+ // Cell ---
7136
+ async function createCell(node, options) {
7098
7137
  const modules = [];
7099
7138
  const variables = [];
7100
7139
  try {
7101
7140
  const text = node.mode && node.mode !== "js" ? `${node.mode}\`${encodeBacktick(node.value)}\`` : node.value;
7102
7141
  const parsedModule = splitModule(text);
7103
- for (const text of parsedModule) {
7104
- const parsed = parseCell(text);
7142
+ for (const cell of parsedModule) {
7143
+ const parsed = parseCell(cell.text);
7105
7144
  switch (parsed.type) {
7106
7145
  case "import":
7107
- modules.push(await createModule(parsed, text, baseUrl));
7146
+ modules.push(await createModule(node, parsed, cell.text, options));
7108
7147
  break;
7109
7148
  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));
7149
+ variables.push(createVariable(node, true, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
7150
+ variables.push(createVariable(node, false, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
7112
7151
  break;
7113
7152
  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));
7153
+ variables.push(createVariable(node, false, parsed.initial.id, parsed.initial.inputs, parsed.initial.func));
7154
+ variables.push(createVariable(node, false, parsed.variable.id, parsed.variable.inputs, parsed.variable.func));
7155
+ variables.push(createVariable(node, true, parsed.variableValue.id, parsed.variableValue.inputs, parsed.variableValue.func, true));
7117
7156
  break;
7118
7157
  case "variable":
7119
- variables.push(createVariable(true, parsed.id, parsed.inputs, parsed.func));
7158
+ variables.push(createVariable(node, true, parsed.id, parsed.inputs, parsed.func));
7120
7159
  break;
7121
7160
  }
7122
7161
  }
7123
7162
  }
7124
7163
  catch (e) {
7125
- variables.push(createVariable(true, undefined, [], e.message));
7164
+ variables.push(createVariable(node, true, undefined, [], e.message));
7126
7165
  }
7127
7166
  const retVal = (runtime, main, inspector) => {
7128
7167
  modules.forEach(imp => imp(runtime, main, inspector));
7129
7168
  variables.forEach(v => v(main, inspector));
7130
7169
  };
7131
- retVal.id = "" + node.id;
7170
+ retVal.id = node.id;
7132
7171
  retVal.modules = modules;
7133
7172
  retVal.variables = variables;
7134
- retVal.dispose = () => {
7135
- variables.forEach(v => v.dispose());
7136
- modules.forEach(mod => mod.dispose());
7173
+ retVal.delete = () => {
7174
+ variables.forEach(v => v.delete());
7175
+ modules.forEach(mod => mod.delete());
7137
7176
  };
7138
7177
  retVal.write = (w) => {
7139
7178
  modules.forEach(imp => imp.write(w));
@@ -7141,14 +7180,14 @@ ${text}
7141
7180
  };
7142
7181
  return retVal;
7143
7182
  }
7144
- function createFile(file, baseUrl) {
7183
+ // File ---
7184
+ function createFile(file, options) {
7145
7185
  function toString() { return globalThis.url; }
7146
- return [file.name, { url: new URL(fullUrl(file.url, baseUrl)), mimeType: file.mime_type, toString }];
7186
+ return [file.name, { url: new URL(fullUrl(file.url, options.baseUrl)), mimeType: file.mime_type, toString }];
7147
7187
  }
7148
- async function compile(notebook, baseUrl = ".") {
7149
- const files = notebook.files.map(f => createFile(f, baseUrl));
7188
+ function notebook(_files = [], _cells = [], { baseUrl = ".", importMode = "precompiled" } = {}) {
7189
+ const files = _files.map(f => createFile(f, { baseUrl, importMode }));
7150
7190
  const fileAttachments = new Map(files);
7151
- const _cells = await Promise.all(notebook.nodes.map(n => createCell(n, baseUrl)));
7152
7191
  const cells = new Map(_cells.map(c => [c.id, c]));
7153
7192
  const retVal = (runtime, inspector) => {
7154
7193
  const main = runtime.module();
@@ -7164,25 +7203,29 @@ ${text}
7164
7203
  };
7165
7204
  retVal.fileAttachments = fileAttachments;
7166
7205
  retVal.cells = cells;
7167
- retVal.appendCell = async (n, baseUrl) => {
7168
- const cell = await createCell(n, baseUrl);
7169
- retVal.disposeCell(cell.id);
7206
+ retVal.set = async (n) => {
7207
+ const cell = await createCell(n, { baseUrl, importMode });
7208
+ retVal.delete(cell.id);
7170
7209
  cells.set(cell.id, cell);
7171
7210
  return cell;
7172
7211
  };
7173
- retVal.disposeCell = async (id) => {
7212
+ retVal.get = (id) => {
7213
+ return cells.get(id);
7214
+ };
7215
+ retVal.delete = (id) => {
7174
7216
  const cell = cells.get(id);
7175
7217
  if (cell) {
7176
- cells.delete(id);
7177
- cell.dispose();
7218
+ cell.delete();
7219
+ return cells.delete(id);
7178
7220
  }
7221
+ return false;
7179
7222
  };
7180
- retVal.dispose = () => {
7181
- cells.forEach(cell => cell.dispose());
7223
+ retVal.clear = () => {
7224
+ cells.forEach(cell => cell.delete());
7182
7225
  cells.clear();
7183
7226
  };
7184
7227
  retVal.write = (w) => {
7185
- w.files(notebook.files);
7228
+ w.files(_files);
7186
7229
  cells.forEach(cell => cell.write(w));
7187
7230
  };
7188
7231
  retVal.toString = (w = new Writer()) => {
@@ -7191,12 +7234,17 @@ ${text}
7191
7234
  };
7192
7235
  return retVal;
7193
7236
  }
7237
+ async function compile(notebookOrOjs, { baseUrl = ".", importMode = "precompiled" } = {}) {
7238
+ const ojsNotebook = typeof notebookOrOjs === "string" ? ojs2notebook(notebookOrOjs) : notebookOrOjs;
7239
+ const _cells = await Promise.all(ojsNotebook.nodes.map(n => createCell(n, { baseUrl, importMode })));
7240
+ return notebook(ojsNotebook.files, _cells, { baseUrl, importMode });
7241
+ }
7194
7242
 
7195
7243
  exports.compile = compile;
7196
7244
  exports.download = download;
7245
+ exports.notebook = notebook;
7197
7246
  exports.ojs2notebook = ojs2notebook;
7198
7247
  exports.omd2notebook = omd2notebook;
7199
- exports.parseOmd = parseOmd;
7200
7248
 
7201
7249
  Object.defineProperty(exports, '__esModule', { value: true });
7202
7250