1
0

Markdown plugin improvements

This commit is contained in:
Zef Hemel 2022-04-09 14:57:59 +02:00
parent 8fafd1cd4a
commit 6c943bacd0
4 changed files with 50 additions and 18 deletions

View File

@ -3,6 +3,7 @@ import { json } from "plugos-syscall/fetch";
import YAML from "yaml"; import YAML from "yaml";
import { invokeFunction } from "plugos-silverbullet-syscall/system"; import { invokeFunction } from "plugos-silverbullet-syscall/system";
import { getCurrentPage, getText } from "plugos-silverbullet-syscall/editor"; import { getCurrentPage, getText } from "plugos-silverbullet-syscall/editor";
import { cleanMarkdown } from "../markdown/markdown";
type Post = { type Post = {
id: string; id: string;
@ -153,14 +154,14 @@ const publishedPostRegex =
/<!-- #ghost-id:\s*(\w+)\s*-->\n#\s*([^\n]+)\n([^$]+)$/; /<!-- #ghost-id:\s*(\w+)\s*-->\n#\s*([^\n]+)\n([^$]+)$/;
const newPostRegex = /#\s*([^\n]+)\n([^$]+)$/; const newPostRegex = /#\s*([^\n]+)\n([^$]+)$/;
function markdownToPost(text: string): Partial<Post> { async function markdownToPost(text: string): Promise<Partial<Post>> {
let match = publishedPostRegex.exec(text); let match = publishedPostRegex.exec(text);
if (match) { if (match) {
let [, id, title, content] = match; let [, id, title, content] = match;
return { return {
id, id,
title, title,
mobiledoc: markdownToMobileDoc(content), mobiledoc: markdownToMobileDoc(await cleanMarkdown(content)),
}; };
} }
match = newPostRegex.exec(text); match = newPostRegex.exec(text);
@ -169,7 +170,7 @@ function markdownToPost(text: string): Partial<Post> {
return { return {
title, title,
status: "draft", status: "draft",
mobiledoc: markdownToMobileDoc(content), mobiledoc: markdownToMobileDoc(await cleanMarkdown(content)),
}; };
} }
throw Error("Not a valid ghost post"); throw Error("Not a valid ghost post");
@ -207,7 +208,7 @@ export async function publishPost(name: string, text: string) {
let config = await getConfig(); let config = await getConfig();
let admin = new GhostAdmin(config.url, config.adminKey); let admin = new GhostAdmin(config.url, config.adminKey);
await admin.init(); await admin.init();
let post = markdownToPost(text); let post = await markdownToPost(text);
post.slug = name.substring(config.pagePrefix.length); post.slug = name.substring(config.pagePrefix.length);
if (post.id) { if (post.id) {
await admin.updatePost(post); await admin.updatePost(post);

View File

@ -2,7 +2,40 @@ import MarkdownIt from "markdown-it";
import { getText, hideRhs, showRhs } from "plugos-silverbullet-syscall/editor"; import { getText, hideRhs, showRhs } from "plugos-silverbullet-syscall/editor";
import * as clientStore from "plugos-silverbullet-syscall/clientStore"; import * as clientStore from "plugos-silverbullet-syscall/clientStore";
import { parseMarkdown } from "plugos-silverbullet-syscall/markdown"; import { parseMarkdown } from "plugos-silverbullet-syscall/markdown";
import { addParentPointers, renderMarkdown, replaceNodesMatching } from "../lib/tree"; import { renderMarkdown, replaceNodesMatching } from "../lib/tree";
const css = `
<style>
body {
font-family: georgia,times,serif;
font-size: 14pt;
max-width: 800px;
margin-left: auto;
margin-right: auto;
padding-left: 20px;
padding-right: 20px;
}
blockquote {
border-left: 1px solid #333;
margin-left: 2px;
padding-left: 10px;
}
hr {
margin: 1em 0 1em 0;
text-align: center;
border-color: #777;
border-width: 0;
border-style: dotted;
}
hr:after {
content: "···";
letter-spacing: 1em;
}
</style>
`;
var taskLists = require("markdown-it-task-lists"); var taskLists = require("markdown-it-task-lists");
@ -26,14 +59,8 @@ function encodePageUrl(name: string): string {
return name.replaceAll(" ", "_"); return name.replaceAll(" ", "_");
} }
export async function updateMarkdownPreview() { export async function cleanMarkdown(text: string) {
if (!(await clientStore.get("enableMarkdownPreview"))) {
return;
}
let text = await getText();
let mdTree = await parseMarkdown(text); let mdTree = await parseMarkdown(text);
// console.log("The tree", mdTree);
addParentPointers(mdTree);
replaceNodesMatching(mdTree, (n) => { replaceNodesMatching(mdTree, (n) => {
if (n.type === "WikiLink") { if (n.type === "WikiLink") {
const page = n.children![1].children![0].text!; const page = n.children![1].children![0].text!;
@ -48,7 +75,16 @@ export async function updateMarkdownPreview() {
} }
}); });
let html = md.render(renderMarkdown(mdTree)); let html = md.render(renderMarkdown(mdTree));
await showRhs(`<html><body>${html}</body></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(`<html><head>${css}</head><body>${html}</body></html>`, 2);
} }
async function hideMarkdownPreview() { async function hideMarkdownPreview() {

View File

@ -2,7 +2,6 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<link href="panel.scss" rel="stylesheet"/>
<base target="_top"> <base target="_top">
<script src="panel_page.ts"/> <script src="panel_page.ts"/>
</head> </head>

View File

@ -1,4 +0,0 @@
body {
background: white;
font-family: "Menlo";
}