Skip to content

Commit f4bd2c3

Browse files
authored
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
1 parent fd4ff85 commit f4bd2c3

File tree

3 files changed

+60
-18
lines changed

3 files changed

+60
-18
lines changed

plugs/editor/editor.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,13 @@ export async function centerCursorCommand() {
109109
}
110110

111111
export async function moveToPosCommand() {
112-
const posString = await editor.prompt("Move to position:");
113-
if (!posString) {
112+
let posString = await editor.prompt("Move to position:");
113+
if (posString === undefined) {
114+
return;
115+
}
116+
posString = posString.trim();
117+
if (posString === "") {
118+
editor.flashNotification("Must provide a position.", "error");
114119
return;
115120
}
116121
const pos = +posString;
@@ -132,10 +137,15 @@ export async function copyLinkCommand() {
132137
}
133138

134139
export async function moveToLineCommand() {
135-
const lineString = await editor.prompt(
140+
let lineString = await editor.prompt(
136141
"Move to line (and optionally column):",
137142
);
138-
if (!lineString) {
143+
if (lineString === undefined) {
144+
return;
145+
}
146+
lineString = lineString.trim();
147+
if (lineString === "") {
148+
editor.flashNotification("Must provide a line number.", "error");
139149
return;
140150
}
141151
// Match sequence of digits at the start, optionally another sequence

plugs/editor/page.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ export async function copyPage(
3131
const fromName = sourcePage || currentPage;
3232
const suggestedName = toName || fromName;
3333

34-
const newName = await editor.prompt(`Copy to page:`, suggestedName);
35-
36-
if (!newName) {
34+
let newName = await editor.prompt(`Copy to page:`, suggestedName);
35+
if (newName === undefined) {
36+
return;
37+
}
38+
newName = newName.trim();
39+
if (newName === "") {
40+
editor.flashNotification("Must provide a non-empty page name.", "error");
3741
return;
3842
}
3943

plugs/index/refactor.ts

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,14 @@ import { notFoundError } from "@silverbulletmd/silverbullet/constants";
3333
*/
3434
export async function renamePageCommand(cmdDef: any) {
3535
const oldName: string = cmdDef.oldPage || await editor.getCurrentPage();
36-
const newName: string = cmdDef.page ||
36+
let newName: string = cmdDef.page ||
3737
await editor.prompt(`Rename ${oldName} to:`, oldName);
38-
if (!newName) {
38+
if (newName === undefined) {
39+
return false;
40+
}
41+
newName = newName.trim();
42+
if (newName === "") {
43+
editor.flashNotification("Must provide a non-empty page title.", "error");
3944
return false;
4045
}
4146
const pageList: [string, string][] = [[oldName + ".md", newName + ".md"]];
@@ -66,8 +71,13 @@ export async function renamePageLinkCommand() {
6671
}
6772
const oldName = wikiLinkPage.children![0].text!;
6873

69-
const newName = await editor.prompt(`Rename ${oldName} to:`, oldName);
70-
if (!newName) {
74+
let newName = await editor.prompt(`Rename ${oldName} to:`, oldName);
75+
if (newName === undefined) {
76+
return false;
77+
}
78+
newName = newName.trim();
79+
if (newName === "") {
80+
editor.flashNotification("Must provide a non-empty page title.", "error");
7181
return false;
7282
}
7383
const pageList: [string, string][] = [[oldName + ".md", newName + ".md"]];
@@ -84,9 +94,17 @@ export async function renamePageLinkCommand() {
8494
*/
8595
export async function renameDocumentCommand(cmdDef: any) {
8696
const oldName: string = cmdDef.oldDocument || await editor.getCurrentPath();
87-
const newName: string = cmdDef.document ||
97+
let newName: string = cmdDef.document ||
8898
await editor.prompt(`Rename ${oldName} to:`, oldName);
89-
if (!newName) {
99+
if (newName === undefined) {
100+
return false;
101+
}
102+
newName = newName.trim();
103+
if (newName === "") {
104+
editor.flashNotification(
105+
"Must provide a non-empty document name.",
106+
"error",
107+
);
90108
return false;
91109
}
92110
const pageList: [string, string][] = [[oldName, newName]];
@@ -300,13 +318,19 @@ async function renameDocument(
300318
export async function renamePrefixCommand(cmdDef: any) {
301319
const oldPrefix = cmdDef.oldPrefix ??
302320
await editor.prompt("Prefix to rename:", "");
303-
if (!oldPrefix) {
321+
if (oldPrefix === undefined) {
322+
return false;
323+
}
324+
// Note, we do *not* trim the old or new prefix input as the user may
325+
// actually want to add or remove white space. They can also input an empty
326+
// string for the new prefix to remove the old prefix.
327+
if (oldPrefix === "") {
328+
editor.flashNotification("Must provide a non-empty prefix.", "error");
304329
return false;
305330
}
306-
307331
const newPrefix = cmdDef.newPrefix ??
308332
await editor.prompt("New prefix:", oldPrefix);
309-
if (!newPrefix) {
333+
if (newPrefix === undefined) {
310334
return false;
311335
}
312336

@@ -348,8 +372,12 @@ export async function extractToPageCommand() {
348372
newName = "new page";
349373
}
350374
newName = await editor.prompt(`New page title:`, newName);
351-
if (!newName) {
352-
return;
375+
if (newName === undefined) {
376+
return false;
377+
}
378+
newName = newName.trim();
379+
if (newName === "") {
380+
editor.flashNotification("Must provide a non-empty page title.", "error");
353381
}
354382

355383
try {

0 commit comments

Comments
 (0)