[rails] Mardown 기재법으로 투고하는 방법을 보여주세요.

7025 단어 RubyRails
rails 투고의 내용을 표시로 입력해도 표시됩니다.
또한 각 언어에 대해 강조 표시할 수도 있습니다.

설치gem


다음 내용 추가
Gemfile
# マークダウン形式で表示するためのgem
gem 'redcarpet', '~> 2.3.0'
# シンタックスハイライトに対応させるためのgem
gem 'coderay'
그리고 bundle install

application_helper.rb에 기술하다


app/helpers/application_helper.rb
module ApplicationHelper
  # マークダウン形式で記入するためにrequire
  require "redcarpet"
  require "coderay"

  class HTMLwithCoderay < Redcarpet::Render::HTML
    def block_code(code, language)
        language = language.split(':')[0]

        case language.to_s
        when 'rb'
            lang = 'ruby'
        when 'yml'
            lang = 'yaml'
        when 'css'
            lang = 'css'
        when 'html'
            lang = 'html'
        when ''
            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,
        no_intra_emphasis: true,
        fenced_code_blocks: true,
        tables: true,
        hard_wrap: true,
        xhtml: true,
        lax_html_blocks: true,
        strikethrough: true
    }
    markdown = Redcarpet::Markdown.new(html_render, options)
    markdown.render(text)
  end
end

show.html.erb에 기술하다


app/views/posts/show.html.erb
<div class="クラス名">
  <%= markdown(@post.content).html_safe %>
</div>
@post.콘텐츠는 하나의 예다.
표시하고 싶은 곳에 추가<%= markdown(モデル名.カラム名).html_safe %>합니다.

편집 페이지에서 편집해 주세요.


rails로 이동해서
자신이 준비한 프로그램의 편집 페이지에 표시된 형식으로 투고를 편집해 보세요.

"@ruby"처럼 하면 돋보이게 할 수 있습니다.
3개의 프롬프트 옆에 언어(확장자)를 쓰는 요령이다.
← 이것은 내가 사용하는 맥과 시프트 키 + option 키 +(밑줄) 를 입력합니다.
상세한 표기법은 이쪽 사이트에 통속적이고 알기 쉽게 실려 있다.
https://qiita.com/Qiita/items/c686397e4a0f4f11683d

페이지에서 확인



참고 자료


https://qiita.com/shu_0115/items/476a51cb4751515f3ac2
https://qiita.com/hkengo/items/978ea1874cf7e07cdbfc

좋은 웹페이지 즐겨찾기