Using Marked with Github Flavored Markdown and Syntax Highlighting

Built-in support

Marked 1.4 can handle 99% of GitHub previewing for you. Fenced code blocks, syntax highlighting, pixel-perfect Github style simulation and more.

Github Preview

Enable the following preferences:

Preferences->Behavior->Convert Fenced Code Blocks

behavior screenshot

Preferences->Style->Automatic syntax highlighting

style screenshot

Fenced code blocks and language specifiers in Github (three or more backticks followed by a language name, with a matched number of backticks for closing the block) or Markdown Extra (tildes instead of backticks, language specifier after the closing tildes) formats will be added as classes to the <pre> element. The automatic syntax highlighting will usually figure out the language regardless of whether you use specifiers, but that way you can write exactly as you would on Github (and your html output will match what you see on Github, too).

The included Github stylesheet has all of the necessary styles to match Github formatting exactly. Set your default style to Github, and set Preferences->Style->Limit text width in Preview to: to 900px and expand the preview past 900px for exact word wrapping.

Exporting HTML with Syntax highlighting enabled will include the Highlight.js library as well.

Manual installation

To use fully-compatible GitHub Flavored Markdown you need to build your own custom processor with a series of Ruby gems, or take a look at Docter.


Note for rvm/rbenv users:

Marked doesn't pick up your user environment, so it doesn't get rvm-type settings. To run Docter or any gem-based script, you'll need to switch to System ruby and sudo gem install your required gems. Change any hashbangs in scripts to /usr/bin/ruby instead of /usr/bin/env ruby. You should be able to run your script at that point, assuming it doesn't rely on one of the more recent versions of Ruby that you usually access through rvm. Docter runs fine for me with rvm installed using this method.


You'll need to install rubygems, which in most systems is available by default. Run gem -v to see if you already have it. Otherwise it can be found at http://rubygems.org/.

Once you have rubygems, you can install the following using gem install [gemname]. Depending on your setup, you may need to use sudo gem install...:

  • redcarpet
  • albino
  • nokogiri
  • pygments.rb

You'll also need Pygments, a Python tool for syntax highlighting:

  sudo easy_install pygments

You may need to link the pygmentize binary to a location in your path:

  sudo ln -s /usr/local/bin/pygmentize /usr/bin

Once these are in place, create a script based on this one and make it executable (chmod a+x /your/script.rb). Point your "Custom Processor" setting in Marked to the full path to the script (don't use "~" to shorten your home directory. The easiest way to get this path is to go to the folder where your script is located in Terminal and type pwd|pbcopy, which will put the current path directly on your clipboard. Paste it and add the name of your script to the end of the path.

Note: If you use this method, be sure to disable the features mentioned at the top of this page (fenced code support and syntax highlighting). The built-in Github style has appropriate highlighting styles built in for pygmentized output. You can also disable "Preserve line breaks" as it's probably handled by your script if you're using any github-flavored version of Markdown.

Marked has a PWD environment variable that gets set to default system folders and the path to the current working document. If needed you can Dir.chdir(ENV['PWD']) to work from the document directory.

A common error that arises is a lack of RVM support in Marked. Marked currently does not inherit the user environment, so all of your variables disappear. Hardcode the default /usr/bin/ruby into the hashbang, and use $stderr to output any errors you catch.