参考リンク
- vmg/redcarpet
- rubychan/coderay
- Markdownの表示を行う【Rails】 « Kntmrkm.new()
- Redcarpet のシンタックスハイライトに CodeRay を使う - すぱぶろ
Gemfile
# Markdown & Syntax Highlight
gem 'redcarpet'
gem 'coderay'
bundle install
app/helpers/application_helper.rb
module ApplicationHelper
class HTMLwithCoderay < Redcarpet::Render::HTML
def block_code(code, language)
case language.to_s
when 'rb'
lang = 'ruby'
when 'yml'
lang = 'yaml'
when ''
# 空欄のままだと「Invalid id given:」エラー
lang = 'md'
else
lang = language
end
CodeRay.scan(code, lang).div
end
end
def markdown(text)
html_render = HTMLwithCoderay.new(filter_html: true, hard_wrap: true)
options = {
autolink: true,
space_after_headers: true,
fenced_code_blocks: true,
}
markdown = Redcarpet::Markdown.new(html_render, options)
markdown.render(text)
end
end
app/views/posts/show.html.haml
= markdown(@post.body).html_safe