Action

open markdown url

Posted by FlohGro, Last update about 2 years ago

UPDATES

about 2 years ago

added credits to description

show all updates...

about 2 years ago

added credits to description

about 2 years ago

description update

created by @FlohGro / more on my Blog

open markdown url

This action will search the current draft for markdown formatted urls e.g.:

- [Google](www.gogle.com) this is famous search engine
- text before [Bear](bear://) and text after
- [text in brackets, no problem] [OneNote](onenote://open)
- [Google](www.gogle.com)
- [BBC](www.bbc.co.uk)

Depending on how many URLs are available in the draft the action will either

  • open the only existing link immediately
  • display a prompt if multiple URLs are present in the draft - you can select by the given title which URL should be opened.

The URLs are opened with Safari by default - this will also enable „app links“ e.g. „drafts://open..)

The action was inspired by this thread in the forums - credits to @RoyRogers for the code examples - I used some of them in the action

[Configuration]

no configuration needed

[Usage]

run this action whenever you want to open a (markdown) URL from a draft. You don’t need to enter link mode in the editor - if only one link is present in the draft this action will speed up your workflow. You can even set a keyboard shortcut to the action or add it to your action bar if you frequently want to open URLs from your drafts.


If you find this useful and want to support me you can

Buy Me A Coffee

Steps

  • script

    // open markdown url
    
    const content = draft.content;
    
    // regel to match markdown urls "[]()”
    let regex = /\[([^\[]+)\]\(([^\[]+)\)/g
    // prompt to select the url to open
    var pSelUrl = Prompt.create();
    // flag to indische if any url was found.
    var foundUrlCounter = 0;
    // if only one url is in the note - open it directly - first url will be stored in this variable
    var firstURL = '';
    var urlToOpen;
    
    
    let foundUrl = findAndStoreUrls();
    
    if (foundUrl) {
      urlToOpen = getUrlToOpen();
    } else {
      let message = "no markdown url '[..](..)' found in draft";
      context.cancel(message);
      app.displayInfoMessage(message);
    }
    
    if (foundUrl) {
      if (urlToOpen) {
        openURL(urlToOpen);
      } else {
        let message = "no url to open selected";
        context.cancel(message);
        app.displayInfoMessage(message);
      }
    }
    
    // functions
    
    // stores the found urls in the prompt as buttons
    function findAndStoreUrls() {
      var matches = [...content.matchAll(regex)]
      if (matches) {
        for (match of matches) {
          foundUrlCounter++;
          let description = match[1];
          let url = match[2];
          // add "http://" if the url is no callback url and just witten as domain.
          if (!url.match(/:\/\//)) {
            url = 'http://' + url;
          }
          // add the found url to the prompt
          pSelUrl.addButton(description, url);
          if (foundUrlCounter == 1) {
            firstURL = url;
          }
        }
      }
      if (foundUrlCounter == 0) {
        return false;
      } else {
        return true;
      }
    }
    
    function getUrlToOpen() {
      if (foundUrlCounter == 1) {
        return firstURL;
      } else if (foundUrlCounter > 0) {
        pSelUrl.title = "select URL:";
    
        var didSelect = pSelUrl.show();
    
        if (didSelect) {
          return pSelUrl.buttonPressed;
        }
      }
    }
    
    function openURL(url) {
      let success = app.openURL(url, false);
      if (!success) {
        let message = "failed to open url: " + url;
        app.displayErrorMessage(message);
        context.fail(message);
      }
    
    }

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.