Action

open forward link

Posted by FlohGro, Last update 7 months ago

created by @FlohGro / more on my Blog

open forward link

finds [[wiki-style-links]] in the current Draft. If one or more are found a prompt is shown to allow you to quickly open those links to other Drafts.


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

Buy Me A Coffee

Steps

  • script

    openForwardLinkFromDraft(draft)
    
    /**
     * opens a selected forward link in wiki-style-format in the given draft (if existing)
     * @param {Draft} d the Draft in which you want to find wiki-style-links
     */
    function openForwardLinkFromDraft(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){
                matches.push(linkText);
            }
        }
    
        if (matches.length > 0) {
            // present prompt to let the user select a draft to open
            let p = new Prompt();
            p.title = "select Draft to open"
            matches.forEach((match) => {
                p.addButton(match)
            })
            if (p.show()) {
                let selechtedTitle = p.buttonPressed
                let foundDrafts = Draft.queryByTitle(selechtedTitle)
                if (foundDrafts.length == 1) {
                    editor.load(foundDrafts[0])
                } else if (foundDrafts.length > 1) {
                    let p2 = new Prompt()
                    p2.title = "found " + foundDrafts.length + " drafts"
                    p2.message = "select draft you want to open"
                    foundDrafts.forEach((curDraft) => {
                        p2.addButton(curDraft.displayTitle, curDraft)
                    })
                    if (p2.show()) {
                        editor.load(p2.buttonPressed);
                    }
                } else {
                    app.displayWarningMessage("no draft found with title \"" + selechtedTitle + "\"")
                }
            }
        }else {
            app.displayWarningMessage("no forward links found in draft")
        }
    }

Options

  • 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.