function main(define, notebook, options) {
const {
pinAll = false,
container = document.body,
filter = (node, i) => true,
runtime = new Runtime()
} = options;
const {nodes} = notebook;
const noop = () => {};
const languages = { md: 'markdown', js: 'javascript', html: 'html', sql: 'sql' };
const observers = [];
const cells = nodes.map((node, i) => {
const {value, mode, pinned, name} = node;
const isImport = mode === 'js' && value.startsWith('import ');
if(!filter(node, i, nodes)) {
if(!isImport) observers.push(() => null);
return null;
}
const input = highlight(value, {language: languages[mode] ?? 'text'});
input.style.display = pinned || pinAll ? null : 'none';
const output = html`<div class="observablehq">`;
const element = html`<div>${output}${input}`;
if(isImport) output.appendChild(inspectImport(parseCell(value)));
else observers.push(() => new Inspector(output));
return element;
});
container.append(...cells.filter(d => d));
let i = 0, skip = false;
return runtime.module(define, name => {
if(!skip) {
// For mutable, we inspect the next variable instead.
if(name?.startsWith('mutable ')) return;
// For viewof, we skip the next variable.
if(name?.startsWith('viewof ')) skip = true;
return observers[i++]();
}
skip = false;
});
}