Action

copy link to marker

Posted by FlohGro, Last update 3 months ago

created by @FlohGro / more on my Blog

copy link to marker

This action copies the link to a marker of your choice for wiki style linking in markdown drafts.

When you run the action it’ll ask you if you want to copy the link of the current section (the cursor is in that section), the next section, or if you want to select from any other marker in the current draft

After selecting an option the Action copies the link to the marker into the clipboard so you can use it inside the current draft or paste it to any other draft.

(the links will be escaped as described in the docs


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

Buy Me A Coffee

Steps

  • script

    // link to marker
    
    const [cursorPosition, selectionLength] = editor.getSelectedRange();
    
    function getNavigationMarkerOfCurrentSection() {
        return editor.navigationMarkerBefore(cursorPosition)
    }
    
    function getNavigationMarkerOfNextSection() {
        return editor.navigationMarkerAfter(cursorPosition)
    }
    
    let currentMarker = getNavigationMarkerOfCurrentSection()
    let nextMarker = getNavigationMarkerOfNextSection()
    // label property is interesting for the linking
    function selectMarkerInCurrentDraft() {
        let p = new Prompt()
        p.title = "select section"
        editor.navigationMarkers.forEach(marker => {
            //exclude draft title
            if (marker.label != draft.displayTitle)
            {
                p.addButton(marker.label, marker)
            }
        });
        if (p.show()) {
            return p.buttonPressed
        } else {
            app.displayInfoMessage("no marker selected")
            return undefined
        }
    }
    
    function createLinkToMarker(marker) {
        // [[Title of Draft/Marker Name]]
        let title = draft.displayTitle
        let escapedTitle = title.replaceAll("/", "\\/")
        let label = marker.label
        let escapedLabel = label.replaceAll("/", "\\/")
        return "[[" + escapedTitle + "/" + escapedLabel + "]]"
    }
    
    if (editor.navigationMarkers.length > 0) {
        let p = new Prompt()
        p.title = "which marker to link"
        if (currentMarker) {
            p.addButton("current section")
        }
        if (nextMarker) {
            p.addButton("next section")
        }
        p.addButton("select from all markers")
        if (p.show()) {
            let marker = undefined
            switch (p.buttonPressed) {
                case "current section":
                    marker = currentMarker;
                    break;
                case "next section":
                    marker = nextMarker;
                    break;
                case "select from all markers":
                    marker = selectMarkerInCurrentDraft();
                    break;
                default:
                    marker = undefined;
                    break;
            }
            if (marker) {
                app.setClipboard(createLinkToMarker(marker))
                app.displaySuccessMessage("copied link to marker\"" + marker.label + "\"")
            }
        }
    } else {
        app.displayInfoMessage("no markers in current 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.