middleman-meta-tags 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: fe44adf5d0dd9f8adbca8e20085e5e426c50c824
4
- data.tar.gz: ecee9516396c7bc7b71355eb019f13411bd1a683
2
+ SHA256:
3
+ metadata.gz: 59169e69789518c47ce31641beab6af4880726df72a26ab5941b254a7b5dca37
4
+ data.tar.gz: 74d48cfd537a22182d15698f131e56a9b12f796ac6d3d7f3fe9f9154b60c0980
5
5
  SHA512:
6
- metadata.gz: 3277ee46a35b74a6ef1734e25ce839ee3e3faf919e542f71d29c27265d0c02f9eb90b4a1ee99386f3ffc45087aa728cab93cf9f569de633a0b7b80b4c8cd7897
7
- data.tar.gz: 2dfd69b8e6f6aa83343c994fe2b14914f6dd4d48d2f61579e0559a2315122c516245899de185f709ebd94e74a5c85baf87ad8d2c48eccf1ea4b1c92b6aa98555
6
+ metadata.gz: 20ffc6fcaa9b666ab2c85afd5f2ce0d30dc95bc32fae5fc3ccfe957239d239fc8ee9f91d160931aac9d4fef1937f8200d877eaa5e616f3cebeab75fff23d37c1
7
+ data.tar.gz: 6695dd19fd1cc06da7da79831d90147d64441e30c54b534d1e04898f20bf538d087f15c5c6f884f66433d549bde7d89c06b47279fc68e5675413871764bb7207
@@ -0,0 +1,23 @@
1
+ name: Ruby
2
+
3
+ on:
4
+ push:
5
+ branches: [ master ]
6
+ pull_request:
7
+ branches: [ master ]
8
+
9
+ jobs:
10
+ test:
11
+
12
+ runs-on: ubuntu-latest
13
+
14
+ steps:
15
+ - uses: actions/checkout@v2
16
+ - name: Set up Ruby
17
+ uses: ruby/setup-ruby@v1
18
+ with:
19
+ ruby-version: 2.6
20
+ - name: Install dependencies
21
+ run: bundle install
22
+ - name: Run tests
23
+ run: bundle exec rspec
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .idea
@@ -1,4 +1,8 @@
1
- ## Next release
1
+ ## 0.5.0 (08/01/20)
2
+
3
+ * Description truncate to 220 chars
4
+ * Add test
5
+ * Add more tags
2
6
 
3
7
  ## 0.4.0 (03/28/18)
4
8
 
data/README.md CHANGED
@@ -143,3 +143,8 @@ pull_image 'page/to/image/jpg'
143
143
  3. Commit your changes (`git commit -am 'Add some feature'`)
144
144
  4. Push to the branch (`git push origin my-new-feature`)
145
145
  5. Create new Pull Request
146
+
147
+ ## Publishing
148
+
149
+ 1. `gem build middleman-meta-tags.gemspec`
150
+ 2. `bundle exec rake release`
@@ -1,3 +1,4 @@
1
+ require 'middleman-core'
1
2
  require 'middleman-meta-tags/helpers'
2
3
 
3
4
  module Middleman
@@ -5,46 +5,89 @@ module Middleman
5
5
  @meta_tags ||= ActiveSupport::HashWithIndifferentAccess.new
6
6
  end
7
7
 
8
+ def link_tags
9
+ @link_tags ||= ActiveSupport::HashWithIndifferentAccess.new
10
+ end
11
+
8
12
  def set_meta_tags(meta_tags = {})
9
13
  self.meta_tags.merge! meta_tags
10
14
  end
11
15
 
12
- def description(description = nil)
13
- set_meta_tags(description: description) unless description.nil?
16
+ def set_link_tags(link_tags = {})
17
+ self.link_tags.merge! link_tags
14
18
  end
15
19
 
16
- def keywords(keywords = nil)
17
- set_meta_tags(keywords: keywords) unless keywords.nil?
20
+ def site_data
21
+ (data['site'] || {}).with_indifferent_access
18
22
  end
19
23
 
20
24
  def title(title = nil)
21
25
  set_meta_tags(title: title) unless title.nil?
22
26
  end
23
27
 
