2023-01-08 11:24:12 +00:00
|
|
|
import { SETTINGS_TEMPLATE } from "./settings_template.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
import { YAML } from "./deps.ts";
|
2023-01-08 11:24:12 +00:00
|
|
|
import { Space } from "./spaces/space.ts";
|
2022-08-02 10:43:39 +00:00
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
export function safeRun(fn: () => Promise<void>) {
|
|
|
|
fn().catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isMacLike() {
|
|
|
|
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
|
|
|
|
}
|
|
|
|
|
2022-08-02 10:43:39 +00:00
|
|
|
// TODO: This is naive, may be better to use a proper parser
|
|
|
|
const yamlSettingsRegex = /```yaml([^`]+)```/;
|
|
|
|
|
|
|
|
export function parseYamlSettings(settingsMarkdown: string): {
|
|
|
|
[key: string]: any;
|
|
|
|
} {
|
|
|
|
const match = yamlSettingsRegex.exec(settingsMarkdown);
|
|
|
|
if (!match) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
const yaml = match[1];
|
2022-12-15 12:23:49 +00:00
|
|
|
try {
|
|
|
|
return YAML.parse(yaml) as {
|
|
|
|
[key: string]: any;
|
|
|
|
};
|
|
|
|
} catch (e: any) {
|
|
|
|
console.error("Error parsing SETTINGS as YAML", e.message);
|
|
|
|
return {};
|
|
|
|
}
|
2022-08-02 10:43:39 +00:00
|
|
|
}
|
2023-01-08 11:24:12 +00:00
|
|
|
|
2023-01-13 14:41:29 +00:00
|
|
|
export async function ensureAndLoadSettings(
|
|
|
|
space: Space,
|
|
|
|
dontCreate: boolean,
|
|
|
|
): Promise<any> {
|
|
|
|
if (dontCreate) {
|
|
|
|
return {
|
|
|
|
indexPage: "index",
|
|
|
|
};
|
|
|
|
}
|
2023-01-08 11:24:12 +00:00
|
|
|
try {
|
|
|
|
await space.getPageMeta("SETTINGS");
|
|
|
|
} catch {
|
|
|
|
await space.writePage(
|
|
|
|
"SETTINGS",
|
|
|
|
SETTINGS_TEMPLATE,
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { text: settingsText } = await space.readPage("SETTINGS");
|
|
|
|
const settings = parseYamlSettings(settingsText);
|
|
|
|
if (!settings.indexPage) {
|
|
|
|
settings.indexPage = "index";
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await space.getPageMeta(settings.indexPage);
|
|
|
|
} catch {
|
|
|
|
await space.writePage(
|
|
|
|
settings.indexPage,
|
2023-01-16 11:06:37 +00:00
|
|
|
`Hello! And welcome to your brand new SilverBullet space!
|
|
|
|
|
|
|
|
<!-- #include [[💭 silverbullet.md/Getting Started]] -->
|
|
|
|
<!-- /include -->`,
|
2023-01-08 11:24:12 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
}
|