Fixes #984 adds Page: Rename Linked Page command

pull/1054/head
Zef Hemel 2024-08-18 15:24:18 +02:00
parent 9dd589fcfe
commit 225443aef1
3 changed files with 49 additions and 37 deletions

View File

@ -164,7 +164,6 @@ functions:
command: command:
name: "Page: Rename" name: "Page: Rename"
mac: Cmd-Alt-r mac: Cmd-Alt-r
key: Ctrl-Alt-r
page: "" page: ""
requireMode: rw requireMode: rw
renamePrefixCommand: renamePrefixCommand:
@ -172,6 +171,13 @@ functions:
command: command:
name: "Page: Batch Rename Prefix" name: "Page: Batch Rename Prefix"
requireMode: rw 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 # Refactoring Commands
extractToPageCommand: extractToPageCommand:

View File

@ -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 { validatePageName } from "@silverbulletmd/silverbullet/lib/page_ref";
import { getBackLinks, type LinkObject } from "./page_links.ts"; import { getBackLinks, type LinkObject } from "./page_links.ts";
import { queryObjects } from "./api.ts"; import { queryObjects } from "./api.ts";
@ -7,6 +7,13 @@ import {
folderName, folderName,
} from "@silverbulletmd/silverbullet/lib/resolve"; } from "@silverbulletmd/silverbullet/lib/resolve";
import type { ObjectValue } from "@silverbulletmd/silverbullet/types"; 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. * Renames a single page.
@ -29,6 +36,38 @@ export async function renamePageCommand(cmdDef: any) {
return true; 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. * Renames any amount of files.
* If renaming pages, names should be passed with a .md extension * 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; return true;
} catch (e: any) { } catch (e: any) {
await editor.flashNotification(e.message, "error"); await editor.flashNotification(e.message, "error");

View File

@ -1015,10 +1015,10 @@ export class Client implements ConfigContainer {
if (newWindow) { if (newWindow) {
console.log( console.log(
"Navigating to new page in new window", "Navigating to new page in new window",
`${location.origin}/${encodePageRef(pageRef)}`, `${location.origin}/${encodeURIComponent(encodePageRef(pageRef))}`,
); );
const win = globalThis.open( const win = globalThis.open(
`${location.origin}/${encodePageRef(pageRef)}`, `${location.origin}/${encodeURIComponent(encodePageRef(pageRef))}`,
"_blank", "_blank",
); );
if (win) { if (win) {