From 6347c2b4d8f301b5bd0c553285ed344fe668a97b Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Wed, 15 Nov 2023 16:14:15 +0100 Subject: [PATCH] Rewrite page references in federated pages --- common/util.ts | 1 - plug-api/lib/resolve.ts | 27 +++++++++++++++++++++++++++ plugs/directive/util.ts | 3 ++- plugs/federation/federation.plug.yaml | 1 + plugs/query/template.ts | 6 ++---- 5 files changed, 32 insertions(+), 6 deletions(-) diff --git a/common/util.ts b/common/util.ts index 1493f93..a63bf28 100644 --- a/common/util.ts +++ b/common/util.ts @@ -1,7 +1,6 @@ import { SETTINGS_TEMPLATE } from "./settings_template.ts"; import { YAML } from "./deps.ts"; import { SpacePrimitives } from "./spaces/space_primitives.ts"; -import { template } from "https://esm.sh/v130/handlebars@4.7.7/runtime.d.ts"; export function safeRun(fn: () => Promise) { fn().catch((e) => { diff --git a/plug-api/lib/resolve.ts b/plug-api/lib/resolve.ts index d9fb8d4..08acdce 100644 --- a/plug-api/lib/resolve.ts +++ b/plug-api/lib/resolve.ts @@ -55,6 +55,24 @@ export function rewritePageRefs(tree: ParseTree, containerPageName: string) { return true; } + if (n.type === "FencedCode") { + const codeInfo = findNodeOfType(n, "CodeInfo"); + if (!codeInfo) { + return true; + } + if (!["query", "template"].includes(codeInfo.children![0].text!)) { + return true; + } + const codeText = findNodeOfType(n, "CodeText"); + if (!codeText) { + return true; + } + let bodyText = codeText.children![0].text!; + bodyText = rewritePageRefsInString(bodyText, containerPageName); + codeText.children![0].text = bodyText; + + return true; + } if (n.type === "WikiLinkPage") { n.children![0].text = resolvePath( containerPageName, @@ -67,6 +85,15 @@ export function rewritePageRefs(tree: ParseTree, containerPageName: string) { }); } +export function rewritePageRefsInString( + bodyText: string, + containerPageName: string, +) { + return bodyText.replaceAll(/\[\[(.+)\]\]/g, (_match, pageRefName) => { + return `[[${resolvePath(containerPageName, pageRefName)}]]`; + }); +} + export function cleanPageRef(pageRef: string) { if (pageRef.startsWith("[[") && pageRef.endsWith("]]")) { return pageRef.slice(2, -2); diff --git a/plugs/directive/util.ts b/plugs/directive/util.ts index 2b09139..c3aa640 100644 --- a/plugs/directive/util.ts +++ b/plugs/directive/util.ts @@ -1,7 +1,7 @@ import { handlebars, space } from "$sb/syscalls.ts"; import { handlebarHelpers } from "../../common/syscalls/handlebar_helpers.ts"; import { PageMeta } from "$sb/types.ts"; -import { cleanTemplate, renderTemplate } from "../template/plug_api.ts"; +import { cleanTemplate } from "../template/plug_api.ts"; export function defaultJsonTransformer(_k: string, v: any) { if (v === undefined) { @@ -62,6 +62,7 @@ export async function renderQueryTemplate( ): Promise { let templateText = await space.readPage(templatePage); templateText = await cleanTemplate(templateText); + if (!renderAll) { templateText = `{{#each .}}\n${templateText}\n{{/each}}`; } diff --git a/plugs/federation/federation.plug.yaml b/plugs/federation/federation.plug.yaml index 962db57..c7f3460 100644 --- a/plugs/federation/federation.plug.yaml +++ b/plugs/federation/federation.plug.yaml @@ -4,6 +4,7 @@ requiredPermissions: functions: listFiles: path: ./federation.ts:listFiles + env: server pageNamespace: pattern: "!.+" operation: listFiles diff --git a/plugs/query/template.ts b/plugs/query/template.ts index a248672..fb50c3c 100644 --- a/plugs/query/template.ts +++ b/plugs/query/template.ts @@ -1,10 +1,10 @@ import { WidgetContent } from "$sb/app_event.ts"; import { markdown, space, system, YAML } from "$sb/syscalls.ts"; -import { rewritePageRefs } from "$sb/lib/resolve.ts"; import { loadPageObject, replaceTemplateVars } from "../template/template.ts"; -import { renderToText } from "$sb/lib/tree.ts"; import { PageMeta } from "$sb/types.ts"; import { renderTemplate } from "../template/plug_api.ts"; +import { renderToText } from "$sb/lib/tree.ts"; +import { rewritePageRefs } from "$sb/lib/resolve.ts"; type TemplateConfig = { // Pull the template from a page @@ -40,8 +40,6 @@ export async function widget( ) : undefined; - console.log("Value", value); - let { text: rendered } = config.raw ? { text: templateText } : await renderTemplate(