obsidian-clipper icon indicating copy to clipboard operation
obsidian-clipper copied to clipboard

Error: File already exists

Open trustin opened this issue 2 years ago • 7 comments

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 avatar May 15 '23 10:05 trustin

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 avatar May 15 '23 10:05 trustin

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.

trustin avatar May 15 '23 10:05 trustin

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!

jgchristopher avatar May 15 '23 17:05 jgchristopher

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

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

trustin avatar May 16 '23 06:05 trustin

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 avatar May 19 '23 01:05 jgchristopher

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 avatar May 19 '23 01:05 jgchristopher

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.

jgchristopher avatar Jun 20 '23 14:06 jgchristopher