Class: Kramdown::Parser::NoopenerGFM

Inherits:
GFM
  • Object
show all
Defined in:
lib/kramdown/parser/noopener_gfm.rb

Constant Summary collapse

SAFE_OPTS =
{ "target" => "_blank", "rel"=>"nofollow noopener noreferrer" }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(source, options) ⇒ NoopenerGFM

Returns a new instance of NoopenerGFM.



7
8
9
10
# File 'lib/kramdown/parser/noopener_gfm.rb', line 7

def initialize(source, options)
  super
  @same_tab_domains = options.fetch(:same_tab_domains, [])
end

Instance Method Details

#update_elements(element) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/kramdown/parser/noopener_gfm.rb', line 12

def update_elements(element)
  if element.type == :a && \
     element.attr.respond_to?(:[]) && \
     (href = element.attr['href']) && \
     URI::DEFAULT_PARSER.regexp[:ABS_URI].match(href) && \
     !@same_tab_domains.include?(URI::parse(href).host)

    element.attr.merge!(SAFE_OPTS)
  end
  super
end