Action

Fill Markdown Links (Reference Links) (1.2)

Posted by kjaymiller, Last update about 6 years ago

Description:
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.

Ver. 1.2.4 Update:
- FIX: menu message now terminated on new lines.
- FIX: resolved bug where sometimes updates would overwrite each other.
- FIX: ensures that reference links and main content have a space between them.
- FIX: Reference Links will continue sequence if other links already exist.

Ver. 1.2.3 Update:
- ADD: Title of menu prompt includes link text.

This is good if you have multiple links in one sentence. If no text, title will indicate that.

Ver. 1.2.2 Update:
- FIX: loop bug now corrected. If you reach the end on the text without finding punctuation, the action no gets stuck in an infinite loop.

Steps

  • script

    /* Remove Empty Lines
    
    It's important that any empty lines are removed from the end of the draft as to ensure that indexing works correctly. 
    
    // See online documentation for examples
    // http://getdrafts.com/scripting
    */ 
    
    lines = draft.content.split('\n');
    
    while (lines[lines.length - 1] == '')
    lines.pop();
    
    draft.content = lines.join('\n')
    draft.update()
  • script

    
    
    re = /(!{0,1}\[(.*?)\][\[\(][\]\)])/g;
    
    function addToDraft(draftMatch, content){
        i = draft.content.indexOf(draftMatch) + draftMatch.length - 2; 
        return editor.setTextInRange(i, 2, content);
    }
    
    function addLinkReference(url, description) {
    return editor.setTextInRange(draft.content.length,0, "\n" + '[' + index.toString() + ']: ' + 
        url + ' ' + description);
     }
    
    // get the last line and set the index
    lines = draft.content.split('\n');
    possibleIndex = lines[lines.length - 1].substring(1,2)
    
    var index = isNaN(possibleIndex) ? 1: parseInt(possibleIndex) + 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', 'autocorrect': 'false'});
    menu.addTextField('description','Description: ','',);
    menu.addButton('Open Safari','safari')
      
        while ((match = re.exec(draft.content)) != null){
        var fullLink = match[1]
        var linkText = match[2]
        var descriptionPosition = match.index
        var entryLength = match[0].length
        
       while (['.','?','!','\n','#'].includes(editor.getTextInRange(descriptionPosition,1))==false && descriptionPosition 
       >= 0)
       {
       descriptionPosition --
       entryLength ++
       }
     
    while (!['.','?','!','\n','#'].includes(editor.getTextInRange(descriptionPosition + entryLength,1)) && +
       entryLength <= draft.content.length)
       {
       entryLength ++
       }
    descriptionPosition ++;
    entryLength --;
    
    menu.message  = editor.getTextInRange(descriptionPosition, entryLength);
        menu.title = fullLink.substring(0,1) == '!' ? 'Image':'Link';
        var menuTitle = !match[2] ? 'No Link Text': match[2];
    menu.title += ' - ' + menuTitle;
    m = menu.show();
    if ( m==false ) quit;
    url = menu.fieldValues['url'];
        description = '"' + menu.fieldValues['description'] + '"';
    
    switch (menu.buttonPressed) {
    
    case 'ok':
       addToDraft(fullLink, '['+ index + ']')
    
       addLinkReference(url, description)
       
      menu.addButton(description, index); 
        index++;
        break;
        
      case 'clipboard':
      addToDraft(fullLink, '['+ index + ']');
      addLinkReference(app.getClipboard(), description);
      menu.addButton(description, index); 
        index++;
        break;
    
    case 'safari':
        linkTitle = draft.processTemplate('{{' + linkText +'}}'); 
           app.openURL('https://www.google.com/search?q='+linkTitle);   
    var secondMenu = Prompt.create();
    secondMenu.message = 'The contents of your clipboard will be copied to "' + fullLink + '"';
    
    secondMenu.addButton('OK','ok'); //adds the corrent item
    
    m2 = secondMenu.show();
    
    if (m2) {
    addToDraft(fullLink, '['+ index + ']');
      addLinkReference(app.getClipboard(), description);
      menu.addButton(description, index); 
        index++;
    };
       break;
       
       default:
       addToDraft(fullLink, '['+ menu.buttonPressed + ']');
        }
    }
  • script

    /* If there is no empty line between the text and the index, this script will create it. 
    
    This is important for reference links to process correctly. 
    
    See online documentation for examples
    http://getdrafts.com/scripting
    
    */
    let lineCheck = /\[.*?\]:/g;
    let firstLink =  lineCheck.exec(draft.content).index
    
    if (editor.getTextInRange(firstLink-2,2) != '\n\n') 
    {
    editor.setTextInRange(firstLink-1,1,'\n\n')}
    

Options

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