Fix template embedding via federation

pull/528/head
Zef Hemel 2023-10-03 18:09:03 +02:00
parent beba6a2c09
commit 91387d1e96
3 changed files with 10 additions and 11 deletions

View File

@ -1,5 +1,6 @@
import { WidgetContent } from "$sb/app_event.ts"; import { WidgetContent } from "$sb/app_event.ts";
import { editor, handlebars, markdown, space, YAML } from "$sb/syscalls.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 { renderMarkdownToHtml } from "../markdown/markdown_render.ts";
import { prepareJS, wrapHTML } from "./util.ts"; import { prepareJS, wrapHTML } from "./util.ts";
@ -20,16 +21,12 @@ export async function widget(bodyText: string): Promise<WidgetContent> {
try { try {
const config: TemplateConfig = await YAML.parse(bodyText); const config: TemplateConfig = await YAML.parse(bodyText);
let templateText = config.template || ""; let templateText = config.template || "";
if (config.page) { let templatePage = config.page;
let page = config.page; if (templatePage) {
if (!page) { if (templatePage.startsWith("[[")) {
throw new Error("Missing `page`"); templatePage = templatePage.slice(2, -2);
} }
templateText = await space.readPage(templatePage);
if (page.startsWith("[[")) {
page = page.slice(2, -2);
}
templateText = await space.readPage(page);
} }
const rendered = config.raw const rendered = config.raw
@ -42,6 +39,10 @@ export async function widget(bodyText: string): Promise<WidgetContent> {
}, },
); );
const parsedMarkdown = await markdown.parseMarkdown(rendered); const parsedMarkdown = await markdown.parseMarkdown(rendered);
if (templatePage) {
rewritePageRefs(parsedMarkdown, templatePage);
}
const html = renderMarkdownToHtml(parsedMarkdown, { const html = renderMarkdownToHtml(parsedMarkdown, {
smartHardBreak: true, smartHardBreak: true,
}); });

View File

@ -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}}/ - `{{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 `* `{{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 - `{{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 ` - `{{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. - `{{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`). - `{{niceDate @page.lastModified}}` translates any timestamp into a “nice” format (e.g. `2023-06-20`).

View File

@ -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}}/ - `{{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 `* `{{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 - `{{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 ` - `{{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. - `{{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`). - `{{niceDate @page.lastModified}}` translates any timestamp into a “nice” format (e.g. `2023-06-20`).