<% tp.user.getNotSetBaclinks(tp.file.path(true), this.app)%>
Last active
October 20, 2021 00:08
-
-
Save beaussan/05456594da48dfc8315eb3279f134daf to your computer and use it in GitHub Desktop.
Generate a list of backlinks that are not yet in the document for Obsidian Templater's plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getNotSetBaclinks = (path, obsidian) => { | |
const allFiles = obsidian.vault.getFiles(); | |
const currentFile = allFiles.find((file) => file.path === path); | |
const allFilesWithoutCurrent = allFiles.filter( | |
(file) => file.path !== path, | |
); | |
const currentLinks = obsidian.metadataCache.getCache(path).links || []; | |
const basename = currentFile.basename; | |
const allBacklinkFiles = allFilesWithoutCurrent | |
.filter((file) => { | |
const cache = obsidian.metadataCache.getFileCache(file); | |
if (!cache || !cache.links) { | |
return false; | |
} | |
return cache.links.some((link) => link.link === basename); | |
}) | |
.map((val) => val.basename) | |
.filter( | |
(inboundLink) => | |
!currentLinks.find((outLink) => outLink.link === inboundLink), | |
) | |
.map((val) => `- [[${val}]]`); | |
if (allBacklinkFiles.length === 0) { | |
return ''; | |
} | |
return `## To sort | |
${allBacklinkFiles.join('\n')} | |
`; | |
} | |
module.exports = getNotSetBaclinks; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment