Creating backlinks in Templater

Things I have tried

I have been experimenting with Templater for applying templates to notes.

What I’m trying to do

I am clicking on a link to a note that doesn’t exist and then applying a template. I would like to create a backlink to the note that initiated the creation of the current note. How would I identify the note and create a backlink to it?

Example

If the initiating note is:
[[Start Note]]
[[New Note]]
New note would include a link to Start Note.

Within the template you could use tp.config.active_file(see tp.config - Templater) to get where you came from.

Thanks @holroy Tried

let parent_tfile = tp.config.active_file; 

<? parent_tfile ?>

Comes out as [object Object] do you know what I’m doing wrong?

And what do you want out of <? ... ?> ? The objecs is TFile, and you can access a lot of stuff with it. Like the fields within, the metadata of it, the link to it, so what do you want?

Originating file: <% tp.config.active_file.basename %>
Link to that file: [[<% tp.config.active_file.path %> | My parent ]]

Could be one way to access it. And after doing console.log(tp.config.active_file) I could get this image:
image

This was a note called “River house”, showing some of the information available.

Thanks @holroy

When I apply the code below I just get the name of the filename of the current note not the one that initiated the creation. Is there field I should be trying?

<% tp.config.active_file.name %>

Learnt something new on the console log. Never knew you could do that. Thanks!

It shouldn’t do that. It should report the original file, as far as I know. So I posted a bug report regarding this on github.

There are various other dubious method to get to the last files, but I’m not entirely convinced which is the best to use. Some use this.app.workspace.lastOpenFiles[0], but I’m semi reluctant to relying on that. Not quite sure why, but…

I couldn’t get tp.config.active_file.name to work either. Finally, I ended up using the {{fromTitle}} option in Note Refactor (extract selection to new note - first line as file name) to achieve this. Other option is to use the Note composer plugin. Check this thread for details Create new note with a title and a backlink to original note

I haven’t explored other dubious methods holroy has mentioned, but I tried this.app.workspace.lastOpenFiles[0]. Unfortunately, it won’t work reliably in all cases. For example, say I opened file-A, then file-B, then I clicked on an uncreated link in file-A, then this.app.workspace.lastOpenFiles[0] would return file-B instead of file-A.

Looks like the (relatively) recent back history feature makes it possible to achieve this reliably. Here is an example. I tested this in several different ways and it worked all the time. Let me know if you hit any issues. I have been wanting to implement this using the Templater plugin for some time, so I am glad we have started this discussion.

<%*
    let last_file = ""
    let recent_leaf = this.app.workspace.getMostRecentLeaf();
    let back_history = recent_leaf.history.backHistory;
    //skip when there is no back history 
    //this can happen when the file gets created directly from the GUI
    if(back_history.length > 0) {
        // the last entry is the most recent file
        last_file = "[[" + back_history[back_history.length - 1].title + "]]";
    }    
    tR += last_file  ;
_%>
1 Like

Hi @liberated_potato,

Gave the code a try and works perfectly in templater. Success!

@holroy will retest once the fix goes in.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.