Commit f332ac13 authored by Ash McKenzie's avatar Ash McKenzie

Merge branch '33603-ruby-part-refactor-playable-link-filters' into 'master'

Part of #33603, Tech Debt - Combine Code For Embedded Video and Audio, Ruby Part

See merge request gitlab-org/gitlab!18366
parents 78700ef0 1042c61a
...@@ -3,63 +3,15 @@ ...@@ -3,63 +3,15 @@
# Generated HTML is transformed back to GFM by app/assets/javascripts/behaviors/markdown/nodes/audio.js # Generated HTML is transformed back to GFM by app/assets/javascripts/behaviors/markdown/nodes/audio.js
module Banzai module Banzai
module Filter module Filter
# Find every image that isn't already wrapped in an `a` tag, and that has class AudioLinkFilter < PlayableLinkFilter
# a `src` attribute ending with an audio extension, add a new audio node and
# a "Download" link in the case the audio cannot be played.
class AudioLinkFilter < HTML::Pipeline::Filter
def call
doc.xpath('descendant-or-self::img[not(ancestor::a)]').each do |el|
el.replace(audio_node(doc, el)) if has_audio_extension?(el)
end
doc
end
private private
def has_audio_extension?(element) def media_type
src = element.attr('data-canonical-src').presence || element.attr('src') "audio"
return unless src.present?
src_ext = File.extname(src).sub('.', '').downcase
Gitlab::FileTypeDetection::SAFE_AUDIO_EXT.include?(src_ext)
end end
def audio_node(doc, element) def safe_media_ext
container = doc.document.create_element( Gitlab::FileTypeDetection::SAFE_AUDIO_EXT
'div',
class: 'audio-container'
)
audio = doc.document.create_element(
'audio',
src: element['src'],
controls: true,
'data-setup' => '{}',
'data-title' => element['title'] || element['alt'])
link = doc.document.create_element(
'a',
element['title'] || element['alt'],
href: element['src'],
target: '_blank',
rel: 'noopener noreferrer',
title: "Download '#{element['title'] || element['alt']}'")
# make sure the original non-proxied src carries over
if element['data-canonical-src']
audio['data-canonical-src'] = element['data-canonical-src']
link['data-canonical-src'] = element['data-canonical-src']
end
download_paragraph = doc.document.create_element('p')
download_paragraph.children = link
container.add_child(audio)
container.add_child(download_paragraph)
container
end end
end end
end end
......
# frozen_string_literal: true
module Banzai
module Filter
# Find every image that isn't already wrapped in an `a` tag, and that has
# a `src` attribute ending with an audio or video extension, add a new audio or video node and
# a "Download" link in the case the media cannot be played.
class PlayableLinkFilter < HTML::Pipeline::Filter
def call
doc.xpath('descendant-or-self::img[not(ancestor::a)]').each do |el|
el.replace(media_node(doc, el)) if has_media_extension?(el)
end
doc
end
private
def media_type
raise NotImplementedError
end
def safe_media_ext
raise NotImplementedError
end
def extra_element_attrs
{}
end
def has_media_extension?(element)
src = element.attr('data-canonical-src').presence || element.attr('src')
return unless src.present?
src_ext = File.extname(src).sub('.', '').downcase
safe_media_ext.include?(src_ext)
end
def media_element(doc, element)
media_element_attrs = {
src: element['src'],
controls: true,
'data-setup': '{}',
'data-title': element['title'] || element['alt']
}.merge!(extra_element_attrs)
if element['data-canonical-src']
media_element_attrs['data-canonical-src'] = element['data-canonical-src']
end
doc.document.create_element(media_type, media_element_attrs)
end
def download_paragraph(doc, element)
link_content = element['title'] || element['alt']
link_element_attrs = {
href: element['src'],
target: '_blank',
rel: 'noopener noreferrer',
title: "Download '#{link_content}'"
}
# make sure the original non-proxied src carries over
if element['data-canonical-src']
link_element_attrs['data-canonical-src'] = element['data-canonical-src']
end
link = doc.document.create_element('a', link_content, link_element_attrs)
doc.document.create_element('p').tap do |paragraph|
paragraph.children = link
end
end
def media_node(doc, element)
container_element_attrs = { class: "#{media_type}-container" }
doc.document.create_element( "div", container_element_attrs).tap do |container|
container.add_child(media_element(doc, element))
container.add_child(download_paragraph(doc, element))
end
end
end
end
end
...@@ -3,64 +3,19 @@ ...@@ -3,64 +3,19 @@
# Generated HTML is transformed back to GFM by app/assets/javascripts/behaviors/markdown/nodes/video.js # Generated HTML is transformed back to GFM by app/assets/javascripts/behaviors/markdown/nodes/video.js
module Banzai module Banzai
module Filter module Filter
# Find every image that isn't already wrapped in an `a` tag, and that has class VideoLinkFilter < PlayableLinkFilter
# 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.
class VideoLinkFilter < HTML::Pipeline::Filter
def call
doc.xpath('descendant-or-self::img[not(ancestor::a)]').each do |el|
el.replace(video_node(doc, el)) if has_video_extension?(el)
end
doc
end
private private
def has_video_extension?(element) def media_type
src = element.attr('data-canonical-src').presence || element.attr('src') "video"
return unless src.present?
src_ext = File.extname(src).sub('.', '').downcase
Gitlab::FileTypeDetection::SAFE_VIDEO_EXT.include?(src_ext)
end end
def video_node(doc, element) def safe_media_ext
container = doc.document.create_element( Gitlab::FileTypeDetection::SAFE_VIDEO_EXT
'div',
class: 'video-container'
)
video = doc.document.create_element(
'video',
src: element['src'],
width: '100%',
controls: true,
'data-setup' => '{}',
'data-title' => element['title'] || element['alt'])
link = doc.document.create_element(
'a',
element['title'] || element['alt'],
href: element['src'],
target: '_blank',
rel: 'noopener noreferrer',
title: "Download '#{element['title'] || element['alt']}'")
# 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 end
download_paragraph = doc.document.create_element('p') def extra_element_attrs
download_paragraph.children = link { width: "100%" }
container.add_child(video)
container.add_child(download_paragraph)
container
end end
end end
end end
......
...@@ -32,6 +32,7 @@ describe Banzai::Filter::VideoLinkFilter do ...@@ -32,6 +32,7 @@ describe Banzai::Filter::VideoLinkFilter do
expect(video.name).to eq 'video' expect(video.name).to eq 'video'
expect(video['src']).to eq src expect(video['src']).to eq src
expect(video['width']).to eq "100%"
expect(paragraph.name).to eq 'p' expect(paragraph.name).to eq 'p'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment