Skip to content

Instantly share code, notes, and snippets.

@lengarvey
Created May 9, 2013 03:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lengarvey/5545434 to your computer and use it in GitHub Desktop.
Save lengarvey/5545434 to your computer and use it in GitHub Desktop.
A guide for implementing Maruku as the markdown engine instead of Redcarpet in Reinteractive's InstallFest blog guides. http://reinteractive.net/posts/32-ruby-on-rails-3-2-blog-in-15-minutes-step-by-step

In your Gemfile install the Maruku gem by putting:

gem 'maruku'

at the bottom. Make sure you delete the redcarpet and rouge gems since you won't be using those. Run bundle install to install the gem.

Open spec/services/markdown_service_spec.rb and set the content to be:

require 'spec_helper'

describe MarkdownService do
  it { should be_a MarkdownService }

  describe '#render' do
    # the markdown engine is just a test double we can monitor in our test
    let(:markdown_engine) { double('Markdown') }

    before do
      Maruku.stub(:new) { markdown_engine }
    end

    it 'should delegate to the markdown engine' do
      # Set up the expectation of what our code should accomplish
      markdown_engine.should_receive(:to_html)
      MarkdownService.new.render("anything")
    end
  end
end

You can run this spec which will fail with rspec spec/services/markdown_service_spec.rb.

Open: app/services/markdown_service.rb which will look like:

class MarkdownService
  def render(text)
    Maruku.new(text).to_html
  end
end

This might be a small class but wrapping the Maruku class like this means we can easily switch between different markdown engines without changing any other areas in our application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment