question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unfold when templater command runs

See original GitHub issue

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 ?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
liamcaincommented, Mar 8, 2022

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)
_%>
1reaction
liamcaincommented, Mar 8, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

15 Easy Templater Commands For Obsidian - Red Gregory
Navigate to command palette (command + P). Find Templater : Open Insert Template modal. All files inside the template folder will be found...
Read more >
Automation of AddNewTransfer - from a folded to unfolded ...
I've got a script (below) which uses the AddNewTransfer method, but gives a null result, by which I mean that the command runs,...
Read more >
Using the Obsidian Templater Plugin - YouTube
What IS the Obsidian Templater plugin and more importantly, how do you use it? I outline the three main use cases for it,...
Read more >
Trial templates & tables - PennController for IBEX
Multi-trial experiments using templates ... the experiment runs (for more on how the group value is chosen on a given run of the...
Read more >
SilentVoid13/Templater: A template plugin for obsidian - GitHub
Only run code / commands that you understand, from trusted sources. Template Showcase / Questions / Ideas / Help. Go to the discussion...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found