Sha256: 1a60f1307904644b4cce08d4caa8750872212cfa0b609c69f1cb1aba6edbfdb2

Contents?: true

Size: 1.14 KB

Versions: 22

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

require 'redcarpet'

module WCC::Contentful::App
  class CustomMarkdownRender < Redcarpet::Render::HTML
    def initialize(options)
      super
      @options = options
    end

    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

    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

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

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

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
wcc-contentful-app-1.7.1 lib/wcc/contentful/app/custom_markdown_render.rb
wcc-contentful-app-1.7.0 lib/wcc/contentful/app/custom_markdown_render.rb
wcc-contentful-app-1.6.2 lib/wcc/contentful/app/custom_markdown_render.rb
wcc-contentful-app-1.6.1 lib/wcc/contentful/app/custom_markdown_render.rb
wcc-contentful-app-1.6.0 lib/wcc/contentful/app/custom_markdown_render.rb
wcc-contentful-app-1.5.1 lib/wcc/contentful/app/custom_markdown_render.rb
wcc-contentful-app-1.5.0 lib/wcc/contentful/app/custom_markdown_render.rb
wcc-contentful-app-1.5.0.rc1 lib/wcc/contentful/app/custom_markdown_render.rb
wcc-contentful-app-1.4.0 lib/wcc/contentful/app/custom_markdown_render.rb
wcc-contentful-app-1.4.0.rc3 lib/wcc/contentful/app/custom_markdown_render.rb
wcc-contentful-app-1.4.0.rc2 lib/wcc/contentful/app/custom_markdown_render.rb
wcc-contentful-app-1.3.2 lib/wcc/contentful/app/custom_markdown_render.rb
wcc-contentful-app-1.4.0.rc1 lib/wcc/contentful/app/custom_markdown_render.rb
wcc-contentful-app-1.3.1 lib/wcc/contentful/app/custom_markdown_render.rb
wcc-contentful-app-1.3.0 lib/wcc/contentful/app/custom_markdown_render.rb
wcc-contentful-app-1.2.1 lib/wcc/contentful/app/custom_markdown_render.rb
wcc-contentful-app-1.2.0 lib/wcc/contentful/app/custom_markdown_render.rb
wcc-contentful-app-1.1.2 lib/wcc/contentful/app/custom_markdown_render.rb
wcc-contentful-app-1.1.1 lib/wcc/contentful/app/custom_markdown_render.rb
wcc-contentful-app-1.1.0 lib/wcc/contentful/app/custom_markdown_render.rb