function toc(config) {
const selector = config && config.selector ? config.selector : "h1,h2,h3,h4,h5,h6";
const heading = config && config.heading ? config.heading : "Table of Contents";
return Generators.observe(notify => {
let headings = [];
function observed() {
const minSelector = Math.min(...selector.split(",").map(d => +d.replace(/.*h/, "")));
const h = Array.from(document.querySelectorAll(selector));
if (h.length !== headings.length || h.some((h, i) => headings[i] !== h)) {
notify(html`<b>${heading}</b><ul>${Array.from(headings = h, h => {
console.log(h.tagName, minSelector, h)
const level = parseInt(h.tagName.slice(1));
return Object.assign(
html`${level > minSelector ? '<ul>'.repeat(level - minSelector) + '<li>' : '<li>'}<a href=#${h.id}>${DOM.text(h.textContent)}`,
{onclick: e => (e.preventDefault(), h.scrollIntoView())}
);
})}`);
}
}
const observer = new MutationObserver(observed);
observer.observe(document.body, {childList: true, subtree: true});
observed();
return () => observer.disconnect();
});
}