I've been writing/ready quite a bit of documentation lately and have settled on Markdown. It continues to be the most powerful way to structure and express my thoughts.

Recently I began to notice that docs I have been reading which are written in markdown had syntax highlighting in their code blocks. At first I thought perhaps they were just creating gists and placing the url in the post which would embed the code block.

However after poking around I came to find out that Github flavored Markdown supports the following syntax:

```javascript
var foo = 'bar';
```

Which will produce the following javascript:

var foo = 'bar';

My guess is that it supports any langaugae that you can get syntax highlighting for in a gist.

Turn it on

Github parses Markdown with Redcarpet. As of Jekyll 0.12.0 redcarpet can be turned on by simply adding the following line to _config.yml:

markdown: redcarpet

You'll also need to include the syntax highlighting CSS file

That's all!

That should do it. You should now be able to use the sweet new syntax for getting syntax highlighting in code blocks!

Examples

Ruby

require 'redcarpet'
markdown = Redcarpet.new("Hello World!")
puts markdown.to_html

PHP

<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)){
  echo "Got Irix";
}

if (in_array("mac", $os)){
  echo "Got mac";
}
?>

HTML

<p>hello world</p>

Thanks for reading! Follow me on Twitter and/or G+ for more.

See something wrong or got something to add/change? Fork this git repo and send me a pull request

If you particularly enjoy my work, I appreciate donations given with Gitcoin.co



Published

20 February 2013

Tags