Sha256: a14caf415913c94a05b85f4c94c900d0bfadaa8da8573f2bd83d2b6d64280091

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 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 Govuk < Kramdown::Parser::Kramdown
      CUSTOM_INLINE_ELEMENTS = %w(govspeak-embed-attachment-link).freeze

      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

      def parse_block_html
        return false if CUSTOM_INLINE_ELEMENTS.include?(@src[1].downcase)

        super
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
govspeak-6.5.1 lib/kramdown/parser/govuk.rb