✪ Link
Smart 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 in cursor position, leaving cursor ready to type link text
  • If text is selected, insert selectedText leaving cursor after close parens.

Posted by agiletortoise

Install

Details

Label ✪ Link
Voice Over Smart Markdown link
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 = getClipboard();
var link = "";
if (isUrl(clip)) {
  link = clip;
}

var sel = getSelectedText();
var selRange = getSelectedRange();

if (!sel || sel.length == 0) {
  setSelectedText("[](" + link + ")");
  setSelectedRange(selRange[0]+1,0);
}
else {
  setSelectedText("["+sel+"](" + link + ")");
  setSelectedRange(selRange[0]+selRange[1]+link.length+4,0);
}

Advanced

Keyboard shortcut Command+L
Hide key Yes

Discussion

comments powered by Disqus