liamcain / obsidian-creases

👕 Tools for effectively folding markdown sections in Obsidian
MIT License
228 stars 8 forks source link

Unfold when templater command runs #12

Closed imeed166 closed 2 years ago

imeed166 commented 2 years ago

I'm using a templater command that replace - [x] with - [ ], here is the code

<%*
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)
_%>

And 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 ?

liamcain commented 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.

imeed166 commented 2 years ago

Thank you for explaining and for providing me with two alternative solutions.

imeed166 commented 2 years ago

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.

imeed166 commented 2 years ago

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')
_%>
liamcain commented 2 years ago

Just to confirm, does the heading have a little 👕 next to it?

image

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:

image

Let me see if there's a better solution though. I'll get back to you.

imeed166 commented 2 years ago

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.

liamcain commented 2 years ago

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)
_%>
imeed166 commented 2 years ago

It works, thank you for your time.