Class: Banzai::Filter::ImageLinkFilter

Inherits:
HTML::Pipeline::Filter
  • Object
show all
Defined in:
lib/banzai/filter/image_link_filter.rb

Overview

HTML filter that wraps links around inline images.

Instance Method Summary collapse

Instance Method Details

#callObject

Find every image that isn't already wrapped in an `a` tag, create a new node (a link to the image source), copy the image as a child of the anchor, and then replace the img with the link-wrapped version.


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/banzai/filter/image_link_filter.rb', line 9

def call
  doc.xpath('descendant-or-self::img[not(ancestor::a)]').each do |img|

    link = doc.document.create_element(
      'a',
      class: 'no-attachment-icon',
      href: img['src'],
      target: '_blank'
    )

    link.children = img.clone
    img.replace(link)
  end

  doc
end