Skip to content

Instantly share code, notes, and snippets.

@GitMurf
Created September 23, 2021 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GitMurf/8a915a9050930feb58123cd0c0832957 to your computer and use it in GitHub Desktop.
Save GitMurf/8a915a9050930feb58123cd0c0832957 to your computer and use it in GitHub Desktop.
obsidian.templater.search.dnp.backlinks.js
@Liong1976
Copy link

Thanks for making and sharing this templater. Unfortunately, this one doesn't work anymore.

Here is the console report:
image

I used to used a similar templater that I forget where I get it from (maybe from your Github?), below.

<%*
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);
    }
}
%>

I do need your help to fix this templater if you don't mind, since it is so helpful for me to find other files that contain a particular unresolved link. I made tons of unresolved links as opposed to tags. Also, I don't know about coding at all.

Many thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment