Sha256: 2958b3d4963d1bedafc66c9426e5c05cf7fff72734a6fb4a78a4a8ccc39d39cc

Contents?: true

Size: 1.1 KB

Versions: 22

Compression:

Stored size: 1.1 KB

Contents

require "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)
        if el.type == :a
          begin
            host = URI.parse(href).host
            unless host.nil? || (@document_domains.compact.include?(host))
              el.attr['rel'] = 'external'
            end
          rescue URI::InvalidURIError, URI::InvalidComponentError
            # it's safe to ignore these very *specific* exceptions
          end
        end
        super
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
govspeak-3.5.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-3.4.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-3.3.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-3.2.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-3.1.1 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-3.1.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-3.0.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-2.0.2 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-2.0.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-1.6.2 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-1.6.1 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-1.6.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-1.5.4 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-1.5.3 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-1.5.2 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-1.5.1 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-1.5.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-1.4.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-1.3.0 lib/kramdown/parser/kramdown_with_automatic_external_links.rb
govspeak-1.2.5 lib/kramdown/parser/kramdown_with_automatic_external_links.rb