1
0
silverbullet/plugs/markdown/share.ts
2022-11-24 16:08:51 +01:00

22 lines
827 B
TypeScript

import { markdown, space } from "$sb/silverbullet-syscall/mod.ts";
import { fs } from "$sb/plugos-syscall/mod.ts";
import { asset } from "$sb/plugos-syscall/mod.ts";
import { renderMarkdownToHtml } from "./markdown_render.ts";
import { PublishEvent } from "$sb/app_event.ts";
export async function sharePublisher(event: PublishEvent) {
const path = event.uri.split(":")[1];
const pageName = event.name;
const text = await space.readPage(pageName);
const tree = await markdown.parseMarkdown(text);
const css = await asset.readAsset("assets/styles.css");
const markdownHtml = renderMarkdownToHtml(tree, {
smartHardBreak: true,
});
const html =
`<html><head><style>${css}</style></head><body><div id="root">${markdownHtml}</div></body></html>`;
await fs.writeFile(path, html, "utf8");
return true;
}