-
Notifications
You must be signed in to change notification settings - Fork 31
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
Comments
Bug for 2. SilentVoid13/Templater#636 (comment) |
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? |
I also encountered this issue but with the Using this template, the note is placed directly in a specific folder and renamed using the move function:
As a temporary "workaround", if you can call it that, you can place the move or rename function call inside a setTimeout :
A little bit clunky since the note doesn't remain open but it is created with all the content from the template |
Good idea! With that strategy, this was the best I could come up with:
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. |
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 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! |
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 :
|
I think another option would be to be able to select a "one-way" sync. So have options to
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. |
any news on this by any chance? :) |
Hi yeah also would love to see this resolved Z |
Looked a bit into it Templater emits a bunch of hooks:
Example: https://github.com/SilentVoid13/Templater/blob/master/src/core/Templater.ts#L265C44-L265C71 We can detect whether templater is installed and enabled with
We could check if templater is active, then wait for these events before syncing the heading. |
People with Templater issues, can you provide me a dead-simple step-by-step guide how I can reproduce the error? With template you're using and everything please |
It has been a while since I looked into this problem, but I can share my dummy setup to recreate it, as well as how I overcome it. My current Obsidian theme is Minimal but it should not matter. FilesI have the <%*
if (tp.file.title.startsWith('Untitled'))
{
const extra = await tp.system.prompt("<sprint>-<day> || sp/r&r-<sprint>");
let name = '';
let templatePath = '';
if (extra.includes("sp")) {
name = `${tp.date.now()} (${extra})`;
templatePath = `[[sp]]`;
} else if (extra.includes("r&r")) {
name = `${tp.date.now()} (${extra})`;
templatePath = `[[r&r]]`;
} else {
name = `${tp.date.now()} (d ${extra})`;
templatePath = `[[daily]]`
}
// Append the file name as the first heading
// Disable "Use File Open Hook" in "Filename Heading Sync" plugin to prevent bugs
// If bugs still occur, add this to the ignore regex rule of the plugin "|~AAA/Sprints/*"
await tp.file.cursor_append(`# ${name}\n\n`);
// Include the specific template based on the input
// For it to work, you need to create the note in one of the folders ignored by the regex rule
// There is an open issue: https://github.com/dvcrn/obsidian-filename-heading-sync/issues/55
if (templatePath) {
const templateContent = await tp.file.include(templatePath);
await tp.file.cursor_append(templateContent);
}
// Move the file
const sprintNumberMatch = extra.match(/\b\d+\b/);
if (sprintNumberMatch !== null) {
const sprintNumber = sprintNumberMatch[0];
await tp.file.move(`~AAA/Sprints/Sprint ${sprintNumber}/` + name);
}
}
-%> The sample
Plugin settingsTemplater
Templates (built-in Obsidian plugin)I am not sure if its settings matter, but here they are:
Filename Heading Sync
Working solutionWith the aforementioned settings, open any file in
Recreating the bugIn the
Or, when you additionally enable the
The file will be named correctly in both examples, but its content is wrong. |
Thanks! Let me take a look today and see if it's as simple as adding some waiting for the Templater hook, maybe behind a setting |
Try to wait for templater events for a short time before activating sync Ref #55
I've added a proof of concept that waits for the templater hooks (if templater is installed). I need a few more simple templates to test this The example from @eternialz above is now working with this Could someone with more Templater usage give the following |
Would anyone be able to give this version a try and tell me if they still have issues? |
Initial tests seem to show its working. will use it under some more scenarios and update soon thx so much X |
I've released the experimental support in https://github.com/dvcrn/obsidian-filename-heading-sync/releases/tag/1.9.3 It's basically just adding a short wait for the Templater hook. It's very barebones but may resolve some of this race condition. If no Templater hook is fired, it runs after waiting for 100ms so the wait should not be perceivable |
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.
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.
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.
The text was updated successfully, but these errors were encountered: