1
0
silverbullet/plugs/markdown/preview.ts
Zef Hemel a2dbf7b3db
PlugOS refactor and other tweaks (#631)
* Prep for in-process plug loading (e.g. for CF workers, Deno Deploy)
* Prototype of fixed in-process loading plugs
* Fix: buttons not to scroll with content
* Better positioning of modal especially on mobile
* Move query caching outside query
* Fix annoying mouse behavior when filter box appears
* Page navigator search tweaks
2024-01-15 16:43:12 +01:00

49 lines
1.5 KiB
TypeScript

import { asset, clientStore, editor, markdown, system } from "$sb/syscalls.ts";
import { renderMarkdownToHtml } from "./markdown_render.ts";
import { resolveAttachmentPath } from "$sb/lib/resolve.ts";
import { expandCodeWidgets } from "./api.ts";
export async function updateMarkdownPreview() {
if (!(await clientStore.get("enableMarkdownPreview"))) {
return;
}
const currentPage = await editor.getCurrentPage();
const text = await editor.getText();
const mdTree = await markdown.parseMarkdown(text);
// const cleanMd = await cleanMarkdown(text);
const css = await asset.readAsset("markdown", "assets/preview.css");
const js = await asset.readAsset("markdown", "assets/preview.js");
await expandCodeWidgets(mdTree, currentPage);
const html = renderMarkdownToHtml(mdTree, {
smartHardBreak: true,
annotationPositions: true,
translateUrls: (url) => {
if (!url.includes("://")) {
url = resolveAttachmentPath(currentPage, decodeURI(url));
}
return url;
},
});
await editor.showPanel(
"rhs",
2,
`<html><head><style>${css}</style></head><body><div id="root">${html}</div></body></html>`,
js,
);
}
export async function previewClickHandler(e: any) {
const [eventName, arg] = JSON.parse(e);
// console.log("Got click", eventName, arg);
switch (eventName) {
case "pos":
// console.log("Moving cursor to", +arg);
await editor.moveCursor(+arg, true);
break;
case "command":
await system.invokeCommand(arg);
break;
}
}