Sha256: bcf26da308e94c99f6c3660eac52c8db68ae037b0bc832e38ce293ebc1e7014e
Contents?: true
Size: 1.58 KB
Versions: 2
Compression:
Stored size: 1.58 KB
Contents
# frozen_string_literal: true require_relative "anti_image_reflow/version" require "jekyll" require "nokogiri" require "fastimage" module Jekyll class AntiImageReflow def self.process(content) html = content.output content.output = process_tags(html) if process_tags?(html) end def self.process?(doc) (doc.is_a?(Jekyll::Page) || doc.write?) && doc.output_ext == ".html" || doc.permalink&.end_with?("/") end def self.process_tags?(html) html.include?("<img") || html.include?("<iframe") end def self.process_tags(html) content = Nokogiri.HTML(html) tags = content.css("img[src]") tags.each do |tag| # add height and width attributes if tag.name == "img" path = File.join(Dir.pwd, tag["src"]) # check if file exists if File.exist?(path) size = FastImage.size(path) tag["width"] = size[0] unless tag["width"] || size.nil? tag["height"] = size[1] unless tag["height"] || size.nil? tag["loading"] = "lazy" unless tag["loading"] end end end content.to_html end private_class_method :process_tags private_class_method :process_tags? end end Jekyll::Hooks.register [:pages, :documents], :post_render do |doc| Jekyll::AntiImageReflow.process(doc) if Jekyll::AntiImageReflow.process?(doc) end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
anti_image_reflow-0.1.2 | lib/anti_image_reflow.rb |
anti_image_reflow-0.1.1 | lib/anti_image_reflow.rb |