Implementing Markdown Parsing in your Ruby on Rails App

I was recently built a simple Wiki app for work that required markdown parsing. To get this going I needed to use html-pipeline to parse the content of the articles and display markdown.

First thing, this post assumes you have an Article model or some Post model with a body attribute with a datatype of text.

Once you have your model setup and the attributes going you will need to add the following gems to your Gemfile


gem 'html-pipeline', '~> 2.6'  
gem 'commonmarker', '~> 0.16.0'  
gem 'sanitize'  
gem 'github-markdown', '~> 0.6.7'  

Make sure to run bundle install to get the gems installed. If you are working on a Mac and are using MacOS Sierra, you will need to install cmake to satisfy the dependencies for commonmarker. To do this via homebrew simply run brew install cmake before you bundle in case you get an error that cmake is not installed.

Now we will create a helper method called markdownify in the ArticlesHelper (but this can do in whatever helper you've namespaced.

Articles Helper

def markdownify(body)  
  pipeline_context = { gfm: false }
  pipeline = [
  ], pipeline_context[:output].to_s.html_safe

The above method will take a body of text from your Article that uses markdown and transform it into markdown with github flavored markdown.

Here we are using SimpleMDE as a native markdown editor for our posts, but you can use a regular text_area field and manually input markdown:


Now here's the method line that parses the markdown in your show view

Article show view:

  <%= markdownify(@article.body) %>

This will display the proper article parsed as markdown:


It's that simple. With a few lines of code, we were able to introduce markdown parsing in a Ruby on Rails app.

If I've missed something, please be sure to let me know: nynhex [@] gmail dot com