diff --git a/plug-api/lib/yaml_page.ts b/plug-api/lib/yaml_page.ts index 0cdc8e1..39f4aec 100644 --- a/plug-api/lib/yaml_page.ts +++ b/plug-api/lib/yaml_page.ts @@ -43,7 +43,10 @@ export async function readYamlPage( export async function writeYamlPage( pageName: string, data: any, + prelude = "", ): Promise { - const text = YAML.stringify(data); - await space.writePage(pageName, "```yaml\n" + text + "\n```"); + const text = YAML.stringify(data, { + noCompatMode: true, + }); + await space.writePage(pageName, prelude + "```yaml\n" + text + "\n```"); } diff --git a/plugs/core/core.plug.yaml b/plugs/core/core.plug.yaml index 55eafb2..871bf7c 100644 --- a/plugs/core/core.plug.yaml +++ b/plugs/core/core.plug.yaml @@ -333,6 +333,11 @@ functions: path: "./plugmanager.ts:getPlugGithubRelease" events: - get-plug:ghr + addPlugCommand: + path: ./plugmanager.ts:addPlugCommand + command: + name: "Plugs: Add" + # Debug commands parseCommand: path: ./debug.ts:parsePageCommand diff --git a/plugs/core/plugmanager.ts b/plugs/core/plugmanager.ts index a7bce18..9230ecd 100644 --- a/plugs/core/plugmanager.ts +++ b/plugs/core/plugmanager.ts @@ -3,6 +3,10 @@ import type { Manifest } from "../../common/manifest.ts"; import { editor, space, system } from "$sb/silverbullet-syscall/mod.ts"; import { readYamlPage } from "$sb/lib/yaml_page.ts"; +import { writePage } from "$sb/silverbullet-syscall/space.ts"; + +const plugsPrelude = + "This file lists all plugs that SilverBullet will load. Run the {[Plugs: Update]} command to update and reload this list of plugs.\n\n"; export async function updatePlugsCommand() { await editor.save(); @@ -16,6 +20,37 @@ export async function updatePlugsCommand() { } } +export async function addPlugCommand() { + let name = await editor.prompt("Plug URI:"); + if (!name) { + return; + } + // Support people copy & pasting the YAML version + if (name.startsWith("-")) { + name = name.replace(/^\-\s*/, ""); + } + let plugList: string[] = []; + try { + plugList = await readYamlPage("PLUGS"); + } catch (e: any) { + console.error("ERROR", e); + } + if (plugList.includes(name)) { + await editor.flashNotification("Plug already installed", "error"); + return; + } + plugList.push(name); + // await writeYamlPage("PLUGS", plugList, plugsPrelude); + await writePage( + "PLUGS", + plugsPrelude + "```yaml\n" + plugList.map((p) => `- ${p}`).join("\n") + + "\n```", + ); + await editor.navigate("PLUGS"); + await system.invokeFunction("server", "updatePlugs"); + await editor.flashNotification("Plug added!"); +} + export async function updatePlugs() { let plugList: string[] = []; try { diff --git a/website/CHANGELOG.md b/website/CHANGELOG.md index 3091226..f600bd3 100644 --- a/website/CHANGELOG.md +++ b/website/CHANGELOG.md @@ -3,6 +3,10 @@ release. --- +## 0.2.1 + +* New `Plugs: Add` command + ## 0.2.0 * The editor is now in "live preview" mode where a lot of markdown is hidden unless the cursor is present. This will take some getting used to, but results in a much more distraction free look. * Clicking on the page name in the top bar now allows you to quickly rename pages, hit enter to apply the change.