Action

Markdown Link

Posted by RosemaryOrchard, Last update almost 3 years ago

Insert Markdown link “”. If there is a text selection, use it as the link text, if a URL is in the clipboard (something beginning with ftp/http/https/mailto, and containing no space characters), place it as the link.

Steps

  • script

    /*
    ### Create Markdown link
    
    #### if text in clipboard is NOT a URL:
    
    - If no text is selected, insert []() in cursor position, leaving cursor ready to type link text.
    - If text is selected, insert [selectedText]() leaving cursor in parens to type URL.
    
    #### If URL in Clipboard:
    
    - If no text is selected, insert [](urlFromClipboard) in cursor position, leaving cursor ready to type link text
    - If text is selected, insert [selectedText](urlFromClipboard) leaving cursor after close parens.
    */
    
    // helper to test for URL
    function isUrl(s) {
       var regexp = new RegExp('(ftp|http|https|mailto):\/\/\S+', 'i')
       return regexp.test(s);
    }
    
    var sel = editor.getSelectedText();
    let [st, len] = editor.getSelectedRange();
    
    if (!sel || sel.length == 0) {
      editor.setSelectedText("[]()");
      editor.setSelectedRange(st+1,0);
    }
    else {
      editor.setSelectedText("["+sel+"]()");
      editor.setSelectedRange(st+len+3, 0);
    }
    

Options

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