A custom TextMate command that uses ‘sed’

In this post I share the contents of a custom TextMate command I just created that uses pandoc and sed to convert markdown content in the TextMate editor to a “pretty printer” version of HTML:

#!/bin/sh

PATH=$PATH:/usr/local/bin

# note: 'sed -E' gives you the advanced regex's

# use pandoc to convert from markdown to html,
# then use sed to clean up the resulting html
pandoc -f markdown -t html |\
sed -Ee "/<p|<h2|<h3|<h4|<aside|<div|<ul|<ol/i\\
\\"

You can try to use a command like tidy to clean the HTML, but the version of tidy I have does not know about HTML5 tags. The TextMate Markdown plugin also doesn’t work the way I want it. Besides that, I’m trying to learn more about writing TextMate commands anyway.

As an important note, when you set this up as a TextMate command and then run it, it will convert the TextMate editor contents from markdown to HTML.

(In a related note, serenity.de is also a good resource for TextMate command and bundle documentation.)

In summary, this code shows:

* How to execute a Unix shell command from TextMate
* Specifically, how to execute a sed command from TextMate
* How to use modern regular expressions with sed (the -E option)
* How to search for multiple regex search patterns with sed