Rouge and redcarpet don't hightlight ruby code

34 Views Asked by At

In app/helper/application_helper:

module ApplicationHelper
  require 'redcarpet'
  require 'rouge'
  require 'rouge/plugins/redcarpet'

  class HTML < Redcarpet::Render::HTML
    include Rouge::Plugins::Redcarpet
  end

  def markdown(text)
    return '' if text.nil?

    options = {
      filter_html: true,
      hard_wrap: true,
      link_attributes: { rel: 'nofollow', target: '_blank' },
      prettify: true
    }

    extensions = {
      autolink: true,
      tables: true,
      fenced_code_blocks: true,
      lax_spacing: true,
      no_intra_emphasis: true,
      strikethrough: true,
      superscript: true,
      disable_indented_code_blocks: true,
    }

    # Redcarpet::Markdown.new(HTML.new(options), extensions).render(text).html_safe
    # these 3 lines do same as above 1 line
    renderer = HTML.new(options)
    markdown = Redcarpet::Markdown.new(renderer, extensions)
    markdown.render(text).html_safe
  end

end

In app/assets/stylesheets/application.css:

@import "rouge";

In app/assets/stylesheets/rouge.css.erb:

<%= Rouge::Themes::Base16.mode(:light).render %>

In app/views/articles/index.html.erb:

<% @articles.each do |article|%>
<p>
    <%= link_to article.title, article_path(article) %> 
    
</p>

<p>
    <%= markdown(article.body)%>
</p>
<hr/>
<%end%>

View page sourse:

<p>
    <a href="/articles/20">test</a> 
    
</p>

<p>
    <div class="highlight"><pre class="highlight ruby"><code>
<span class="k">def</span> <span class="nf">test</span>
<span class="nb">puts</span> <span class="s1">'Hello'</span>
<span class="k">end</span>

</code></pre></div>
</p>

Similar question exist on this site, and I tried to do, but I can't. I looked similar video on youtube, but I can't to do it. I use Active Admin for creating articles. Am I missing something?

0

There are 0 best solutions below