-
-
Notifications
You must be signed in to change notification settings - Fork 198
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
Unexpected behavior when using hooks.on_all_templates_executed on a blank file #1309
Comments
…ing file content conflicts refs: #1309
I believe I've fixed this in the latest release, can you verify on your end? |
Yes! It works now, thanks! |
…n using `tp.hooks.on_all_templates_executed()` refs: #1309
Scenario 1: Scenario 2: |
I am still experiencing this bug as well. Obsidian 1.5.8, Templater 2.2.3 This template overrides the text for me if inserted into an empty file
If a delay is added with |
The issue is back for me as well, when I try to run the same example template as I posted earlier the text once again disappears once the properties have been added. obsidian version 1.5.12, templater version 2.2.3 |
I can also reproduce this issue. Obsidian: 1.5.12 Steps to reproduce:
Minimal example template: ## Attendees
- John
- Hendrik
-
## Agenda
- CallGate
<%*
tp.hooks.on_all_templates_executed(async () => {
const file = tp.file.find_tfile(tp.file.path(true));
await app.fileManager.processFrontMatter(file, (frontmatter) => {
frontmatter["status"] = "In progress"; // create a new property or update an existing one by name
frontmatter["Title"] = "New title";
});
});
-%> The "Note.md was modified externally, merging changes automatically" notice does not appear when the issue occurs. When it does, the issue does not occur, as far as I can tell. If you add more than the one aforementioned line, this notice appears, and the note content is not overwritten! |
Thank you all, I can reproduce this issue and am investigating. I know exactly what line of code I can add to resolve this issue, however it would cause a regression for another issue #1253. I am investigating how I can resolve both of these issues. |
…ocessFrontMatter` on blank files refs: #1309
…pp.fileManager.processFrontMatter` on blank files refs: #1309
Should be fixed in 2.2.4! |
I'm seeing some very unpredictable behaviour with this change. EDIT: The weird part is, once this error occurs Obsidian never recovers. You must reload the app and then everything will work again, until the race condition is triggered and the error occurs. If I revert back to the previous version of Templater or remove the line |
Should be fixed in 2.3.0, thanks @johnfromoptus ! |
Hi, It still happens in 2.3.1 when using Here is a template. The only workaround is to replace hook by <%*
/* Be sure to switch to edit mode first (live preview)
It is required so it can insert the template in existing file. */
tp.user.Switch_to_edit_mode();
// start of template construct
let title = tp.file.title;
// tags are type of list and are expressed as array
const defTag = ["Obsidian/Plugin"];
if (title.startsWith("Untitled")) {
title = await tp.system.prompt("What is the plugin name ?");
await tp.file.rename(title);
}
let repo = await tp.system.prompt("What is the plugin repo URL ?");
setTimeout(() => {
app.fileManager.processFrontMatter(tp.config.target_file, frontmatter => {
frontmatter["tags"] = defTag;
frontmatter["repo"] = repo;
frontmatter["type"] = "plugin";
})
}, 500) // increase this timeout until the bug goes away
await tp.file.move("Categories/Tutos/Obsidian/Plugins/" + title);
%>
# Description
<% tp.file.cursor(1) %><%* app.workspace.activeLeaf.view.editor?.focus(); %>
# My use
# Shortkeys
| Shortkeys | Actions |
| --------- | ------- |
| | |
| | |
If use this script version using hook with command <%*
/* Be sure to switch to edit mode first (live preview)
It is required so it can insert the template in existing file. */
tp.user.Switch_to_edit_mode();
// start of template construct
let title = tp.file.title;
// tags are type of list and are expressed as array
const defTag = ["Obsidian/Plugin"];
if (title.startsWith("Untitled")) {
title = await tp.system.prompt("What is the plugin name ?");
await tp.file.rename(title);
}
//don’t insert the title as a tag because it can be long and contains space
//defTag.push(title)
let repo = await tp.system.prompt("What is the plugin repo URL ?");
tp.hooks.on_all_templates_executed(async () => {
const file = tp.file.find_tfile(tp.file.path(true));
await app.fileManager.processFrontMatter(file, (frontmatter) => {
frontmatter["tags"] = defTag;
frontmatter["repo"] = repo;
frontmatter["type"] = "plugin";
});
});
/* setTimeout(() => {
app.fileManager.processFrontMatter(tp.config.target_file, frontmatter => {
frontmatter["tags"] = defTag;
frontmatter["repo"] = repo;
frontmatter["type"] = "plugin";
})
}, 500) */ // this timeout will prevent a bug when insert the template to an existing file that content only one empty line. When it happens, the note content is deleted. See: https://github.com/SilentVoid13/Templater/issues/1309#issuecomment-2079178033
await tp.file.move("Categories/Tutos/Obsidian/Plugins/" + title);
%> |
@Fred-Vatin I tested your template (with the userscript part removed), and got the same result when inserting into a blank file: The content was lost, and the frontmatter changes stayed. The file was moved as expected. When I compared your problematic template to my working ones, the main difference was that you had the prompts and renaming outside <%*
tp.hooks.on_all_templates_executed(async () => {
// start of template construct
let title = tp.file.title;
// tags are type of list and are expressed as array
const defTag = ["Obsidian/Plugin"];
if (title.startsWith("Untitled")) {
title = await tp.system.prompt("What is the plugin name ?");
await tp.file.rename(title);
}
//don’t insert the title as a tag because it can be long and contains space
//defTag.push(title)
let repo = await tp.system.prompt("What is the plugin repo URL ?");
const file = tp.file.find_tfile(tp.file.path(true));
await app.fileManager.processFrontMatter(file, (frontmatter) => {
frontmatter["tags"] = defTag;
frontmatter["repo"] = repo;
frontmatter["type"] = "plugin";
});
});
/* setTimeout(() => {
app.fileManager.processFrontMatter(tp.config.target_file, frontmatter => {
frontmatter["tags"] = defTag;
frontmatter["repo"] = repo;
frontmatter["type"] = "plugin";
})
}, 1000); */ // this timeout will prevent a bug when insert the template to an existing file that content only one empty line. When it happens, the note content is deleted. See: https://github.com/SilentVoid13/Templater/issues/1309#issuecomment-2079178033
%> Actually, moving the renaming part to after <%*
tp.hooks.on_all_templates_executed(async () => {
console.log("inside tp.hooks");
//don’t insert the title as a tag because it can be long and contains space
//defTag.push(title)
let repo = await tp.system.prompt("What is the plugin repo URL ?");
const file = tp.file.find_tfile(tp.file.path(true));
await app.fileManager.processFrontMatter(file, (frontmatter) => {
frontmatter["tags"] = defTag;
frontmatter["repo"] = repo;
frontmatter["type"] = "plugin";
});
});
// start of template construct
console.log("after tp.hooks");
let title = tp.file.title;
// tags are type of list and are expressed as array
const defTag = ["Obsidian/Plugin"];
if (title.startsWith("Untitled")) {
title = await tp.system.prompt("What is the plugin name ?");
await tp.file.rename(title);
}
%> |
Doesn't matter if you move the file, or whether the content is before the codeblock, or after. |
This is the last version that works in any case using <%*
/* Be sure to switch to edit mode first (live preview)
It is required so it can insert the template in existing file.
It must be called first and outside the tp.hooks. */
tp.user.Switch_to_edit_mode();
tp.hooks.on_all_templates_executed(async () => {
// tp.hooks will prevent a bug when insert the template to an existing file that content only one empty line. When it happens, the note content is deleted. See: https://github.com/SilentVoid13/Templater/issues/1309#issuecomment-2079178033
// start of template construct
let title = tp.file.title;
// tags are type of list and are expressed as array
const defTag = ["Obsidian/Plugin"];
if (title.startsWith("Untitled")) {
title = await tp.system.prompt("What is the plugin name ?");
await tp.file.rename(title);
}
//don’t insert the title as a tag because it can be long and contains space
//defTag.push(title)
let repo = await tp.system.prompt("What is the plugin repo URL ?");
const file = tp.file.find_tfile(tp.file.path(true));
await app.fileManager.processFrontMatter(file, (frontmatter) => {
frontmatter["tags"] = defTag;
frontmatter["repo"] = repo;
frontmatter["type"] = "plugin";
});
await tp.file.move("Categories/Tutos/Obsidian/Plugins/" + title);
});
%>
# Description
<% tp.file.cursor(1) %><%* app.workspace.activeLeaf.view.editor?.focus(); %>
# Comment je m’en sers
# Shortkeys
| Shortkeys | Actions |
| --------- | ------- |
| | |
| | |
Content of my script function switch_to_editmode () {
/* Be sure to switch to edit mode first (live preview)
It is required so it can insert the template in existing file. */
const view = app.workspace.activeLeaf.getViewState()
view.state.mode = 'source'
view.state.source = false
app.workspace.activeLeaf.setViewState(view)
}
module.exports = switch_to_editmode; Thanks. |
I'm afraid the issue still exists for me as of 2.3.1. Again using
on a new empty file the text that should stay doesn't stay. However, now the text doesn't pop up for half a second before it disappears which it used to do. |
Should be fixed in 2.3.2! |
I re-tested @Fred-Vatin's previously problematic template here, and can confirm that the fix seems to have worked 🎉 |
However, when testing this simple template, the text stays but the frontmatter disappears (which is strange, since @simonalveteg reported that the text would disappear): However, I can't reproduce it reliably. Sometimes, the frontmatter stays, sometimes it disappears. |
But I suppose the above doesn't apply directly to this issue, because the issue is about inserting templates into blank notes. In the above testing, I was creating a new note from a template. I can't reproduce the issue when inserting the template, so I suppose the fix did work. So it's only creating new notes from templates that is bugging out, sometimes. |
Reverted the fix for this template as it was causing more harm than good, will look into this further when I get a chance. |
Still having this issue, any updates? |
Describe the bug
When using the hooks.on_all_templates_executed callback like in the documentation with some text that should get added to the note before the frontmatter is updated:
if the note the template is added to is blank (no text, no frontmatter), the text gets added and then removed when the frontmatter is added. By just adding a single character to the note before executing the template, both the text and frontmatter gets added without problem. (There is a pop-up "error" that the file has been modified externally and that it's merging the changes, not sure if that's an issue but you asked about it on discord)
Expected behavior
The text and frontmatter should get added to the note even if the note is completely empty.
Additional context
I've tried running the template above on two windows 11 computers with the same result, and also tried it in a new empty vault with only templater installed. I'm not 100% sure, but I believe that it worked as expected before, since I have a few templates that rely on this method. I don't use them regularly though, and I updated them to use this method last time I needed to use them (when the hook had just gotten added), so it's possible that I'm misremembering or that the notes I tested them on weren't completely empty.
link to message on discord
The text was updated successfully, but these errors were encountered: