How to get backlinks for a file?

Hi, I would like to develop my own plugin, based on that request :point_down: since I also would love to see something like this implemented.

I noticed it is possible to get links with:

const links = this.app.metadataCache.getFileCache(file)?.links;

So half of the job is done. But is there a way to get backlinks?

I kinda donā€™t want to iterate over whole vault since there is already a backlinks official plugin, so I assume optimal solution is already made.
Like I could just look at official plugin source code and continue from here, couldnā€™t I? :eyes:

I noticed there is answer to similar question. Even though it is said here that getBacklinksForFile is not documented officially, I couldnā€™t find it in Typescript typing definitions.

I also think I could just take both ā€œBacklinksā€ and ā€œOutgoing linksā€ html content and put it inside one tab, but itā€™s not that fun.

So to sum up: is there a viable way to find backlinks for the current file? If not, what possible workaround?

1 Like

It not being documented means that itā€™s not found in the typings.
You can test the code in the dev tools console to see what it returns.

1 Like

Could you describe what new user experience youā€™re trying to design. FYI a forward/outgoing links pane was added to Obsidian w/ v0.12.4 as the ā€œOutgoing Linksā€ core plugin.

Hi, I would like to develop my own plugin, too. Its function includes merging all notes linked to this note. Therefore, I need to find backlinks for the specified note.
:face_with_peeking_eye:For now, is there a feasible way to find the backlink of the current file?

It appears that backlink files can be retrieved using the method in the link given above even in the current version.
I wonder why it is not part of the official API :thinking:

Do you know someone who can approve the 2 PRs I just opened?

One fixes line ending inconsistencies, hopefully for good Line endings by mfp22 Ā· Pull Request #115 Ā· obsidianmd/obsidian-api Ā· GitHub

The other adds the type for getBacklinksForFile Add getBacklinksToFile by mfp22 Ā· Pull Request #116 Ā· obsidianmd/obsidian-api Ā· GitHub

There are actually a lot of things that are available in JS but not in TS.

console.log(Object.keys(this.app.metadataCache))
/* [
  "_",
  "worker",
  "inProgressTaskCount",
  "db",
  "fileCache",
  "metadataCache",
  "workQueue",
  "uniqueFileLookup",
  "initialized",
  "resolvedLinks",
  "unresolvedLinks",
  "linkResolverQueue",
  "onCleanCacheCallbacks",
  "workerResolve",
  "userIgnoreFilters",
  "userIgnoreFiltersString",
  "userIgnoreFilterCache",
  "app",
  "vault",
  "blockCache",
] */

console.log(Object.keys(this.app.metadataCache._))
// ['finished', 'resolved', 'resolve', 'changed']

// Inspecting the this.app.metadataCache prototype and storing as global var `temp4`
console.log(Object.keys(temp4))
/* [
  "constructor",
  "getLinkSuggestions",
  "getTags",
  "getLinkpathDest",
  "getFirstLinkpathDest",
  "_getLinkpathDest",
  "getLinks",
  "getFileCache",
  "getCache",
  "getFileInfo",
  "getCachedFiles",
  "iterateReferences",
  "getBacklinksForFile",
  "fileToLinktext",
  "watchVaultChanges",
  "initialize",
  "clear",
  "showIndexingNotice",
  "linkResolver",
  "resolveLinks",
  "onCleanCache",
  "checkCleanCache",
  "isCacheClean",
  "updateRelatedLinks",
  "onCreate",
  "onCreateOrModify",
  "onDelete",
  "deletePath",
  "onRename",
  "saveFileCache",
  "saveMetaCache",
  "work",
  "computeMetadataAsync",
  "onReceiveMessageFromWorker",
  "cleanupDeletedCache",
  "updateUserIgnoreFilters",
  "isSupportedFile",
  "isUserIgnored",
  "getFrontmatterFieldValuesForKey",
  "trigger",
  "on",
] */

I can see youā€™ve already been answered on Github. But please do not ping the developers directly for your requests.

Source: Our Code of Conduct related to pinging devs and moderators.

Hi, maybe itā€™s too late, but let me share my idea here for those who arrive on this page.

Dataview provides an API for plugin developers.
Its syntax is almost the same as DataviewJS.
For example, you can write something like this to display all the backlinks for path.

const dv = getAPI(this.app);
if (dv) {
    let page = dv.page(path); // Dataview page object
    if (page) {
        for (let backlink of page.file.inlinks) {
            console.log(backlink.path);
        }
    }
}

Alternatively:

getAPI(this.app)?.index.links.invMap.get(path)

EDIT: There seems to be another plugin that offers an API for getting backlinks for ALL the TFiles in the vault, while the Dataview API only provides backlinks for markdown files.

1 Like