"Automating Markdown with Taio, Part 2"


In this week’s installment of the Shortcuts Corner, I’m continuing to explore the automation possibilities offered by Taio, the all-in-one text editor and clipboard manager that comes with a built-in action editor.

Last week, I shared the first actions I created for the app’s editor to speed up various tasks related to Markdown writing and editing such as adding links and footnotes. This week, I’m going to share an additional set of four custom actions for Taio’s editor. As with last week’s issue of MacStories Weekly, you can find direct URLs to download my Taio actions at the end of this post.

Let’s dive in.

Insert Image Link

As I explained on different occasions in the past, I tend to leave screenshots and photos as one of the final steps to complete when I’m editing a story. Because of this approach, I usually have to go through dozens of images that have to be turned into URLs on our CDN one after the other (we have an internal web app with Shortcuts integration to manage these uploads), and I want to make the process of entering the necessary Markdown syntax for images as seamless as possible.

In Taio, I was able to create an action that takes a URL from the clipboard and inserts it into the text editor, placing the cursor in between the square brackets so I can start typing an image caption right away if I want to.

Inserting image links in Taio.

The action takes advantage of two techniques I explained last week: first, it validates the contents of the system clipboard by confirming that a URL was copied via John Gruber’s regular expression, which is used as a condition in the ‘If’ block; additionally, the cursor’s original position is restored at the end of the action thanks to two variables assigned at the very beginning to ‘Selected Location’ and ‘Selected Length’. Unlike last week’s actions, however, in this case we want the cursor to move by two characters so that it can be placed after the ![ characters and be ready to type an image caption inside the square brackets. To do this, we can use a simple ‘Math’ action and calculate the original location (as I explained last week, a numeric value that indicates the position of the cursor in the current document) plus 2, which is the number of characters we want to skip.

Ideally, it shouldn’t be necessary to create a custom action for this kind of Markdown feature: in iA Writer, for instance, adding images is done via a native keyboard shortcut that does exactly what I want. At the same time, this is also what I love about user automation: if a particular feature doesn’t exist, you can just roll your own.

Lookup Footnote

This action is another example of one of my old Editorial workflows that I was able to reimagine in Taio. I love sprinkling footnotes throughout my longer stories and, as I detailed last week, I prefer to insert them at the very end of the document I’m working on. Because of my proclivity for footnotes and this approach, however, I often find myself forgetting what a footnote is about if, weeks after I typed it, I’m editing the document, see the footnote reference, but don’t want to manually scroll to the end of the page to find the actual footnote. I know, it’s a trivial problem I created for myself, but this is the way I like to manage my Markdown, and I’m fussy about my structural choices in plain text.

At a high level, this action needs to accomplish two main tasks: first, it needs to find the closest footnote label based on where the cursor is; then, it has to scan the current document for the same [^footnoteLabel] and find the text associated with it. The first part is relatively easy: using Taio’s ‘Smart Select’ action configured to ‘Closest Word’, we can retrieve a full word that’s closest to the cursor’s current position in the document. In the example below, Taio will match the portsStandard label contained inside the [^portsStandard] footnote reference by placing the cursor inside the square brackets (outside won’t work).

A footnote marker in MultiMarkdown.

Once the action has identified the closest word, we can save it as a variable named ‘Footnote Name’. This is where the second part of the action kicks in: we have to scan the whole document to find the Markdown syntax for the footnote, but rather than just matching (again, based on the example above) [^portsStandard], we have to find the footnote reference, followed by a colon, followed by a space, followed by some text.

Essentially, we have to match a block of text like this:

A footnote’s actual text at the end of a document.

As you can imagine, we have to rely on good old regex to make this happen. After fetching the current document’s text with the ‘Get Text’ action, we can use a ‘Text Filter’ step to match text in the document based on a regex pattern. The action has to be set to ‘Match Mode: Regular Expression’ and ‘Multi Line Regex’ with the following pattern:

^\\[\^Footnote Name\\]\:\s.+

For those interested in what the regular expression means, it says: match text at the beginning of a line (^) where you see a literal square bracket and caret (the “escaped” \\[\^), followed by the name of the footnote, followed by a literal square bracket and colon (\\]\:), a whitespace character (\s), and finally any character, one or more times (.+). Connect this text filter with a ‘Show Alert’ action and voilà, Taio will present the contents of a footnote, even if it’s located at the end of the document, without scrolling to it:

Matching a footnote’s text via regex thanks to Taio.

I’m very happy with how this action turned out, and I’m particularly proud of the regex implementation since it shows off the power of regular expressions when combined with Markdown in a scriptable text editor. If I end up writing my annual iOS review in Taio this year, I already know I’m going to use this action a lot.

Count Words

This one’s a simple action, and like the one for Markdown image links above, it does something that should really be built into Taio by default. As the name suggests, the Count Words action displays a word count for the current document as a HUD-style alert in the middle of the screen.

A word count powered by Taio.

To build this action, we only need three steps in Taio: after retrieving the current document’s text with the ‘Get Text’ action, we can count words using the ‘Count’ action with the ‘By Word’ parameter, then display the final result in a ‘Show Toast’ action set to ‘Text Only’ mode. I’d like Taio to adopt an iA Writer-style floating (and customizable) inline word count eventually, but this will do for now.

Google Search

The last action in this week’s column is another simple one that, however, speeds up a common task during my editing process in Taio: opening new Google searches in Safari, which is usually running next to Taio in either Slide Over or Split View. With my ‘Google Search’ action, I can type the search query into a single-line input field then watch as a new Google page is opened in the browser.

Type a Google search query in Taio… …and view results in Safari next to it.

When you search for something on Google, words in your search query are joined in the search URL with + characters rather than standard URL-encoded spaces. For this reason, I inserted a ‘Find & Replace’ action after the ‘Text Input’ one that replaces spaces with a ‘+’ character. Then, I passed the result to the ‘Create Text’ action (the equivalent of Shortcuts’ ‘Text’ action), in which I combined the default Google search URL and the encoded query. The final step involved passing the newly constructed Google search URL to the ‘Open URL’ action, which can be configured to open webpages in Safari View Controller (inside Taio), Safari, or a custom web view.

Download the actions:

-"Automating Markdown with Taio, Part 2"