Last active
June 10, 2023 05:00
-
-
Save GitMurf/357af96944b8c19fd33b4153d6853a6d to your computer and use it in GitHub Desktop.
Open a [[Link]] under your cursor in Search pane with regex that shows all Backlinks.
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 search = app.workspace.getLeavesOfType("search")[0]; | |
const editor = app.workspace.activeLeaf.view.editor; | |
const getCur = editor.getCursor(); | |
const curLineNum = getCur.line; | |
const curPos = getCur.ch; | |
let thisFile = app.workspace.getActiveFile(); | |
let mdCache = this.app.metadataCache.getFileCache(thisFile); | |
let mdLinks = mdCache.links; | |
if (mdLinks) { | |
let otherLink; | |
let foundLink = mdLinks.find(eachLink => { | |
const startPos = eachLink.position.start; | |
const endPos = eachLink.position.end; | |
if(startPos.line === curLineNum) { | |
otherLink = eachLink; | |
if(startPos.col <= curPos && endPos.col >= curPos) { | |
return true; | |
} else {return false;} | |
} else {return false;} | |
}); | |
if(!foundLink){foundLink = otherLink} | |
if(foundLink) { | |
//SortOrder Options: alphabeticalReverse, alphabetical, byModifiedTime, byModifiedTimeReverse, byCreatedTime, byCreatedTimeReverse | |
search.view.setSortOrder("byCreatedTime"); | |
search.view.setCollapseAll(false); | |
search.view.setExtraContext(true); | |
let myString = foundLink.link; | |
myString = myString.replace(/[-[\]{}()+?.,\\^$|#]/g, '\\$&'); | |
search.view.setQuery(`/\\[\\[${myString}(\\]\\]|\\|)/`); | |
app.workspace.revealLeaf(search); | |
} | |
} | |
%> |
Hello @GitMurf @GitMurf !!!!!!!
Thank you very much for sharing this script.
I would like to ask your help for a problem that I am facing in a script that is related to this script → instead of getting view.setQuery()
I would like to get the search matches. Moreover I need to get the .basename
of these matches.
Do you have any idea on how to get this ?
Thanks for your help 🙏
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Demo: https://user-images.githubusercontent.com/64155612/119441469-06a0e700-bcdb-11eb-974e-9108766f30eb.mp4