Class: WCC::Contentful::App::CustomMarkdownRender

Inherits:
Redcarpet::Render::HTML
  • Object
show all
Defined in:
lib/wcc/contentful/app/custom_markdown_render.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ CustomMarkdownRender

Returns a new instance of CustomMarkdownRender.



7
8
9
10
# File 'lib/wcc/contentful/app/custom_markdown_render.rb', line 7

def initialize(options)
  super
  @options = options
end

Instance Method Details



27
28
29
30
31
32
33
34
35
# File 'lib/wcc/contentful/app/custom_markdown_render.rb', line 27

def hyperlink_attributes(title, url, link_class = nil)
  link_attrs = { title: title, class: link_class }

  link_attrs[:target] = use_target_blank?(url) ? '_blank' : nil

  return link_attrs unless @options[:link_attributes]

  @options[:link_attributes].merge(link_attrs)
end


12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/wcc/contentful/app/custom_markdown_render.rb', line 12

def link(link, title, content)
  link_with_class_data =
    @options[:links_with_classes]&.find do |link_with_class|
      link_with_class[0] == link &&
        link_with_class[2] == CGI.unescape_html(content)
    end

  link_class = link_with_class_data ? link_with_class_data[3] : nil
  ActionController::Base.helpers.link_to(
    content,
    link,
    hyperlink_attributes(title, link, link_class)
  )
end

#table(header, body) ⇒ Object



41
42
43
# File 'lib/wcc/contentful/app/custom_markdown_render.rb', line 41

def table(header, body)
  "<table class=\"table\">#{header}#{body}</table>"
end

#use_target_blank?(url) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/wcc/contentful/app/custom_markdown_render.rb', line 37

def use_target_blank?(url)
  url.scan(/(\s|^)(https?:\/\/\S*)/).present?
end