Templater icon indicating copy to clipboard operation
Templater copied to clipboard

3rd party syncing triggers new file fill-in in templater folder

Open saezhe opened this issue 1 year ago • 5 comments

Plugin information:

  • OS: Windows 10
  • Templater version: 1.12.0
  • Obsidian version: v0.15.9
  • Templater settings:
    • Template folder location: "9 extras/90090 hidden!/hidden! 8 templates/hidden! 801 templater",
    • Trigger Templater on new file creation: true

Describe the bug google drive being used for syncing vault across multiple devices triggers new file creation when syncing new template files.

Expected behavior [during syncing, or anytime] automatically added template files in the designated templater folder are ignored by new file trigger.

Additional context unintentional population of all of my template files has happened 3 times to me now, usually because a folder is renamed and then synced to my other computer, where the files are promptly populated and then synced back to the cloud all in a few seconds. i had to go through each file in file recovery and paste their old contents all in 30 minutes. i am not sure if it is possible to differentiate whether a new file is from a syncing or not, which is why i suggested the expected behavior to be ignoring newly added files in the templater folder, but really any safety measure feature for this would be helpful.

saezhe avatar Aug 10 '22 22:08 saezhe

I wonder how this would be implemented since the sync is likely creating the file on the target device and it seems difficult to distinguish it from an existing file being synced over, from a file metadata standpoint.

AB1908 avatar Aug 10 '22 23:08 AB1908

This seems to be the same as #554. It's not specific to third party sync, it occurs any time a new file is created which contains templates, e.g. with official sync, or even with bash: echo '<% tp.system.prompt("test") %>' > $OBSISIAN_VAULT/test.md.

My suggested solution: only trigger Templater on new file creation if the file is open in a pane. Then, Templater will only be triggered in the instance of Obsidian which created the file.

I'd guess this might be a bit tricky to implement, because I'd assume that the file creation event happens some micro/milliseconds before the file is actually opened in a pane.

To cope with this:

  • in the file creation event handler:
    • create a new "file-open" event handler for when a file is opened in a pane
      • in the inner handler, check if the opened file is the same as the created file
        • if so, then trigger Templater
    • create a setTimeout to clear the "file-open" event handlers after a short timeout, say 300ms or so.

Rough pseudocode:

app.vault.on("create", (createdFile) => {
  let timeout
  const openEvtRef = app.workspace.on("file-open", (openedFile) => {
    if (openedFile === createdFile) {
      app.workspace.offref(openEvtRef)
      clearTimeout(timeout)
      templater.trigger()
    }
  })
  timeout = setTimeout(() => app.workspace.offref(openEvtRef), 300)
})

b0o avatar Sep 10 '22 00:09 b0o

Hey @shabegom, since #816 was reverted, can we re-open this issue until a new fix is implemented?

b0o avatar Sep 20 '22 03:09 b0o

if it is for the templates only you could just check if a new file is created in the template directory and ignore it. Otherwise I have an issue if you just blankly ignore files which are not open in a pane. I use Readwise, Raindrop etc integration and it just works with specifying the templater syntax in their templates. The same is true if you create a new note without opening it directly.

I think the main question is why just copying a file into the vault is seen as a new note creation by Obsidian, this is a "bug" on their end. The new file event should only trigger if obsidian code is being used to create the file.

MMoMM-org avatar Nov 14 '22 14:11 MMoMM-org

Still not resolved?

dxcore35 avatar Oct 05 '23 05:10 dxcore35