Closed imeed166 closed 2 years ago
Hi @imeed166, this behavior is expected because your template is using vault.modify
. The modify
function is "dumb" in that it replaces all the contents with a file with new contents. So Obsidian doesn't know anything about the nature of the changes. It can't tell that most of the content is the same and that just the checkboxes are unchecked. So the expected behavior is that all your fold information is cleared out. This behavior will happen with or without this plugin.
The simplest fix for now is to include the following at the end of your template:
app.commands.executeCommandById('creases:fold');
That will refold your creases after unchecking the checkboxes.
A better fix would involve using CodeMirror to actually just modify the lines with the checkboxes. But I think this behavior is likely too complex for a simple template.
Thank you for explaining and for providing me with two alternative solutions.
The simplest fix for now is to include the following at the end of your template:
app.commands.executeCommandById('creases:fold');
Sorry to bother you, but the added line doesn't re-fold the heading.
plugin in lastest version (0.4.3) file: testing.md template:
<%*
const text = tp.file.content
const output = text.replaceAll('- [x]','- [ ]')
const file = tp.file.find_tfile(tp.file.title)
await app.vault.modify(file, output)
app.commands.executeCommandById('creases:fold')
_%>
Just to confirm, does the heading have a little 👕 next to it?
If it doesn't, then it's just a regular fold.
For the command I sent to work, you'd need to add: %% fold %%
to the heading so that it looks like this:
Let me see if there's a better solution though. I'll get back to you.
Just to confirm, does the heading have a little 👕 next to it?
Yes, it's there.
Let me see if there's a better solution though. I'll get back to you.
Okay, thank you.
So it's ugly but you can do this:
<%*
const text = tp.file.content
const output = text.replaceAll('- [x]','- [ ]')
const file = tp.file.find_tfile(tp.file.title)
await app.vault.modify(file, output)
setTimeout(() => app.commands.executeCommandById('creases:fold'), 50)
_%>
It works, thank you for your time.
I'm using a templater command that replace
- [x]
with- [ ]
, here is the codeAnd when I execute this command, (because the text being replaced is underneath a folded heading with a crease) it automatically unfolds the heading.
Is the plugin supposed to behave this way ? Is there a way to make it stay folded even when executing the templater command ?