Getting backlinks, tags and frontmatter entries for a note

I’d like to create a view panel on the right that displays a filtered view of some of the currently selected editor’s note. My plugin should

  1. Detect when the currently displayed note is replaced by another
  2. Update the filtered backlink list based on parameters read in the current note frontmatter

For example when reading a #project note, the filtered view could display the list of #meeting notes linking to that particular project note. Can see this as a bit of a dataview but materialized in a side panel instead of being included in the document.

Is there an easy API endpoint for ask for all backlinks of the current note, get the tags or the yaml of a current note? All the API i saw are pretty low level and require parsing all the content, i expected the Vault object to give access to the linked view. Maybe i missed something ?

you can try this code in the console, it should give you all the data you need:

file = app.vault.getAbstractFileByPath("your/file.md")
metadata = app.vault.metadataCache.getFileCache(file)
backlinks = app.vault.metadataCache.getBacklinksForFile(file)

metadata contains all values except backlinks, if a file has no frontmatter for example, metadata.frontmatter will not exist.
https://github.com/obsidianmd/obsidian-api/blob/c01fc3074deeb3dfc6ee02546d113b448735b294/obsidian.d.ts#L388-L425

Tags need to be collected in two places, one is on the metadata.frontmatter.tags the tags that are placed in document are on metadata.tags.

getBacklinksForFile is not document officially, so it might break at some point.


For detecting when the active note has been changed you probably want to listen to this event: https://github.com/obsidianmd/obsidian-api/blob/c01fc3074deeb3dfc6ee02546d113b448735b294/obsidian.d.ts#L3724

4 Likes

Sorry for bumping, but Google results still lead here, and just noting:

app.vault.metadataCache.getFileCache(file)

looks to now be just:

app.metadataCache.getFileCache(file)

1 Like