Importing local notebook
What is your strategy for importing local notebooks (I also want to keep supporting importing them from observablehq)?
If you put your local notebook modules in notebooks/, you could try something like this (not tested, but hopefully you see the idea)?
// copied from src/utils.js
const extractPath = path => {
let source = path;
let m;
// "https://api.observablehq.com/@jashkenas/inputs.js?v=3" => strip off ".js"
if ((m = /\.js(\?|$)/i.exec(source))) source = source.slice(0, m.index);
// "74f872c4fde62e35" => "d/..."
if ((m = /^[0-9a-f]{16}$/i.test(source))) source = `d/${source}`;
// link of notebook
if ((m = /^https:\/\/(api\.|beta\.|)observablehq\.com\//i.exec(source)))
source = source.slice(m[0].length);
return source;
};
// copied from src/compiler.js
const defaultResolver = async path => {
const source = extractPath(path);
return import(`https://api.observablehq.com/${source}.js?v=3`).then(
m => m.default
);
};
const resolve = async path => {
let module;
try {
// look for the notebook module on observablehq.com
return await defaultResolver(path);
} catch (e) { // if loading fails
// look for the notebook module in the notebooks/ directory
const source = extractPath(path);
return (await import(`notebooks/${source}.js`)).default;
}
};
const compile = new Compiler(resolve);
You could also write logic in the resolve function that parses the notebook name to decide where to load a notebook module from. E.g. if your notebook has:
import {cell} from "local/notebook"
Then your resolve function is passed local/notebook and it could detect that it starts with local and act accordingly.
awesome I will try this soon! we should make this somehow a default in the library
btw, I am planning to use this lib for importing local and remote ojs notebooks in this ext. for vscode I've been working on:
https://github.com/RandomFractals/js-notebook-inspector
relevant issue in my repo: https://github.com/RandomFractals/js-notebook-inspector/issues/29