Removed markdown plug with markdown preview

This commit is contained in:
Zef Hemel
2025-06-17 11:11:59 +02:00
parent fbf2e4bc45
commit e17d6b7bb7
18 changed files with 125 additions and 444 deletions
-1
View File
@@ -6,7 +6,6 @@ export const builtinPlugNames = [
"sync",
"plug-manager",
"emoji",
"markdown",
"tasks",
"search",
"image-viewer",
-13
View File
@@ -1,13 +0,0 @@
import { parseMarkdown } from "../../plug-api/syscalls/markdown.ts";
import {
type MarkdownRenderOptions,
renderMarkdownToHtml,
} from "./markdown_render.ts";
export async function markdownToHtml(
markdown: string,
options: MarkdownRenderOptions = {},
) {
const mdTree = await parseMarkdown(markdown);
return renderMarkdownToHtml(mdTree, options);
}
-76
View File
@@ -1,76 +0,0 @@
html,
html {
overflow-y: scroll !important;
width: 90% !important;
}
body {
overflow: initial !important;
color: var(--top-color);
font-family: georgia, times, serif;
font-size: 14pt;
max-width: 800px;
margin-left: auto;
margin-right: auto;
margin-top: revert;
margin-bottom: revert;
padding-left: 20px;
padding-right: 20px;
}
img {
max-width: 100%;
}
table {
width: 100%;
border-spacing: 0;
}
ul li p {
margin: 0;
}
thead tr {
background-color: var(--editor-table-head-background-color);
color: var(--editor-table-head-color);
}
th,
td {
padding: 8px;
}
tbody tr:nth-of-type(even) {
background-color: var(--editor-table-even-background-color);
}
a[href] {
text-decoration: none;
color: var(--link-color);
}
blockquote {
border-left: 1px solid var(--editor-blockquote-border-color);
margin-left: 2px;
padding-left: 10px;
background-color: var(--editor-blockquote-background-color);
color: var(--editor-blockquote-color);
}
hr {
margin: 1em 0 1em 0;
text-align: center;
border-color: var(--editor-ruler-color);
border-width: 0;
border-style: dotted;
}
hr:after {
content: "···";
letter-spacing: 1em;
}
span.highlight {
background-color: var(--highlight-color);
}
-15
View File
@@ -1,15 +0,0 @@
document.getElementById("root").addEventListener("click", (e) => {
// console.log("Got click", e.target)
const dataSet = e.target.dataset;
if (dataSet["onclick"]) {
syscall("event.dispatch", "preview:click", dataSet["onclick"]).catch((e) =>
console.log("Error", e)
);
} else if (dataSet["pos"]) {
syscall(
"event.dispatch",
"preview:click",
JSON.stringify(["pos", dataSet["pos"]]),
).catch((e) => console.log("Error", e));
}
});
-32
View File
@@ -1,32 +0,0 @@
name: markdown
assets:
- "assets/*"
functions:
# API
markdownToHtml:
path: api.ts:markdownToHtml
syscall: markdown.markdownToHtml
# User facing
toggle:
path: "./markdown.ts:togglePreview"
command:
name: "Markdown Preview: Toggle"
key: Ctrl-p
mac: Cmd-p
requireEditor: page
preview:
path: "./preview.ts:updateMarkdownPreview"
env: client
events:
- plug:load
- editor:updated
- editor:pageLoaded
- editor:pageReloaded
previewClickHandler:
path: "./preview.ts:previewClickHandler"
events:
- preview:click
-16
View File
@@ -1,16 +0,0 @@
import { clientStore, editor } from "@silverbulletmd/silverbullet/syscalls";
import { updateMarkdownPreview } from "./preview.ts";
export async function togglePreview() {
const currentValue = !!(await clientStore.get("enableMarkdownPreview"));
await clientStore.set("enableMarkdownPreview", !currentValue);
if (!currentValue) {
await updateMarkdownPreview();
} else {
await hideMarkdownPreview();
}
}
async function hideMarkdownPreview() {
await editor.hidePanel("rhs");
}
-67
View File
@@ -1,67 +0,0 @@
import { parse } from "../../web/markdown_parser/parse_tree.ts";
import { System } from "../../lib/plugos/system.ts";
import { createSandbox } from "../../lib/plugos/sandboxes/deno_worker_sandbox.ts";
import { renderMarkdownToHtml } from "./markdown_render.ts";
import { extendedMarkdownLanguage } from "../../web/markdown_parser/parser.ts";
import { assertEquals } from "@std/assert";
import { fileURLToPath } from "node:url";
Deno.test("Markdown render", async () => {
const system = new System<any>("server");
await system.load(
"editor",
createSandbox(
new URL("../../dist_plug_bundle/_plug/editor.plug.js", import.meta.url),
),
);
await system.load(
"tasks",
createSandbox(
new URL("../../dist_plug_bundle/_plug/tasks.plug.js", import.meta.url),
),
);
const testFile = Deno.readTextFileSync(
fileURLToPath(new URL("test/example.md", import.meta.url)),
);
const tree = parse(extendedMarkdownLanguage, testFile);
renderMarkdownToHtml(tree, {
failOnUnknown: true,
});
// console.log("HTML", html);
await system.unloadAll();
});
Deno.test("Smart hard break test", () => {
const example = `**Hello**
*world!*`;
const tree = parse(extendedMarkdownLanguage, example);
const html = renderMarkdownToHtml(tree, {
failOnUnknown: true,
smartHardBreak: true,
});
assertEquals(
html,
`<span class="p"><strong>Hello</strong><br><em>world!</em></span>`,
);
const example2 = `This is going to be a text. With a new line.
And another
* and a list
* with a second item
### [[Bla]]
Url: something
Server: something else
📅 last_updated - [Release notes](release_notes_url)`;
const tree2 = parse(extendedMarkdownLanguage, example2);
const html2 = renderMarkdownToHtml(tree2, {
failOnUnknown: true,
smartHardBreak: true,
});
console.log(html2);
});
-75
View File
@@ -1,75 +0,0 @@
import {
asset,
clientStore,
editor,
markdown,
} from "@silverbulletmd/silverbullet/syscalls";
import { renderMarkdownToHtml } from "./markdown_render.ts";
import {
isLocalPath,
resolvePath,
} from "@silverbulletmd/silverbullet/lib/resolve";
export async function updateMarkdownPreview() {
if (!(await clientStore.get("enableMarkdownPreview"))) {
return;
}
const currentPage = await editor.getCurrentPage();
const text = await editor.getText();
let mdTree = await markdown.parseMarkdown(text);
const css = await asset.readAsset("markdown", "assets/preview.css");
const js = await asset.readAsset("markdown", "assets/preview.js");
mdTree = await markdown.expandMarkdown(mdTree);
const html = renderMarkdownToHtml(mdTree, {
smartHardBreak: true,
annotationPositions: true,
translateUrls: (url) => {
if (isLocalPath(url)) {
url = resolvePath(currentPage, decodeURI(url));
}
return url;
},
});
const customStyles = await editor.getUiOption("customStyles");
const toolbar = renderToolbar();
await editor.showPanel(
"rhs",
2,
`
<link rel="stylesheet" href=".client/main.css" />
<style>
${css}
${customStyles ?? ""}
</style>
<div id="root" class="sb-preview">${toolbar}${html}</div>
`,
js,
);
}
function renderToolbar(): string {
return `<div class="sb-markdown-toolbar">
<button onClick="window.print()">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24"
viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
class="feather feather-printer">
<polyline points="6 9 6 2 18 2 18 9"/>
<path d="M6 18H4a2 2 0 0 1-2-2v-5a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2v5a2 2 0 0 1-2 2h-2"/>
<rect x="6" y="14" width="12" height="8"/>
</svg>
</button>
</div>`;
}
export async function previewClickHandler(e: any) {
const [eventName, arg] = JSON.parse(e);
switch (eventName) {
case "pos":
// console.log("Moving cursor to", +arg);
await editor.moveCursor(+arg, true);
break;
}
}
-82
View File
@@ -1,82 +0,0 @@
---
name: Sup
---
# Hello world
This is **bold** and _italic_, or _italic_. And a **_mix_**. And ==highlight==!
This is one line and this another.
Lists:
- This
- Is a
- list
- And here we go nested
1. This is a numbered
2. Two
- And different
- Bla
- More bla
And:
1. Numbered
2. Two
## Second heading
And some
```
Code
bla
bla
bla
```
And like this:
More code
Bla
And a blockquote:
> Sup yo Empty line\
> Second part
<!-- this is a comment -->
And more custom stuff [[Page link]]
{[Command button]}
- [ ] #next Task
- [x] #next Task 2
- [ ] Task with dealine 📅 2022-05-06 fef
https://community.mattermost.com
[A link](https://silverbullet.md)
## Tables
| type | actor_login | created_at | payload_ref |
| --------- | ----------- | -------------------- | ---------------------- |
| PushEvent | avb | 2022-10-27T08:27:48Z | refs/heads/master |
| PushEvent | avb | 2022-10-27T04:31:27Z | refs/heads/jitterSched |
Here is something
---
A new thing.
![alt text](https:/
## Weird stuff
- [{{#if done}}x{{else}} {{/if}}] [[{{page}}@{{pos}}]] {{name}} {{#if
deadline}}📅 {{deadline}}{{/if}} {{#each tags}}{{.}} {{/each}}
-63
View File
@@ -1,63 +0,0 @@
import {
findNodeOfType,
renderToText,
replaceNodesMatching,
} from "@silverbulletmd/silverbullet/lib/tree";
import { markdown } from "@silverbulletmd/silverbullet/syscalls";
import { isLocalPath } from "@silverbulletmd/silverbullet/lib/resolve";
export function encodePageUrl(name: string): string {
return name;
}
export async function cleanMarkdown(
text: string,
validPages?: string[],
): Promise<string> {
const mdTree = await markdown.parseMarkdown(text);
replaceNodesMatching(mdTree, (n) => {
if (n.type === "WikiLink") {
const page = n.children![1].children![0].text!;
if (validPages && !validPages.includes(page)) {
return {
// HACK
text: `_${page}_`,
};
}
return {
// HACK
text: `[${page}](/${encodePageUrl(page)})`,
};
}
// Simply get rid of these
if (
n.type === "CommentBlock" ||
n.type === "Comment" ||
n.type === "NamedAnchor"
) {
return null;
}
if (n.type === "Hashtag") {
return {
text: `__${n.children![0].text}__`,
};
}
if (n.type === "URL") {
const url = n.children![0].text!;
if (isLocalPath(url)) {
n.children![0].text = `fs/${url}`;
}
console.log("Link", url);
}
if (n.type === "FencedCode") {
const codeInfoNode = findNodeOfType(n, "CodeInfo");
if (!codeInfoNode) {
return;
}
if (codeInfoNode.children![0].text === "meta") {
return null;
}
}
});
return renderToText(mdTree);
}
+2 -2
View File
@@ -11,7 +11,7 @@ import { SpaceServer } from "./space_server.ts";
import type { KvPrimitives } from "../lib/data/kv_primitives.ts";
import { extendedMarkdownLanguage } from "../web/markdown_parser/parser.ts";
import { parse } from "../web/markdown_parser/parse_tree.ts";
import { renderMarkdownToHtml } from "../plugs/markdown/markdown_render.ts";
import { renderMarkdownToHtml } from "../web/markdown/markdown_render.ts";
import {
decodePageURI,
looksLikePathWithExtension,
@@ -19,7 +19,7 @@ import {
import { LockoutTimer } from "./lockout.ts";
import type { AuthOptions } from "../cmd/server.ts";
import type { ClientConfig } from "../web/client.ts";
import { htmlEscape } from "../plugs/markdown/html_render.ts";
import { htmlEscape } from "../web/markdown/html_render.ts";
import { applyUrlPrefix, removeUrlPrefix } from "../lib/url_prefix.ts";
const authenticationExpirySeconds = 60 * 60 * 24 * 7; // 1 week
+1 -1
View File
@@ -1,6 +1,6 @@
import { WidgetType } from "@codemirror/view";
import type { Client } from "../client.ts";
import { renderMarkdownToHtml } from "../../plugs/markdown/markdown_render.ts";
import { renderMarkdownToHtml } from "../markdown/markdown_render.ts";
import {
isLocalPath,
resolvePath,
+1 -1
View File
@@ -7,7 +7,7 @@ import {
isCursorInRange,
} from "./util.ts";
import { renderMarkdownToHtml } from "../../plugs/markdown/markdown_render.ts";
import { renderMarkdownToHtml } from "../markdown/markdown_render.ts";
import { type ParseTree, renderToText } from "../../plug-api/lib/tree.ts";
import { lezerToParseTree } from "../markdown_parser/parse_tree.ts";
import type { Client } from "../client.ts";
+109
View File
@@ -0,0 +1,109 @@
import { parse } from "../markdown_parser/parse_tree.ts";
import { renderMarkdownToHtml } from "./markdown_render.ts";
import { extendedMarkdownLanguage } from "../markdown_parser/parser.ts";
import { assertEquals } from "@std/assert";
const sampleMarkdown = `---
name: Sup
---
# Hello world
This is **bold** and _italic_, or _italic_. And a **_mix_**. And ==highlight==!
Lists:
- This
- Is a
- list
- And here we go nested
1. This is a numbered
2. Two
- And different
- Bla
- More bla
And:
1. Numbered
2. Two
## Second heading
And some
And like this:
More code
Bla
And a blockquote:
> Sup yo Empty line\
> Second part
<!-- this is a comment -->
And more custom stuff [[Page link]]
{[Command button]}
- [ ] #next Task
- [x] #next Task 2
## Tables
| type | actor_login | created_at | payload_ref |
| --------- | ----------- | -------------------- | ---------------------- |
| PushEvent | avb | 2022-10-27T08:27:48Z | refs/heads/master |
| PushEvent | avb | 2022-10-27T04:31:27Z | refs/heads/jitterSched |
Here is something
---
A new thing.
`;
Deno.test("Markdown render", () => {
const tree = parse(extendedMarkdownLanguage, sampleMarkdown);
renderMarkdownToHtml(tree, {
failOnUnknown: true,
});
});
Deno.test("Smart hard break test", () => {
const example = `**Hello**
*world!*`;
const tree = parse(extendedMarkdownLanguage, example);
const html = renderMarkdownToHtml(tree, {
failOnUnknown: true,
smartHardBreak: true,
});
assertEquals(
html,
`<span class="p"><strong>Hello</strong><br><em>world!</em></span>`,
);
const example2 = `This is going to be a text. With a new line.
And another
* and a list
* with a second item
### [[Bla]]
Url: something
Server: something else
📅 last_updated - [Release notes](release_notes_url)`;
const tree2 = parse(extendedMarkdownLanguage, example2);
const html2 = renderMarkdownToHtml(tree2, {
failOnUnknown: true,
smartHardBreak: true,
});
console.log(html2);
});
+12
View File
@@ -5,6 +5,10 @@ import { extendedMarkdownLanguage } from "../markdown_parser/parser.ts";
import { expandMarkdown } from "../markdown.ts";
import type { Client } from "../client.ts";
import { LuaStackFrame } from "../../lib/space_lua/runtime.ts";
import {
type MarkdownRenderOptions,
renderMarkdownToHtml,
} from "../markdown/markdown_render.ts";
export function markdownSyscalls(client: Client): SysCallMapping {
return {
@@ -22,5 +26,13 @@ export function markdownSyscalls(client: Client): SysCallMapping {
LuaStackFrame.lostFrame,
);
},
"markdown.markdownToHtml": (
_ctx,
text: string,
options: MarkdownRenderOptions = {},
) => {
const mdTree = parse(extendedMarkdownLanguage, text);
return renderMarkdownToHtml(mdTree, options);
},
};
}