2022-10-10 20:50:21 +08:00
|
|
|
import { YAML } from "./deps.ts";
|
2024-02-09 04:00:45 +08:00
|
|
|
import { INDEX_TEMPLATE, SETTINGS_TEMPLATE } from "./PAGE_TEMPLATES.ts";
|
2023-05-24 02:53:53 +08:00
|
|
|
import { SpacePrimitives } from "./spaces/space_primitives.ts";
|
2024-02-09 04:00:45 +08:00
|
|
|
import { expandPropertyNames } from "../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
|
|
|
|
2024-02-23 16:03:14 +08:00
|
|
|
const yamlSettingsRegex = /^(```+|~~~+)ya?ml\r?\n([\S\s]+)\1/m;
|
2022-08-02 18:43:39 +08:00
|
|
|
|
2024-01-05 03:08:12 +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 {};
|
|
|
|
}
|
2024-02-23 16:03:14 +08:00
|
|
|
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
|
|
|
}
|
2023-01-08 19:24:12 +08:00
|
|
|
|
2024-01-25 18:42:36 +08:00
|
|
|
export const defaultSettings: BuiltinSettings = {
|
|
|
|
indexPage: "index",
|
|
|
|
hideSyncButton: false,
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2024-01-05 03:08:12 +08:00
|
|
|
/**
|
|
|
|
* 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(
|
2023-05-24 02:53:53 +08:00
|
|
|
space: SpacePrimitives,
|
2023-12-18 23:54:55 +08:00
|
|
|
): Promise<BuiltinSettings> {
|
2023-05-29 15:53:49 +08:00
|
|
|
let settingsText: string | undefined;
|
2023-01-08 19:24:12 +08:00
|
|
|
try {
|
2023-05-29 15:53:49 +08:00
|
|
|
settingsText = new TextDecoder().decode(
|
|
|
|
(await space.readFile("SETTINGS.md")).data,
|
|
|
|
);
|
2023-06-14 02:47:05 +08:00
|
|
|
} catch (e: any) {
|
|
|
|
if (e.message === "Not found") {
|
2023-12-17 18:46:18 +08:00
|
|
|
console.log("No settings found, creating default settings");
|
2023-06-14 02:47:05 +08:00
|
|
|
await space.writeFile(
|
|
|
|
"SETTINGS.md",
|
|
|
|
new TextEncoder().encode(SETTINGS_TEMPLATE),
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
console.error("Error reading settings", e.message);
|
2023-12-18 23:54:55 +08:00
|
|
|
console.warn("Falling back to default settings");
|
2024-01-25 18:42:36 +08:00
|
|
|
return defaultSettings;
|
2023-06-14 02:47:05 +08:00
|
|
|
}
|
2023-05-29 15:53:49 +08:00
|
|
|
settingsText = SETTINGS_TEMPLATE;
|
2023-06-14 02:47:05 +08:00
|
|
|
// Ok, then let's also check the index page
|
2023-05-24 02:53:53 +08:00
|
|
|
try {
|
|
|
|
await space.getFileMeta("index.md");
|
2023-12-17 18:46:18 +08:00
|
|
|
} catch (e: any) {
|
|
|
|
console.log(
|
|
|
|
"No index page found, creating default index page",
|
|
|
|
e.message,
|
|
|
|
);
|
2023-05-24 02:53:53 +08:00
|
|
|
await space.writeFile(
|
|
|
|
"index.md",
|
2024-01-21 02:16:07 +08:00
|
|
|
new TextEncoder().encode(INDEX_TEMPLATE),
|
2023-05-24 02:53:53 +08:00
|
|
|
);
|
|
|
|
}
|
2023-01-08 19:24:12 +08:00
|
|
|
}
|
2023-05-29 15:53:49 +08:00
|
|
|
|
2023-12-18 23:54:55 +08:00
|
|
|
const settings: any = parseYamlSettings(settingsText);
|
2023-12-17 18:46:18 +08:00
|
|
|
expandPropertyNames(settings);
|
2024-01-25 18:42:36 +08:00
|
|
|
return { ...defaultSettings, ...settings };
|
2023-01-08 19:24:12 +08:00
|
|
|
}
|