首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Rails里应用Markdown

Rails里应用Markdown

作者头像
用户2183996
发布2018-06-28 10:47:24
7500
发布2018-06-28 10:47:24
举报
文章被收录于专栏:技术沉淀技术沉淀技术沉淀

Use Markdown and Pygments

Markdown is pretty awesome for writing, you just type in some tag and your article is formated. You just need to concentrate on the content of your article without worrying about format.

In your rails app, you can use easily do this. Here is a quick guide on how to use markdown and pygments in your rails app.

Add Gem

First thing you need to do is to add redcarpet and pygments.rb gems to you Gemfile. Just add gem 'gem 'redcarpet', '~> 3.3' and gem 'pygments.rb', '~> 0.6.3'to your Gemfile and run bundle install and restart your server.

Add helper

A helper need to be created to handle this markdown stuff. In you application_helper.rb, add this markdown method.

def markdown(content)
    renderer = Redcarpet::Render::HTML.new(hard_wrap: true, filter_html: true)
    options = {
        autolink: true,
        no_intra_emphasis: true,
        disable_indented_code_blocks: true,
        fenced_code_blocks: true,
        lax_html_blocks: true,
        strikethrough: true,
        superscript: true
    }
    Redcarpet::Markdown.new(renderer, options).render(content).html_safe
end

And then instead of using @post.content, try markdown(@post.content) and your article written in markdown syntax will be rendered to html elements. Simple enough? Yes, that's it!

Syntax Highlighting

Wait, one more thing. The code is not highlighted, which is not cool. Remember pygments.rb? Yes, we already installed that gem. We need some CSS to style the code. Here is a github like syntax highlighting, it's pretty awesome, go to checkout! Create a file _pygments.css.scss and add the css here, and then @import 'pygments' in your application.css.scss.

Also, you need to change your helper a bit.

class HTMLwithPygments < Redcarpet::Render::HTML
    def block_code(code, language)
        Pygments.highlight(code, lexer: language)
    end
end

Then change Redcarpet::Render::HTML to HTMLwithPygments in your markdown method. Finally, your code should looks something like this:

module ApplicationHelper
    class HTMLwithPygments < Redcarpet::Render::HTML
        def block_code(code, language)
            Pygments.highlight(code, lexer: language)
        end

    end
    def markdown(content)
        renderer = HTMLwithPygments.new(hard_wrap: true, filter_html: true)
        options = {
            autolink: true,
            no_intra_emphasis: true,
            disable_indented_code_blocks: true,
            fenced_code_blocks: true,
            lax_html_blocks: true,
            strikethrough: true,
            superscript: true
        }
        Redcarpet::Markdown.new(renderer, options).render(content).html_safe
    end
end
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.07.01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Use Markdown and Pygments
  • Add Gem
  • Add helper
  • Syntax Highlighting
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档