diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2016-06-16 16:22:41 +0300 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2016-06-16 20:28:06 +0300 |
commit | c804fef77ae6ec0dd94665f2b895fea36dc72c1d (patch) | |
tree | 8977ca42e58d49265e8520f2da6640b5dc5fe83c /lib | |
parent | f4599c6f7a09c96d63adfec6fba173d41da8a4c4 (diff) |
Merge branch '18582-banzai-filter-external-link-filter' into 'master'
Banzai::Filter::ExternalLinkFilter use XPath
See merge request !4702
Diffstat (limited to 'lib')
-rw-r--r-- | lib/banzai/filter/external_link_filter.rb | 13 |
1 files changed, 2 insertions, 11 deletions
diff --git a/lib/banzai/filter/external_link_filter.rb b/lib/banzai/filter/external_link_filter.rb index f73ecfc9418..0a29c547a4d 100644 --- a/lib/banzai/filter/external_link_filter.rb +++ b/lib/banzai/filter/external_link_filter.rb @@ -3,17 +3,8 @@ module Banzai # HTML Filter to modify the attributes of external links class ExternalLinkFilter < HTML::Pipeline::Filter def call - doc.search('a').each do |node| - link = node.attr('href') - - next unless link - - # Skip non-HTTP(S) links - next unless link.start_with?('http') - - # Skip internal links - next if link.start_with?(internal_url) - + # Skip non-HTTP(S) links and internal links + doc.xpath("descendant-or-self::a[starts-with(@href, 'http') and not(starts-with(@href, '#{internal_url}'))]").each do |node| node.set_attribute('rel', 'nofollow noreferrer') node.set_attribute('target', '_blank') end |