Mini John
Mini John

Reputation: 7941

Redcarpet Syntax Highlighting

I'm trying to get Syntax Highlighting with Redcarpet working

My 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

Although the output is displayed in a codeblock (redcarpet):

enter image description here

I can't find the Syntax Highlighting.

I just got into Redcarpet, someone know a solution for this?

Upvotes: 4

Views: 5113

Answers (3)

wpp
wpp

Reputation: 7313

Here is a quick way to do it with Rouge:

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

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

Of course this requires rouge to be in your Gemfile.

Upvotes: 5

Mini John
Mini John

Reputation: 7941

Ok i found -> Markdown and code syntax highlighting in Ruby on Rails (using RedCarpet and CodeRay) which pretty much worked (together with some custom css for Coderay).

Gemfile:

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

Upvotes: 11

Robin van Dijk
Robin van Dijk

Reputation: 827

I don't think Redcarpet can do that. In my project I followed the Railscasts episode about Redcarpet which also tackles syntax highlighting. It makes use of Pygments and Albino.

Link to the ASCIIcast version is here.

Upvotes: 2

Related Questions