1
0
This commit is contained in:
Zef Hemel 2024-01-25 12:06:01 +01:00
parent 8404256ccb
commit 928614c724
11 changed files with 17 additions and 26 deletions

View File

@ -10,6 +10,8 @@ import { parseQuery } from "$sb/lib/parse-query.ts";
type TemplateWidgetConfig = { type TemplateWidgetConfig = {
// Pull the template from a page // Pull the template from a page
page?: string; page?: string;
// Include a page raw (without template processing)
raw?: string;
// Or use a string directly // Or use a string directly
template?: string; template?: string;
// To feed data into the template you can either use a concrete value // To feed data into the template you can either use a concrete value
@ -17,9 +19,6 @@ type TemplateWidgetConfig = {
// Or a query // Or a query
query?: string; query?: string;
// If true, don't render the template, just use it as-is
raw?: boolean;
}; };
export async function widget( export async function widget(
@ -31,7 +30,8 @@ export async function widget(
try { try {
const config: TemplateWidgetConfig = await YAML.parse(bodyText); const config: TemplateWidgetConfig = await YAML.parse(bodyText);
let templateText = config.template || ""; let templateText = config.template || "";
let templatePage = config.page; let templatePage = config.page ||
(typeof config.raw !== "boolean" && config.raw);
if (templatePage) { if (templatePage) {
// Rewrite federation page references // Rewrite federation page references
templatePage = rewritePageRefsInString(templatePage, pageName); templatePage = rewritePageRefsInString(templatePage, pageName);
@ -39,13 +39,13 @@ export async function widget(
templatePage = templatePage.slice(2, -2); templatePage = templatePage.slice(2, -2);
} }
if (!templatePage) { if (!templatePage) {
throw new Error("No template page specified"); throw new Error("No page specified");
} }
try { try {
templateText = await space.readPage(templatePage); templateText = await space.readPage(templatePage);
} catch (e: any) { } catch (e: any) {
if (e.message === "Not found") { if (e.message === "Not found") {
throw new Error(`Template page ${templatePage} not found`); throw new Error(`Page "${templatePage}" not found`);
} }
} }
} }

View File

@ -34,9 +34,7 @@ template: |
{{/each}} {{/each}}
``` ```
If you just want to render the raw markdown without handling it as a handlebars template, set `raw` to true: If you want to include another _page_ (not necessarily a template) unprocessed (so without replacing template placeholders), you can use `raw`:
```template ```template
template: | raw: "[[internal/test page]]"
This is not going to be {{processed}} by Handlebars
raw: true
``` ```

View File

@ -7,6 +7,5 @@ share-support: true
--- ---
```template ```template
page: "[[!raw.githubusercontent.com/silverbulletmd/silverbullet-ghost/main/README]]" raw: "[[!raw.githubusercontent.com/silverbulletmd/silverbullet-ghost/main/README]]"
raw: true
``` ```

View File

@ -6,6 +6,5 @@ author: Zef Hemel
--- ---
```template ```template
page: "[[!raw.githubusercontent.com/silverbulletmd/silverbullet-git/main/README]]" raw: "[[!raw.githubusercontent.com/silverbulletmd/silverbullet-git/main/README]]"
raw: true
``` ```

View File

@ -7,6 +7,5 @@ tags: plug
--- ---
```template ```template
page: "[[!raw.githubusercontent.com/silverbulletmd/silverbullet-github/main/README]]" raw: "[[!raw.githubusercontent.com/silverbulletmd/silverbullet-github/main/README]]"
raw: true
``` ```

View File

@ -7,6 +7,5 @@ author: Zef Hemel
--- ---
```template ```template
page: "[[!raw.githubusercontent.com/silverbulletmd/silverbullet-katex/main/README]]" raw: "[[!raw.githubusercontent.com/silverbulletmd/silverbullet-katex/main/README]]"
raw: true
``` ```

View File

@ -10,6 +10,5 @@ tags: plug
```template ```template
page: "[[!raw.githubusercontent.com/silverbulletmd/silverbullet-mattermost/main/README]]" raw: "[[!raw.githubusercontent.com/silverbulletmd/silverbullet-mattermost/main/README]]"
raw: true
``` ```

View File

@ -12,6 +12,5 @@ flowchart TD
``` ```
```template ```template
page: "[[!raw.githubusercontent.com/silverbulletmd/silverbullet-mermaid/main/README]]" raw: "[[!raw.githubusercontent.com/silverbulletmd/silverbullet-mermaid/main/README]]"
raw: true
``` ```

View File

@ -6,6 +6,5 @@ tags: plug
--- ---
```template ```template
page: "[[!raw.githubusercontent.com/m1lt0n/silverbullet-serendipity/main/README]]" raw: "[[!raw.githubusercontent.com/m1lt0n/silverbullet-serendipity/main/README]]"
raw: true
``` ```

View File

@ -8,6 +8,5 @@ author: SilverBullet Authors
Remember Twitter? Remember Twitter?
```template ```template
page: "[[!raw.githubusercontent.com/silverbulletmd/silverbullet-twitter/main/README]]" raw: "[[!raw.githubusercontent.com/silverbulletmd/silverbullet-twitter/main/README]]"
raw: true
``` ```

View File

@ -0,0 +1 @@
This is a simple test page