Adding Custom Tags to Markdown Plugin

| No Comments

In this article I'll be tinkering with one of Movable Types default plugins, John Gruber's Markdown, which is one of the text formatting choices available when working in the Entry Editor.

The brilliance of Markdown comes from it's powerful translations, combined with it's utter ease of use. Granted, it's not your typical WYSIWYG and did pose a slight comfort curve (the time and effort it takes to adjust to a new way of editing; for me at least!), but after writing only three articles with it I'm hooked!

From it's website, "Markdown allows you to write using an easy-to-read, easy-to-write plain text format, then convert it to structurally valid XHTML (or HTML)". This is accomplished by a number of impressive regular expressions which substitute the Markdown syntax with tags. As an example, this text in the Entry Editor:

#Heading One

Is reformatted when the entry is published, to:

<H1>Heading One</H1>

Brilliant! So it didn't take long before I had to see if Markdown could be leveraged to handle custom syntax for my own defined style blocks. A quick review of the Markdown plugin code confirmed my hopes and in less than an hour I had two working custom Markdown tags.

The tags I added were inspired by the "Note" and "Warning" text blocks which are popular on many Wiki sites. They're great for drawing the readers attention to necessary information amidst the rest of an article's text.

The first step was to choose the Markdown syntax I wanted to represent each style block. Obviously, I needed something that would work with the plugin's default syntax, but I also wanted something easy to remember. I ultimately chose ~! for the Warning style and ~? for the Note style. I really didn't have a scientific process for determining my choices (and they may change if I find conflicts in the future) but in my mind an exclamation point certainly relates to a warning, as a question mark denotes a thought or note.

Implementing the two new custom tags only required the modification of two files; Markdown.pl and screen.css.

First, I created a new subroutine in Markdown.pl called _DoNotesAndWarnings, which will handle the conversion of my custom syntax:

sub _DoNotesAndWarnings {
    my $text = shift;

    $text =~ s{
            \n~([\!\?])      # $1 = style class
            (.+?)           # $2 = Block text
            ~[\!\?]         # closing syntax
        }{
            my $style = ($1 eq '!') ? "Warning" : "Note";
            "<div class=\"$style\">" .  _RunSpanGamut("<b>$style:</b> \n" . $2)  .  "</div>\n\n";
        }egsx;

    return $text;
}

Next, I modified the _RunBlockGamut() subroutine to add a call to my new subroutine and I also added a second call to _HashHTMLBlocks(). This is so <p> tags aren't wrapped around my <div> blocks.

$text = _HashHTMLBlocks($text);

$text = _DoNotesAndWarnings($text);
$text = _HashHTMLBlocks($text);

$text = _FormParagraphs($text);

return $text;
}
Note: If you're running Movable Type under FastCGI then you'll need to force a plugin reload for the changes to take effect. This is done simply by updating the timestamp of the main MT script.

touch [your MT install directory]/mt.fcgi

The last step is to add the style definitions to the Cascading Style Sheet:

Update: 7/29/2008 - I tweaked the styles after spotting a better color scheme in Antonio Lupetti's CSS Message Box Collection article.
.Warning {
    width: 90%;
    margin-left: 5%;
    padding: .2em .3em;
    background: #F7CBCA;
    color: #000000;
    border: solid 1px #CC0000;
    font-size: 100%;
}

.Note {
    width: 90%;
    margin-left: 5%;
    padding: .2em .3em;
    background: #C9FFCA;
    color: #000000;
    border: solid 1px #349534;
    font-size: 100%;
}

Now to test the new custom Markdown tags I use the defined syntax in an entry:

~? This is a Note Block ~?

~! This is a Warning Block ~!

Which produces:

Note: This is a Note Block

and...

Warning: This is a Warning Block

However, those are just simple single line examples but multi-line input is handled just as well so we can do things like this:

~? This is a multi-line example.
<br/><br/>
`with an embedded code block` ~?

Will produce:

Note: This is a multi-line example.

with an embedded code block

So there you have it! My Note and Warning custom Markdown tags are just two simple examples that show the flexibility of the Markdown plugin. Kudos to John Gruber and his development team!

Leave a comment

About this Entry

This page contains a single entry by Dave published on April 11, 2008 5:58 PM.

Kubuntu on a MacBook Pro was the previous entry in this blog.

Custom Tags with Markdown Plugin (Redux) is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.