chhoumann / quickadd

QuickAdd for Obsidian
https://quickadd.obsidian.guide
MIT License
1.53k stars 136 forks source link

[BUG] TemplateChoice can not open existing file in subfolder #74

Open mrpurplecloud opened 3 years ago

mrpurplecloud commented 3 years ago

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:

  1. Add a template choice
  2. In choice settings, turn on "File Name Format", "Create in Folder", "Open"
  3. Specify file name format as {{Date}}
  4. Add a non-root folder path for creating new note
  5. Invoke the newly added QuickAdd command and create a new daily note in some subfolder of the vault
  6. 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:

getAbstractFileByPath(path: string): TAbstractFile | null; exists(normalizedPath: string, sensitive?: boolean): Promise;

So the different treatment of leading slash may be by design.

mrpurplecloud commented 3 years ago

BTW when user explicitly specify to do Nothing with existing file, the "(Error) File not written to." prompt seems to be a little bit disturbing :-)