LoginSignup
16
14

More than 5 years have passed since last update.

Railsのプロジェクトにgem 'redcarpet' を用いてマークダウンエディタを導入してみた

Posted at

背景

サービスの修正や機能開発の際に、リリース報をうちたい。
社内のメンバーはマークダウンでの記述に慣れているため、今回はマークダウンで書いてもらうことを前提とする。

流れ

  • gem 'redcarpet' をインストール
  • markdown_helper.rbを作成し、オプションを追加
  • フォームでマークダウンで記述

Gem追加

$ bundle install redcarpet

Helper追加

markdown_helper.rb
# gem 'redcarpet' 用
module MarkdownHelper
  def markdown(text)
    unless @markdown
      options = {
        filter_html: true,
        autolink: true,
        space_after_headers: true,
        no_intra_emphasis: true,
        fenced_code_blocks: true,
        tables: true,
        hard_wrap: true,
        xhtml: true,
        lax_html_blocks: true,
        strikethrough: true
      }
      renderer = Redcarpet::Render::HTML.new(options)
      @markdown = Redcarpet::Markdown.new(renderer)
    end

    @markdown.render(text).html_safe
  end
end

フォームでマークダウンによる記述を行う

スクリーンショット 2018-07-13 20.23.25.png

レンダリング

任意の箇所でhelper内にあるmarkdownメソッドを呼び出します。

show.html.slim
= markdown(@blog.content)

実際の表示画面

スクリーンショット 2018-07-13 20.23.01.png

参考記事

オプションの例が日本語で豊富に書かれている
https://programming-beginner-zeroichi.jp/articles/57

16
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
14