Created
January 9, 2014 18:01
-
-
Save remino/8338854 to your computer and use it in GitHub Desktop.
kramdown-rails: Use Kramdown (Markdown) in Rails 4
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
# app/helpers/application_helper.rb | |
module ApplicationHelper | |
def markdown(source) | |
Kramdown::Document.new(source).to_html.html_safe | |
end | |
end |
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
# Gemfile | |
gem 'kramdown' |
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
# config/initializers/kramdown.rb | |
module Kramdown | |
module Converter | |
class Html | |
include ActionView::Helpers::AssetTagHelper | |
def convert_img(el, indent) | |
attrs = el.attr.dup | |
link = attrs.delete 'src' | |
image_tag ActionController::Base.helpers.asset_path(link), attrs | |
end | |
end | |
end | |
end |
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
# config/initializers/markdown_handler.rb | |
module MarkdownHandler | |
def self.erb | |
@erb ||= ActionView::Template.registered_template_handler(:erb) | |
end | |
def self.call(template) | |
compiled_source = erb.call(template) | |
"Kramdown::Document.new(begin;#{compiled_source};end, auto_ids: false).to_html.html_safe" | |
end | |
end | |
ActionView::Template.register_template_handler :md, MarkdownHandler | |
ActionView::Template.register_template_handler :markdown, MarkdownHandler |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment