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

21 lines
591 B
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
2017-09-10 17:25:29 +05:30
module Banzai
module Filter
2018-03-17 18:26:18 +05:30
# HTML filter that moves the value of image `src` attributes to `data-src`
# so they can be lazy loaded.
2017-09-10 17:25:29 +05:30
class ImageLazyLoadFilter < HTML::Pipeline::Filter
def call
doc.xpath('descendant-or-self::img').each do |img|
2018-11-18 11:00:15 +05:30
img.add_class('lazy')
2017-09-10 17:25:29 +05:30
img['data-src'] = img['src']
2018-03-17 18:26:18 +05:30
img['src'] = LazyImageTagHelper.placeholder_image
2017-09-10 17:25:29 +05:30
end
2018-03-17 18:26:18 +05:30
2017-09-10 17:25:29 +05:30
doc
end
end
end
end