jekyll-srcset-hook 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 54aa1448f897d2e33593aa63b890dd225cac028cf902f95a38a1ad878ed13aa8
4
+ data.tar.gz: 3391cd5b3421fe9265278422bb30e04227bd529c315a2f99807c0421521a6570
5
+ SHA512:
6
+ metadata.gz: b91d045a1b60f0c6359456c0fd2d6ab597d80214fcab8ffc51e7b54b4f05cc12e47aa645fe7bc556a1846f498a95c876c978e0da338b14f8b89eee7a28a62010
7
+ data.tar.gz: 69b734c68d77977c841aa833de57dde0484d3ef90876e6fe2aa011c7626ce171deade56f2b4352ae877112c5201b78d0fff6aca73a5b843dc966de78dfeabe5e
data/.prettierignore ADDED
@@ -0,0 +1 @@
1
+ *.md
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## [0.1.0] - 2022-10-09
2
+
3
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at 95389546+juicy-g@users.noreply.github.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in jekyll-srcset-hook.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rubocop", "~> 1.21"
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Jocelyn Gaudette
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # Jekyll-srcset-hook
2
+
3
+ A Jekyll plugin to add srcset and sizes attributes to images without Liquid tags.
4
+
5
+ This plugin is intended to be used with media libraries in the cloud like ImageKit.
6
+
7
+ For example, it will take images in posts written in Markdown:
8
+
9
+ ```
10
+ ![alt text](https://ik.imagekit.io/your_imagekit_id/image.jpg)
11
+ ```
12
+
13
+ And change them to (based on the config in `_config.yml`):
14
+
15
+ ```
16
+ <img src="https://ik.imagekit.io/your_imagekit_id/image.jpg" alt="alt text"
17
+ srcset="https://ik.imagekit.io/your_imagekit_id/image.jpg?tr=w-300 300w,
18
+ https://ik.imagekit.io/your_imagekit_id/image.jpg?tr=w-600 600w,
19
+ https://ik.imagekit.io/your_imagekit_id/image.jpg?tr=w-900 900w"
20
+ sizes="(max-width: 300px) 100vw, (max-width: 600px) 50vw, (max-width: 900px) 33vw, 900px"
21
+ />
22
+ ```
23
+
24
+ ## Installation
25
+
26
+ Add gem 'jekyll-srcset-hook' to the `jekyll_plugin` group in your `Gemfile`:
27
+
28
+ ```
29
+ source 'https://rubygems.org'
30
+
31
+ gem 'jekyll'
32
+
33
+ group :jekyll_plugins do
34
+ gem 'jekyll-srcset-hook'
35
+ end
36
+ ```
37
+
38
+ Then run `bundle` to install the gem.
39
+
40
+ ## Configuration
41
+
42
+ In `_config.yml` add one of the configuration below. By default, `jekyll-srcset-hook` will apply to both posts and pages:
43
+
44
+ ```
45
+ jekyll-srcset-hook:
46
+ url_endpoint: https://ik.imagekit.io/your_imagekit_id/
47
+ transformations_widths: ["?tr=w-300 300w", "?tr=w-600 600w", "?tr=w-900 900w"]
48
+ sizes: "(max-width: 300px) 100vw, (max-width: 600px) 50vw, (max-width: 900px) 33vw, 900px"
49
+ ```
50
+
51
+ Individual configuration for images in posts and pages can be provided independently using the same URL endpoint:
52
+
53
+ ```
54
+ jekyll-srcset-hook:
55
+ url_endpoint: https://ik.imagekit.io/your_imagekit_id/
56
+ posts:
57
+ transformations_widths: ["?tr=w-300 300w", "?tr=w-600 600w", "?tr=w-900 900w"]
58
+ sizes: "(max-width: 300px) 100vw, (max-width: 600px) 50vw, (max-width: 900px) 33vw, 900px"
59
+ pages:
60
+ transformations_widths: ["?tr=w-480 480w", "?tr=w-800 800w"]
61
+ sizes: "(max-width: 600px) 480px, 800px"
62
+ ```
63
+
64
+ Or with separate endpoints:
65
+
66
+ ```
67
+ jekyll-srcset-hook:
68
+ posts:
69
+ url_endpoint: https://ik.imagekit.io/your_imagekit_id1/
70
+ transformations_widths: ["?tr=w-300 300w", "?tr=w-600 600w", "?tr=w-900 900w"]
71
+ sizes: "(max-width: 300px) 100vw, (max-width: 600px) 50vw, (max-width: 900px) 33vw, 900px"
72
+ pages:
73
+ url_endpoint: https://ik.imagekit.io/your_imagekit_id2/
74
+ transformations_widths: ["?tr=w-480 480w", "?tr=w-800 800w"]
75
+ sizes: "(max-width: 600px) 480px, 800px"
76
+ ```
77
+
78
+ ## Usage
79
+
80
+ `jekyll-srcset-hook` will not modify images with the `src` attribute which do not begin with the `url_endpoint` value so
81
+ it can be used with local images or images from other sources.
82
+
83
+ ## Contributing
84
+
85
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jekyll-srcset-hook. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/jekyll-srcset-hook/blob/main/CODE_OF_CONDUCT.md).
86
+
87
+ ## License
88
+
89
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rubocop/rake_task"
5
+
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: :rubocop
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module SrcsetHook
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "nokogiri"
4
+
5
+ module JekyllSrcsetHook
6
+ @@config = {}
7
+
8
+ def set_config
9
+ proc do |site|
10
+ @@config = site.config["jekyll-srcset-hook"]
11
+
12
+ if @@config.nil?
13
+ Jekyll.logger.warn(
14
+ "WARNING: jekyll-srcset-hook configuration is not present in _config.yml",
15
+ )
16
+ next
17
+ end
18
+
19
+ unless @@config["transformations_widths"].nil? && @@config["sizes"].nil?
20
+ Jekyll::Hooks.register(:posts, :post_render, &modify_post_output)
21
+ Jekyll::Hooks.register(:pages, :post_render, &modify_page_output)
22
+ end
23
+
24
+ unless @@config["posts"].nil?
25
+ Jekyll::Hooks.register(:posts, :post_render, &modify_post_output)
26
+ end
27
+
28
+ unless @@config["pages"].nil?
29
+ Jekyll::Hooks.register(:pages, :post_render, &modify_page_output)
30
+ end
31
+ end
32
+ end
33
+
34
+ def modify_post_output
35
+ proc do |post|
36
+ # posts are fragments (i.e. no DOCTYPE declaration/<html>...</html>)
37
+ fragment = Nokogiri::HTML5.fragment(post.content)
38
+ fragment
39
+ .xpath(".//img")
40
+ .each do |image|
41
+ if image[:src].start_with?(
42
+ @@config["url_endpoint"] || @@config["posts"]["url_endpoint"],
43
+ )
44
+ original_image = image
45
+ original_image = original_image.to_s.gsub!(">", " />")
46
+
47
+ image =
48
+ process_image(
49
+ image,
50
+ @@config["transformations_widths"] ||
51
+ @@config["posts"]["transformations_widths"],
52
+ @@config["sizes"] || @@config["posts"]["sizes"],
53
+ )
54
+
55
+ post.output.gsub!(original_image, image)
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ def modify_page_output
62
+ proc do |page|
63
+ doc = Nokogiri.HTML5(page.content)
64
+ elements = doc.xpath(".//body//img")
65
+ # for some reason, need to remove last element as it is empty
66
+ elements.pop
67
+
68
+ elements.each do |image|
69
+ if image[:src].start_with?(
70
+ @@config["url_endpoint"] || @@config["pages"]["url_endpoint"],
71
+ )
72
+ original_image = image
73
+ original_image = original_image.to_s.gsub!(">", " />")
74
+
75
+ image =
76
+ process_image(
77
+ image,
78
+ @@config["transformations_widths"] ||
79
+ @@config["pages"]["transformations_widths"],
80
+ @@config["sizes"] || @@config["pages"]["sizes"],
81
+ )
82
+
83
+ page.output.gsub!(original_image, image)
84
+ end
85
+ end
86
+ end
87
+ end
88
+
89
+ def process_image(image, transformations, sizes)
90
+ srcset_value = ""
91
+ transformations.each { |t_w| srcset_value += "#{image[:src]}#{t_w}, " }
92
+ # remove the extra ", " at the end of srcset_value
93
+ srcset_value.chomp!(", ")
94
+ # add the attributes to the image
95
+ image[:srcset] = srcset_value
96
+ image[:sizes] = sizes
97
+ image.to_s.gsub!(">", " />")
98
+ end
99
+
100
+ module_function :set_config
101
+ module_function :modify_post_output
102
+ module_function :modify_page_output
103
+ module_function :process_image
104
+
105
+ Jekyll::Hooks.register(:site, :after_init, &set_config)
106
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-srcset-hook
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jocelyn Gaudette
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-10-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.13.8
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.13'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.13.8
33
+ description:
34
+ email:
35
+ - 95389546+juicy-g@users.noreply.github.com
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - ".prettierignore"
41
+ - ".rubocop.yml"
42
+ - CHANGELOG.md
43
+ - CODE_OF_CONDUCT.md
44
+ - Gemfile
45
+ - LICENSE
46
+ - README.md
47
+ - Rakefile
48
+ - lib/jekyll-srcset-hook.rb
49
+ - lib/jekyll-srcset-hook/version.rb
50
+ homepage: https://github.com/juicy-g/jekyll-srcset-hook
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ homepage_uri: https://github.com/juicy-g/jekyll-srcset-hook
55
+ source_code_uri: https://github.com/juicy-g/jekyll-srcset-hook
56
+ changelog_uri: https://github.com/juicy-g/jekyll-srcset-hook/blob/main/CHANGELOG.md
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 2.6.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.3.5
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: A Jekyll plugin to add srcset and sizes attributes to images without Liquid
76
+ tags.
77
+ test_files: []