Sha256: a067853d5445ea2b59196c72bda16338c63a4b6b29bd051fa3e1e13cd8a16455

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 KB

Contents

require 'addressable/uri'
require 'redcarpet'

module LadyJosephine
  module Redcarpet
    class CustomMarkdownFormatter < ::Redcarpet::Render::SmartyHTML

      def block_code(code, language)
        "<p>" << code << "</p>"
      end

      def paragraph(text)
        text
      end

      def block_quote(text)
        text
      end

      def list(contents, list_type)
        contents
      end

      def codespan(code)
        code
      end

      def postprocess(full_document)
        full_document.gsub(/&lt;br&gt;/, "<br>\n")
      end

      def preprocess(full_document)
        sanitized_document = full_document.gsub(/\"(.*?)\"/) do
          "„#{$1}“"
        end
        sanitized_document.gsub!(/\./, "\\.")
        sanitized_document.gsub!(/\n/, "<br>\n")
        sanitized_document
      end

      def link(link, title, context)
        if is_internal_link?(link)
          "<a href=\"#{link}\">#{context}</a>"
        else
          "<a href=\"#{link}\" target=\"_blank\" ref=\"noreferrer noopener\">#{context}</a>"
        end
      end

      private

      def is_internal_link?(link)
        host = Addressable::URI.parse(link).host
        host.nil? || host.casecmp(host_from_config) == 0
      end

      def host_from_config
        config_host = Rails.configuration.action_mailer.default_url_options[:host]
        unless config_host =~ /^http(s)?:\/\//i
          config_host = "http://" + config_host
        end
        URI(config_host).host
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lady_josephine-0.7.0 lib/lady_josephine/redcarpet/custom_markdown_formatter.rb
lady_josephine-0.6.3 lib/lady_josephine/redcarpet/custom_markdown_formatter.rb
lady_josephine-0.6.2 lib/lady_josephine/redcarpet/custom_markdown_formatter.rb
lady_josephine-0.6.1 lib/lady_josephine/redcarpet/custom_markdown_formatter.rb