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.
This commit is contained in:
东龙 2025-11-12 16:48:24 +08:00
parent ac353bb5a6
commit e3de313bdd
2 changed files with 31 additions and 6 deletions

View File

@ -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 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 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) | <kbd>CmdOrCtrl+Shift+Q</kbd> | | 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) | <kbd>CmdOrCtrl+Shift+Q</kbd> |
| 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 ## Installation

30
main.js
View File

@ -159,7 +159,7 @@ const cleanup = () => {
}; };
const addQuickNote = () => { const addQuickNote = () => {
const { quickNoteLocation, quickNoteDateFormat } = plugin.settings, const { quickNoteLocation, quickNoteDateFormat, quickNoteAlwaysCreate } = plugin.settings,
pattern = quickNoteDateFormat || DEFAULT_DATE_FORMAT, pattern = quickNoteDateFormat || DEFAULT_DATE_FORMAT,
date = obsidian.moment().format(pattern), date = obsidian.moment().format(pattern),
name = obsidian name = obsidian
@ -172,11 +172,24 @@ const addQuickNote = () => {
// set to "same folder as current file") // set to "same folder as current file")
leaf = plugin.app.workspace.getLeaf(), leaf = plugin.app.workspace.getLeaf(),
root = plugin.app.fileManager.getNewFileParent(""), root = plugin.app.fileManager.getNewFileParent(""),
openMode = { active: true, state: { mode: "source" } }; openMode = { active: true, state: { mode: "source" } },
filePath = `${name}.md`;
if (quickNoteAlwaysCreate) {
plugin.app.fileManager plugin.app.fileManager
.createNewMarkdownFile(root, name) .createNewMarkdownFile(root, name)
.then((file) => leaf.openFile(file, openMode)); .then((file) => leaf.openFile(file, openMode))
showWindows(); .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) => { replaceVaultName = (str) => {
return str.replace(/{{vault}}/g, plugin.app.vault.getName()); return str.replace(/{{vault}}/g, plugin.app.vault.getName());
@ -358,6 +371,15 @@ const OPTIONS = [
onBeforeChange: unregisterHotkeys, onBeforeChange: unregisterHotkeys,
onChange: registerHotkeys, 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) => const keyToLabel = (key) =>