28
+ def description(description = nil)
29
+ set_meta_tags(description: description) unless description.nil?
30
+ end
31
+
32
+ def keywords(keywords = nil)
33
+ set_meta_tags(keywords: keywords) unless keywords.nil?
34
+ end
35
+
24
36
  def display_meta_tags(default = {})
25
37
  result = []
26
38
  meta_tags = default.merge(self.meta_tags).with_indifferent_access
39
+ link_tags = default.merge(self.link_tags)
40
+
41
+ charset = meta_tags.delete(:charset)
42
+ result << tag(:meta, charset: charset) if charset.present?
43
+
44
+ viewport = meta_tags.delete(:viewport)
45
+ result << tag(:meta, name: :viewport, content: viewport) if viewport.present?
46
+
47
+ http_equiv = meta_tags.delete('http-equiv')
48
+ result << tag(:meta, 'http-equiv': 'X-UA-Compatible', content: http_equiv) if http_equiv.present?
27
49
 
28
- title = full_title(meta_tags)
50
+ need_full_title = true
51
+ need_full_title = meta_tags['full_title'] unless meta_tags['full_title'].nil?
52
+ need_full_title = current_page.data['full_title'] unless current_page.data['full_title'].nil?
53
+ title = need_full_title ? full_title(meta_tags) : safe_title(meta_tags[:title])
29
54
  meta_tags.delete(:title)
30
55
  meta_tags.delete(:separator)
31
- result << content_tag(:title, title) unless title.blank?
56
+ result << content_tag(:title, title) if title.present?
32
57
 
33
58
  description = safe_description(meta_tags.delete(:description))
34
- result << tag(:meta, name: :description, content: description) unless description.blank?
59
+ result << tag(:meta, name: :description, content: description) if description.present?
35
60
 
36
61
  keywords = meta_tags.delete(:keywords)
37
62
  keywords = keywords.join(', ') if keywords.is_a?(Array)
38
- result << tag(:meta, name: :keywords, content: keywords) unless keywords.blank?
63
+ result << tag(:meta, name: :keywords, content: keywords) if keywords.present?
39
64
 
40
65
  refresh = meta_tags.delete(:refresh)
41
- result << tag(:meta, { :content => refresh, :"http-equiv" => "refresh" }) unless refresh.blank?
66
+ result << tag(:meta, {content: refresh, 'http-equiv': 'refresh'}) if refresh.present?
42
67
 
43
68
  meta_tags.each do |name, content|
44
- if name.start_with?('og:')
45
- result << tag(:meta, property: name, content: content ) unless content.blank?
69
+ unless content.blank? || %(site latitude longitude).include?(name)
70
+ if name.start_with?('itemprop:')
71
+ result << tag(:meta, itemprop: name.gsub('itemprop:', ''), content: content)
72
+ elsif name.start_with?('og:')
73
+ result << tag(:meta, property: name, content: content)
74
+ else
75
+ result << tag(:meta, name: name, content: content)
76
+ end
77
+ end
78
+ end
79
+
80
+ position = [meta_tags.delete(:latitude), meta_tags.delete(:longitude)] if meta_tags['latitude'] && meta_tags['longitude']
81
+ result << tag(:meta, name: 'geo:position', content: position.join(';')) if position
82
+ result << tag(:meta, name: 'ICBM', content: position.join(', ')) if position
83
+
84
+ link_tags.each do |rel, href|
85
+ if href.kind_of?(Array)
86
+ href.each do |link|
87
+ result << tag(:link, rel: rel, href: link)
88
+ end
46
89
  else
47
- result << tag(:meta, name: name, content: content ) unless content.blank?
90
+ result << tag(:link, rel: rel, href: href) unless href.blank?
48
91
  end
49
92
  end
50
93
 
@@ -53,110 +96,149 @@ module Middleman
53
96
  end
54
97
 
55
98
  def auto_display_meta_tags(default = {})
56
- auto_tag
99
+ auto_set_meta_tags
57
100
 
58
101
  display_meta_tags default
59
102
  end
60
103
 
61
- def auto_tag
62
- site_data = (data['site'] || {}).with_indifferent_access
104
+ def auto_set_meta_tags
105
+ geocoding_data = site_data['geocoding'] || {}
106
+ author_data = site_data['author'] || {}
63
107
 
