From f4bd2c3abcf5e1a8cdc4e02293ff63e8ec913dd1 Mon Sep 17 00:00:00 2001 From: rktjmp Date: Wed, 25 Feb 2026 20:16:37 +1100 Subject: [PATCH] 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 --- plugs/editor/editor.ts | 18 +++++++++++---- plugs/editor/page.ts | 10 ++++++--- plugs/index/refactor.ts | 50 ++++++++++++++++++++++++++++++++--------- 3 files changed, 60 insertions(+), 18 deletions(-) diff --git a/plugs/editor/editor.ts b/plugs/editor/editor.ts index 266798f7..26e8faf3 100644 --- a/plugs/editor/editor.ts +++ b/plugs/editor/editor.ts @@ -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 diff --git a/plugs/editor/page.ts b/plugs/editor/page.ts index cd349f2f..d4a0f9a6 100644 --- a/plugs/editor/page.ts +++ b/plugs/editor/page.ts @@ -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; } diff --git a/plugs/index/refactor.ts b/plugs/index/refactor.ts index 9e4f8b52..4bc8411d 100644 --- a/plugs/index/refactor.ts +++ b/plugs/index/refactor.ts @@ -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 {