2023-08-28 15:12:15 +00:00
|
|
|
import { asset, clientStore, editor, markdown, system } from "$sb/syscalls.ts";
|
2022-11-01 14:01:28 +00:00
|
|
|
import { renderMarkdownToHtml } from "./markdown_render.ts";
|
2023-07-29 21:41:37 +00:00
|
|
|
import { resolvePath } from "$sb/lib/resolve.ts";
|
2023-11-02 15:09:25 +00:00
|
|
|
import { expandCodeWidgets } from "./api.ts";
|
2022-04-13 12:46:52 +00:00
|
|
|
|
|
|
|
export async function updateMarkdownPreview() {
|
2023-08-26 06:31:51 +00:00
|
|
|
if (!(await clientStore.get("enableMarkdownPreview"))) {
|
2022-04-13 12:46:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-07-29 21:41:37 +00:00
|
|
|
const currentPage = await editor.getCurrentPage();
|
2022-10-14 13:11:33 +00:00
|
|
|
const text = await editor.getText();
|
2023-08-28 15:12:15 +00:00
|
|
|
const mdTree = await markdown.parseMarkdown(text);
|
2022-11-01 14:01:28 +00:00
|
|
|
// const cleanMd = await cleanMarkdown(text);
|
2023-10-29 09:02:50 +00:00
|
|
|
const css = await asset.readAsset("assets/preview.css");
|
|
|
|
const js = await asset.readAsset("assets/preview.js");
|
2023-11-02 15:09:25 +00:00
|
|
|
|
|
|
|
await expandCodeWidgets(mdTree, currentPage);
|
2023-05-26 12:04:32 +00:00
|
|
|
const html = renderMarkdownToHtml(mdTree, {
|
2022-11-01 14:01:28 +00:00
|
|
|
smartHardBreak: true,
|
|
|
|
annotationPositions: true,
|
2023-07-02 09:25:32 +00:00
|
|
|
translateUrls: (url) => {
|
2023-02-23 14:33:51 +00:00
|
|
|
if (!url.includes("://")) {
|
2023-07-29 21:41:37 +00:00
|
|
|
url = resolvePath(currentPage, decodeURI(url), true);
|
2023-02-23 14:33:51 +00:00
|
|
|
}
|
|
|
|
return url;
|
|
|
|
},
|
2022-11-01 14:01:28 +00:00
|
|
|
});
|
2022-10-14 13:11:33 +00:00
|
|
|
await editor.showPanel(
|
2022-09-30 15:05:45 +00:00
|
|
|
"rhs",
|
|
|
|
2,
|
2022-11-01 14:01:28 +00:00
|
|
|
`<html><head><style>${css}</style></head><body><div id="root">${html}</div></body></html>`,
|
|
|
|
js,
|
2022-04-13 12:46:52 +00:00
|
|
|
);
|
|
|
|
}
|
2022-11-01 14:01:28 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|