custom markdown in user input custom markdown in user input ruby ruby

custom markdown in user input


Answering my own question:

Defining custom renderer and overwriting normal_text method does a job.

class HTMLwithCards < Redcarpet::Render::HTML  def preprocess(full_document)    full_document.gsub(/\[card:(.*)\]/) do      card = Card.find_by_name($1)      if card        "<span class='preview' data-card='/cards/#{card.id}'>#{$1}</span>"      else        $1      end     end  endend

and then you can call it like this:

def markdown(text)  renderer = HTMLwithCards.new(hard_wrap: true, filter_html: true)  Redcarpet::Markdown.new(renderer).render(text).html_safeend