[TxMt] Setext-style heading for Markdown
Dr. Drang
drdrang at gmail.com
Fri May 19 03:32:50 UTC 2006
Here's quick little command/macro pair for handling Markdown headings
using the setext format. I wrote it because I always find myself
changing the text of my headings, which means erasing the underline
and redoing it so the line of equals or hyphens matches the length of
the new heading text. (Yes, I know that Markdown doesn't require the
length of the underline to match the length of the heading. That's one
of the many niceties John Gruber designed into Markdown. But I'm anal
and like my underlines to match.)
Start with this command for `<h1>` headings.
#!/usr/bin/perl
chomp(@lines = <>);
$under = "=" x length($lines[0]);
print $lines[0] . "\n" . $under . "\n";
unless ($#lines == 0 or $lines[1] =~ /^[-=~]+$/) {
print $lines[1] . "\n";
}
The input should be **Selected Text** or **Nothing**, and the output
should be **Replace Selected Text**. I named it "Heading 1."
Now record a macro that prepares the input and runs the command. The
idea is to be able to call the macro with the caret anywhere on the
line with the heading text. If the heading isn't already underlined it
will underline it; if the heading is underlined it will adjust the
underline to the new heading length.
To make the macro, put the caret on the heading line, start recording and
1. Type **Command-Leftarrow** to move the caret to the beginning of the line.
2. Type **Shift-Downarrow** twice to select the heading line and the
line below it. (Don't worry if there is no line below it; the macro
will record both keystrokes and the command was written to handle the
single-line case.)
4. Invoke the "Heading 1" command to transform the selected text.
5. Type **Rightarrow** to put the caret at the end of the selected
text and deselect it.
I called this macro "Heading 1" also. For a "Heading 2" macro that
inserts a line of hyphens, do exactly the same thing, but change the
quoted equals sign in the fourth line of the command to a quoted
hyphen.
There's a writeup of this on [my blog][1] if you need to see more details.
--
Dr. Drang
[1]: http://www.leancrew.com/all-this/2006/05/markdown_headings_in_textmate.html
More information about the textmate
mailing list