A whole lot of enhancements
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
functions:
|
||||
mdTest:
|
||||
path: "./markdown.ts:renderMarkdown"
|
||||
env: client
|
||||
toggle:
|
||||
path: "./markdown.ts:togglePreview"
|
||||
command:
|
||||
name: "Markdown: Render"
|
||||
name: "Toggle Markdown Preview"
|
||||
preview:
|
||||
path: "./markdown.ts:updateMarkdownPreview"
|
||||
env: client
|
||||
events:
|
||||
- plug:load
|
||||
- editor:updated
|
||||
- editor:pageSwitched
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import MarkdownIt from "markdown-it";
|
||||
import { getText, showRhs } from "plugos-silverbullet-syscall/editor";
|
||||
import {getText, hideRhs, showRhs} from "plugos-silverbullet-syscall/editor";
|
||||
import * as clientStore from "plugos-silverbullet-syscall/clientStore";
|
||||
import {parseMarkdown} from "plugos-silverbullet-syscall/markdown";
|
||||
import {addParentPointers, renderMarkdown, replaceNodesMatching,} from "../lib/tree";
|
||||
|
||||
var taskLists = require("markdown-it-task-lists");
|
||||
|
||||
@@ -9,8 +12,45 @@ const md = new MarkdownIt({
|
||||
typographer: true,
|
||||
}).use(taskLists);
|
||||
|
||||
export async function renderMarkdown() {
|
||||
export async function togglePreview() {
|
||||
let currentValue = !!(await clientStore.get("enableMarkdownPreview"));
|
||||
await clientStore.set("enableMarkdownPreview", !currentValue);
|
||||
if (!currentValue) {
|
||||
updateMarkdownPreview();
|
||||
} else {
|
||||
hideMarkdownPreview();
|
||||
}
|
||||
}
|
||||
|
||||
function encodePageUrl(name: string): string {
|
||||
return name.replaceAll(" ", "_");
|
||||
}
|
||||
|
||||
export async function updateMarkdownPreview() {
|
||||
if (!(await clientStore.get("enableMarkdownPreview"))) {
|
||||
return;
|
||||
}
|
||||
let text = await getText();
|
||||
let html = md.render(text);
|
||||
let mdTree = await parseMarkdown(text);
|
||||
// console.log("The tree", mdTree);
|
||||
addParentPointers(mdTree);
|
||||
replaceNodesMatching(mdTree, (n) => {
|
||||
if (n.type === "WikiLink") {
|
||||
const page = n.children![1].children![0].text!;
|
||||
return {
|
||||
// HACK
|
||||
text: `[${page}](/${encodePageUrl(page)})`,
|
||||
};
|
||||
}
|
||||
// Simply get rid of these
|
||||
if (n.type === "CommentBlock" || n.type === "Comment") {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
let html = md.render(renderMarkdown(mdTree));
|
||||
await showRhs(`<html><body>${html}</body></html>`);
|
||||
}
|
||||
|
||||
async function hideMarkdownPreview() {
|
||||
await hideRhs();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user