64
- set_meta_tags site: site_data['site']
65
- set_meta_tags 'og:site_name' => site_data['site']
108
+ set_meta_tags charset: 'utf-8',
109
+ viewport: 'width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no',
110
+ 'http-equiv': 'IE=edge,chrome=1'
66
111
 
112
+ fall_through(site_data, :site, 'name')
113
+ fall_through(site_data, :site, 'site')
67
114
  fall_through(site_data, :title, 'title')
68
115
  fall_through(site_data, :description, 'description')
69
116
  fall_through(site_data, :keywords, 'keywords')
70
117
 
118
+ # Microdata
119
+ fall_through(site_data, 'itemprop:name', 'name')
120
+ fall_through(site_data, 'itemprop:name', 'site')
121
+ fall_through(site_data, 'itemprop:name', 'title')
122
+ fall_through(site_data, 'itemprop:description', 'description')
123
+ fall_through_image(site_data, 'itemprop:image', 'thumbnail')
124
+
71
125
  # Twitter cards
72
- fall_through(site_data, 'twitter:card', 'twitter_card', 'summary_large_image')
73
- fall_through(site_data, 'twitter:creator', 'twitter_author')
74
- fall_through(site_data, 'twitter:description', 'description')
75
- fall_through_image(site_data, 'twitter:image:src', 'pull_image')
76
- fall_through(site_data, 'twitter:site', 'publisher_twitter')
77
- fall_through(site_data, 'twitter:title', 'title')
126
+ set_meta_tags 'twitter:card': 'summary_large_image'
127
+ fall_through(site_data, 'twitter:card', 'twitter_card')
128
+ fall_through(site_data, 'twitter:site', 'name')
129
+ fall_through(site_data, 'twitter:site', 'site')
130
+ fall_through(author_data, 'twitter:creator', 'twitter')
131
+ set_meta_tags 'twitter:url': current_page_url
132
+ fall_through(site_data, 'twitter:url', 'url')
133
+ fall_through(site_data, 'twitter:title', 'name')
134
+ fall_through(site_data, 'twitter:title', 'site')
135
+ fall_through(site_data, 'twitter:title', 'title')
136
+ fall_through(site_data, 'twitter:description', 'description')
137
+ fall_through_image(site_data, 'twitter:image', 'thumbnail')
78
138
 
79
139
  # Open Graph
80
- fall_through(site_data, 'og:description', 'description')
81
- fall_through_image(site_data, 'og:image', 'pull_image')
82
- fall_through(site_data, 'og:title', 'title')
140
+ set_meta_tags 'og:url': current_page_url
141
+ fall_through(site_data, 'og:url', 'url')
142
+ set_meta_tags 'og:type': 'website'
143
+ fall_through(site_data, 'og:type', 'type')
144
+ fall_through(site_data, 'og:title', 'name')
145
+ fall_through(site_data, 'og:title', 'site')
146
+ fall_through(site_data, 'og:title', 'title')
147
+ fall_through_image(site_data, 'og:image', 'thumbnail')
148
+ fall_through(site_data, 'og:description', 'description')
149
+ fall_through(site_data, 'og:site_name', 'name')
150
+ fall_through(site_data, 'og:site_name', 'site')
151
+ set_meta_tags 'og:locale': 'en_US'
152
+ fall_through(site_data, 'og:locale', 'locale')
153
+
154
+ # Theme color
155
+ fall_through(site_data, 'theme-color', 'base_color')
156
+ fall_through(site_data, 'msapplication-TileColor', 'base_color')
157
+
158
+ # Geocoding
159
+ fall_through(geocoding_data, 'latitude', 'latitude')
160
+ fall_through(geocoding_data, 'longitude', 'longitude')
161
+ fall_through(geocoding_data, 'geo.placename', 'place')
162
+ fall_through(geocoding_data, 'geo.region', 'region')
163
+
164
+ # Author
165
+ set_link_tags author: author_data['website'] if author_data['website']
166
+ set_link_tags license: site_data['license'] if site_data['license']
167
+
168
+ me_link_tags = []
169
+ me_link_tags << author_data['website'] if author_data['website']
170
+ me_link_tags << "mailto:#{author_data['email']}" if author_data['email'].present?
171
+ me_link_tags += ["tel:#{author_data['phone']}", "sms:#{author_data['phone']}"] if author_data['phone'].present?
172
+ %w(github twitter dribbble medium linkedin facebook instagram gitlab bitbucket).each do |social|
173
+ me_link_tags << "https://#{social}#{['bitbucket'].include?(social) ? '.org' : '.com'}/#{'in/' if social == 'linkedin'}#{author_data[social]}" if author_data[social].present?
174
+ set_link_tags me: me_link_tags
175
+ end
83
176
  end
