Sha256: d0535b06a1b9ef2837294069e576063945ed7fa4c86d0be2e2bf80b55b5507af
Contents?: true
Size: 1.46 KB
Versions: 1
Compression:
Stored size: 1.46 KB
Contents
# frozen_string_literal: true require_relative "anti_image_reflow/version" require "jekyll" require "nokogiri" require "fastimage" #module AntiImageReflow # class Error < StandardError; end # # Your code goes here... #end 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" size = FastImage.size(File.join(Dir.pwd, tag["src"])) 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 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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
anti_image_reflow-0.1.0 | lib/anti_image_reflow.rb |