Drafts Action Directory

Markdown Link

Posted by agiletortoise, Last updated: 2018-06-23 20:07:40 UTC

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.

Install

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
    var clip = app.getClipboard();
    var link = "";
    if (isUrl(clip)) {
      link = clip;
    }
    
    var sel = editor.getSelectedText();
    var selRange = editor.getSelectedRange();
    
    if (!sel || sel.length == 0) {
      editor.setSelectedText("[](" + link + ")");
      editor.setSelectedRange(selRange[0]+1,0);
    }
    else {
      editor.setSelectedText("["+sel+"](" + link + ")");
      editor.setSelectedRange(selRange[0]+selRange[1]+link.length+4,0);
    }
    

Comments

Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.