debian-mirror-gitlab/lib/banzai/filter/image_link_filter.rb

35 lines
903 B
Ruby
Raw Normal View History

2016-06-02 11:05:42 +05:30
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|
2016-08-24 12:49:21 +05:30
div = doc.document.create_element(
'div',
class: 'image-container'
)
2016-06-02 11:05:42 +05:30
link = doc.document.create_element(
'a',
class: 'no-attachment-icon',
href: img['src'],
target: '_blank'
)
link.children = img.clone
2016-08-24 12:49:21 +05:30
div.children = link
img.replace(div)
2016-06-02 11:05:42 +05:30
end
doc
end
end
end
end