renderNotebook = async (account, name, visual) => {
const container = DOM.element("div");
const library = new observable.Library();
const contentWidth = () => {
return library.Generators.observe((change) => {
let currentWidth = change(container.clientWidth - 32);
const onResize = () => {
let resizedWidth = container.clientWidth - 32;
if (resizedWidth !== currentWidth)
change((currentWidth = resizedWidth));
};
window.addEventListener("resize", onResize);
return () => window.removeEventListener("resize", onResize);
});
};
Object.assign(library, { width: contentWidth });
const cellTypes = { h1: 1, h2: 1, n: 1, t: 1, v: 1 };
const urlPrefix = "https://api.observablehq.com/";
const notebook = await import(`${urlPrefix}${account}/${name}.js?v=3`);
const module = new observable.Runtime(library).module(
notebook.default,
(cellName) => {
if (!cellName) return true;
const cellType = cellName.split("_")[0];
if (!cellTypes[cellType]) return true;
return {
fulfilled: async (value) => {
value.id = cellName;
const element = container.querySelector(`#${cellName}`);
if (element) element.replaceWith(value);
else container.appendChild(value);
},
rejected: (error) => {
console.error(error);
}
};
}
);
visual.replaceChildren(container);
return container;
}