Sha256: 46950fddfbbf11e4269ec525658192ca636ecd64400888015d62ae0c2a70b939

Contents?: true

Size: 1.12 KB

Versions: 14

Compression:

Stored size: 1.12 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}, <<EOF) do |val|
Defines the domains which are considered local to the document

Default: www.gov.uk
Used by: KramdownWithAutomaticExternalLinks
EOF
      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(el, href, title, alt_text = nil, ial = nil)
        if el.type == :a
          begin
            host = Addressable::URI.parse(href).host
            unless host.nil? || (@document_domains.compact.include?(host))
              el.attr['rel'] = 'external'
            end
          rescue Addressable::URI::InvalidURIError
            # it's safe to ignore these very *specific* exceptions
          end
        end
        super
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
govspeak-5.6.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-5.5.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-5.4.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-5.3.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-5.2.2 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-5.2.1 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-5.2.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-5.1.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-5.0.3 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-5.0.2 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-5.0.1 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-5.0.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-4.0.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-3.6.2 lib/kramdown/parser/kramdown_with_automatic_external_links.rb