class Redcarpet::Render::GitlabHTML < Redcarpet::Render::HTML
  attr_reader :template
  alias_method :h, :template
  def initialize(template, options = {})
    @template = template
    @project = @template.instance_variable_get("@project")
    @options = options.dup
    super options
  end
  def block_code(code, language)
    # New lines are placed to fix an rendering issue
    # with code wrapped inside 
 tag for next case:
    #
    # # Title kinda h1
    #
    #     ruby code here
    #
    <<-HTML
  
    #{h.send(:html_escape, code)}
   
 
    HTML
  end
  def link(link, title, content)
    h.link_to_gfm(content, link, title: title)
  end
  def header(text, level)
    if @options[:no_header_anchors]
      "#{text}"
    else
      id = ActionController::Base.helpers.strip_tags(h.gfm(text)).downcase() \
          .gsub(/[^a-z0-9_-]/, '-').gsub(/-+/, '-').gsub(/^-/, '').gsub(/-$/, '')
      "#{text}"
    end
  end
  def postprocess(full_document)
    unless @template.instance_variable_get("@project_wiki") || @project.nil?
      full_document = h.create_relative_links(full_document)
    end
    h.gfm(full_document)
  end
end