Trim user input from prompts where appropriate (#1854)

* Trim user input from prompts where appropriate

* Run formatter

* Use "page title" language instead of "page name" in notifications.

Existing language in prompt asks for a "page title".

* Use const for prefix inputs as they are never trimmed
This commit is contained in:
rktjmp
2026-02-25 10:16:37 +01:00
committed by GitHub
parent fd4ff8518d
commit f4bd2c3abc
3 changed files with 60 additions and 18 deletions
+14 -4
View File
@@ -109,8 +109,13 @@ export async function centerCursorCommand() {
}
export async function moveToPosCommand() {
const posString = await editor.prompt("Move to position:");
if (!posString) {
let posString = await editor.prompt("Move to position:");
if (posString === undefined) {
return;
}
posString = posString.trim();
if (posString === "") {
editor.flashNotification("Must provide a position.", "error");
return;
}
const pos = +posString;
@@ -132,10 +137,15 @@ export async function copyLinkCommand() {
}
export async function moveToLineCommand() {
const lineString = await editor.prompt(
let lineString = await editor.prompt(
"Move to line (and optionally column):",
);
if (!lineString) {
if (lineString === undefined) {
return;
}
lineString = lineString.trim();
if (lineString === "") {
editor.flashNotification("Must provide a line number.", "error");
return;
}
// Match sequence of digits at the start, optionally another sequence
+7 -3
View File
@@ -31,9 +31,13 @@ export async function copyPage(
const fromName = sourcePage || currentPage;
const suggestedName = toName || fromName;
const newName = await editor.prompt(`Copy to page:`, suggestedName);
if (!newName) {
let newName = await editor.prompt(`Copy to page:`, suggestedName);
if (newName === undefined) {
return;
}
newName = newName.trim();
if (newName === "") {
editor.flashNotification("Must provide a non-empty page name.", "error");
return;
}
+39 -11
View File
@@ -33,9 +33,14 @@ import { notFoundError } from "@silverbulletmd/silverbullet/constants";
*/
export async function renamePageCommand(cmdDef: any) {
const oldName: string = cmdDef.oldPage || await editor.getCurrentPage();
const newName: string = cmdDef.page ||
let newName: string = cmdDef.page ||
await editor.prompt(`Rename ${oldName} to:`, oldName);
if (!newName) {
if (newName === undefined) {
return false;
}
newName = newName.trim();
if (newName === "") {
editor.flashNotification("Must provide a non-empty page title.", "error");
return false;
}
const pageList: [string, string][] = [[oldName + ".md", newName + ".md"]];
@@ -66,8 +71,13 @@ export async function renamePageLinkCommand() {
}
const oldName = wikiLinkPage.children![0].text!;
const newName = await editor.prompt(`Rename ${oldName} to:`, oldName);
if (!newName) {
let newName = await editor.prompt(`Rename ${oldName} to:`, oldName);
if (newName === undefined) {
return false;
}
newName = newName.trim();
if (newName === "") {
editor.flashNotification("Must provide a non-empty page title.", "error");
return false;
}
const pageList: [string, string][] = [[oldName + ".md", newName + ".md"]];
@@ -84,9 +94,17 @@ export async function renamePageLinkCommand() {
*/
export async function renameDocumentCommand(cmdDef: any) {
const oldName: string = cmdDef.oldDocument || await editor.getCurrentPath();
const newName: string = cmdDef.document ||
let newName: string = cmdDef.document ||
await editor.prompt(`Rename ${oldName} to:`, oldName);
if (!newName) {
if (newName === undefined) {
return false;
}
newName = newName.trim();
if (newName === "") {
editor.flashNotification(
"Must provide a non-empty document name.",
"error",
);
return false;
}
const pageList: [string, string][] = [[oldName, newName]];
@@ -300,13 +318,19 @@ async function renameDocument(
export async function renamePrefixCommand(cmdDef: any) {
const oldPrefix = cmdDef.oldPrefix ??
await editor.prompt("Prefix to rename:", "");
if (!oldPrefix) {
if (oldPrefix === undefined) {
return false;
}
// Note, we do *not* trim the old or new prefix input as the user may
// actually want to add or remove white space. They can also input an empty
// string for the new prefix to remove the old prefix.
if (oldPrefix === "") {
editor.flashNotification("Must provide a non-empty prefix.", "error");
return false;
}
const newPrefix = cmdDef.newPrefix ??
await editor.prompt("New prefix:", oldPrefix);
if (!newPrefix) {
if (newPrefix === undefined) {
return false;
}
@@ -348,8 +372,12 @@ export async function extractToPageCommand() {
newName = "new page";
}
newName = await editor.prompt(`New page title:`, newName);
if (!newName) {
return;
if (newName === undefined) {
return false;
}
newName = newName.trim();
if (newName === "") {
editor.flashNotification("Must provide a non-empty page title.", "error");
}
try {