Using templater to create a link to the local folder in which a note is stored

What I’m trying to do

Hi everyone. I’m trying to use templater to create an automatic link to the local folder that the note is in.

To clarify, I mean one of those links to show the file in the system explorer such as: [The name of the folder](file:///C:%5CUsers%5Cblahblahblah%File%Folder)

Things I have tried

I was asking on Discord and someone sent me this which will create a link to the .md file:

<%*
// Get the TFile of the note to which the template is applied
const file = tp.config.target_file;
const absoluteLink = app.vault.adapter.getFilePath(file.path);
-%>
<% absoluteLink %>

Also, I saw this post here about linking to the file path: Inherit or pass on Metadata Values from Active File when creating New File - #4 by holroy
and was most interested in this bit:

const baseFolder = app.vault.getAbstractFileByPath(dvCurr.file.folder)

I was trying to combine the two but so far I haven’t had any luck getting something to work.

Any help is appreciated!

Just to be clear, when I mentioned combining them, I was thinking maybe something like this:

<%*
const folder = tp.file.folder(true);
const absoluteLink = app.vault.adapter.getFolderByPath(dvCurr.file.folder);
-%>
<% absoluteLink %>

So I’ve been thinking about this and I’m really close to making this work. Chat GPT helped me (lol)

Currently the code is:

<%*
// Get the TFile of the note to which the template is applied
const file = tp.config.target_file;
const filePath = file.path;
const lastIndex = filePath.lastIndexOf('/');
const directoryPath = filePath.substring(0, lastIndex);
const absoluteLink = `${window.location.origin}/#path=${encodeURIComponent(directoryPath)}`;
-%>
<% absoluteLink %>

Basically, it:

  • gets the file path using the tp.config.target_file thing
  • Then cuts off the file name, so the link only goes to the folder

But I’m not quite sure how to make it show up as the “absolute link”

Does anyone have any ideas?

I won!!

(Although there is probably a nicer way to make this happen. lol)

Local folder: [Find the local folder here](file:///C:THISISTHELINKTOTHEVAULTHERE<%*
// Get the TFile of the note to which the template is applied
const file = tp.config.target_file;
const filePath = file.path;
const lastIndex = filePath.lastIndexOf('/');
const directoryPath = filePath.substring(0, lastIndex);
const absoluteLink = `${encodeURIComponent(directoryPath)}`;
-%><% absoluteLink %>)

Anyways, sharing in case anyone else has similar needs.