Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop to edit attribute of the <img> on inner of <pre> or <code> or <blockquote> #1

Merged
merged 3 commits into from Nov 29, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -4,5 +4,5 @@ Feature: Set loading="auto"
When I cd to "build"
Then the file "index.html" should contain:
"""
<img loading="auto"
loading="auto"
"""
@@ -4,5 +4,5 @@ Feature: Set loading="eager"
When I cd to "build"
Then the file "index.html" should contain:
"""
<img loading="eager" src="/images/1x1.png"
loading="eager"
"""
@@ -4,5 +4,5 @@ Feature: Set loading="lazy" with asset_hash
When I cd to "build"
Then the file "index.html" should contain:
"""
<img loading="lazy" src="/images/1x1-0ec63b14.png"
loading="lazy"
"""
@@ -0,0 +1,8 @@
Feature: Didn't Set loading="lazy"
Scenario: Should add 'pre'
Given a successfully built app at "pre-app"
When I cd to "build"
Then the file "index.html" should not contain:
"""
loading="lazy"
"""
@@ -0,0 +1,4 @@
activate :asset_hash
activate :img_loading_attribute do |c|
c.loading = 'lazy'
end
Binary file not shown.
@@ -0,0 +1,3 @@
<pre>
<%= image_tag '1x1.png' %>
</pre>
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Test App</title>
</head>
<body>
<%= yield %>
</body>
</html>
@@ -1,5 +1,6 @@
# Require core library
require 'middleman-core'
require 'nokogiri'

class Middleman::ImgLoadingAttribute < ::Middleman::Extension
option :loading, 'auto', 'A value of "loading" attribute in <img> tag'
@@ -22,10 +23,14 @@ def after_configuration
def after_build(builder)
files = Dir.glob(File.join(app.config[:build_dir], "**", "*.html"))
files.each do |file|
contents = File.read(file)
replaced = contents.gsub(%r[<img], "<img loading=\"#{options[:loading]}\"")
doc = Nokogiri::HTML(File.read(file))
doc.css('img').each do |elem|
next if elem.path.include?('pre') || elem.path.include?('code') || elem.path.include?('blockquote')

elem['loading'] = options[:loading]
end
File.open(file, 'w') do |f|
f.write replaced
f.write doc.to_html
end
end
end
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
# The version of middleman-core your extension depends on
s.add_runtime_dependency("middleman-core")
s.add_runtime_dependency("middleman-cli")
s.add_runtime_dependency("nokogiri")

# Additional dependencies
# s.add_runtime_dependency("gem-name", "gem-version")
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.