Class: Kramdown::Parser::KramdownWithAutomaticExternalLinks

Inherits:
Kramdown
  • Object
show all
Defined in:
lib/kramdown/parser/kramdown_with_automatic_external_links.rb

Instance Method Summary collapse

Constructor Details

#initialize(source, options) ⇒ KramdownWithAutomaticExternalLinks

Returns a new instance of KramdownWithAutomaticExternalLinks.



24
25
26
27
# File 'lib/kramdown/parser/kramdown_with_automatic_external_links.rb', line 24

def initialize(source, options)
  @document_domains = options[:document_domains] || %w(www.gov.uk)
  super
end

Instance Method Details



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/kramdown/parser/kramdown_with_automatic_external_links.rb', line 29

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