3

我正在尝试使用Redcarpet进行语法突出显示

我的appliaction_helper.rb

module ApplicationHelper

  def markdown(text)
    render_options = {
        # will remove from the output HTML tags inputted by user
        filter_html:     true,
        # will insert <br /> tags in paragraphs where are newlines
        hard_wrap:       true,
        # hash for extra link options, for example 'nofollow'
        link_attributes: { rel: 'nofollow' },
        # Prettify
        prettify:        true
    }
    renderer = Redcarpet::Render::HTML.new(render_options)

    extensions = {
        #will parse links without need of enclosing them
        autolink:           true,
        # blocks delimited with 3 ` or ~ will be considered as code block.
        # No need to indent.  You can provide language name too.
        # ```ruby
        # block of code
        # ```
        fenced_code_blocks: true,
        # will ignore standard require for empty lines surrounding HTML blocks
        lax_spacing:        true,
        # will not generate emphasis inside of words, for example no_emph_no
        no_intra_emphasis:  true,
        # will parse strikethrough from ~~, for example: ~~bad~~
        strikethrough:      true,
        # will parse superscript after ^, you can wrap superscript in ()
        superscript:        true
        # will require a space after # in defining headers
        # space_after_headers: true
    }
    Redcarpet::Markdown.new(renderer, extensions).render(text).html_safe
  end

end

虽然输出显示在代码块(红地毯)中:

在此处输入图像描述

我找不到Syntax Highlighting.

我刚进入 Redcarpet,有人知道解决方案吗?

4

3 回答 3

9

好的,我发现 -> Markdown 和 Ruby on Rails 中的代码语法高亮(使用 RedCarpet 和 CodeRay)非常有效(以及一些用于 Coderay 的自定义 css)。

宝石文件:

gem 'redcarpet', github: 'vmg/redcarpet'
gem 'coderay'

Application_helper.rb

class CodeRayify < Redcarpet::Render::HTML
  def block_code(code, language)
    CodeRay.scan(code, language).div
  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
于 2014-05-21T15:07:02.317 回答
4

这是使用Rouge的快速方法:

require 'redcarpet'
require 'rouge'
require 'rouge/plugins/redcarpet'

class HTML < Redcarpet::Render::HTML
  include Rouge::Plugins::Redcarpet # yep, that's it.
end

当然,这需要rouge在您的Gemfile.

于 2015-11-18T11:42:22.453 回答
2

我不认为Redcarpet可以这样做。在我的项目中,我关注了 Railscasts 的一集,Redcarpet其中也涉及语法高亮。它使用PygmentsAlbino

ASCIIcast 版本的链接在这里

于 2014-05-21T14:31:35.193 回答