From 6c943bacd0a57644aad71ae0d2f8e2ccc24ae6cf Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Sat, 9 Apr 2022 14:57:59 +0200 Subject: [PATCH] Markdown plugin improvements --- plugs/ghost/ghost.ts | 9 +++--- plugs/markdown/markdown.ts | 54 ++++++++++++++++++++++++++++++------ webapp/components/panel.html | 1 - webapp/components/panel.scss | 4 --- 4 files changed, 50 insertions(+), 18 deletions(-) delete mode 100644 webapp/components/panel.scss diff --git a/plugs/ghost/ghost.ts b/plugs/ghost/ghost.ts index 0bd2e49..3a166f5 100644 --- a/plugs/ghost/ghost.ts +++ b/plugs/ghost/ghost.ts @@ -3,6 +3,7 @@ import { json } from "plugos-syscall/fetch"; import YAML from "yaml"; import { invokeFunction } from "plugos-silverbullet-syscall/system"; import { getCurrentPage, getText } from "plugos-silverbullet-syscall/editor"; +import { cleanMarkdown } from "../markdown/markdown"; type Post = { id: string; @@ -153,14 +154,14 @@ const publishedPostRegex = /\n#\s*([^\n]+)\n([^$]+)$/; const newPostRegex = /#\s*([^\n]+)\n([^$]+)$/; -function markdownToPost(text: string): Partial { +async function markdownToPost(text: string): Promise> { let match = publishedPostRegex.exec(text); if (match) { let [, id, title, content] = match; return { id, title, - mobiledoc: markdownToMobileDoc(content), + mobiledoc: markdownToMobileDoc(await cleanMarkdown(content)), }; } match = newPostRegex.exec(text); @@ -169,7 +170,7 @@ function markdownToPost(text: string): Partial { return { title, status: "draft", - mobiledoc: markdownToMobileDoc(content), + mobiledoc: markdownToMobileDoc(await cleanMarkdown(content)), }; } throw Error("Not a valid ghost post"); @@ -207,7 +208,7 @@ export async function publishPost(name: string, text: string) { let config = await getConfig(); let admin = new GhostAdmin(config.url, config.adminKey); await admin.init(); - let post = markdownToPost(text); + let post = await markdownToPost(text); post.slug = name.substring(config.pagePrefix.length); if (post.id) { await admin.updatePost(post); diff --git a/plugs/markdown/markdown.ts b/plugs/markdown/markdown.ts index 914f118..5650c71 100644 --- a/plugs/markdown/markdown.ts +++ b/plugs/markdown/markdown.ts @@ -2,7 +2,40 @@ import MarkdownIt from "markdown-it"; 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"; +import { renderMarkdown, replaceNodesMatching } from "../lib/tree"; + +const css = ` + +`; var taskLists = require("markdown-it-task-lists"); @@ -26,14 +59,8 @@ function encodePageUrl(name: string): string { return name.replaceAll(" ", "_"); } -export async function updateMarkdownPreview() { - if (!(await clientStore.get("enableMarkdownPreview"))) { - return; - } - let text = await getText(); +export async function cleanMarkdown(text: string) { 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!; @@ -48,7 +75,16 @@ export async function updateMarkdownPreview() { } }); let html = md.render(renderMarkdown(mdTree)); - await showRhs(`${html}`, 2); + return html; +} + +export async function updateMarkdownPreview() { + if (!(await clientStore.get("enableMarkdownPreview"))) { + return; + } + let text = await getText(); + let html = await cleanMarkdown(text); + await showRhs(`${css}${html}`, 2); } async function hideMarkdownPreview() { diff --git a/webapp/components/panel.html b/webapp/components/panel.html index 92e018a..8a583f6 100644 --- a/webapp/components/panel.html +++ b/webapp/components/panel.html @@ -2,7 +2,6 @@ -