When using TemplateChoice and setting it to open file when already exists, always get error if the file is in any subfolder of the vault.
To Reproduce
Steps to reproduce the behavior:
Add a template choice
In choice settings, turn on "File Name Format", "Create in Folder", "Open"
Specify file name format as {{Date}}
Add a non-root folder path for creating new note
Invoke the newly added QuickAdd command and create a new daily note in some subfolder of the vault
Invoke the command again, get error: "FilePath already exists and is not a valid markdown file." (FilePath is actually the name of the previously created daily note)
Expected behavior
Should see options to append, overwriting or do nothing to the existing note
Additional context
I actually figured out the reason but don't know how to fix (not a js programmer).
The following lines are in TemplateChoiceEngine:
if (await this.app.vault.adapter.exists(filePath)) {
const file = this.app.vault.getAbstractFileByPath(filePath);
if (!(file instanceof obsidian.TFile && file.extension === 'md')) {
log.logError(`'${filePath}' already exists and is not a valid markdown file.`);
return;
}
...
I did some test and found that this.app.vault.adapter.exists(filePath) recognizes the leading slash in filePath while this.app.vault.getAbstractFileByPath(filePath) doesn't.
E.g. filePath='/pages/abc', exists(filePath) returns true but getAbstractFileByPath(filePath) returns null, unless the leading slash is removed.
In Obsidian API doc these APIs do have different parameter definition:
Describe the bug
When using TemplateChoice and setting it to open file when already exists, always get error if the file is in any subfolder of the vault.
To Reproduce Steps to reproduce the behavior:
Expected behavior Should see options to append, overwriting or do nothing to the existing note
Additional context
I actually figured out the reason but don't know how to fix (not a js programmer).
The following lines are in TemplateChoiceEngine:
I did some test and found that this.app.vault.adapter.exists(filePath) recognizes the leading slash in filePath while this.app.vault.getAbstractFileByPath(filePath) doesn't. E.g. filePath='/pages/abc', exists(filePath) returns true but getAbstractFileByPath(filePath) returns null, unless the leading slash is removed.
In Obsidian API doc these APIs do have different parameter definition:
getAbstractFileByPath(path: string): TAbstractFile | null; exists(normalizedPath: string, sensitive?: boolean): Promise;
So the different treatment of leading slash may be by design.