Blockquoted code blocks

Written in the mid-afternoon in English • Tags: , ,

I’ve been writing in Markdown for nine years now, because it allows me to spend more time on content as opposed to formatting. Using it with WordPress has required a couple of bug-hunt sessions, but overall I feel it has been a time-saver.

This week I spent a little bit of time experimenting with formatting code snippets. While looking into my earlier writing I realized I’ve had an issue with code snippets repeatedly. Today I took the time to fix the problem by changing the Markdown parser slightly.

I’ve been using blockquoted code blocks like this:

> ~~~~
code()
{
    goes here;
}
> ~~~~

They work almost like regular code blocks (without the leading >) except that at least some Markdown markup is interpreted inside the block and empty lines terminate the block. This is because the blockquote is scoped first (until the end of the paragraph, i.e. the empty line) and then further transformations are applied to its content. I haven’t really analyzed what happens to the extra > in the no-empty-lines case.

I thought having blockquoted code blocks by design would be desirable and had a look into whether I could manage to make such a modification. Fortunately it is relatively easy to find the relevant pieces in the PHP Markdown Extra code for transformations of each markup, thanks to clear function naming and comments.

The change is small and can be found here:

http://kimmo.suominen.com/stuff/markdown.blockquoted-code.diff

The benefit (for me, at least) is that the whole block now works like a code block, so something like this is possible:

> ~~~~
# apt-get install markdown
> ~~~~

Without the patch the line of “code” turns into a first level heading. I could work around the problem with not so elegant an incantation of mixed HTML:

<blockquote markdown="1">
~~~~
# apt-get install markdown
~~~~
</blockquote>

That’s just tedious as in a typical document it would be used repeatedly many, many times.