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

34 lines
1.1 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2019-03-02 22:35:43 +05:30
# Generated HTML is transformed back to GFM by app/assets/javascripts/behaviors/markdown/nodes/image.js
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|
link = doc.document.create_element(
'a',
class: 'no-attachment-icon',
2017-09-10 17:25:29 +05:30
href: img['data-src'] || img['src'],
2017-08-17 22:00:37 +05:30
target: '_blank',
rel: 'noopener noreferrer'
2016-06-02 11:05:42 +05:30
)
2019-09-04 21:01:54 +05:30
# make sure the original non-proxied src carries over to the link
link['data-canonical-src'] = img['data-canonical-src'] if img['data-canonical-src']
2016-06-02 11:05:42 +05:30
link.children = img.clone
2016-08-24 12:49:21 +05:30
2017-08-17 22:00:37 +05:30
img.replace(link)
2016-06-02 11:05:42 +05:30
end
doc
end
end
end
end