Last active
October 1, 2021 19:48
-
-
Save GitMurf/0c19de53a8be2236f7b79dd8f94ba86e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| <%* | |
| //Find leaf next door | |
| const thisLeaf = app.workspace.activeLeaf; | |
| const thisFile = thisLeaf.view.file; | |
| let leafToUse = app.workspace.getAdjacentLeafInDirection(thisLeaf, "right"); | |
| if(!leafToUse){leafToUse = app.workspace.getAdjacentLeafInDirection(thisLeaf, "left");} | |
| if(!leafToUse){leafToUse = app.workspace.getAdjacentLeafInDirection(thisLeaf, "bottom");} | |
| if(!leafToUse){leafToUse = app.workspace.getAdjacentLeafInDirection(thisLeaf, "top");} | |
| if(leafToUse) { | |
| //Find link under cursor | |
| const editor = app.workspace.activeLeaf.view.editor; | |
| const getCur = editor.getCursor(); | |
| const curLineNum = getCur.line; | |
| const curPos = getCur.ch; | |
| const curLineCont = editor.getLine(curLineNum); | |
| if(curLineCont === '' || curLineCont === null) { | |
| //Selected a blank line which means you want to open the current file in the active pane in the one next to it (duplicate panes) | |
| app.workspace.setActiveLeaf(leafToUse); | |
| await app.workspace.activeLeaf.openFile(thisFile); | |
| } else { | |
| let thisFile = app.workspace.getActiveFile(); | |
| let mdCache = this.app.metadataCache.getFileCache(thisFile); | |
| let mdLinks = mdCache.links; | |
| let foundLink; | |
| let otherLink; | |
| if (mdLinks) { | |
| foundLink = mdLinks.find(eachLink => { | |
| const startPos = eachLink.position.start; | |
| const endPos = eachLink.position.end; | |
| if(startPos.line === curLineNum) { | |
| let theLink = eachLink.link; | |
| if(theLink.indexOf('#^') > -1){theLink = theLink.substring(0,theLink.indexOf('#^'));} | |
| let thisLinkFile = app.metadataCache.getFirstLinkpathDest(theLink, ''); | |
| if(!thisLinkFile || theLink === "") { | |
| if(theLink.indexOf('#') > -1){theLink = theLink.substring(0,theLink.indexOf('#'));} | |
| thisLinkFile = app.metadataCache.getFirstLinkpathDest(theLink, ''); | |
| } | |
| if(thisLinkFile && theLink !== "") { | |
| otherLink = eachLink; | |
| if(startPos.col <= curPos && endPos.col >= curPos) { | |
| return true; | |
| } else {return false;} | |
| } else {return false;} | |
| } else {return false;} | |
| }); | |
| } | |
| if (!foundLink) { | |
| mdLinks = mdCache.embeds; | |
| if (mdLinks) { | |
| foundLink = mdLinks.find(eachLink => { | |
| const startPos = eachLink.position.start; | |
| const endPos = eachLink.position.end; | |
| if(startPos.line === curLineNum) { | |
| let theLink = eachLink.link; | |
| if(theLink.indexOf('#^') > -1){theLink = theLink.substring(0,theLink.indexOf('#^'));} | |
| let thisLinkFile = app.metadataCache.getFirstLinkpathDest(theLink, ''); | |
| if(!thisLinkFile || theLink === "") { | |
| if(theLink.indexOf('#') > -1){theLink = theLink.substring(0,theLink.indexOf('#'));} | |
| thisLinkFile = app.metadataCache.getFirstLinkpathDest(theLink, ''); | |
| } | |
| if(thisLinkFile && theLink !== "") { | |
| otherLink = eachLink; | |
| if(startPos.col <= curPos && endPos.col >= curPos) { | |
| return true; | |
| } else {return false;} | |
| } else {return false;} | |
| } else {return false;} | |
| }); | |
| } | |
| } | |
| if(!foundLink){foundLink = otherLink} | |
| if(foundLink) { | |
| let finalLink = foundLink.link; | |
| if(finalLink.indexOf('#^') > -1){finalLink = finalLink.substring(0,finalLink.indexOf('#^'));} | |
| let myLinkFile = app.metadataCache.getFirstLinkpathDest(finalLink, ''); | |
| if(finalLink && myLinkFile) { | |
| app.workspace.setActiveLeaf(leafToUse); | |
| if(myLinkFile){await app.workspace.activeLeaf.openFile(myLinkFile);} | |
| } else { | |
| if(finalLink.indexOf('#') > -1){finalLink = finalLink.substring(0,finalLink.indexOf('#'));} | |
| myLinkFile = app.metadataCache.getFirstLinkpathDest(finalLink, ''); | |
| if(finalLink && myLinkFile) { | |
| app.workspace.setActiveLeaf(leafToUse); | |
| if(myLinkFile){await app.workspace.activeLeaf.openFile(myLinkFile);} | |
| } | |
| } | |
| } | |
| } | |
| } | |
| %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment