2018-07-23 09:32:32 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-23 11:21:12 +00:00
|
|
|
# Generated HTML is transformed back to GFM by app/assets/javascripts/behaviors/markdown/nodes/video.js
|
2016-04-03 05:00:06 +00:00
|
|
|
module Banzai
|
|
|
|
module Filter
|
2016-07-12 17:28:39 +00:00
|
|
|
# Find every image that isn't already wrapped in an `a` tag, and that has
|
|
|
|
# a `src` attribute ending with a video extension, add a new video node and
|
|
|
|
# a "Download" link in the case the video cannot be played.
|
2016-04-03 05:00:06 +00:00
|
|
|
class VideoLinkFilter < HTML::Pipeline::Filter
|
|
|
|
def call
|
2019-10-03 18:06:11 +00:00
|
|
|
doc.xpath('descendant-or-self::img[not(ancestor::a)]').each do |el|
|
|
|
|
el.replace(video_node(doc, el)) if has_video_extension?(el)
|
2016-04-03 05:00:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
doc
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-10-03 18:06:11 +00:00
|
|
|
def has_video_extension?(element)
|
|
|
|
src = element.attr('data-canonical-src').presence || element.attr('src')
|
2016-07-12 17:31:20 +00:00
|
|
|
|
2019-10-03 18:06:11 +00:00
|
|
|
return unless src.present?
|
2019-10-01 12:05:59 +00:00
|
|
|
|
2019-10-03 18:06:11 +00:00
|
|
|
src_ext = File.extname(src).sub('.', '').downcase
|
|
|
|
Gitlab::FileTypeDetection::SAFE_VIDEO_EXT.include?(src_ext)
|
2016-04-03 05:00:06 +00:00
|
|
|
end
|
|
|
|
|
2016-07-08 16:55:49 +00:00
|
|
|
def video_node(doc, element)
|
2016-07-12 17:31:20 +00:00
|
|
|
container = doc.document.create_element(
|
|
|
|
'div',
|
|
|
|
class: 'video-container'
|
|
|
|
)
|
|
|
|
|
|
|
|
video = doc.document.create_element(
|
2016-07-08 16:55:49 +00:00
|
|
|
'video',
|
2016-07-12 17:31:20 +00:00
|
|
|
src: element['src'],
|
2019-09-30 18:06:34 +00:00
|
|
|
width: '100%',
|
2016-07-12 17:31:20 +00:00
|
|
|
controls: true,
|
2017-01-16 20:27:05 +00:00
|
|
|
'data-setup' => '{}',
|
|
|
|
'data-title' => element['title'] || element['alt'])
|
2016-07-12 17:31:20 +00:00
|
|
|
|
|
|
|
link = doc.document.create_element(
|
|
|
|
'a',
|
|
|
|
element['title'] || element['alt'],
|
|
|
|
href: element['src'],
|
|
|
|
target: '_blank',
|
2017-03-15 21:21:48 +00:00
|
|
|
rel: 'noopener noreferrer',
|
2016-07-12 17:28:39 +00:00
|
|
|
title: "Download '#{element['title'] || element['alt']}'")
|
2019-02-20 23:51:55 +00:00
|
|
|
|
|
|
|
# make sure the original non-proxied src carries over
|
|
|
|
if element['data-canonical-src']
|
|
|
|
video['data-canonical-src'] = element['data-canonical-src']
|
|
|
|
link['data-canonical-src'] = element['data-canonical-src']
|
|
|
|
end
|
|
|
|
|
2016-07-12 17:31:20 +00:00
|
|
|
download_paragraph = doc.document.create_element('p')
|
|
|
|
download_paragraph.children = link
|
|
|
|
|
|
|
|
container.add_child(video)
|
|
|
|
container.add_child(download_paragraph)
|
|
|
|
|
|
|
|
container
|
2016-04-03 05:00:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|