Templates 2.0 (#636)

Templates 2.0 and a whole bunch of other refactoring
This commit is contained in:
Zef Hemel
2024-01-20 19:16:07 +01:00
committed by GitHub
parent 0a6a0016a2
commit f30b1d3418
171 changed files with 2038 additions and 1628 deletions
@@ -6,3 +6,11 @@ A list of built-in settings [[!silverbullet.md/SETTINGS|can be found here]].
indexPage: index
\`\`\`
`;
export const INDEX_TEMPLATE =
`Hello! And welcome to your brand new SilverBullet space!
\`\`\`template
page: "[[!silverbullet.md/Getting Started]]"
\`\`\`
`;
+8 -1
View File
@@ -71,7 +71,12 @@ export {
Text,
Transaction,
} from "@codemirror/state";
export type { ChangeSpec, Extension, StateCommand } from "@codemirror/state";
export type {
ChangeSpec,
Compartment,
Extension,
StateCommand,
} from "@codemirror/state";
export {
codeFolding,
defaultHighlightStyle,
@@ -132,3 +137,5 @@ export {
export { mime } from "https://deno.land/x/mimetypes@v1.0.0/mod.ts";
export { compile as gitIgnoreCompiler } from "https://esm.sh/gitignore-parser@0.0.2";
export { z } from "https://deno.land/x/zod@v3.22.4/mod.ts";
+1
View File
@@ -34,6 +34,7 @@ export const builtinLanguages: Record<string, Language> = {
"meta": StreamLanguage.define(yamlLanguage),
"yaml": StreamLanguage.define(yamlLanguage),
"template": StreamLanguage.define(yamlLanguage),
"block": StreamLanguage.define(yamlLanguage),
"embed": StreamLanguage.define(yamlLanguage),
"data": StreamLanguage.define(yamlLanguage),
"toc": StreamLanguage.define(yamlLanguage),
+15
View File
@@ -0,0 +1,15 @@
import { FunctionMap } from "$sb/types.ts";
import { builtinFunctions } from "$sb/lib/builtin_query_functions.ts";
export function buildQueryFunctions(allKnownPages: Set<string>): FunctionMap {
return {
...builtinFunctions,
pageExists: (name: string) => {
if (name.startsWith("!") || name.startsWith("{{")) {
// Let's assume federated pages exist, and ignore template variable ones
return true;
}
return allKnownPages.has(name);
},
};
}
+1 -1
View File
@@ -16,7 +16,7 @@ export async function ensureSpaceIndex(ds: DataStore, system: System<any>) {
if (currentIndexVersion !== desiredIndexVersion && !indexOngoing) {
console.info("Performing a full space reindex, this could take a while...");
indexOngoing = true;
await system.loadedPlugs.get("index")!.invoke("reindexSpace", []);
await system.invokeFunction("index.reindexSpace", []);
console.info("Full space index complete.");
await markFullSpaceIndexComplete(ds);
indexOngoing = false;
+9
View File
@@ -40,6 +40,15 @@ export function handlebarHelpers() {
nextWeek.setDate(nextWeek.getDate() + 7);
return niceDate(nextWeek);
},
weekStart: (startOnMonday = true) => {
const d = new Date();
const day = d.getDay();
let diff = d.getDate() - day;
if (startOnMonday) {
diff += day == 0 ? -6 : 1;
}
return niceDate(new Date(d.setDate(diff)));
},
ifEq: function (v1: any, v2: any, options: any) {
if (v1 === v2) {
return options.fn(this);
+16 -8
View File
@@ -10,14 +10,22 @@ export function handlebarsSyscalls(): SysCallMapping {
obj: any,
globals: Record<string, any> = {},
): string => {
const templateFn = Handlebars.compile(
template,
{ noEscape: true },
);
return templateFn(obj, {
helpers: handlebarHelpers(),
data: globals,
});
return renderHandlebarsTemplate(template, obj, globals);
},
};
}
export function renderHandlebarsTemplate(
template: string,
obj: any,
globals: Record<string, any>,
) {
const templateFn = Handlebars.compile(
template,
{ noEscape: true },
);
return templateFn(obj, {
helpers: handlebarHelpers(),
data: globals,
});
}
+3 -9
View File
@@ -1,8 +1,9 @@
import { SETTINGS_TEMPLATE } from "./settings_template.ts";
import { SETTINGS_TEMPLATE } from "./PAGE_TEMPLATES.ts";
import { YAML } from "./deps.ts";
import { SpacePrimitives } from "./spaces/space_primitives.ts";
import { expandPropertyNames } from "$sb/lib/json.ts";
import type { BuiltinSettings } from "../web/types.ts";
import { INDEX_TEMPLATE } from "./PAGE_TEMPLATES.ts";
/**
* Runs a function safely by catching any errors and logging them to the console.
@@ -88,14 +89,7 @@ export async function ensureSettingsAndIndex(
);
await space.writeFile(
"index.md",
new TextEncoder().encode(
`Hello! And welcome to your brand new SilverBullet space!
\`\`\`template
page: "[[!silverbullet.md/Getting Started]]"
\`\`\`
`,
),
new TextEncoder().encode(INDEX_TEMPLATE),
);
}
}