Last active
March 18, 2022 03:32
-
-
Save GitMurf/cd8ba41048d6e00e615029cb55e15437 to your computer and use it in GitHub Desktop.
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 folderLocation = '/all-links'; | |
const fileNameSuffix = 'Outgoing and Incoming Links'; | |
const outlinksName = "outlinks"; | |
const inlinksName = "inlinks"; | |
let newFileName = `${tp.date.now('YYYY_MM_DD_hhmmss')}_${fileNameSuffix}`; | |
function removeNamespace(linkStr) { | |
let newLink = linkStr; | |
if(linkStr.indexOf("/") > -1) { | |
let splitArr = linkStr.split("/"); | |
newLink = splitArr.last(); | |
} | |
return newLink; | |
} | |
let finalString = ""; | |
//All markdown notes | |
const mdFiles = await app.vault.getMarkdownFiles(); | |
let ctr = 0; | |
mdFiles.forEach(eachFile => { | |
ctr++; | |
//if(ctr > 50) { return }; | |
if(eachFile.basename.indexOf(fileNameSuffix) > -1) { | |
console.log("Found one of your Link Files... skipping: " + eachFile.basename); | |
return; | |
} | |
console.log(`${ctr}: ${eachFile.basename}`); | |
finalString = `${finalString}\n- [` + `[${removeNamespace(eachFile.basename)}]]`; | |
const mdCache = app.metadataCache.getFileCache(eachFile); | |
if(mdCache) { | |
finalString = `${finalString}\n - [` + `[${outlinksName}]]`; | |
if(mdCache.embeds) { | |
//Add embed links | |
const embeds = mdCache.embeds; | |
embeds.forEach(eachEmbed => { | |
if(eachEmbed.link && eachEmbed.link.substring(0,1) !== "#") { | |
finalString = `${finalString}\n - [` + `[${removeNamespace(eachEmbed.link)}]]`; | |
} | |
}); | |
} | |
if(mdCache.links) { | |
//Add all other links | |
const links = mdCache.links; | |
links.forEach(eachLink => { | |
if(eachLink.link && eachLink.link.substring(0,1) !== "#") { | |
finalString = `${finalString}\n - [` + `[${removeNamespace(eachLink.link)}]]`; | |
} | |
}); | |
} | |
} | |
//Add backlinks | |
const getBacklinks = app.metadataCache.getBacklinksForFile(eachFile); | |
if(getBacklinks) { | |
if(getBacklinks.data) { | |
finalString = `${finalString}\n - [` + `[${inlinksName}]]`; | |
Object.keys(getBacklinks.data).forEach(eachBackLink => { | |
const foundTfile = app.vault.getAbstractFileByPath(eachBackLink); | |
if(foundTfile) { | |
if(foundTfile.basename !== eachFile.basename && foundTfile.basename.indexOf(fileNameSuffix) === -1) { | |
finalString = `${finalString}\n - [` + `[${removeNamespace(foundTfile.basename)}]]`; | |
} | |
} | |
}); | |
} | |
} | |
}); | |
newFileName = newFileName.replace(/[\.#\*"\/\\<>\:\|\[\]\?]/gim, '').trim().slice(0, 255); | |
//let vaultRootPath = app.fileManager.getNewFileParent('').path; | |
let bSearchFolder = await app.vault.adapter.exists(folderLocation, false); | |
if(!bSearchFolder){await app.vault.createFolder(folderLocation);} | |
let newFilePath = folderLocation + '/' + newFileName + ".md"; | |
let newFileObj = await app.vault.create(newFilePath, `${finalString}\n`); | |
//Opening the file afterwards throws an error for some reason but it still works fine | |
await app.workspace.openLinkText(newFileObj.basename, newFileObj.path, true); | |
return; | |
%> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment