Fixes #984 adds Page: Rename Linked Page command
parent
9dd589fcfe
commit
225443aef1
|
@ -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:
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue