Sha256: 20cba09b821d755f55cf6cc41673b77084701c4e17ab883d4ad298418be7037b
Contents?: true
Size: 1.63 KB
Versions: 1
Compression:
Stored size: 1.63 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" # get path relative to root path = File.join(Dir.pwd, tag["src"]) # skip if directory or invalid path next if File.directory?(path) || !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 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.9 | lib/anti_image_reflow.rb |