Skip to content

Commit

Permalink
Fix template embedding via federation
Browse files Browse the repository at this point in the history
  • Loading branch information
zefhemel committed Oct 3, 2023
1 parent beba6a2 commit 91387d1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
19 changes: 10 additions & 9 deletions plugs/query/template.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -20,16 +21,12 @@ export async function widget(bodyText: string): Promise<WidgetContent> {
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
Expand All @@ -42,6 +39,10 @@ export async function widget(bodyText: string): Promise<WidgetContent> {
},
);
const parsedMarkdown = await markdown.parseMarkdown(rendered);

if (templatePage) {
rewritePageRefs(parsedMarkdown, templatePage);
}
const html = renderMarkdownToHtml(parsedMarkdown, {
smartHardBreak: true,
});
Expand Down
1 change: 0 additions & 1 deletion website/Templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down
1 change: 0 additions & 1 deletion website/🔌 Template.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down

0 comments on commit 91387d1

Please sign in to comment.