Markdown links in TextMate: The Final Frontier

OK, let’s hope this is my final posting on making Markdown reference links in TextMate. When we last left our intrepid hero, I had refined my command/snippet/macro system to automate the numbering of the references, while still allowing the references to be overridden by, say, words or abbreviations.

The problem with that system in real-world use is that it assumes that I know I’m going to make a link as I’m typing along. For example, if I am writing about wombats and want to insert a link, I have to know, a priori, that a certain word or phrase is going to be a link and invoke the macro just as I get to that point in the text. Unfortunately, I often find myself typing along without any links and then deciding later which words and phrases should be links. So I also need a system that allows me to selected a string of text and turn that into a reference link.

My new command/snippet/macro combination does just that. It consists of two commands and a macro. The first command takes the selected text, puts brackets around it, and leaves the caret just after the closing bracket. I cleverly call it “Brackets,” and it consists of this bit of Perl

#!/usr/bin/perl

$text = $ENV{'TM_SELECTED_TEXT'};
$text =~ s/([\$\\`])/\\$1/g;
print "[$text]" . '$0';

which looks like this when entered in the Bundle Editor window:

(Click on screenshot images to see them at full size.)

The other command is much like the earlier reference link command. It assumes that the text from the end of the bracketed link text to the end of the document is selected, and it inserts a snippet with placeholders (“tab stops” in TM-speak) for the reference number and the URL. The Perl code is this

#!/usr/bin/perl
use List::Util qw(max);

$text = $ENV{'TM_SELECTED_TEXT'};

# Get the highest-numbered reference.
@nums = $text =~ /^\[(\d+)\]: /mg;
$n = max(@nums) + 1;

# Escape special characters.
$text =~ s/([\$\\`])/\\$1/g;

# Insert the snippet.
print '[${1:' . $n . '}]$0'.
       $text . '[$1]: ${2:http://}' . "\n";

and it looks like this in the Bundle Editor:

To make the macro that puts everything together, I select some text and start the macro recording ( Automation:Start Macro Recording ). I then

  1. invoke the Bracketing command from the little gear-like menu at the bottom of the window, which leaves the caret at the end of the closing bracket;
  2. select the text from the caret to the end of the document with Command-Shift-Downarrow;
  3. invoke the snippet-inserting command from the little gear menu, which pops the snippet and placeholders into the document;
  4. stop the macro from recording and Undo a few times to get the document back to where it was before I started.

At this point I save the macro, name it (I chose “Reference link for selection”), and make sure it’s in the Markdown bundle. The Scope Selector should be “text.html.markdown,” and because it’s to be used when text is selected, the Activation has to be a Key Equivalent rather than a Tab Trigger. I chose Control-Option-L. The macro looks like this in the Bundle Editor:

So now I have two macros for making reference-style links in Markdown documents:

Thanks again to Haris Skiadas, Allan Odgaard, and Fred B. from the TextMate mailing list for their help and suggestions.

Technorati Tags: ,