jekyll-firstimage 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +5 -0
- data/LICENSE +21 -0
- data/README.md +10 -0
- data/RELEASES.md +5 -0
- data/Rakefile +4 -0
- data/lib/jekyll-firstimage.rb +3 -0
- data/lib/jekyll/firstimage.rb +39 -0
- data/lib/jekyll/firstimage/version.rb +7 -0
- metadata +124 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1b8b44d79ecf3da40ec89bcc135cc45015731265e78f8d85a56b2ba8c3a3aeb2
|
4
|
+
data.tar.gz: b7cb84d521e5f75069404a8be39a192ac446b93223e77df770b276afbb892545
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 436875097d6001474d8655d22ce6c162a0eff203b29e3bc4cecc543ca24871d1abd745b287bf55a80b1fd8c7bcec48b56203102cfda4b4f8d3df21da18fb0ee8
|
7
|
+
data.tar.gz: b1e8c7296fdf3ae418bb92733ff3d546eefdd929bcf6607f6a4f644ab71f84ecceabfba624bb932a9b0d020e165d469c711b99e4d0c751cb09b45664270d9524
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2018 Nicolas Hoizey
|
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,10 @@
|
|
1
|
+
# jekyll-firstimage
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/jekyll-firstimage.svg)](https://badge.fury.io/rb/jekyll-firstimage)
|
4
|
+
[![Gem Downloads](https://img.shields.io/gem/dt/jekyll-firstimage.svg?style=flat)](http://rubygems.org/gems/jekyll-firstimage)
|
5
|
+
|
6
|
+
`jekyll-firstimage` is a [Jekyll](http://jekyllrb.com/) plugin that adds a Liquid filter finding the first image in a HTML content string, including responsive images srcset
|
7
|
+
|
8
|
+
## Contributing
|
9
|
+
|
10
|
+
Thanks for your interest in contributing! There are many ways to contribute to this project. [Get started here](https://github.com/nhoizey/jekyll-firstimage/blob/master/CONTRIBUTING.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'nokogiri'
|
4
|
+
|
5
|
+
module Jekyll
|
6
|
+
module FirstImageFilter
|
7
|
+
|
8
|
+
# https://gist.github.com/nhoizey/224a1c9dfb396a4c7b41ea114f175712#gistcomment-2637934
|
9
|
+
def extract_largest_image(srcset)
|
10
|
+
srcset
|
11
|
+
.scan(/(\S+)\s+(\d+)w/)
|
12
|
+
.map { |url, size| { url: url.strip, size: size.to_i } }
|
13
|
+
.inject { |acc, cur| acc[:size] < cur[:size] ? cur : acc }[:url]
|
14
|
+
end
|
15
|
+
|
16
|
+
def first_image(content)
|
17
|
+
return '' if content.nil?
|
18
|
+
|
19
|
+
doc = Nokogiri::HTML.fragment(content.encode('UTF-8', :invalid => :replace, :undef => :replace, :replace => ''))
|
20
|
+
|
21
|
+
image_elements = doc.css('img')
|
22
|
+
|
23
|
+
return '' if image_elements.empty?
|
24
|
+
|
25
|
+
image = image_elements.first.to_h
|
26
|
+
|
27
|
+
src = ''
|
28
|
+
if image['srcset'] then
|
29
|
+
src = extract_largest_image(image['srcset'])
|
30
|
+
elsif image['src'] then
|
31
|
+
src = image['src']
|
32
|
+
end
|
33
|
+
|
34
|
+
src
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
Liquid::Template.register_filter(Jekyll::FirstImageFilter)
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jekyll-firstimage
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nicolas Hoizey
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-07-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: jekyll
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.6'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: nokogiri
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.8'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.8'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.16'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.16'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '12.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '12.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.55.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.55.0
|
83
|
+
description: " A Jekyll plugin that adds a Liquid filter finding the first image
|
84
|
+
in a HTML content string, including responsive images srcset\n"
|
85
|
+
email:
|
86
|
+
- nicolas@hoizey.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE
|
93
|
+
- README.md
|
94
|
+
- RELEASES.md
|
95
|
+
- Rakefile
|
96
|
+
- lib/jekyll-firstimage.rb
|
97
|
+
- lib/jekyll/firstimage.rb
|
98
|
+
- lib/jekyll/firstimage/version.rb
|
99
|
+
homepage: https://nhoizey.github.io/jekyll-firstimage/
|
100
|
+
licenses:
|
101
|
+
- MIT
|
102
|
+
metadata: {}
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
requirements: []
|
118
|
+
rubyforge_project:
|
119
|
+
rubygems_version: 2.7.4
|
120
|
+
signing_key:
|
121
|
+
specification_version: 4
|
122
|
+
summary: A Jekyll plugin that adds a Liquid filter finding the first image in a HTML
|
123
|
+
content string, including responsive images srcset
|
124
|
+
test_files: []
|