A Siri Shortcut to Share as a Markdown File

Page content

The Problem

I’m a big fan of Drafts for iOS. It’s a terrific writing environment and it can integrate with just about anything through URL schemes. But, URL schemes have a major limitation when I write long blocks of text. A URL can only support a finite number of characters (around 2000).

I want to export text from Drafts and send it to other apps as a Markdown file. If I have a markdown header line with a “title” defined I want the file to be named with that title.

I do this often and it has become a time suck to use the clipboard as the only way to share more than 2000 characters.

Here’s a typical example in Drafts:

Drafts Export

When I use a Shortcut like the excellent DEVONmenu by Federico, my text is truncated and there’s no indication of what I’ve lost. I hate losing text so I decided to write a Shortcut that converts a text block to a Markdown file and attempts to auto-title it based on Markdown front-matter.

Regular Expressions in Shortcuts

Shortcuts has a couple of actions that are poorly documented but extremely powerful. They are the “Match Text”, “Replace Text”, and “Get Group from Matched Text.” They give the user super powers when working with text.


But much like the suit from Greatest American Hero, they come with so little instructions that it’s almost hazardous to work with these actions. Let’s break these three actions down very quickly.

The Match Text action is the star. It does exactly what you think. It matches incoming text against a regular expression. It passes the match groups through. The Match Text action does not respond to the ‘/m’ flag to treat the incoming text as multi-lines.1 I’ll show a work around for this later.

The Replace Text action uses a regular expression to do a find and replace. I’m not using this action for today’s post but it works much like the Match Text.

The final action is strange. The Get Group from Matched Text has access to the groups found with the Match Text action. If you don’t know what match groups are, there are plenty of primers. Regular expressions aren’t just for pulling out the exact match. Sometimes you want to extract just a portion of a match. I’ll show an example of where this helps. The Shortcuts action can retrieve a match group by index which is nice.

I have a Shortcut that let’s me play around with regular expressions. I paste text into a text action and then tweak the regex to see the results.

Regex Tester

For actual regex testing I highly recommend Regexr.com.


The Shortcut

The Shortcut starts by splitting the incoming text into lines. I do this because the Match Text action doesn’t support the multi-line option. By splitting the text I can operate over each line in the text file and look for anything that looks like a “title” header line.


I then loop over every line and use the following regular expression:

(?:^\btitle: \b)(.*)$

In simple pseudo code this says “find a line that starts with the word ‘title: ' and then extract everything after that.”

The first set of parentheses starts with a ‘?’ which makes it a non-capturing group. That part of the match is thrown away. The second set of parentheses says to capture everything after the first group up to the end of the line.

At this point the Shortcut as either found a match or it hasn’t. We try to grab the captured group text with the Get Group from Matched Text and then count how many items we have.

If we have less than one item then the text didn’t have a title header line and it asks us to enter one with a popup text box (Ask for Input). We set a variable named Title that we can use to name this new file.

If the Matched Text count is greater than zero then we fall into the Otherwise statement. Here we can grab the string returned by the Group from Matched Text and use that to set the Title variable.

So now we should have a name for our file. We use the “magic variable” to recall the original input for the Shortcut (you remember, that big long block of text we shared from Drafts). The Set Name action adds a file name to that block of text and appends the “.md” extension.

To get an actual file, we trigger an “Open In” action which we can now use to send the file object into another app like DEVONthink To Go.

Clip to DTTG


I tried a heck of a lot of other things to accomplish this transformation. I tried using JavaScript to encode the text as a base64 string I could handle like any other text file. That failed when sending to DEVONthink’s URL scheme.

I also tried to convert the text block into a base64 string with a Shortcuts action. That works but then I couldn’t use it as a file object with any other apps.

It wasn’t until I stumbled on the unique ability of Shortcut’s “Open In” action to push out a file object did I realize I had a solution. The documentation for Shortcuts is bad. As with many of Apple’s most powerful tools, Shortcuts relies on accidental discovery and many hours of trial and error. I don’t think Shortcuts will be approachable until it has better discoverability and better documentation.

Thanks Siri

  1. It doesn’t seem to respond to any flags at all. ↩︎