Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect code language for markdown?

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?

like image 819
Vladimir Avatar asked Feb 03 '26 10:02

Vladimir


1 Answers

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
like image 108
matt Avatar answered Feb 06 '26 16:02

matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!