address code review
This commit is contained in:
parent
dd3cfd8e98
commit
96569aab0c
@ -1,5 +1,7 @@
|
|||||||
import { flashNotification } from "@silverbulletmd/plugos-silverbullet-syscall/editor";
|
import { readYamlPage } from "./yaml_page";
|
||||||
import { readYamlPage, writeYamlPage } from "./yaml_page";
|
import { notifyUser } from "./util";
|
||||||
|
import YAML from "yaml";
|
||||||
|
import { writePage } from "@silverbulletmd/plugos-silverbullet-syscall/space";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convenience function to read a specific set of settings from the `SETTINGS` page as well as default values
|
* Convenience function to read a specific set of settings from the `SETTINGS` page as well as default values
|
||||||
@ -12,7 +14,6 @@ import { readYamlPage, writeYamlPage } from "./yaml_page";
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
const SETTINGS_PAGE = "SETTINGS";
|
const SETTINGS_PAGE = "SETTINGS";
|
||||||
const SETTINGS_TEMPLATE = `This page contains settings for configuring SilverBullet and its Plugs:\n\`\`\`yaml\n\`\`\``; // might need \r\n for windows?
|
|
||||||
|
|
||||||
export async function readSettings<T extends object>(settings: T): Promise<T> {
|
export async function readSettings<T extends object>(settings: T): Promise<T> {
|
||||||
try {
|
try {
|
||||||
@ -46,13 +47,11 @@ export async function writeSettings<T extends object>(settings: T) {
|
|||||||
try {
|
try {
|
||||||
readSettings = (await readYamlPage(SETTINGS_PAGE, ["yaml"])) || {};
|
readSettings = (await readYamlPage(SETTINGS_PAGE, ["yaml"])) || {};
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.log("Couldn't read settings, generating a new settings page");
|
await notifyUser("Creating a new SETTINGS page...", "info");
|
||||||
flashNotification("Creating a new SETTINGS page...", "info");
|
|
||||||
}
|
}
|
||||||
const writeSettings = {...readSettings, ...settings};
|
const writeSettings = {...readSettings, ...settings};
|
||||||
if(await writeYamlPage(SETTINGS_PAGE, writeSettings, SETTINGS_TEMPLATE)) {
|
const doc = new YAML.Document();
|
||||||
flashNotification("SETTINGS page written successfully", "info");
|
doc.contents = writeSettings;
|
||||||
} else {
|
const contents = `This page contains settings for configuring SilverBullet and its Plugs.\nAny changes outside of the yaml block will be overwritten.\n\`\`\`yaml\n${doc.toString()}\n\`\`\``; // might need \r\n for windows?
|
||||||
flashNotification("SETTINGS page failed to update", "error");
|
await writePage(SETTINGS_PAGE, contents)
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,3 +1,5 @@
|
|||||||
|
import { flashNotification } from "@silverbulletmd/plugos-silverbullet-syscall/editor";
|
||||||
|
|
||||||
export async function replaceAsync(
|
export async function replaceAsync(
|
||||||
str: string,
|
str: string,
|
||||||
regex: RegExp,
|
regex: RegExp,
|
||||||
@ -12,3 +14,21 @@ export async function replaceAsync(
|
|||||||
const data = await Promise.all(promises);
|
const data = await Promise.all(promises);
|
||||||
return str.replace(regex, () => data.shift()!);
|
return str.replace(regex, () => data.shift()!);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isServer() {
|
||||||
|
return typeof window === 'undefined' || typeof window.document === "undefined"; // if something defines window the same way as the browser, this will fail.
|
||||||
|
}
|
||||||
|
|
||||||
|
// this helps keep if's condition as positive
|
||||||
|
export function isBrowser() {
|
||||||
|
return !isServer();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function notifyUser(message: string, type?: "info"|"error") {
|
||||||
|
if (isBrowser()) {
|
||||||
|
return flashNotification(message, type);
|
||||||
|
}
|
||||||
|
const log = type === "error" ? console.error : console.log;
|
||||||
|
log(message); // we should end up sending the message to the user, users dont read logs.
|
||||||
|
return;
|
||||||
|
}
|
@ -40,42 +40,3 @@ export async function readYamlPage(
|
|||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function writeYamlPage(
|
|
||||||
pageName: string,
|
|
||||||
properties: any,
|
|
||||||
templateOnEmpty: string,
|
|
||||||
): Promise<Boolean> {
|
|
||||||
let text;
|
|
||||||
try {
|
|
||||||
const page = await readPage(pageName);
|
|
||||||
text = page.text;
|
|
||||||
} catch {
|
|
||||||
// page doesn't exist, so let's create one.
|
|
||||||
text = templateOnEmpty;
|
|
||||||
}
|
|
||||||
const tree = await parseMarkdown(text);
|
|
||||||
// if we use any other language than yaml... how to create it?
|
|
||||||
const doc = new YAML.Document();
|
|
||||||
doc.contents = properties;
|
|
||||||
// generate a new node for the properties
|
|
||||||
const newCode = `\`\`\`yaml\n${doc.toString()}\n\`\`\``;
|
|
||||||
const subtree = await parseMarkdown(newCode);
|
|
||||||
// find the original set of properties and replace
|
|
||||||
let replaced = false;
|
|
||||||
replaceNodesMatching(tree, (node: ParseTree) => {
|
|
||||||
if (node.type !== 'FencedCode') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const codeinfoNode = findNodeOfType(node, "CodeInfo");
|
|
||||||
if (!codeinfoNode || codeinfoNode.children![0].text! !== "yaml") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
replaced = true;
|
|
||||||
return subtree;
|
|
||||||
});
|
|
||||||
if (replaced) {
|
|
||||||
await writePage(pageName, renderToText(tree));
|
|
||||||
}
|
|
||||||
return replaced;
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user