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

Race Condition with Templater #55

Open
dredhorse opened this issue May 10, 2022 · 8 comments
Open

Race Condition with Templater #55

dredhorse opened this issue May 10, 2022 · 8 comments

Comments

@dredhorse
Copy link

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
Copy link
Author

Bug for 2. SilentVoid13/Templater#636 (comment)

@evanharmon1
Copy link

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?

@eternialz
Copy link

eternialz commented Jul 22, 2022

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

@evanharmon1
Copy link

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.

@dvcrn
Copy link
Owner

dvcrn commented Aug 5, 2022

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!

@eternialz
Copy link

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');
-%>

@dredhorse
Copy link
Author

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.

@labgeea
Copy link

labgeea commented Nov 22, 2024

any news on this by any chance? :)

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

5 participants