obsidian-filename-heading-sync icon indicating copy to clipboard operation
obsidian-filename-heading-sync copied to clipboard

Race Condition with Templater

Open dredhorse opened this issue 2 years ago • 7 comments

Use File Open Hook is disabled.

There are two issue I notice with my templates.

I do rename the title of the note with the templater function: await tp.file.rename(newFilename) after that, I include a template file with tp.file.include(otherTemplate) which adds the frontmatter and then I include another template which includes the H1.

  1. Related to your plugin: If the plugin is enabled the file rename doesn't work if the plugin is disabled the file rename does work.

  2. Probably related to templater: Templater doesn't pick up the correct note title after the rename in the included templates. Looking into this issue atm too.

A workaround for 1) could be to either wait a little (which you already suggested for the original bug) or perhaps to allow more options to configure what the plugin does. For example, only update the H1 if the file is renamed, or only update the filename if the H1 is renamed.

dredhorse avatar May 10 '22 19:05 dredhorse

Bug for 2. https://github.com/SilentVoid13/Templater/issues/636#issue-1231624729

dredhorse avatar May 10 '22 20:05 dredhorse

I just ran into this issue as well. Filename Heading Sync causes templater's await tp.file.rename(newFilename) to not work. Specifically, in my case, it renames the file but doesn't include the rest of the template commands. Or perhaps the other template commands work but then Filename Heading sync deletes the content. Any insight on how this might be fixed?

evanharmon1 avatar Jul 18 '22 03:07 evanharmon1

I also encountered this issue but with the tp.file.move function:

Using this template, the note is placed directly in a specific folder and renamed using the move function:

# Untitled

Anything here doesn't appear in the created note, only the first heading

<% tp.file.move("Topic/Untitled")) %>

As a temporary "workaround", if you can call it that, you can place the move or rename function call inside a setTimeout :

<%*
setTimeout(() =>{
	tp.file.move("Topic/Untitled")
}, 0)
-%>

A little bit clunky since the note doesn't remain open but it is created with all the content from the template

eternialz avatar Jul 22 '22 12:07 eternialz

Good idea! With that strategy, this was the best I could come up with:

<%*
setTimeout(() =>{
	tp.file.rename("Title preface string " + tp.file.selection().split('\n')[1])
}, 0)
-%>

<% tp.file.selection() %>

Close, but I'm not sure it quite does the trick in my situation since it closes the note and then I need to go back to the note I grab the selection from and delete the selection and link to the note. But I might keep tinkering.

evanharmon1 avatar Jul 31 '22 16:07 evanharmon1

Sorry I don't use Templater and not sure what I could add to fix this race condition besides artificial delays, like was suggested here with setTimeout

filename-heading-sync is just using the normal obsidian events to do it's thing. If you rely heavily on templater and the artificial delay isn't doing it for you, maybe deactivate both hooks and sync only manually. The last release included 2 commands for manual syncing

Any PRs for this issue are very welcome!

dvcrn avatar Aug 05 '22 01:08 dvcrn

I didn't want to disable file save hook because it was the feature that made me use this plugin but I guess the two manual commands will do for now.

However, if you don't need heading sync after the template has been parsed and the file created, you can trigger those 2 commands through templater, after renaming or moving has occured :

<%* 
    this.app.commands.executeCommandById('obsidian-filename-heading-sync:sync-filename-to-heading');
    this.app.commands.executeCommandById('obsidian-filename-heading-sync:sync-heading-to-filename');
-%>

eternialz avatar Aug 09 '22 09:08 eternialz

I think another option would be to be able to select a "one-way" sync.

So have options to

  • only update the filename if the header changes
  • only update the header if the filename changes

I think the issue also comes because "best practice" for templater is to use

[[filename]]

as a header.

Need to test if removing the [[]] would remove the issue.

On the other side I have

[[filename|This is my Test File]]

also as header.

Perhaps my use case just doesn't support the syncing.

dredhorse avatar Aug 09 '22 17:08 dredhorse