A Test Entry with Markdown+Strikedown

| | Comments (1)

I like Markdown; it works very well. But it doesn’t have the ability to emphasize text with strikethroughs, which are a mainstay of modern conversation in blogs these days.

So I modified it. Watch this:

Here is a strikethrough.

See how easy that was? All I did was type a dash before and after the word “strikethrough,” and Markdown now makes that into a strikethrough.

To do this, all I did was modify the Markdown.pl code a bit. Here’s how you can do it, too.

Find Markdown.pl. For MoveableType users, it’s in mt/plugins/Markdown. Edit it with your favorite editor and go to line 1040. Make that block of code look like this by adding the last two or three lines (you can credit me if you want):

sub _DoItalicsAndBold {
    my $text = shift;

    # <strong> must go first:                                                                                                                                     
    $text =~ s{ (\*\*|__) (?=\S) (.+?[*_]*) (?<=\S) \1 }
        {<strong>$2</strong>}gsx;

    $text =~ s{ (\*|_) (?=\S) (.+?) (?<=\S) \1 }
        {<em>$2</em>}gsx;

    # These lines added by Bill Eccles, 2008-07-04
    $text =~ s{ (\s) (-) (?=\S) (.+?) (?<=\S) (-) }
    {$1<strike>$3</strike>}gsx;

    return $text;
}

Be forewarned that I’ve only tested it in very simple use (you’ve seen all the testing I’ve done, above) and that it might do unexpected things. But so far, so excellent good!

UPDATE: I wonder if this will make problem for people who-make-use of words-with hyphens-randomly scattered throughout their text… YES! AGH!

I’ll have to go work on that for a bit… back in a while.

UPDATE: OK, that problem is fixed (as you can see above). It does fail, however, with striking through hyphenated words, (Here’s an example-.) but that’s OK in my book. Just leave out the hyphen—it’d be lost in the strikethrough anyway.

1 Comments

Bill Eccles Author Profile Page said:

Rob Shearer had this to say:

From: Rob Shearer Date: July 6, 2008 8:49:11 PM EDT Subject: Your blog comment system is broken
Tried "registering"; tried commenting anonymously. Apparently the text entered was wrong. Sigh.
I was commenting on your strikethrough Markdown feature:
Hyphens don't really carry the same semantics as strikethrough, and the point of Markdown is that the text looks like the HTML. The nearest equivalent for strikethrough I know of is repeated "^H" characters. As in "here is some crap^H^H^H^H stuff". A simple Markdown implementation would be to add the following to _DoItalicsAndBold:
   my $newtext;
   while ($text =~ m/(.*?)((\^H)+|$)/g) {
       if ($2) {
           my $n = length($2)/2;
           $newtext .= (substr $1, 0, -$n) . "strikethrough" . (substr $1, -$n) . "/strikethrough";
       } else {
           $newtext .= $1;
       }
   }
   $text = $newtext;

[Bill says: Don't know what happened with the commenting system. Sometimes I find that the recaptcha text isn't necessarily a recognizable word and I've got to guess, too. But it works. And I tried--and this is what might have messed up Rob--to get his code into MT, but it didn't work worth a hoot. Where you see "strikethrough" above, substitute in the real HTML tag element with GT's and LT's around 'em.

Anyway, I disagree that the purpose of Markdown is to make something that looks like HTML. In fact, John explicitly states:

Markdown is a text-to-HTML conversion tool for web writers. 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).
and
The overriding design goal for Markdown’s formatting syntax is to make it as readable as possible.

Rob's absolutely correct that dashes don't convey the same semantics, but Markdown isn't about semantics. It's about ease of use and readability, and perhaps I haven't got the right-est solution, but it works for me. And, I assume, Rob's works for him, too.

Hey, the best part about all of this? To each his own!

Thanks for the comment, Rob, and sorry you had trouble with the commenting system. /Bill]