import { clientStore, codeWidget, editor, system } from "$sb/syscalls.ts"; import { CodeWidgetContent } from "$sb/types.ts"; import { queryObjects } from "./api.ts"; import { LinkObject } from "./page_links.ts"; const hideMentionsKey = "hideMentions"; export async function toggleMentions() { let hideMentions = await clientStore.get(hideMentionsKey); hideMentions = !hideMentions; await clientStore.set(hideMentionsKey, hideMentions); await codeWidget.refreshAll(); } export async function renderMentions(): Promise { if (await clientStore.get(hideMentionsKey)) { return null; } const page = await editor.getCurrentPage(); const linksResult = await queryObjects("link", { // Query all links that point to this page filter: ["and", ["!=", ["attr", "page"], ["string", page]], ["=", [ "attr", "toPage", ], ["string", page]]], }); if (linksResult.length === 0) { // Don't show the panel if there are no links here. return null; } else { let renderedMd = "# Linked Mentions\n"; for (const link of linksResult) { let snippet = await system.invokeFunction( "markdown.markdownToHtml", link.snippet, ); // strip HTML tags snippet = snippet.replace(/<[^>]*>?/gm, ""); renderedMd += `* [[${link.ref}]]: ...${snippet}...\n`; } return { markdown: renderedMd, buttons: [ { description: "Reload", svg: ``, invokeFunction: "index.refreshWidgets", }, { description: "Hide", svg: ``, invokeFunction: "index.toggleMentions", }, ], }; } }