-
-
Save joshRpowell/ad822d475c981bd6e14eacfb3142d957 to your computer and use it in GitHub Desktop.
Markdown helper & parser for Middleman apps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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