Railsã®ã¢ãã«ã§Markdownã使ç¨ãã
Railsã§Wikiçãªãã®ãä½ãããã«ãç°¡æãã¼ã¯ã¢ããè¨èªã«Markdownã使ã£ã¦ã¿ãã®ã§ããã®ã¡ã¢ã
Rubyç¨ã®Markdownã©ã¤ãã©ãªã¯ãããããã模æ§ãä»åã¯redcarpetã¨ããã®ã使ã£ããç¹ã«ã©ã¤ãã©ãªãæ¯è¼ããä¸ã§é¸æããããã§ã¯ãªãã
# Gemfile gem 'redcarpet'
Redcarpet::Markdownãªãã¸ã§ã¯ãã¯ä½åº¦ã使ãã¾ããããããªã®ã§ãåæåæã«ä½æãã¦ã¢ãã«ã¯ã©ã¹ã«ä¿æãããã
# app/models/entry.rb class Entry < ActiveRecord::Base class << self attr_accessor :markdown end @markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML) end
è¨å®ãå¤é¨ãã¡ã¤ã«ã«åºãããå ´åã¯åæåç¨ã®ã¹ã¯ãªããã追å ãã¦ããã«æ¸ãã
# config/initializers/entry.rb Entry.markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true)
Markdownå½¢å¼ããHTMLã«å¤æããã«ã¯Markdownå½¢å¼ã®ãã¼ã¿ã®æååãrenderã¡ã½ããã«æ¸¡ãã
# app/models/entry.rb def Entry < ActiveRecord::Base def to_html markdown.render(self.content) end end