2016-03-30 22:03:49 +00:00
|
|
|
module Banzai
|
|
|
|
module Filter
|
|
|
|
# HTML filter that wraps links around inline images.
|
|
|
|
class ImageLinkFilter < HTML::Pipeline::Filter
|
|
|
|
# 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.
|
|
|
|
def call
|
|
|
|
doc.xpath('descendant-or-self::img[not(ancestor::a)]').each do |img|
|
|
|
|
link = doc.document.create_element(
|
|
|
|
'a',
|
|
|
|
class: 'no-attachment-icon',
|
2017-07-24 17:36:52 +00:00
|
|
|
href: img['data-src'] || img['src'],
|
2017-03-15 21:21:48 +00:00
|
|
|
target: '_blank',
|
|
|
|
rel: 'noopener noreferrer'
|
2016-03-30 22:03:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
link.children = img.clone
|
2016-06-21 16:24:00 +00:00
|
|
|
|
2016-08-13 05:13:39 +00:00
|
|
|
img.replace(link)
|
2016-03-30 22:03:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
doc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|