Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
obsidian.templater.search.dnp.backlinks.js
@Liong1976
Copy link

Liong1976 commented Jun 16, 2022

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