Action

Markdown Link

Posted by agiletortoise, Last update about 5 years ago

Insert Markdown link “”. If there is a text selection, use it as the link text, if a URL is in the clipboard, 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 = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
       return regexp.test(s);
    }
    
    // get clipboard and test if it's a URL
    let clip = app.getClipboard();
    let link = "";
    if (isUrl(clip)) {
      link = clip;
    }
    
    var sel = editor.getSelectedText();
    let [st, len] = editor.getSelectedRange();
    
    if (!sel || sel.length == 0) {
      editor.setSelectedText("[](" + link + ")");
      editor.setSelectedRange(st+1,0);
    }
    else {
      editor.setSelectedText("["+sel+"](" + link + ")");
      if (link.length > 0) {
        editor.setSelectedRange(st+len+link.length+4,0);
      }
      else {
        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.