How to detect code language for markdown?

574 views Asked by At

I've written in a textarea:

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

I won't get:

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

Instead I got:

<code>puts hello word!</code>

I tried different attributes. My helper:

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

Why? How can I detect code language?

1

There are 1 answers

0
matt On BEST ANSWER

The option you want is fenced_code_blocks, with an s. You also seem to be mixing renderer and extension options. Try this:

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