Is there a way to intercept the generating of Jekyll via Jekyll::Generate and add the lazing loading element to all IMGs?
I've got this but it seems the variable that holds the content at this point is frozen (Error: can't modify frozen string)
module Jekyll
class LazyLoadImagesGenerator < Jekyll::Generator
def generate(site)
site.pages.each do |page|
page.content = lazy_load_images(page.content)
end
site.posts.docs.each do |post|
next if post.data["layout"] == "none" # Skip posts with "none" layout
post.content = lazy_load_images(post.content)
end
end
def lazy_load_images(content)
content.gsub!(/(<img[^>]+)(?<!loading="lazy")>/i, '\1 loading="lazy">')
end
end
end