Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: File already exists #28

Open
trustin opened this issue May 15, 2023 · 7 comments
Open

Error: File already exists #28

trustin opened this issue May 15, 2023 · 7 comments

Comments

@trustin
Copy link

trustin commented May 15, 2023

Hi! Thanks a lot for creating this plugin first of all. 🙇

When I clip something when there's no target note created, it creates a new daily/weekly note and adds the clip there. However, if I clip some other thing again, it fails with the following error:

plugin:obsidian-clipper:341
Failed to create file: 'Periodic notes/Weekly notes/2023Q2W20.md' Error: File already exists.
    at t.<anonymous> (app.js:1:966983)
    at app.js:1:235927
    at Object.next (app.js:1:236032)
    at a (app.js:1:234750)

I think it may be related with some plugins I'm using, such as Templater, Periodic notes and Calendar, but it'd be nice if the clipper plugin doesn't attempt to create a new note when it exists already.

@trustin
Copy link
Author

trustin commented May 15, 2023

Looks like it's happening somewhere in here:

async function createWeeklyNote2(date) {
      const { vault } = window.app;
      const { template, format: format2, folder } = getWeeklyNoteSettings();
      const [templateContents, IFoldInfo] = await getTemplateInfo(template);
      const filename = date.format(format2);
      const normalizedPath = await getNotePath(folder, filename);
      try {
        const createdFile = await vault.create(normalizedPath, templateContents.replace(/{{\s*(date|time)\s*(([+-]\d+)([yqmwdhs]))?\s*(:.+?)?}}/gi, (_, _timeOrDate, calc, timeDelta, unit, momentFormat) => {
          const now2 = window.moment();
          const currentDate = date.clone().set({
            hour: now2.get("hour"),
            minute: now2.get("minute"),
            second: now2.get("second")
          });
          if (calc) {
            currentDate.add(parseInt(timeDelta, 10), unit);
          }
          if (momentFormat) {
            return currentDate.format(momentFormat.substring(1).trim());
          }
          return currentDate.format(format2);
        }).replace(/{{\s*title\s*}}/gi, filename).replace(/{{\s*time\s*}}/gi, window.moment().format("HH:mm")).replace(/{{\s*(sunday|monday|tuesday|wednesday|thursday|friday|saturday)\s*:(.*?)}}/gi, (_, dayOfWeek, momentFormat) => {
          const day = getDayOfWeekNumericalValue(dayOfWeek);
          return date.weekday(day).format(momentFormat.trim());
        }));
        window.app.foldManager.save(createdFile, IFoldInfo);
        return createdFile;
      } catch (err) {
        console.error(`Failed to create file: '${normalizedPath}'`, err);
        new obsidian.Notice("Unable to create new file.");
      }
}

We could avoid calling vault.create() if the note exists at normalizedPath already.

@trustin
Copy link
Author

trustin commented May 15, 2023

It seems like this issue only affects weekly notes. The plugin adds a clipped entry into daily notes without a problem even if the note exists already.

@jgchristopher
Copy link
Owner

I haven't been able to reproduce this yet, though I did find other issues unrelated to this.

Can you share what Operating System you are on. Can you also share what version of Obsidian you are using?

Thanks!

@trustin
Copy link
Author

trustin commented May 16, 2023

Hey, thanks for a reply. Here's the information:

  • OS: macOS 13.3.1 (Ventura)
  • Obsidian: 1.2.8

@jgchristopher
Copy link
Owner

I was able to reproduce this now.

Clipper uses the periodic-notes plugin for weekly notes. Clipper would fail and show a Notice that this plugin is required when trying to use the weekly note.

Oddly, I can only recreate this if I use a Note Title Format like yours :).

  1. gggg[Q]Q[W]w (like yours fails if note exists)
  2. default in periodic notes works
  3. gggg[W]w works as well.

@jgchristopher
Copy link
Owner

This is an issue in the periodic-notes-interface code. For some reason it is trying to recreate a date from file names and the Q2 part of the filename being a weekly file is breaking it.

Not sure why the code doesn't just take the date, the format, generate the path and see if that exists.

I don't think I will be able to fix this one on my end currently.

@jgchristopher
Copy link
Owner

The periodic-notes-interface library is causing the problem. It was designed/extracted from the calendar plugin so has an odd interface. I am going to play around to see if I can support this a different way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants