/** * notion-enhancer: outliner * (c) 2021 CloudHill (https://github.com/CloudHill) * (c) 2024 dragonwocky (https://dragonwocky.me/) * (https://notion-enhancer.github.io/) under the MIT license */ "use strict"; export default async (api, db) => { const { html, addMutationListener, addPanelView } = api, frame = ".notion-sidebar-container + div", page = ".notion-page-content", headings = [ ".notion-header-block", ".notion-sub_header-block", ".notion-sub_sub_header-block", ], $view = html`
`; addPanelView({ title: "Outliner", // prettier-ignore $icon: html` `, $view, }); function Heading({ indent, ...props }, ...children) { return html`
${children}
`; } const getHeadings = () => { return [...document.querySelectorAll(headings.join(", "))]; }, getHeadingLevel = ($heading) => { for (let i = 0; i < headings.length; i++) if ($heading.matches(headings[i])) return i + 1; }, updateHeadings = () => { $view.innerHTML = ""; for (const $heading of getHeadings()) { const title = $heading.innerText, indent = getHeadingLevel($heading); if (!title) continue; const $h = html`<${Heading} indent=${indent} onclick=${() => { // todo: scroll into view }}>${title}

`; $view.append($h); } }; let $page; addMutationListener(page, () => { if (!document.contains($page)) $page = document.querySelector(page); if ($page) updateHeadings(); }); };