From e3de313bdd3a4153ac22cc164044038f4933e4e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=9C=E9=BE=99?= Date: Wed, 12 Nov 2025 16:48:24 +0800 Subject: [PATCH] feat(quick note): add option to control how duplicate filenames are handled Introduce a new quickNoteAlwaysCreate setting. When disabled, opening a note with an existing filename will switch to that file instead of creating a new one with an incremented number. The legacy behavior (always create a numbered file) remains the default. --- README.md | 3 +++ main.js | 34 ++++++++++++++++++++++++++++------ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 34577ff..5f650cb 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,9 @@ Hotkeys can be assigned to the commands via Obsidian's built-in hotkey manager. | Quick note location | New quick notes will be placed in this folder. | | | Quick note date format | New quick notes will use a filename of this pattern. Format: [Moment.js format string](https://momentjs.com/docs/#/displaying/format/) | `YYYY-MM-DD` | | Quick note hotkey | This hotkey is registered globally and will be detected even if Obsidian does not have keyboard focus. Format: [Electron accelerator](https://www.electronjs.org/docs/latest/tutorial/keyboard-shortcuts#accelerators) | CmdOrCtrl+Shift+Q | +| Quick note always create | If a quick note with the same name already exists, create a new numbered file. Disable to open the existing file instead. | Enabled | + + ## Installation diff --git a/main.js b/main.js index 4f38a71..98de934 100644 --- a/main.js +++ b/main.js @@ -159,7 +159,7 @@ const cleanup = () => { }; const addQuickNote = () => { - const { quickNoteLocation, quickNoteDateFormat } = plugin.settings, + const { quickNoteLocation, quickNoteDateFormat, quickNoteAlwaysCreate } = plugin.settings, pattern = quickNoteDateFormat || DEFAULT_DATE_FORMAT, date = obsidian.moment().format(pattern), name = obsidian @@ -172,11 +172,24 @@ const addQuickNote = () => { // set to "same folder as current file") leaf = plugin.app.workspace.getLeaf(), root = plugin.app.fileManager.getNewFileParent(""), - openMode = { active: true, state: { mode: "source" } }; - plugin.app.fileManager - .createNewMarkdownFile(root, name) - .then((file) => leaf.openFile(file, openMode)); - showWindows(); + openMode = { active: true, state: { mode: "source" } }, + filePath = `${name}.md`; + if (quickNoteAlwaysCreate) { + plugin.app.fileManager + .createNewMarkdownFile(root, name) + .then((file) => leaf.openFile(file, openMode)) + .then(() => showWindows()); + } else { + const existing = plugin.app.vault.getAbstractFileByPath(filePath); + if (existing && existing instanceof obsidian.TFile) { + leaf.openFile(existing, openMode).then(() => showWindows()); + } else { + plugin.app.fileManager + .createNewMarkdownFile(root, name) + .then((file) => leaf.openFile(file, openMode)) + .then(() => showWindows()); + } + } }, replaceVaultName = (str) => { return str.replace(/{{vault}}/g, plugin.app.vault.getName()); @@ -358,6 +371,15 @@ const OPTIONS = [ onBeforeChange: unregisterHotkeys, onChange: registerHotkeys, }, + { + key: "quickNoteAlwaysCreate", + desc: ` + If a quick note with the same name already exists, create a new numbered file. + Disable to open the existing file instead. + `, + type: "toggle", + default: true, + }, ]; const keyToLabel = (key) =>