class Qiita::Markdown::Filters::ExternalLink

Public Instance Methods

call() click to toggle source
# File lib/qiita/markdown/filters/external_link.rb, line 7
def call
  doc.search("a").each do |anchor|
    next unless anchor["href"]

    href = anchor["href"].strip
    href_host = host_of(href)
    next unless href_host

    if href_host != hostname
      anchor["rel"] = "nofollow noopener"
      anchor["target"] = "_blank"
    end
  end

  doc
end
validate() click to toggle source
# File lib/qiita/markdown/filters/external_link.rb, line 24
def validate
  needs :hostname
end

Private Instance Methods

host_of(url) click to toggle source
# File lib/qiita/markdown/filters/external_link.rb, line 30
def host_of(url)
  uri = Addressable::URI.parse(url)
  uri.host
rescue Addressable::URI::InvalidURIError
  nil
end
hostname() click to toggle source
# File lib/qiita/markdown/filters/external_link.rb, line 37
def hostname
  context[:hostname]
end