1
0
silverbullet/plugs/markdown/preview.ts

29 lines
807 B
TypeScript
Raw Normal View History

import MarkdownIt from "https://esm.sh/markdown-it@13.0.1";
import taskLists from "https://esm.sh/markdown-it-task-lists@2.1.1";
2022-10-14 13:11:33 +00:00
import { clientStore, editor } from "$sb/silverbullet-syscall/mod.ts";
import { asset } from "$sb/plugos-syscall/mod.ts";
import { cleanMarkdown } from "./util.ts";
const md = new MarkdownIt({
linkify: true,
html: false,
typographer: true,
}).use(taskLists);
export async function updateMarkdownPreview() {
if (!(await clientStore.get("enableMarkdownPreview"))) {
return;
}
2022-10-14 13:11:33 +00:00
const text = await editor.getText();
const cleanMd = await cleanMarkdown(text);
const css = await asset.readAsset("styles.css");
await editor.showPanel(
"rhs",
2,
2022-10-14 13:11:33 +00:00
`<html><head><style>${css}</style></head><body>${
md.render(cleanMd)
}</body></html>`,
);
}