silverbullet/common/settings.ts

102 lines
2.9 KiB
TypeScript
Raw Normal View History

import YAML from "js-yaml";
import { INDEX_TEMPLATE, SETTINGS_TEMPLATE } from "./PAGE_TEMPLATES.ts";
import { SpacePrimitives } from "./spaces/space_primitives.ts";
2024-02-29 22:23:05 +08:00
import { expandPropertyNames } from "../plug-api/lib/json.ts";
2024-02-09 04:12:23 +08:00
import type { BuiltinSettings } from "../type/web.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-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);
expandPropertyNames(settings);
2024-01-25 18:42:36 +08:00
return { ...defaultSettings, ...settings };
}