Rebuilt frontmatter templates as template widgets
This commit is contained in:
@@ -1,76 +0,0 @@
|
||||
import { assertEquals } from "../../test_deps.ts";
|
||||
import { isTemplate } from "./util.ts";
|
||||
|
||||
Deno.test("Test template extraction", () => {
|
||||
assertEquals(
|
||||
isTemplate(`---
|
||||
name: bla
|
||||
tags: template
|
||||
---
|
||||
|
||||
Sup`),
|
||||
true,
|
||||
);
|
||||
|
||||
assertEquals(
|
||||
isTemplate(`---
|
||||
tags: template, something else
|
||||
---
|
||||
`),
|
||||
true,
|
||||
);
|
||||
|
||||
assertEquals(
|
||||
isTemplate(`---
|
||||
tags: something else, template
|
||||
---
|
||||
`),
|
||||
true,
|
||||
);
|
||||
|
||||
assertEquals(
|
||||
isTemplate(`---
|
||||
tags:
|
||||
- bla
|
||||
- template
|
||||
---
|
||||
`),
|
||||
true,
|
||||
);
|
||||
|
||||
assertEquals(
|
||||
isTemplate(`#template`),
|
||||
true,
|
||||
);
|
||||
|
||||
assertEquals(
|
||||
isTemplate(` #template This is a template`),
|
||||
true,
|
||||
);
|
||||
|
||||
assertEquals(
|
||||
isTemplate(`---
|
||||
tags:
|
||||
- bla
|
||||
somethingElse:
|
||||
- template
|
||||
---
|
||||
`),
|
||||
false,
|
||||
);
|
||||
|
||||
assertEquals(
|
||||
isTemplate(`---
|
||||
name: bla
|
||||
tags: aefe
|
||||
---
|
||||
|
||||
Sup`),
|
||||
false,
|
||||
);
|
||||
|
||||
assertEquals(
|
||||
isTemplate(`Sup`),
|
||||
false,
|
||||
);
|
||||
});
|
||||
@@ -1,34 +1,8 @@
|
||||
import { determineTags } from "$sb/lib/cheap_yaml.ts";
|
||||
import { handlebarHelpers } from "../../common/syscalls/handlebar_helpers.ts";
|
||||
import { PageMeta } from "$sb/types.ts";
|
||||
import { handlebars, space } from "$sb/syscalls.ts";
|
||||
import { cleanTemplate } from "./plug_api.ts";
|
||||
|
||||
const frontMatterRegex = /^---\n(([^\n]|\n)*?)---\n/;
|
||||
|
||||
/**
|
||||
* Quick and dirty way to check if a page is a template or not
|
||||
* @param pageText
|
||||
* @returns
|
||||
*/
|
||||
export function isTemplate(pageText: string): boolean {
|
||||
const frontmatter = frontMatterRegex.exec(pageText);
|
||||
// Poor man's YAML frontmatter parsing
|
||||
if (frontmatter) {
|
||||
pageText = pageText.slice(frontmatter[0].length);
|
||||
const frontmatterText = frontmatter[1];
|
||||
const tags = determineTags(frontmatterText);
|
||||
if (tags.includes("template")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// Or if the page text starts with a #template tag
|
||||
if (/^\s*#template(\W|$)/.test(pageText)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function buildHandebarOptions(pageMeta: PageMeta) {
|
||||
return {
|
||||
helpers: handlebarHelpers(),
|
||||
|
||||
Reference in New Issue
Block a user