Code widget refactor

This commit is contained in:
Zef Hemel
2023-10-31 10:33:38 +01:00
parent 4ef857acf4
commit d8318c4ad7
13 changed files with 64 additions and 32 deletions
+5 -1
View File
@@ -6,7 +6,7 @@ import { extractFrontmatter } from "$sb/lib/frontmatter.ts";
import { extractAttributes } from "$sb/lib/attribute.ts";
import { indexObjects } from "./api.ts";
type PageObject = ObjectValue<
export type PageObject = ObjectValue<
// The base is PageMeta, but we override lastModified to be a string
Omit<PageMeta, "lastModified"> & {
lastModified: string; // indexing it as a string
@@ -14,6 +14,10 @@ type PageObject = ObjectValue<
>;
export async function indexPage({ name, tree }: IndexTreeEvent) {
if (name.startsWith("_")) {
// Don't index pages starting with _
return;
}
const pageMeta = await space.getPageMeta(name);
let pageObj: PageObject = {
ref: name,
+1 -1
View File
@@ -24,7 +24,7 @@ async function init() {
const body = widget.innerText;
try {
const result = await syscall("widget.render", lang, body);
const result = await syscall("codeWidget.render", lang, body, pageName);
const iframe = document.createElement("iframe");
iframe.src = "about:blank";
+4 -2
View File
@@ -5,6 +5,7 @@ import { renderMarkdownToHtml } from "./markdown_render.ts";
export async function markdownContentWidget(
markdownText: string,
pageName: string,
): Promise<WidgetContent> {
// Parse markdown to a ParseTree
const mdTree = await markdown.parseMarkdown(markdownText);
@@ -12,16 +13,17 @@ export async function markdownContentWidget(
const html = renderMarkdownToHtml(mdTree, { smartHardBreak: true });
return {
html: await wrapHTML(html),
script: await prepareJS(markdownText),
script: await prepareJS(markdownText, pageName),
// And add back the markdown text so we can render it in a different way if desired
markdown: markdownText,
};
}
export async function prepareJS(originalMarkdown: string) {
export async function prepareJS(pageName: string, originalMarkdown: string) {
const iframeJS = await asset.readAsset("assets/markdown_widget.js");
return `
const panelHtml = ${JSON.stringify(panelHtml)};
const pageName = ${JSON.stringify(pageName)};
const originalMarkdown = ${JSON.stringify(originalMarkdown)};
${iframeJS}
`;
+6 -4
View File
@@ -1,13 +1,15 @@
import type { WidgetContent } from "$sb/app_event.ts";
import { editor, events, language, space, system } from "$sb/syscalls.ts";
import { events, language, space, system } from "$sb/syscalls.ts";
import { parseTreeToAST } from "$sb/lib/tree.ts";
import { astToKvQuery } from "$sb/lib/parse-query.ts";
import { jsonToMDTable, renderTemplate } from "../directive/util.ts";
import { replaceTemplateVars } from "../template/template.ts";
import { parse } from "../../common/markdown_parser/parse_tree.ts";
export async function widget(bodyText: string): Promise<WidgetContent> {
const pageMeta = await space.getPageMeta(await editor.getCurrentPage());
export async function widget(
bodyText: string,
pageName: string,
): Promise<WidgetContent> {
const pageMeta = await space.getPageMeta(pageName);
try {
const queryAST = parseTreeToAST(