Automating Markdown links with TextExpander

January 5, 2015 · 2 minutes read

Adding links to Markdown documents can be tedious at times. Especially when you’re doing it a lot or under pressure, e.g. if you’re creating show notes for your podcast while you’re recording. You’ll usually get a link from somewhere, from the chat, a guest or just by searching for it and all you want to do is insert the link with the proper title and formatting. What would you normally do? Insert the link into your document. Then open it in a new browser tab and copy the headline. Go back to your document. Insert it and fix the formatting.
There must be a better way to do this, right?

And indeed, there is. Several actually. First let me explain how I did it, then I’m going to mention a different approach created by the infamous Brett Terpstra. Depending on your workflow, you might prefer one or the other which is totally fine. Whatever works for you!

My weapon of choice is TextExpander and a little bash script. Simply because that way it will work with literally any document and app on my Mac. Whether I’m using it in Google Docs, Pages or FoldingText, it will work.

Parser snippet

Parser snippet

We’ll need two TextExpander snippet to achieve this. The first one is essentially just a script: #!/bin/bash curl -s –compressed -L –max-redirs 10 “%clipboard” | grep -o ‘.’ | sed ’s/(.)</title>/\1/g’ | tr -d ‘\n’

It takes the link from your clipboard, fetches the source of the webpage and extracts the content of the title HTML element. You could also use this snippet on it’s own, if you want to.

Wrapper snippet

Wrapper snippet

The second element provides the markdown wrapper around the first one: %snippet:xlwtl%

Some sites take a relatively long time to load, so I’d suggest to increase TextExpander’s script timeout time by executing this on the command line: osascript -e ’tell application “TextExpander” to set allowed computation time to “28”’ This will increase the time to 28s, which is plenty time for most pages.

That’s it. Whenever you need to insert a link, all you need to do is copy it, type the magic TextExpander abbreviation into your document and a few seconds later the link appears.

As I mentioned before, there’s another solution Brett Terpstra created: it’s called SearchLink and allows you to insert search terms into your document and let the tool search for the appropriate link on its own. It’s more a solution for people who are writing articles and don’t want to interrupt their flow to look up some random link.