Complete redo of content indexing and querying (#517)
Complete redo of data store Introduces live queries and live templates
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import { niceDate } from "$sb/lib/dates.ts";
|
||||
|
||||
export function handlebarHelpers() {
|
||||
return {
|
||||
json: (v: any) => JSON.stringify(v),
|
||||
niceDate: (ts: any) => niceDate(new Date(ts)),
|
||||
escapeRegexp: (ts: any) => {
|
||||
return ts.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
|
||||
},
|
||||
replaceRegexp: (s: string, regexp: string, replacement: string) => {
|
||||
return s.replace(new RegExp(regexp, "g"), replacement);
|
||||
},
|
||||
prefixLines: (v: string, prefix: string) =>
|
||||
v.split("\n").map((l) => prefix + l).join("\n"),
|
||||
substring: (s: string, from: number, to: number, elipsis = "") =>
|
||||
s.length > to - from ? s.substring(from, to) + elipsis : s,
|
||||
|
||||
today: () => niceDate(new Date()),
|
||||
tomorrow: () => {
|
||||
const tomorrow = new Date();
|
||||
tomorrow.setDate(tomorrow.getDate() + 1);
|
||||
return niceDate(tomorrow);
|
||||
},
|
||||
yesterday: () => {
|
||||
const yesterday = new Date();
|
||||
yesterday.setDate(yesterday.getDate() - 1);
|
||||
return niceDate(yesterday);
|
||||
},
|
||||
lastWeek: () => {
|
||||
const lastWeek = new Date();
|
||||
lastWeek.setDate(lastWeek.getDate() - 7);
|
||||
return niceDate(lastWeek);
|
||||
},
|
||||
nextWeek: () => {
|
||||
const nextWeek = new Date();
|
||||
nextWeek.setDate(nextWeek.getDate() + 7);
|
||||
return niceDate(nextWeek);
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { SysCallMapping } from "../../plugos/system.ts";
|
||||
import { handlebarHelpers } from "./handlebar_helpers.ts";
|
||||
import Handlebars from "handlebars";
|
||||
|
||||
export function handlebarsSyscalls(): SysCallMapping {
|
||||
return {
|
||||
"handlebars.renderTemplate": (
|
||||
_ctx,
|
||||
template: string,
|
||||
obj: any,
|
||||
globals: Record<string, any> = {},
|
||||
): string => {
|
||||
const templateFn = Handlebars.compile(
|
||||
template,
|
||||
{ noEscape: true },
|
||||
);
|
||||
return templateFn(obj, {
|
||||
helpers: handlebarHelpers(),
|
||||
data: globals,
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { SysCallMapping } from "../../plugos/system.ts";
|
||||
import { parse } from "../markdown_parser/parse_tree.ts";
|
||||
import type { ParseTree } from "$sb/lib/tree.ts";
|
||||
import { languageFor } from "../languages.ts";
|
||||
|
||||
export function languageSyscalls(): SysCallMapping {
|
||||
return {
|
||||
"language.parseLanguage": (
|
||||
_ctx,
|
||||
language: string,
|
||||
code: string,
|
||||
): ParseTree => {
|
||||
const lang = languageFor(language);
|
||||
if (!lang) {
|
||||
throw new Error(`Unknown language ${language}`);
|
||||
}
|
||||
return parse(lang, code);
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { SysCallMapping } from "../../plugos/system.ts";
|
||||
import { parse } from "../markdown_parser/parse_tree.ts";
|
||||
import { Language } from "../../web/deps.ts";
|
||||
import type { ParseTree } from "$sb/lib/tree.ts";
|
||||
|
||||
export function markdownSyscalls(lang: Language): SysCallMapping {
|
||||
return {
|
||||
"markdown.parseMarkdown": (_ctx, text: string): ParseTree => {
|
||||
return parse(lang, text);
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { SysCallMapping } from "../../plugos/system.ts";
|
||||
import { YAML } from "../../web/deps.ts";
|
||||
|
||||
export function yamlSyscalls(): SysCallMapping {
|
||||
return {
|
||||
"yaml.parse": (_ctx, text: string): any => {
|
||||
return YAML.parse(text);
|
||||
},
|
||||
"yaml.stringify": (_ctx, obj: any): string => {
|
||||
return YAML.stringify(obj, {
|
||||
noArrayIndent: true,
|
||||
noCompatMode: true,
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user