Using TextExpander for Markdown Reference Links

Patrick Rhone (from MinimalMac) did his first screencast this week about creating a Text Expander snippet for Markdown links. Like Patrick, I write everything in Markdown (although I use the MultiMarkdown variant), which lets me export to nearly any format and write on the iPhone, iPad, and Mac without friction. I wrote the whole book in MultiMarkdown.*

Anyway, Patrick’s video got me thinking. Patrick puts his links right next to the linked words. I use Markdown’s reference links. The advantage of using referenced links is that the text is much easier to read. You can also use the same reference more than once. Reference links can be set anywhere in the document. Some folks do it at the end of the paragraph. I do them all at the bottom.

The syntax for reference links in Markdown works as follows:

First you type some [Link Text][Reference]
Then more text … yadda yadda yadda

[Reference]:Web URL

The Link Text is the text that shows up on the page. The Reference is an index. It can be a number or words. The Web URL is the link you are pointing toward. Here is concrete example.

This is one [nerdy Mac site.][1]
Really, the guy just won’t shut up.

[1]:http://www.macsparky.com/

So I decided to take a page from Patrick’s playbook and create my own TextExpander snippet to create referenced links. It’s not that hard.

Snippet Walkthrough

If you are thinking ahead while you write, you can do this all in one step. This snippet, upon activation, asks you to insert the Link Text and Reference and then creates a Reference entry on the next line using the Web link from your clipboard. Finally, it puts the cursor back at the end of your link text. Fire up TextExpander and follow along.

Create a new snippet and start with two sets of brackets. The first will hold the Link Text and the second will hold the Reference. The Snippet should now look like this:

— Begin Snippet Code —

[][]

— End Snippet Code —

Next, create a Fill-In blank for the Link Text. These are blanks that Text Expander will prompt you to fill when the snippet runs. To create a Fill-In blank, insert the cursor between the first two brackets and select Fill-In from the Cursor drop down shown below.

— Begin Snippet Code —

[%fill:name%][]

— End Snippet Code —

Next, create a second Fill-In blank in the second set of brackets. This will be the blank for the Reference. When you create this second FIll-In blank, change the text from fill:name to fill:reference. This name is a variable. If you use the same name twice, TextExpander will copy it into both places. You will use this later.

— Begin Snippet Code —

[%fill:name%][%fill:reference%]

— End Snippet Code —

Next you’ll create the reference. First add a few carriage returns and then create the reference syntax, which is a pair of brackets and a colon.

    — Begin Snippet Code —

[%fill:name%][%fill:reference%]

[]:

— End Snippet Code —

Add a Fill-In blank in the brackets and rename it fill:reference. This duplicates the reference used above so you only need to type it once.

— Begin Snippet Code —

[%fill:name%][%fill:reference%]

[%fill:reference%]:

— End Snippet Code —

Next, add the Web URL. Place the cursor after the colon and select the Clipboard option from the cursor drop down shown in the figure below. This step dumps your clipboard in as a link. In order to make this work, you must first select and copy the link you want to use before activating the snippet. (You can do this in Safari by navigating to the desired page, pressing Command-L and then Command-C.) Alternatively, you could also make this a Fill-In field and type the URL in manually.

— Begin Snippet Code —

[%fill:name%][%fill:reference%]

[%fill:reference%]:%clipboard

— End Snippet Code —

Finally, move the cursor back to the top line at the end of the link and select Position Cursor Here from the Cursor menu. This resets the cursor back in your text so you can continue typing.

— Begin Snippet Code —

[%fill:name%][%fill:reference%] %|

[%fill:reference%]:%clipboard

— End Snippet Code —

I named this snippet Markdown Link and use the keyboard combination .[ to set it off.

Here is a picture of it in action.

And here is the resulting code.

Link as Afterthought

The above workflow assumes you think of creating the link while you are typing. If you want to add a reference link later, I created two more snippets.

The first takes highlighted text in the body of your text and sets up the text and reference number. Here is the TextExpander snippet code. I use .] to activate this snippet.

— Begin Snippet Code —

[%clipboard][%fill:reference%]

— End Snippet Code —

The second snippet creates the reference link. I use .: for this one.

— Begin Snippet Code —

[%fill:reference%]:%clipboard

— End Snippet Code —

Are you a Markdown nerd and using TextExpander? Let me know or, better yet, leave a comment and describe it for everyone.