SilentVoid13 / Templater

A template plugin for obsidian
https://silentvoid13.github.io/Templater
GNU Affero General Public License v3.0
3.15k stars 189 forks source link

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

Open saezhe opened 2 years ago

saezhe commented 2 years ago

Plugin information:

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.

AB1908 commented 2 years ago

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.

b0o commented 2 years ago

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:

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 commented 2 years ago

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

MMoMM-org commented 1 year ago

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.

dxcore35 commented 11 months ago

Still not resolved?

gregmuellegger commented 1 day ago

I also had the problem of expanding default templates on syncing a file between devices.

Inspired by this issue I have integrated this snippet into my default template:

if (tp.file.path(true) != app.workspace.activeEditor.file.path) {
    // This template is triggered on new files. However this might also be during
    // sync. We avoid running this template by checking if the file is currently
    // open in the active editor (would only be the case if this device created
    // the file).
    console.log("New file created, but is not in active editor. Skipping.");
    return;
}

This seems to work at first glance.