Markdown関数を利用できるように実装していきます。
Markdown関数の詳しい使用方法は下記のページを参照して下さい。
guides.github.com
記述追加 GemFile(91行目)
gem 'redcarpet', '~> 3.2.3' gem 'coderay', '~> 1.1.0'
GemFile
source 'https://rubygems.org' git_source(:github) do |repo_name| repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/") "https://github.com/#{repo_name}.git" end # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.0.7', '>= 5.0.7.1' # Use postgresql as the database for Active Record gem 'pg', '>= 0.18', '< 2.0' # Use Puma as the app server gem 'puma', '~> 3.0' # Use SCSS for stylesheets gem 'sass-rails', '~> 5.0' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # Use CoffeeScript for .coffee assets and views gem 'coffee-rails', '~> 4.2' # See https://github.com/rails/execjs#readme for more supported runtimes # gem 'therubyracer', platforms: :ruby # Use jquery as the JavaScript library gem 'jquery-rails' # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks gem 'turbolinks', '~> 5' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.5' # Use Redis adapter to run Action Cable in production # gem 'redis', '~> 3.0' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platform: :mri end group :development do # Access an IRB console on exception pages or by using <%= console %> anywhere in the code. gem 'web-console', '>= 3.3.0' gem 'listen', '~> 3.0.5' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] # bulma gem 'bulma-rails', '~> 0.7.4' gem 'bulma-extensions-rails', '~> 1.0.30' # デバイス gem 'devise', '~>4.2' # アバター gem 'gravtastic' # toastr gem 'toastr-rails', '~> 1.0' # 日本語化 gem 'rails-i18n' # google認証 gem 'omniauth' gem 'omniauth-google-oauth2' # 管理ダッシュボード gem 'activeadmin' # 管理ダッシュボードのテーマ gem 'active_skin' #画像アップロード gem 'paperclip', '~> 5.1.0' # アマゾンS3 gem 'aws-sdk', '~> 2.8' # タスクの順番を変える gem "active_admin-sortable_tree", "~> 2.0.0" # Markdown関数 gem 'redcarpet', '~> 3.2.3' gem 'coderay', '~> 1.1.0'
コマンド
bundle
「app\helpers\application_helper.rb」ファイルの記述を以下のように編集します。
記述変更 app\helpers\application_helper.rb
module ApplicationHelper class CodeRayify < Redcarpet::Render::HTML def block_code(code, language) CodeRay.scan(code, language).div(:line_numbers => :table) end end def markdown(text) coderayified = CodeRayify.new(:filter_html => true, :hard_wrap => true) options = { :fenced_code_blocks => true, :no_intra_emphasis => true, :autolink => true, :strikethrough => true, :lax_html_blocks => true, :superscript => true } markdown_to_html = Redcarpet::Markdown.new(coderayified, options) markdown_to_html.render(text).html_safe end end
「app\views\task\show.html.erb」ファイルを編集します。
記述編集 app\views\task\show.html.erb
<section class="section"> <div class="container"> <div class="col m8"> <div class="card z-depth-2"> <%= @task.video %> <div class="row"> </div> <div class="card-action grey-text text-darken-4"> <div class="container"> <div class="notification"> <%= @task.title %> </div> </div> <div class="box"> <%= markdown(@task.note) %> </div> </div> </div> </div> <!-- タスク Section --> <div class="box"> <article class="media"> <div class="media-content"> <div class="content"> <div class="collection"> <% @tasks.each do |task| %> <% if task.header %> <br/> <div class="collection-item active"> <span class="tag is-link is-large"> <%= task.title %></span> <span class="tag is-white is-large"> <%= task.note %></span> </div> <br/> <% else %> <div class="collection-item active"> <span class="tag is-light is-medium" style="margin: 3px;"> <%= link_to [task.project, task], class: "collection-item" do %> <%= task.title %> <% end %> </span> </div> <% end %> <% end %> </div> </div> </div> </article> </div> </div> </section>
以下のようなMarkdown記法が利用できます。
ルビーのコードを書く場合 <br> ```ruby def index @projects = Project.all end def show @project = Project.find(params[:id]) @tasks = @project.tasks end ``` <br> Javascriptを書く場合 <br> ```javascript function fancyAlert(arg) { if(arg) { $.facebox({div:'#foo'}) } } ```
ブラウザ確認
http://localhost:3000/project/1/task/3