如何检测markdown的代码语言?

时间:2016-12-26 12:41:28

标签: ruby markdown redcarpet

我写在textarea:

```ruby
 puts 'hello word!'
```

我赢了:

<pre lang='ruby'><code>puts hello word!</code></pre>

相反,我得到了:

<code>puts hello word!</code>

我尝试了不同的属性。我的帮手:

def markdown(text)
    renderer = Redcarpet::Render::HTML.new(
                                            hard_wrap:         true,
                                            fenced_code_block: true,
                                            no_intra_emphasis: true,
                                            filter_html:       true
                                            )
    markdown =
      Redcarpet::Markdown.new(
                              renderer,
                              fenced_code_block: true,
                              no_intra_emphasis: true,
                              fenced_code:       true,
                              gh_blockcode:      true,
                              autolink:          true,
                              hard_wrap:         true,
                              filter_html:       true
                              )

      markdown.render(text).html_safe
  end

为什么?如何检测代码语言?

1 个答案:

答案 0 :(得分:1)

您需要的选项是fenced_code_blockss。您似乎也在混合渲染器和扩展选项。试试这个:

renderer = Redcarpet::Render::HTML.new(hard_wrap:   true,
                                       filter_html: true)

markdown = Redcarpet::Markdown.new(renderer,
                                   fenced_code_blocks: true,
                                   no_intra_emphasis:  true,
                                   autolink:           true)

markdown.render(text).html_safe