炸蝦碎碎念。

[Notes, GNU/Linux, Open Source, Ruby on Rails, Computer Science, Archlinux]

[Rails] Add Markdown Support in Rails

之前那個TIOJ本來說要用Node做的,最後搞得很麻煩還是回來用Rails做XDDD

Reference: Authoring with Markdown in Rails

Gemfile
1
gem 'redcarpet'
app/helpers/application_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
module ApplicationHelper
  def markdown(text)
    renderer = Redcarpet::Render::HTML.new(hard_wrap: true, filter_html: true)
    options = {
      autolink: true,
      no_intra_emphasis: true,
      fenced_code_blocks: true,
      lax_html_blocks: true,
      strikethrough: true,
      superscript: true
    }
    Redcarpet::Markdown.new(renderer, options).render(text).html_safe
  end
end

Comments