84
177
 
85
- private
178
+ private
86
179
 
87
180
  def fall_through(site_data, name, key, default = nil)
88
- need_customized = site_data[:customize_by_frontmatter]
89
- value = self.meta_tags[key] ||
90
- (need_customized && current_page.data[key]) ||
181
+ value = current_page.data[key] ||
182
+ meta_tags[key] ||
91
183
  site_data[key] ||
92
184
  default
93
- value = yield value if block_given?
94
185
 
95
- if key === "description"
96
- value = safe_description(value)
97
- end
186
+ value = yield value if block_given?
98
187
 
99
- if key === "title"
100
- value = safe_title(value)
101
- end
188
+ value = safe_title(value) if key == 'title'
189
+ value = safe_description(value) if key == 'description'
102
190
 
103
- set_meta_tags name => value unless value.blank?
191
+ set_meta_tags name => value if value.present?
104
192
  value
105
193
  end
106
194
 
107
195
  def fall_through_image(*args)
108
196
  fall_through(*args) do |path|
109
- is_uri?(path) ? path : meta_tags_image_url(path)
197
+ uri?(path) && path ? path : meta_tags_image_url(path)
110
198
  end
111
199
  end
112
200
 
201
+ def current_page_url
202
+ meta_tags_host + current_page.url unless (data['site'] || {})['host'].nil?
203
+ end
204
+
113
205
  def meta_tags_image_url(source)
114
- meta_tags_host + image_path(source)
206
+ File.join(meta_tags_host, image_path(source))
115
207
  end
116
208
 
117
209
  def meta_tags_host
118
210
  (data['site'] || {})['host'] || ''
119
211
  end
120
212
 
