Class: Redmine::WikiFormatting::CommonMark::ExternalLinksFilter

Inherits:
HTML::Pipeline::Filter
  • Object
show all
Defined in:
lib/redmine/wiki_formatting/common_mark/external_links_filter.rb

Overview

adds class=“external” to external links, and class=“email” to mailto links

Instance Method Summary collapse

Instance Method Details

#callObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/redmine/wiki_formatting/common_mark/external_links_filter.rb', line 28

def call
  doc.search("a").each do |node|
    url = node["href"]
    next unless url
    next if url.starts_with?("/") || url.starts_with?("#") || !url.include?(':')

    scheme = begin
      URI.parse(url).scheme
    rescue
      nil
    end
    next if scheme.blank?

    klass = node["class"].presence
    node["class"] = [
      klass,
      (scheme == "mailto" ? "email" : "external")
    ].compact.join " "

    if node["target"].present? && scheme != "mailto"
      rel = node["rel"]&.split || []
      rel << "noopener"
      node["rel"] = rel.join(" ")
    end
  end
  doc
end