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-05-23 18:53:53 +00:00
|
|
|
import { SpacePrimitives } from "./spaces/space_primitives.ts";
|
2023-12-17 10:46:18 +00:00
|
|
|
import { expandPropertyNames } from "$sb/lib/json.ts";
|
2023-12-18 15:54:55 +00:00
|
|
|
import type { BuiltinSettings } from "../web/types.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 {
|
2023-11-29 15:51:28 +00:00
|
|
|
return YAML.load(yaml) as {
|
2022-12-15 12:23:49 +00:00
|
|
|
[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-05-23 18:53:53 +00:00
|
|
|
export async function ensureSettingsAndIndex(
|
|
|
|
space: SpacePrimitives,
|
2023-12-18 15:54:55 +00:00
|
|
|
): Promise<BuiltinSettings> {
|
2023-05-29 07:53:49 +00:00
|
|
|
let settingsText: string | undefined;
|
2023-01-08 11:24:12 +00:00
|
|
|
try {
|
2023-05-29 07:53:49 +00:00
|
|
|
settingsText = new TextDecoder().decode(
|
|
|
|
(await space.readFile("SETTINGS.md")).data,
|
|
|
|
);
|
2023-06-13 18:47:05 +00:00
|
|
|
} catch (e: any) {
|
|
|
|
if (e.message === "Not found") {
|
2023-12-17 10:46:18 +00:00
|
|
|
console.log("No settings found, creating default settings");
|
2023-06-13 18:47:05 +00:00
|
|
|
await space.writeFile(
|
|
|
|
"SETTINGS.md",
|
|
|
|
new TextEncoder().encode(SETTINGS_TEMPLATE),
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
console.error("Error reading settings", e.message);
|
2023-12-18 15:54:55 +00:00
|
|
|
console.warn("Falling back to default settings");
|
|
|
|
return {
|
|
|
|
indexPage: "index",
|
|
|
|
};
|
2023-06-13 18:47:05 +00:00
|
|
|
}
|
2023-05-29 07:53:49 +00:00
|
|
|
settingsText = SETTINGS_TEMPLATE;
|
2023-06-13 18:47:05 +00:00
|
|
|
// Ok, then let's also check the index page
|
2023-05-23 18:53:53 +00:00
|
|
|
try {
|
|
|
|
await space.getFileMeta("index.md");
|
2023-12-17 10:46:18 +00:00
|
|
|
} catch (e: any) {
|
|
|
|
console.log(
|
|
|
|
"No index page found, creating default index page",
|
|
|
|
e.message,
|
|
|
|
);
|
2023-05-23 18:53:53 +00:00
|
|
|
await space.writeFile(
|
|
|
|
"index.md",
|
|
|
|
new TextEncoder().encode(
|
|
|
|
`Hello! And welcome to your brand new SilverBullet space!
|
2023-01-16 11:06:37 +00:00
|
|
|
|
2023-10-03 16:16:07 +00:00
|
|
|
\`\`\`template
|
|
|
|
page: "[[!silverbullet.md/Getting Started]]"
|
|
|
|
\`\`\`
|
|
|
|
`,
|
2023-05-23 18:53:53 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2023-01-08 11:24:12 +00:00
|
|
|
}
|
2023-05-29 07:53:49 +00:00
|
|
|
|
2023-12-18 15:54:55 +00:00
|
|
|
const settings: any = parseYamlSettings(settingsText);
|
2023-12-17 10:46:18 +00:00
|
|
|
expandPropertyNames(settings);
|
|
|
|
return settings;
|
2023-01-08 11:24:12 +00:00
|
|
|
}
|