Skip to content

Instantly share code, notes, and snippets.

@joshRpowell
Forked from erin-dot-io/markdown_helper.rb
Created May 16, 2017 14:01
Show Gist options
  • Save joshRpowell/ad822d475c981bd6e14eacfb3142d957 to your computer and use it in GitHub Desktop.
Save joshRpowell/ad822d475c981bd6e14eacfb3142d957 to your computer and use it in GitHub Desktop.
Markdown helper & parser for Middleman apps
# I use this in conjunction with Contentful CMS to render blocks of markdown into my static middleman templates.
# In your gemfile, add Redcarpet:
gem 'redcarpet'
# ...and do a bundle install
# In your config.rb, add this line:
set :markdown_engine, :redcarpet
# If you dont already have one, create a folder 'helpers' in your base project folder, alongside the 'source' folder.
# Inside this folder create a ruby helper called 'markdown_helper.rb'
# Inside this file paste the code below
module MarkdownHelper
class Markdown
class << self
def renderer
@@renderer ||= Redcarpet::Markdown.new(
Redcarpet::Render::HTML,
:autolink => true,
:no_intra_emphasis => true,
:tables => true,
:fenced_code_blocks => true,
:filter_html => false,
:with_toc_data => true
)
end
end
end
def markdown(&block)
content = capture(&block)
Markdown.renderer.render(content).html_safe
end
end
# Now in your (slim) templates use:
= markdown do
= [markdown entry from contentful]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment