Drafts Action Directory

Fill Markdown Links (Reference Links)

Posted by kjaymiller, Last update about 1 month ago

Ever been writing and had to stop to look up a link.

with this action you can leave those links empty like this “your link text”.

Once you run this action it will look for all of the empty links and then ask you to fill them.

You can manually enter a url (or whatever text you like, who am I to judge), paste from the clipboard if you’ve been doing some research, or you can open google to search for your link text. When you go back to your draft you can paste in the text.

Install

Steps

  • script

    // See online documentation for examples
    // http://getdrafts.com/scripting
    var url = ''
    re = /!*\[(.*?)\]\(\)/g;
    
    function addToDraft(draftMatch, content){
        i = draft.content.indexOf(draftMatch) + draftMatch.length - 2; 
        return editor.setTextInRange(i, 2, content);
    }
    
    function addLinkReference(url, description) {
    draft.content += "\n" + '[' + index.toString() + ']: ' + 
        url + ' ' + description;
        return draft.update();
     }
    
    
    var index = 1;
    
    //create menu and base buttons
    menu = Prompt.create();
    menu.addButton('Add from Clipboard', 'clipboard')
    menu.addButton('OK', 'ok')
     menu.addTextField('url','URL: ','', {'keyboard':'URL'});
    menu.addTextField('description','Description: ','',);
    menu.addButton('Open Safari','safari')
      
        while ((match = re.exec(draft.content)) != null){
        menu.message = match[1];
        if (match[0].substring(0,1) == '!') {menu.title = 'Image'}
        else {menu.title = 'Link'};      
    m = menu.show();
    if ( m==false ) quit;
    url = menu.fieldValues['url'];
        description = '"' + menu.fieldValues['description'] + '"';
    
    switch (menu.buttonPressed) {
    
    case 'ok':
       addToDraft(match[0], '['+ index + ']')
    
       addLinkReference(url, description)
       
      menu.addButton(description, index); 
        index++;
        break;
        
      case 'clipboard':
      addToDraft(match[0], '['+ index + ']');
      addLinkReference(app.getClipboard(), description);
      menu.addButton(description, index); 
        index++;
        break;
    
    case 'safari':
        linkTitle = draft.processTemplate('{{' + match[1] +'}}'); 
           app.openURL('https://www.google.com/search?q='+linkTitle);   
    var secondMenu = Prompt.create();
    secondMenu.message = 'The contents of your clipboard will be copied to "' + match[1] + '"';
    
    secondMenu.addButton('OK','ok'); //adds the corrent item
    
    m2 = secondMenu.show();
    
    if (m2) {
    addToDraft(match[0], '['+ index + ']');
      addLinkReference(app.getClipboard(), description);
      menu.addButton(description, index); 
        index++;
    };
       break;
       
       default:
       addToDraft(match[0], '['+ menu.buttonPressed + ']');
        }
    }

Options

  • After Success Default
    Notification Info
    Log Level Info

Comments

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