Sha256: b564cf5890e0067626fe0af2af8aa61b4df68fb975b8492bb1c491d746ac05e3

Contents?: true

Size: 1.27 KB

Versions: 7

Compression:

Stored size: 1.27 KB

Contents

require "addressable/uri"
require "kramdown/options"

module Kramdown
  module Options
    class AlwaysEqual
      def ==(_other)
        true
      end
    end

    define(:document_domains, Object, %w{www.gov.uk}, <<~DESCRIPTION) do |val|
      Defines the domains which are considered local to the document

      Default: www.gov.uk
      Used by: KramdownWithAutomaticExternalLinks
    DESCRIPTION
      simple_array_validator(val, :document_domains, AlwaysEqual.new)
    end
  end

  module Parser
    class KramdownWithAutomaticExternalLinks < Kramdown::Parser::Kramdown
      def initialize(source, options)
        @document_domains = options[:document_domains] || %w(www.gov.uk)
        super
      end

      def add_link(element, href, title, alt_text = nil, ial = nil)
        if element.type == :a
          begin
            host = Addressable::URI.parse(href).host
            unless host.nil? || @document_domains.compact.include?(host)
              element.attr['rel'] = 'external'
            end
          # rubocop:disable Lint/HandleExceptions
          rescue Addressable::URI::InvalidURIError
            # it's safe to ignore these very *specific* exceptions
          end
          # rubocop:enable Lint/HandleExceptions
        end
        super
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
govspeak-6.1.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-6.0.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-5.9.1 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-5.9.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-5.8.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-5.7.1 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-5.7.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb