Skip to content

Instantly share code, notes, and snippets.

@shu0115
Created December 4, 2013 03:15
Show Gist options
  • Save shu0115/7781776 to your computer and use it in GitHub Desktop.
Save shu0115/7781776 to your computer and use it in GitHub Desktop.
Markdown/シンタックスハイライト導入 - redcarpet/coderay ref: http://qiita.com/shu_0115/items/476a51cb4751515f3ac2
# Markdown & Syntax Highlight
gem 'redcarpet'
gem 'coderay'
bundle install
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
= markdown(@post.body).html_safe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment