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
# 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 = URI.parse(url).scheme
    next if scheme.blank?

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