silverbullet/plug-api/lib/settings_page.ts

32 lines
932 B
TypeScript
Raw Permalink Normal View History

import { system } from "@silverbulletmd/silverbullet/syscalls";
import { readYamlPage } from "@silverbulletmd/silverbullet/lib/yaml_page";
2022-07-18 16:51:28 +08:00
/**
* Retrieves a setting from the space configuration.
* @deprecated Use use `editor.getSpaceConfig` syscall instead
* @param key string
* @param defaultValue
* @returns
2022-07-18 16:51:28 +08:00
*/
export async function readSetting(
key: string,
defaultValue?: any,
): Promise<any> {
try {
return await system.getSpaceConfig(key) ?? defaultValue;
} catch {
// We're running an old version of SilverBullet, fallback to reading from SETTINGS page
try {
const allSettings = (await readYamlPage("SETTINGS")) || {};
const val = allSettings[key];
return val === undefined ? defaultValue : val;
} catch (e: any) {
if (e.message === "Not found") {
// No settings yet, return default values for all
return defaultValue;
}
throw e;
}
}
2022-09-13 14:41:01 +08:00
}