From 225443aef1f1b4de08fcd8dd21cc387f24e85cad Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Sun, 18 Aug 2024 15:24:18 +0200 Subject: [PATCH] Fixes #984 adds Page: Rename Linked Page command --- plugs/index/index.plug.yaml | 8 +++- plugs/index/refactor.ts | 74 ++++++++++++++++++++----------------- web/client.ts | 4 +- 3 files changed, 49 insertions(+), 37 deletions(-) diff --git a/plugs/index/index.plug.yaml b/plugs/index/index.plug.yaml index 0fd38a1d..797ee824 100644 --- a/plugs/index/index.plug.yaml +++ b/plugs/index/index.plug.yaml @@ -164,7 +164,6 @@ functions: command: name: "Page: Rename" mac: Cmd-Alt-r - key: Ctrl-Alt-r page: "" requireMode: rw renamePrefixCommand: @@ -172,6 +171,13 @@ functions: command: name: "Page: Batch Rename Prefix" requireMode: rw + renamePageLinkCommand: + path: "./refactor.ts:renamePageLinkCommand" + command: + name: "Page: Rename Linked Page" + mac: Cmd-Ctrl-Alt-r + key: Ctrl-Shift-Alt-r + requireMode: rw # Refactoring Commands extractToPageCommand: diff --git a/plugs/index/refactor.ts b/plugs/index/refactor.ts index 91148332..2ec760af 100644 --- a/plugs/index/refactor.ts +++ b/plugs/index/refactor.ts @@ -1,4 +1,4 @@ -import { editor, space } from "@silverbulletmd/silverbullet/syscalls"; +import { editor, markdown, space } from "@silverbulletmd/silverbullet/syscalls"; import { validatePageName } from "@silverbulletmd/silverbullet/lib/page_ref"; import { getBackLinks, type LinkObject } from "./page_links.ts"; import { queryObjects } from "./api.ts"; @@ -7,6 +7,13 @@ import { folderName, } from "@silverbulletmd/silverbullet/lib/resolve"; import type { ObjectValue } from "@silverbulletmd/silverbullet/types"; +import { + addParentPointers, + findParentMatching, + nodeAtPos, +} from "@silverbulletmd/silverbullet/lib/tree"; +import type { ParseTree } from "@silverbulletmd/silverbullet/lib/tree"; +import { findNodeOfType } from "@silverbulletmd/silverbullet/lib/tree"; /** * Renames a single page. @@ -29,6 +36,38 @@ export async function renamePageCommand(cmdDef: any) { return true; } +export async function renamePageLinkCommand() { + const mdTree = await markdown.parseMarkdown(await editor.getText()); + const link = nodeAtPos(mdTree, await editor.getCursor()); + if (!link) { + console.error("No link found at cursor position..."); + return; + } + console.log("Link node", mdTree); + addParentPointers(mdTree); + let node: ParseTree | null = link; + if (node.type !== "WikiLink") { + node = findParentMatching(node, (t) => t.type === "WikiLink"); + if (!node) { + console.error("No link found at cursor position"); + return; + } + } + const wikiLinkPage = findNodeOfType(node, "WikiLinkPage"); + if (!wikiLinkPage) { + console.error("No link found at cursor position"); + return; + } + const oldName = wikiLinkPage.children![0].text!; + + const newName = await editor.prompt(`Rename ${oldName} to:`, oldName); + if (!newName) { + return false; + } + const pageList: [string, string][] = [[oldName + ".md", newName + ".md"]]; + await batchRenameFiles(pageList); +} + /** * Renames any amount of files. * If renaming pages, names should be passed with a .md extension @@ -88,39 +127,6 @@ export async function batchRenameFiles(fileList: [string, string][]) { } } - /* Deleting folders not yet implemented - // Cleanup empty folders - const folders = new Set(fileList.flatMap((f) => { - let fileFolder = folderName(f[0]); - const splitFolders: string[] = []; - while (fileFolder) { - splitFolders.push(fileFolder); - const pos = fileFolder.lastIndexOf("/"); - fileFolder = fileFolder.slice(0, pos === -1 ? 0 : pos); - } - return splitFolders; - })); - - const allFiles = await space.listFiles(); - for (const fol of folders) { - console.log(allFiles.some((f) => f.name.startsWith(fol + "/"))); - if (allFiles.some((f) => f.name.startsWith(fol + "/"))) { - folders.delete(fol); - } - } - - if (folders.size > 0) { - try { - for (const fol of folders) { - console.log("Deleting empty folder", fol); - await space.deleteFolder(fol); - } - } catch (e: any) { - throw e; - } - } - */ - return true; } catch (e: any) { await editor.flashNotification(e.message, "error"); diff --git a/web/client.ts b/web/client.ts index 345a26b6..2a0e7c5c 100644 --- a/web/client.ts +++ b/web/client.ts @@ -1015,10 +1015,10 @@ export class Client implements ConfigContainer { if (newWindow) { console.log( "Navigating to new page in new window", - `${location.origin}/${encodePageRef(pageRef)}`, + `${location.origin}/${encodeURIComponent(encodePageRef(pageRef))}`, ); const win = globalThis.open( - `${location.origin}/${encodePageRef(pageRef)}`, + `${location.origin}/${encodeURIComponent(encodePageRef(pageRef))}`, "_blank", ); if (win) {