First Wordpress Plugin: wp-markdown-syntaxhighlighter

I just put the finishing touches on version 0.1 of my very first Wordpress plugin: wp-markdown-syntaxhighliter.

I found a problem that I couldn’t easily solve with existing plugins, and so it made good sense to write one of my own. I have tried to move my blogging to Markdown-formatted content several times, each time running into incompatibilities between WP-Markdown and SyntaxHighlighter Evolved. The problem is that Markdown re-formats code blocks as <pre><code>...</code></pre>. SyntaxHighlighter is looking for bare <pre> sections with class names indicating various formatting options, thus making my code un-pretty when I use Markdown, unless I re-format my posts manually (essentially meaning that I can’t use Markdown as a source format).

In preparation for re-tooling this site, I decided that my first hurdle had to be removing barriers to writing. I like Markdown, I find it easy and less-involved than bare HTML. WP-Markdown now has the option to use Prettify.js to reformat code blocks. It’s simple and looks nice if all you want is syntax highlighting. If you want additional functionality (e.g. line numbers, row highlighting, etc.), you are out of luck.

(Re-)Enter SyntaxHighlighter. Essentially this is a search and replace problem. I have one code format, I want to go to another. Easy-peasy, that’s what preg_replace is for. My plugin simply does this:

1
return preg_replace( '|<pre><code>#!([^n]+)n(.*?)</code></pre>|se', 'wmsh_add_language('$2','$1');', $text );

That returns the code and its language specified on the first line of a markdown code block to a function that simply changes <pre><code>...</pre></code> to <pre class="brush:language">...</pre>.

I have one big enhancement I’d like to do, which is allowing a user to specify all of the SyntaxHighlighter options in this declarative line, probably in JSON. It’s not urgent, so for now the defaults stand.

I wanted to have this plugin hosted by the Wordpress plugins repository, but I’m not comfortable with the GPLv2 license that they insist on. I’m far more comfortable with Creative Commons-based licensing. I might relent... maybe.

Plugin download removed.

Aug 28th, 2012

Comments