Automatic inlinks to a note
I’ve been holding off on submitting a few of my snippets here just to make sure they’re not a fad… but it’s been a few months now and I have to say one dataviewjs block that’s really improved how I use Obsidian has been having a block that nicely displays all the inlinks to a given file.
This is what it looks like:
And this is the code snippet:
// Nicely Render all the inlinks to the current note on a single line
// Checks if an alias exists for inlinks and will render the alias
// if it exists
//
let myInlinks = [];
for (let inlink of dv.current().file.inlinks){
if (inlink.path != dv.current().file.path) {
let inlinkFile = dv.page(inlink.path).file
let displayName = inlinkFile.aliases ? inlinkFile.aliases[0] : inlinkFile.name
let fileLink = dv.fileLink(inlinkFile.path, false, displayName)
myInlinks.push(fileLink)
}
}
let myInlinksStr = ` **Inlinks**: ${myInlinks.join(', ')}`
dv.paragraph(myInlinksStr)
How it works
- Get all the inlinks of the current file
- Iterate through them, ignoring any self reference (such as internal header links)
- Get the inlink file object to read it’s data
- Create a dataview fileLink, using the first inlinks alias as the displayname if there’s an alias.
- Push all the fileLinks into an array
- Render a comma separated list of the fileLinks with a prefix of Inlinks
I find this significantly helps me flow through my notes and lets me links files an organic way.
My typical journeys might be:
I create a note about a new takeaway place i’ve tried, and link it to my notes on Deliveroo, Chinese Food, Local Takeaways, Cheap Eats etc. Infact, i’ve more or less forgone using tags at all now in preference of notes as tags since I find they serve the same purpose with more utility.
By using the dataviewjs I can have more control over the format like using the alias for example. I often use aliases for my Map of Content (MOC) notes.
The only minor issue i’ve had that’s probably worth mentioning is on the new creation of a file, the parent inlink won’t automatically show since it’s not present in the file.inlinks array. But if I really need this immediately, I can reload Obsidian and it’ll update.