import { clientStore, codeWidget, editor, markdown, } from "$sb/silverbullet-syscall/mod.ts"; import { renderToText, traverseTree } from "$sb/lib/tree.ts"; import { CodeWidgetContent } from "$sb/types.ts"; const hideTOCKey = "hideTOC"; const headerThreshold = 3; type Header = { name: string; pos: number; level: number; }; export async function toggleTOC() { let hideTOC = await clientStore.get(hideTOCKey); hideTOC = !hideTOC; await clientStore.set(hideTOCKey, hideTOC); await codeWidget.refreshAll(); } export async function refreshWidgets() { await codeWidget.refreshAll(); } export async function renderTOC(): Promise { if (await clientStore.get(hideTOCKey)) { return null; } const page = await editor.getCurrentPage(); const text = await editor.getText(); const tree = await markdown.parseMarkdown(text); const headers: Header[] = []; traverseTree(tree, (n) => { if (n.type?.startsWith("ATXHeading")) { headers.push({ name: n.children!.slice(1).map(renderToText).join("").trim(), pos: n.from!, level: +n.type[n.type.length - 1], }); return true; } return false; }); if (headers.length < headerThreshold) { console.log("Not enough headers, not showing TOC", headers.length); return null; } // console.log("Headers", headers); // Adjust level down if only sub-headers are used const minLevel = headers.reduce( (min, header) => Math.min(min, header.level), 6, ); const renderedMd = "# Table of Contents\n" + headers.map((header) => `${ " ".repeat((header.level - minLevel) * 2) }* [[${page}@${header.pos}|${header.name}]]` ).join("\n"); // console.log("Markdown", renderedMd); return { markdown: renderedMd, buttons: [ { description: "Reload", svg: ``, invokeFunction: "index.refreshWidgets", }, { description: "Hide", svg: ``, invokeFunction: "index.toggleTOC", }, ], }; }