forked from silverbulletmd/silverbullet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
No longer index templates tagged as #template
Showing
19 changed files
with
307 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { handlebars, markdown, YAML } from "$sb/syscalls.ts"; | ||
import type { PageMeta } from "$sb/types.ts"; | ||
import { extractFrontmatter } from "$sb/lib/frontmatter.ts"; | ||
import { TemplateObject } from "./types.ts"; | ||
import { renderToText } from "$sb/lib/tree.ts"; | ||
|
||
/** | ||
* Strips the template from its frontmatter and renders it. | ||
* The assumption is that the frontmatter has already been parsed and should not appear in thhe rendered output. | ||
* @param templateText the template text | ||
* @param data data to be rendered by the template | ||
* @param globals a set of global variables | ||
* @returns | ||
*/ | ||
export async function renderTemplate( | ||
templateText: string, | ||
pageMeta: PageMeta, | ||
data: any = {}, | ||
): Promise<string> { | ||
const tree = await markdown.parseMarkdown(templateText); | ||
const frontmatter: Partial<TemplateObject> = await extractFrontmatter(tree, { | ||
removeFrontmatterSection: true, | ||
removeTags: ["template"], | ||
}); | ||
templateText = renderToText(tree).trimStart(); | ||
// console.log(`Trimmed template: |${templateText}|`); | ||
// If a 'frontmatter' key was specified in the frontmatter, use that as the frontmatter | ||
if (frontmatter.frontmatter) { | ||
if (typeof frontmatter.frontmatter === "string") { | ||
templateText = "---\n" + frontmatter.frontmatter + "---\n" + templateText; | ||
} else { | ||
templateText = "---\n" + (await YAML.stringify(frontmatter.frontmatter)) + | ||
"---\n" + templateText; | ||
} | ||
} | ||
return handlebars.renderTemplate(templateText, data, { page: pageMeta }); | ||
} | ||
|
||
/** | ||
* Strips a template text from its frontmatter and #template tag | ||
*/ | ||
export async function cleanTemplate( | ||
templateText: string, | ||
): Promise<string> { | ||
const tree = await markdown.parseMarkdown(templateText); | ||
await extractFrontmatter(tree, { | ||
removeFrontmatterSection: true, | ||
removeTags: ["template"], | ||
}); | ||
return renderToText(tree).trimStart(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { IndexTreeEvent } from "$sb/app_event.ts"; | ||
import { system } from "$sb/syscalls.ts"; | ||
|
||
export async function indexTemplate({ name, tree }: IndexTreeEvent) { | ||
// Just delegate to the index plug | ||
await system.invokeFunction("index.indexPage", { name, tree }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { PageMeta } from "$sb/types.ts"; | ||
import { system } from "../../plug-api/syscalls.ts"; | ||
|
||
export function renderTemplate( | ||
templateText: string, | ||
pageMeta: PageMeta, | ||
data: any = {}, | ||
): Promise<string> { | ||
return system.invokeFunction( | ||
"template.renderTemplate", | ||
templateText, | ||
pageMeta, | ||
data, | ||
); | ||
} | ||
|
||
export function cleanTemplate( | ||
templateText: string, | ||
): Promise<string> { | ||
return system.invokeFunction( | ||
"template.cleanTemplate", | ||
templateText, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.