add backlinks to all linked drafts

Posted by FlohGro, Last update 8 months ago

created by @FlohGro / more on my Blog

add backlinks to all linked drafts

Finds [[wiki-style-links]] in the current Draft.
The action will check each linked draft and add a backlink to the current draft at the end if it is not yet existing

If you find this useful and want to support me you can donate or buy me a coffe

Buy Me A Coffee


  • script

    const backlinkSectionTitle = "backlinks"
     * add backlinks to all drafts linked as wiki-style-links in the given draft
     * @param {Draft} d the draft you want to search the outgoing links from
    function addBacklinksToForwardLinkedDraftsInGivenDraft(d){
        const matches = getOutgoingLinksInDraft(d)
        let drafts = getDraftsFromTitles(matches)
        drafts.forEach((curDraft) => {
     * finds all wiki-style-links in the given draft and returns them
     * @param {Draft} d the Draft where you want to search the outgoing links in
     * @returns array of titles in outgoing links
    function getOutgoingLinksInDraft(d){
        let content = d.content
        const regex = /\[\[([^\]]+)\]\]/g;
        const matches = [];
        let match;
        while ((match = regex.exec(content)) !== null) {
            const linkText = match[1];
            if(!matches.includes(linkText) && linkText != draft.displayTitle){
        return matches
     * finds drafts for each given title
     * @param {String[]} titles array of titles that you want to search drafts for
     * @returns array of Drafts[] (only unique titles are retuned)
    function getDraftsFromTitles(titles){
        let drafts = []
        titles.forEach((title) => {
            let foundDrafts = Draft.queryByTitle(title)
            if(foundDrafts.length == 1){
        return drafts
     * insert a backlink to the sourceDraft at the linkedDraftsSection in the destinationDraft
     * @param {Draft} sourceDraft the Draft you want to link from
     * @param {Draft} destinationDraft the Draft where you want to insert the link to the sourceDraft
    function insertBacklinkToDestinationDraftFromSourceDraft(sourceDraft, destinationDraft) {
        // don't allow linking to the same draft
        if (sourceDraft == destinationDraft) {
            return 0
        let linkText = `[[${sourceDraft.displayTitle}]]`
        const backlinkSectionStartingText = `---\n\n## `+ backlinkSectionTitle + `\n\n`
        //check if text is already in given draft d
        if (destinationDraft.content.includes(backlinkSectionStartingText)) {
            // already present, only add link
            // check if link is already exisitng
            let backLinksSectionText = destinationDraft.content.split(backlinkSectionStartingText)[1]
            if (!backLinksSectionText.includes(linkText)) {
                destinationDraft.content = destinationDraft.content.trim() + "\n- " + linkText
        } else {
            // its the first backlink - add section marker and link
            destinationDraft.content = destinationDraft.content.trim() + "\n\n" + backlinkSectionStartingText + "- " + linkText


  • After Success Nothing
    Notification Error
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.