From 2229d8c95c8eec8e2356fdee70032041da7db9c1 Mon Sep 17 00:00:00 2001 From: Ozan Tellioglu Date: Sun, 26 Nov 2023 19:48:51 +0100 Subject: [PATCH] New Note Creation Fix for Slashes --- src/modal.ts | 57 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/src/modal.ts b/src/modal.ts index e68dde1..127ce91 100644 --- a/src/modal.ts +++ b/src/modal.ts @@ -60,32 +60,41 @@ export class CreateNoteModal extends Modal { const onClickCreateButton = async () => { let newFileName = fileNameInputEl.value; - if (newFileName !== '') { - let defFolderSrc = folderInputEl ? folderInputEl.value : this.plugin.settings.defaultFolder; - let defFolder = this.app.vault.getAbstractFileByPath(defFolderSrc); - if (defFolder && defFolder instanceof TFolder) { - // Default Text Preparation for File with YAML and Date - let defaultNewFileText = stripIndents` - --- - ${this.plugin.settings.yamlKey}: ${dayjs(this.destinationDate).format( - this.plugin.settings.dateFormat - )} - --- - `; - // Create the MD File and close the modal - await createNewMarkdownFile( - this.plugin, - defFolder, - newFileName, - this.plugin.settings.dateSource === 'yaml' ? defaultNewFileText : '' - ); - thisModal.close(); - } else { - new Notice('Folder couldnt be found in the Vault'); - } - } else { + + if (newFileName === '') { new Notice('You didnt provide file name'); + return; } + + if (newFileName.includes('/')) { + new Notice('You can not have a slash (/) in file name'); + return; + } + + let defFolderSrc = folderInputEl ? folderInputEl.value : this.plugin.settings.defaultFolder; + let defFolder = this.app.vault.getAbstractFileByPath(defFolderSrc); + + if (!defFolder || !(defFolder instanceof TFolder)) { + new Notice('Folder couldnt be found in the Vault'); + return; + } + + // Default Text Preparation for File with YAML and Date + let defaultNewFileText = stripIndents` + --- + ${this.plugin.settings.yamlKey}: ${dayjs(this.destinationDate).format(this.plugin.settings.dateFormat)} + --- + `; + + // Create the MD File and close the modal + await createNewMarkdownFile( + this.plugin, + defFolder as TFolder, + newFileName, + this.plugin.settings.dateSource === 'yaml' ? defaultNewFileText : '' + ); + + thisModal.close(); }; createButton.addEventListener('click', onClickCreateButton);