How can I turn off certain tags for redcarpet in RoR?

114 Views Asked by At

I have the following function in application_helper.rb:

def markdown(text)
    options = {}

    renderer = CustomRender.new(hard_wrap: true, filter_html: true)
    Redcarpet::Markdown.new(renderer, options).render(text).html_safe
end

I am using a custom renderer. I am confused as to why I can use bulleted lists, italicized text, codespans, etc. I do not have them enabled in my render options.

I thought maybe I could just turn them off:

options = {
    codespan: false,
}

However, codespans are still recognized by redcarpet, and styling is applied. How can I turn off all tags from markdown and only enable a select few?

1

There are 1 best solutions below

0
Chris On

Define method for codespan in your CustomRenderer

class CustomRender < Redcarpet::Render::HTML
  def codespan(code)
    code
  end
end
def markdown(text)
  renderer = CustomRender.new(hard_wrap: true, filter_html: true)
  Redcarpet::Markdown.new(renderer).render(text).html_safe
end

Now you can see no <code> tag is added when calling markdown

markdown('```some code```') =>  "<p>some code</p>\n"

You can overwrite all the methods mentioned here https://github.com/vmg/redcarpet#block-level-calls