121
- # borrowed from Rails 3
122
- # http://apidock.com/rails/v3.2.8/ActionView/AssetPaths/is_uri%3F
123
- def is_uri?(path)
213
+ def uri?(path)
124
214
  path =~ %r{^[-a-z]+://|^(?:cid|data):|^//}
125
215
  end
126
216
 
127
217
  def full_title(meta_tags)
128
- separator = meta_tags[:separator] || '|'
218
+ separator = meta_tags[:separator] || '-'
129
219
  full_title = ''
130
220
  title = safe_title(meta_tags[:title])
131
221
 
132
- (full_title << title) unless title.blank?
222
+ (full_title << title) if title.present?
133
223
  (full_title << " #{separator} ") unless title.blank? || meta_tags[:site].blank?
134
- (full_title << meta_tags[:site]) unless meta_tags[:site].blank?
224
+ (full_title << meta_tags[:site]) if meta_tags[:site].present?
135
225
  full_title
136
226
  end
137
227
 
138
228
  def safe_description(description)
139
- if description.is_a?(Hash) && description[I18n.locale]
140
- description = description[I18n.locale]
141
- end
229
+ description = description[I18n.locale] if description.is_a?(Hash) && description[I18n.locale]
142
230
 
143
- if description && description.start_with?('t:')
144
- description = I18n.t(description[2..-1])
145
- end
231
+ description = I18n.t(description[2..-1]) if description&.start_with?('t:')
146
232
 
147
- truncate(strip_tags(description), length: 300)
233
+ truncate(strip_tags(description), length: 220)
148
234
  end
149
235
 
150
236
  def safe_title(title)
151
- if title.is_a?(Hash) && title[I18n.locale]
152
- title = title[I18n.locale]
153
- end
237
+ title = title[I18n.locale] if title.is_a?(Hash) && title[I18n.locale]
154
238
 
155
- if title && title.start_with?('t:')
156
- title = I18n.t(title[2..-1])
157
- end
239
+ title = I18n.t(title[2..-1]) if title&.start_with?('t:')
158
240
 
159
- title = strip_tags(title)
241
+ strip_tags(title)
160
242
  end
161
243
  end
162
244
  end
@@ -1,5 +1,5 @@
1
1
  module Middleman
2
2
  module MetaTags
3
- VERSION = '0.4.0'
3
+ VERSION = '0.5.0'
4
4
  end
5
5
  end
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_runtime_dependency 'middleman-core', ['>= 3.0.0']
22
22
 
23
- spec.add_development_dependency 'bundler', '~> 1.5'
23
+ spec.add_development_dependency 'bundler'
24
24
  spec.add_development_dependency 'rake'
25
+ spec.add_development_dependency 'rspec', '~> 3.2'
25
26
  end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Middleman::MetaTags::Helpers do
4
+ let(:h) { Class.new { extend Middleman::MetaTags::Helpers } }
5
+
6
+ before(:each) do
7
+ allow(h).to receive_message_chain(:current_page, :data).and_return({ full_title: nil })
8
+ end
9
+
10
+ it 'transforms title tag' do
11
+ h.set_meta_tags title: 'Relevant title'
12
+ expect(h.display_meta_tags).to eql('<title>Relevant title</title>')
13
+ end
14
+
15
+ it 'transforms description tag' do
16
+ h.set_meta_tags description: 'Relevant title of more than 220 characters
17
+ 1 Relevant title of more than 220 characters
18
+ 2 Relevant title of more than 220 characters
19
+ 3 Relevant title of more than 220 characters
20
+ 4 Relevant title of more than 220 characters'
21
+ expect(h.display_meta_tags).to eql('<meta name="description" content="Relevant title of more than 220 characters
22
+ 1 Relevant title of more than 220 characters
23
+ 2 Relevant title of more than 220 characters
24
+ 3 Relevant title of more than 220 characters
25
+ 4 Relevant title of more than 220 chara..." />')
26
+ end
27
+
28
+ it 'transforms keywords tag' do
29
+ h.set_meta_tags keywords: %w(some seo keywords)
30
+ expect(h.display_meta_tags).to eql('<meta name="keywords" content="some, seo, keywords" />')
31
+ end
32
+
33
+ it 'transforms site tag' do
34
+ expect(h.display_meta_tags site: 'My Awesome Website').to eql('<title>My Awesome Website</title>
35
+ <link rel="site" href="My Awesome Website" />')
36
+ end
37
+ end
@@ -0,0 +1,6 @@
1
+ require 'active_support/all'
2
+ require 'middleman-core'
3
+ require 'middleman-meta-tags/extension'
4
+ require 'padrino-helpers'
5
+
6
+ include Padrino::Helpers
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: middleman-meta-tags
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Baptiste Lecocq
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-28 00:00:00.000000000 Z
11
+ date: 2020-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: middleman-core
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '1.5'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '1.5'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
55
69
  description: Easy integration of meta tags into your Middleman applications
56
70
  email:
57
71
  - hello@tiste.io
@@ -59,6 +73,7 @@ executables: []
59
73
  extensions: []
60
74
  extra_rdoc_files: []
61
75
  files:
76
+ - ".github/workflows/ruby.yml"
62
77
  - ".gitignore"
63
78
  - ".ruby-gemset"
64
79
  - ".ruby-version"
@@ -73,11 +88,13 @@ files:
73
88
  - lib/middleman-meta-tags/version.rb
74
89
  - lib/middleman_extension.rb
75
90
  - middleman-meta-tags.gemspec
91
+ - spec/middleman-meta-tags/helpers_spec.rb
92
+ - spec/spec_helper.rb
76
93
  homepage: https://github.com/tiste/middleman-meta-tags
77
94
  licenses:
78
95
  - MIT
79
96
  metadata: {}
80
- post_install_message:
97
+ post_install_message:
81
98
  rdoc_options: []
82
99
  require_paths:
83
100
  - lib
@@ -92,9 +109,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
109
  - !ruby/object:Gem::Version
93
110
  version: '0'
94
111
  requirements: []
95
- rubyforge_project:
96
- rubygems_version: 2.6.14
97
- signing_key:
112
+ rubygems_version: 3.0.8
113
+ signing_key:
98
114
  specification_version: 4
99
115
  summary: Meta tags for Middleman
100
- test_files: []
116
+ test_files:
117
+ - spec/middleman-meta-tags/helpers_spec.rb
118
+ - spec/spec_helper.rb