shabegom / buttons

Buttons in Obsidian
The Unlicense
455 stars 47 forks source link

Feature request: Have templater buttons that just run the template (without prepending o appending) #73

Closed fidel-perez closed 5 months ago

fidel-perez commented 2 years ago

It would be great to have also a button that only runs a template.

Why? Because then we could run templates like the following to, for example (in the code below), replace all the checked boxes for unchecked ones, without adding any text to the note:

<%*

//get file content and replace checked boxes for unchecked
var noteContent = tp.file.content.replaceAll ("[x]","[ ]");

//select all in note
let cmEditorAct = this.app.workspace.activeLeaf.view.editor;
cmEditorAct.setSelection({ line: 0, ch: 0 }, { line: 9999, ch: 9999 });

//replace content + set cursor at the start
cursor = tp.file.cursor(0);
tR = cursor + noteContent.split("<%*")[0];

%>

Right now I do the trick of using append then removing that appended text: noteContent.split("<%*")[0] But this works randomly depending on some speed of an internal process that appends the templater text to the note (because thats the button I am using).

This works perfectly when applying the template using templater command, but with buttons we cannot replicate because of the extra text insertion.

This feature would allow us to run any javascript to modify the current note using templater, I think it would be pretty cool :)

Thank you!

shabegom commented 2 years ago

Really good idea that should be pretty easy to do. Will look into it.

paulrudy commented 2 years ago

+1 to this. I would like to use this to have pass Templater an argument from the current file that it uses to create a new file.

DeutscheGabanna commented 1 year ago

has this been implemented?

simzdk commented 1 year ago