silverbullet/common/settings.ts

140 lines
4.1 KiB
TypeScript
Raw Normal View History

import YAML from "js-yaml";
import { INDEX_TEMPLATE, SETTINGS_TEMPLATE } from "./PAGE_TEMPLATES.ts";
2024-07-30 23:33:33 +08:00
import type { SpacePrimitives } from "./spaces/space_primitives.ts";
2024-07-06 21:07:40 +08:00
import { cleanupJSON } from "../plug-api/lib/json.ts";
2024-07-30 23:33:33 +08:00
import type { BuiltinSettings } from "$type/settings.ts";
import type { DataStore, ObjectEnricher } from "$lib/data/datastore.ts";
import { parseExpression } from "$common/expression_parser.ts";
2024-07-30 23:33:33 +08:00
import type { QueryExpression } from "$sb/types.ts";
2022-08-02 18:43:39 +08:00
const yamlSettingsRegex = /^(```+|~~~+)ya?ml\r?\n([\S\s]+?)\1/m;
2022-08-02 18:43:39 +08:00
/**
* Parses YAML settings from a Markdown string.
* @param settingsMarkdown - The Markdown string containing the YAML settings.
* @returns An object representing the parsed YAML settings.
*/
2022-08-02 18:43:39 +08:00
export function parseYamlSettings(settingsMarkdown: string): {
[key: string]: any;
} {
const match = yamlSettingsRegex.exec(settingsMarkdown);
if (!match) {
return {};
}
const yaml = match[2]; // The first group captures the code fence to look for same terminator
2022-12-15 20:23:49 +08:00
try {
2023-11-29 23:51:28 +08:00
return YAML.load(yaml) as {
2022-12-15 20:23:49 +08:00
[key: string]: any;
};
} catch (e: any) {
console.error("Error parsing SETTINGS as YAML", e.message);
return {};
}
2022-08-02 18:43:39 +08:00
}
2024-01-25 18:42:36 +08:00
export const defaultSettings: BuiltinSettings = {
indexPage: "index",
hideSyncButton: false,
maximumAttachmentSize: 10, // MiB
2024-05-28 02:33:41 +08:00
defaultLinkStyle: "wikilink", // wikilink [[]] or markdown []()
2024-01-25 18:42:36 +08:00
actionButtons: [
{
icon: "Home",
description: "Go to the index page",
command: "Navigate: Home",
},
{
icon: "Book",
description: `Open page`,
command: "Navigate: Page Picker",
},
{
icon: "Terminal",
description: `Run command`,
command: "Open Command Palette",
},
],
};
/**
* Ensures that the settings and index page exist in the given space.
* If they don't exist, default settings and index page will be created.
* @param space - The SpacePrimitives object representing the space.
* @returns A promise that resolves to the built-in settings.
*/
2024-01-25 18:42:36 +08:00
export async function ensureAndLoadSettingsAndIndex(
space: SpacePrimitives,
): Promise<BuiltinSettings> {
2023-05-29 15:53:49 +08:00
let settingsText: string | undefined;
try {
2023-05-29 15:53:49 +08:00
settingsText = new TextDecoder().decode(
(await space.readFile("SETTINGS.md")).data,
);
} catch (e: any) {
if (e.message === "Not found") {
console.log("No settings found, creating default settings");
await space.writeFile(
"SETTINGS.md",
new TextEncoder().encode(SETTINGS_TEMPLATE),
true,
);
} else {
console.error("Error reading settings", e.message);
console.warn("Falling back to default settings");
2024-01-25 18:42:36 +08:00
return defaultSettings;
}
2023-05-29 15:53:49 +08:00
settingsText = SETTINGS_TEMPLATE;
// Ok, then let's also check the index page
try {
await space.getFileMeta("index.md");
} catch (e: any) {
console.log(
"No index page found, creating default index page",
e.message,
);
await space.writeFile(
"index.md",
new TextEncoder().encode(INDEX_TEMPLATE),
);
}
}
2023-05-29 15:53:49 +08:00
const settings: any = parseYamlSettings(settingsText);
2024-07-06 21:07:40 +08:00
cleanupJSON(settings);
2024-01-25 18:42:36 +08:00
return { ...defaultSettings, ...settings };
}
export function updateObjectDecorators(
settings: BuiltinSettings,
ds: DataStore,
) {
if (settings.objectDecorators) {
// Reload object decorators
const newDecorators: ObjectEnricher[] = [];
for (
const decorator of settings.objectDecorators
) {
try {
const parsedWhere = parseExpression(decorator.where);
const parsedDynamicAttributes: Record<string, QueryExpression> = {};
for (const [key, value] of Object.entries(decorator.attributes)) {
parsedDynamicAttributes[key] = parseExpression(value);
}
newDecorators.push({
where: parsedWhere,
attributes: parsedDynamicAttributes,
});
} catch (e: any) {
console.error(
"Error parsing object decorator",
decorator,
"got error",
e.message,
);
}
}
console.info(`Loaded ${newDecorators.length} object decorators`);
ds.objectEnrichers = newDecorators;
}
}