1
0
silverbullet/plugs/markdown/share.ts

24 lines
887 B
TypeScript
Raw Normal View History

2022-11-24 11:04:00 +00:00
import { markdown, space } from "$sb/silverbullet-syscall/mod.ts";
2023-01-13 14:41:29 +00:00
import { LocalFileSystem } from "$sb/plugos-syscall/mod.ts";
2022-11-24 11:04:00 +00:00
import { asset } from "$sb/plugos-syscall/mod.ts";
import { renderMarkdownToHtml } from "./markdown_render.ts";
2022-11-24 15:08:51 +00:00
import { PublishEvent } from "$sb/app_event.ts";
2022-11-24 11:04:00 +00:00
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);
2023-01-13 14:41:29 +00:00
const rootFS = new LocalFileSystem("");
2022-11-24 11:04:00 +00:00
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>`;
2023-01-13 14:41:29 +00:00
await rootFS.writeFile(path, html, "utf8");
2022-11-24 11:04:00 +00:00
return true;
}