Last active
February 23, 2022 13:30
-
-
Save GitMurf/5ba341b8dba7ccd7e24129f4469ca920 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
<%* | |
//Find leaf next door | |
const thisLeaf = app.workspace.activeLeaf; | |
let thisFile = thisLeaf.view.file; | |
let leafNextDoor = app.workspace.getAdjacentLeafInDirection(thisLeaf, "right"); | |
if(!leafNextDoor){leafNextDoor = app.workspace.getAdjacentLeafInDirection(thisLeaf, "left");} | |
let leafToUse = null; | |
if(leafNextDoor) { | |
const openAbove = false; | |
const splitDirection = openAbove === true ? "top" : "bottom"; | |
let bottomPane = leafNextDoor; | |
let paneBelow = app.workspace.getAdjacentLeafInDirection(bottomPane, splitDirection); | |
let loopQuit = 0; | |
while(paneBelow !== null && loopQuit < 10) { | |
loopQuit++; | |
paneBelow = app.workspace.getAdjacentLeafInDirection(bottomPane, splitDirection); | |
if(paneBelow !== null) { bottomPane = paneBelow; } | |
} | |
if(bottomPane !== null) { leafNextDoor = bottomPane } | |
leafToUse = app.workspace.createLeafBySplit(leafNextDoor, "horizontal", openAbove); | |
} | |
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 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