Rebuilt frontmatter templates as template widgets
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { assertEquals } from "../../test_deps.ts";
|
||||
import { determineTags } from "./cheap_yaml.ts";
|
||||
import { determineTags, isTemplate } from "./cheap_yaml.ts";
|
||||
|
||||
Deno.test("cheap yaml", () => {
|
||||
assertEquals([], determineTags(""));
|
||||
@@ -14,3 +14,77 @@ Deno.test("cheap yaml", () => {
|
||||
determineTags(`tags:\n- "#bla"\n- template`),
|
||||
);
|
||||
});
|
||||
|
||||
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,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -34,3 +34,28 @@ export function determineTags(yamlText: string): string[] {
|
||||
}
|
||||
return tags;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ export function render(
|
||||
lang: string,
|
||||
body: string,
|
||||
pageName: string,
|
||||
): Promise<CodeWidgetContent> {
|
||||
): Promise<CodeWidgetContent | null> {
|
||||
return syscall("codeWidget.render", lang, body, pageName);
|
||||
}
|
||||
|
||||
|
||||
+1
-2
@@ -127,14 +127,13 @@ export type ObjectQuery = Omit<Query, "prefix">;
|
||||
export type CodeWidgetCallback = (
|
||||
bodyText: string,
|
||||
pageName: string,
|
||||
) => Promise<CodeWidgetContent>;
|
||||
) => Promise<CodeWidgetContent | null>;
|
||||
|
||||
export type CodeWidgetContent = {
|
||||
html?: string;
|
||||
markdown?: string;
|
||||
script?: string;
|
||||
buttons?: CodeWidgetButton[];
|
||||
banner?: string;
|
||||
};
|
||||
|
||||
export type CodeWidgetButton = {
|
||||
|
||||
Reference in New Issue
Block a user