From 91387d1e96c2e6ce1e9fbc1b7d4d630946ec2b43 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Tue, 3 Oct 2023 18:09:03 +0200 Subject: [PATCH] Fix template embedding via federation --- plugs/query/template.ts | 19 ++++++++++--------- website/Templates.md | 1 - website/πŸ”Œ Template.md | 1 - 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/plugs/query/template.ts b/plugs/query/template.ts index c29285d5..0abad251 100644 --- a/plugs/query/template.ts +++ b/plugs/query/template.ts @@ -1,5 +1,6 @@ import { WidgetContent } from "$sb/app_event.ts"; import { editor, handlebars, markdown, space, YAML } from "$sb/syscalls.ts"; +import { rewritePageRefs } from "$sb/lib/resolve.ts"; import { renderMarkdownToHtml } from "../markdown/markdown_render.ts"; import { prepareJS, wrapHTML } from "./util.ts"; @@ -20,16 +21,12 @@ export async function widget(bodyText: string): Promise { try { const config: TemplateConfig = await YAML.parse(bodyText); let templateText = config.template || ""; - if (config.page) { - let page = config.page; - if (!page) { - throw new Error("Missing `page`"); + let templatePage = config.page; + if (templatePage) { + if (templatePage.startsWith("[[")) { + templatePage = templatePage.slice(2, -2); } - - if (page.startsWith("[[")) { - page = page.slice(2, -2); - } - templateText = await space.readPage(page); + templateText = await space.readPage(templatePage); } const rendered = config.raw @@ -42,6 +39,10 @@ export async function widget(bodyText: string): Promise { }, ); const parsedMarkdown = await markdown.parseMarkdown(rendered); + + if (templatePage) { + rewritePageRefs(parsedMarkdown, templatePage); + } const html = renderMarkdownToHtml(parsedMarkdown, { smartHardBreak: true, }); diff --git a/website/Templates.md b/website/Templates.md index 2e037615..bf485f5b 100644 --- a/website/Templates.md +++ b/website/Templates.md @@ -21,7 +21,6 @@ There are a number of built-in handlebars helpers you can use - `{{escapeRegexp "hello/there"}}` to escape a regexp, useful when injecting e.g. a page name into a query β€” think `name =~ /{{escapeRegexp @page.name}}/ `* `{{replaceRegexp string regexp replacement}}`: replace a regular expression in a string, example use: `{{replaceRegexp name "#[^#\d\s\[\]]+\w+" ""}}` to remove hashtags from a task name - `{{json @page}}` translate any (object) value to JSON, mostly useful for debugging -- `{{relativePath @page.name}}` translate a path to a relative one (to the current page), useful when injecting page names, e.g. `{{relativePath name}}`. - `{{substring "my string" 0 3}}` performs a substring operation on the first argument, which in this example would result in `my ` - `{{prefixLines "my string\nanother" " "}}` prefixes each line (except the first) with the given prefix. - `{{niceDate @page.lastModified}}` translates any timestamp into a β€œnice” format (e.g. `2023-06-20`). diff --git a/website/πŸ”Œ Template.md b/website/πŸ”Œ Template.md index fed0e5ec..1ec7651d 100644 --- a/website/πŸ”Œ Template.md +++ b/website/πŸ”Œ Template.md @@ -87,7 +87,6 @@ Currently supported (hardcoded in the code): - `{{escapeRegexp "hello/there"}}` to escape a regexp, useful when injecting e.g. a page name into a query β€” think `name =~ /{{escapeRegexp @page.name}}/ `* `{{replaceRegexp string regexp replacement}}`: replace a regular expression in a string, example use: `{{replaceRegexp name "#[^#\d\s\[\]]+\w+" ""}}` to remove hashtags from a task name - `{{json @page}}` translate any (object) value to JSON, mostly useful for debugging -- `{{relativePath @page.name}}` translate a path to a relative one (to the current page), useful when injecting page names, e.g. `{{relativePath name}}`. - `{{substring "my string" 0 3}}` performs a substring operation on the first argument, which in this example would result in `my ` - `{{prefixLines "my string\nanother" " "}}` prefixes each line (except the first) with the given prefix. - `{{niceDate @page.lastModified}}` translates any timestamp into a β€œnice” format (e.g. `2023-06-20`).