diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-10-09 15:06:13 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-10-09 15:06:13 +0300 |
commit | 0a850868dfb85086cba8320cee9dac4657dcae6c (patch) | |
tree | 40d17228fe23d9db7b861fe2a20d024d64c50323 /lib/banzai | |
parent | 3744bcc0d10d24104e39985b6833a0ec51791c0a (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/banzai')
-rw-r--r-- | lib/banzai/filter/audio_link_filter.rb | 66 | ||||
-rw-r--r-- | lib/banzai/filter/relative_link_filter.rb | 4 | ||||
-rw-r--r-- | lib/banzai/filter/wiki_link_filter.rb | 2 | ||||
-rw-r--r-- | lib/banzai/pipeline/gfm_pipeline.rb | 1 |
4 files changed, 70 insertions, 3 deletions
diff --git a/lib/banzai/filter/audio_link_filter.rb b/lib/banzai/filter/audio_link_filter.rb new file mode 100644 index 00000000000..83aa520dc4b --- /dev/null +++ b/lib/banzai/filter/audio_link_filter.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +# Generated HTML is transformed back to GFM by app/assets/javascripts/behaviors/markdown/nodes/audio.js +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 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 + + def has_audio_extension?(element) + src = element.attr('data-canonical-src').presence || element.attr('src') + + return unless src.present? + + src_ext = File.extname(src).sub('.', '').downcase + Gitlab::FileTypeDetection::SAFE_AUDIO_EXT.include?(src_ext) + end + + def audio_node(doc, element) + container = doc.document.create_element( + '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 diff --git a/lib/banzai/filter/relative_link_filter.rb b/lib/banzai/filter/relative_link_filter.rb index df181406591..c7589e69262 100644 --- a/lib/banzai/filter/relative_link_filter.rb +++ b/lib/banzai/filter/relative_link_filter.rb @@ -65,7 +65,7 @@ module Banzai el.attribute('href') end - attrs += doc.search('img, video').flat_map do |el| + attrs += doc.search('img, video, audio').flat_map do |el| [el.attribute('src'), el.attribute('data-src')] end @@ -83,7 +83,7 @@ module Banzai get_blob_types(paths).each do |name, type| if type == :blob blob = ::Blob.decorate(Gitlab::Git::Blob.new(name: name), project) - uri_types[name] = blob.image? || blob.video? ? :raw : :blob + uri_types[name] = blob.image? || blob.video? || blob.audio? ? :raw : :blob else uri_types[name] = type end diff --git a/lib/banzai/filter/wiki_link_filter.rb b/lib/banzai/filter/wiki_link_filter.rb index 18947679b69..205f777bc90 100644 --- a/lib/banzai/filter/wiki_link_filter.rb +++ b/lib/banzai/filter/wiki_link_filter.rb @@ -15,7 +15,7 @@ module Banzai doc.search('a:not(.gfm)').each { |el| process_link(el.attribute('href'), el) } - doc.search('video').each { |el| process_link(el.attribute('src'), el) } + doc.search('video, audio').each { |el| process_link(el.attribute('src'), el) } doc.search('img').each do |el| attr = el.attribute('data-src') || el.attribute('src') diff --git a/lib/banzai/pipeline/gfm_pipeline.rb b/lib/banzai/pipeline/gfm_pipeline.rb index bb0d1eaa1e1..08e27257fdf 100644 --- a/lib/banzai/pipeline/gfm_pipeline.rb +++ b/lib/banzai/pipeline/gfm_pipeline.rb @@ -26,6 +26,7 @@ module Banzai Filter::ColorFilter, Filter::MermaidFilter, Filter::VideoLinkFilter, + Filter::AudioLinkFilter, Filter::ImageLazyLoadFilter, Filter::ImageLinkFilter, Filter::InlineMetricsFilter, |