Action

link draft + insert backlink

Posted by @FlohGro, Last update 8 months ago

UPDATES

8 months ago

Fixed issue

created by @FlohGro / more on my Blog

link draft + insert backlink

Shows the Drafts select dialog to choose a draft you want to link to. if you select a draft a [[wiki-style-link]] will be added at the current cursor position.

In the selected draft a backlink section will be created at the end if it is not yet existing. A link back to the current draft will be inserted there.


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

Buy Me A Coffee

Steps

  • script

    const backlinkSectionTitle = "backlinks"
    
    linkDraftFromSelectionAtCurrentCursorPosition();
    
    
    function linkDraftFromSelectionAtCurrentCursorPosition() {
        let d = getDraftFromSelection()
        if (!d) {
            // no draft was selected → terminate
            app.displayInfoMessage("no draft selected")
            return 0
        }
    
        // a draft was selected
        insertLinkToDraft(d)
        insertLinkBacklinkToDestinationDraftFromSourceDraft(draft, d)
    }
    
    /**
     * let the user select a Draft with Drafts select interface
     * @returns Draft
     */
    function getDraftFromSelection() {
        return app.selectDraft();
    }
    
    /**
     * insert a link to the given Draft at the current cursor position
     * @param {Draft} destinationDraft the Draft you want to link to
     */
    function insertLinkToDraft(destinationDraft) {
        editor.setSelectedText(`[[${destinationDraft.displayTitle}]]`);
    }
    
    /**
     * 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 insertLinkBacklinkToDestinationDraftFromSourceDraft(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
        }
        destinationDraft.update()
    }

Options

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