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

When noindex is "robots" and nofollow is a specific robot name, both are treated as "robots" #201

Merged
merged 7 commits into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .circleci/config.yml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
['2.5', '4.2.10'],
['2.5', '5.0.7'],
['2.5', '5.1.6'],
['2.5', '5.2.2', true],
['2.5', '6.0.0.beta1'],
['2.5', '5.2.2'],
['2.5', '6.0.0'],

# 2.6
['2.6', '5.0.7'],
['2.6', '5.1.6'],
['2.6', '5.2.2'],
['2.6', '6.0.0.beta1'],
['2.6', '6.0.0', true],
]

cc_build = builds.find { |_, _, submit_cc| submit_cc }
Expand Down
2 changes: 1 addition & 1 deletion .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ engines:

rubocop:
enabled: true
channel: rubocop-0-60
channel: rubocop-0-67

ratings:
paths:
Expand Down
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.5.1
2.6.3
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 2.12.0 (Development) [☰](https://github.com/kpumuk/meta-tags/compare/v2.11.1...master)

Features:
- Indexing directives (`noindex`, `nofollow`, etc. now support an array of robot names as a value).

Bugfixes:
- When `noindex` uses "robots" as a value, `nofollow` ignores a custom robot name, and switches to "robots" as well

## 2.11.1 (January 19, 2019) [☰](https://github.com/kpumuk/meta-tags/compare/v2.11.0...v2.11.1)

Features:
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

source 'http://rubygems.org'
source 'https://rubygems.org'

# Specify your gem's dependencies in meta-tags.gemspec
gemspec
Expand All @@ -13,7 +13,7 @@ end
group :test do
# Lock rubocop to a specific version we use on CI. If you update this,
# don't forget to switch rubocop channel in the .codeclimate.yml
gem 'rubocop', '0.60.0'
gem 'rubocop', '~> 0.67.0'
# Apply RSpec rubocop cops
gem 'rubocop-rspec', require: false
# We use this gem on CI to calculate code coverage.
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ Use these options to customize the title format:
| `:suffix` | text between separator and page title |
| `:lowercase` | when true, the page name will be lowercase |
| `:reverse` | when true, the page and site names will be reversed |
| `:noindex` | add noindex meta tag; when true, 'robots' will be used, otherwise the string will be used |
| `:index` | add index meta tag; when true, 'robots' will be used, otherwise the string will be used |
| `:nofollow` | add nofollow meta tag; when true, 'robots' will be used, otherwise the string will be used |
| `:follow` | add follow meta tag; when true, 'robots' will be used, otherwise the string will be used |
| `:noarchive` | add noarchive meta tag; when true, 'robots' will be used, otherwise the string will be used |
| `:noindex` | add noindex meta tag; when true, 'robots' will be used; accepts a string with a robot name, or an array of strings |
| `:index` | add index meta tag; when true, 'robots' will be used; accepts a string with a robot name, or an array of strings |
| `:nofollow` | add nofollow meta tag; when true, 'robots' will be used; accepts a string with a robot name, or an array of strings |
| `:follow` | add follow meta tag; when true, 'robots' will be used; accepts a string with a robot name, or an array of strings |
| `:noarchive` | add noarchive meta tag; when true, 'robots' will be used; accepts a string with a robot name, or an array of strings |
| `:canonical` | add canonical link tag |
| `:prev` | add prev link tag |
| `:next` | add next link tag |
Expand Down
76 changes: 31 additions & 45 deletions lib/meta_tags/meta_tags_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,28 +142,20 @@ def extract_separator
#
# @return [Hash<String,String>] noindex attributes.
#
def extract_noindex
noindex_name, noindex_value = extract_noindex_attribute(:noindex)
index_name, index_value = extract_noindex_attribute(:index)

nofollow_name, nofollow_value = extract_noindex_attribute(:nofollow)
follow_name, follow_value = extract_noindex_attribute(:follow)

noindex_attributes = if noindex_name == follow_name && (noindex_value || follow_value)
# noindex has higher priority than index and follow has higher priority than nofollow
[
[noindex_name, noindex_value || index_value],
[follow_name, follow_value || nofollow_value],
]
else
[
[index_name, index_value],
[follow_name, follow_value],
[noindex_name, noindex_value],
[nofollow_name, nofollow_value],
]
end
append_noarchive_attribute group_attributes_by_key noindex_attributes
def extract_robots
result = Hash.new { |h, k| h[k] = [] }

[
# noindex has higher priority than index
[:noindex, :index],
# follow has higher priority than nofollow
[:follow, :nofollow],
:noarchive,
].each do |attributes|
calculate_robots_attributes(result, attributes)
end

result.transform_values { |v| v.join(', ') }
end

protected
Expand All @@ -190,43 +182,37 @@ def extract_separator_section(name, default)
meta_tags[name] == false ? '' : (meta_tags[name] || default)
end

# Extracts noindex attribute name and value without deleting it from meta tags list.
# Extracts robots attribute (noindex, nofollow, etc) name and value.
#
# @param [String, Symbol] name noindex attribute name.
# @return [Array<String>] pair of noindex attribute name and value.
#
def extract_noindex_attribute(name)
def extract_robots_attribute(name)
noindex = extract(name)
noindex_name = noindex.kind_of?(String) ? noindex : 'robots'
noindex_name = noindex.kind_of?(String) || noindex.kind_of?(Array) ? noindex : 'robots'
noindex_value = noindex ? name.to_s : nil

[ noindex_name, noindex_value ]
end

# Append noarchive attribute if it present.
#
# @param [Hash<String, String>] noindex noindex attributes.
# @return [Hash<String, String>] modified noindex attributes.
#
def append_noarchive_attribute(noindex)
noarchive_name, noarchive_value = extract_noindex_attribute :noarchive
if noarchive_value
if noindex[noarchive_name].blank?
noindex[noarchive_name] = noarchive_value
else
noindex[noarchive_name] += ", #{noarchive_value}"
def calculate_robots_attributes(result, attributes)
processed = Set.new
Array(attributes).each do |attribute|
names, value = extract_robots_attribute(attribute)
next unless value

Array(names).each do |name|
apply_robots_value(result, name, value, processed)
end
end
noindex
end

# Convert array of arrays to hashes and concatenate values
#
# @param [Array<Array>] attributes list of noindex keys and values
# @return [Hash<String, String>] hash of grouped noindex keys and values
#
def group_attributes_by_key(attributes)
Hash[attributes.group_by(&:first).map { |k, v| [k, v.map(&:last).tap(&:compact!).join(', ')] }]
def apply_robots_value(result, name, value, processed)
name = name.to_s
return if processed.include?(name)

result[name] << value
processed << name
end
end
end
2 changes: 1 addition & 1 deletion lib/meta_tags/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def render_with_normalization(tags, name)
# @param [Array<Tag>] tags a buffer object to store tag in.
#
def render_noindex(tags)
meta_tags.extract_noindex.each do |name, content|
meta_tags.extract_robots.each do |name, content|
tags << Tag.new(:meta, name: name, content: content) if content.present?
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/meta_tags/text_normalizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module MetaTags
# Module contains helpers that normalize text meta tag values.
module TextNormalizer
extend self # rubocop:disable Style/ModuleFunction
extend self

# Normalize title value.
#
Expand Down
3 changes: 2 additions & 1 deletion lib/meta_tags/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

module MetaTags
# Gem version.
VERSION = '2.11.1'
VERSION = '2.12.0'
public_constant :VERSION
end
2 changes: 1 addition & 1 deletion spec/view_helper/module_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
subject { ActionView::Base.new }

it 'is mixed into ActionView::Base' do
expect(ActionView::Base.included_modules).to include(MetaTags::ViewHelper)
expect(ActionView::Base.included_modules).to include(described_class)
end

it 'responds to "title" helper' do
Expand Down
42 changes: 42 additions & 0 deletions spec/view_helper/noindex_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
end
end

it 'accepts multiple custom noindex robots in an array' do
subject.noindex(['some-noindex', 'another-noindex'])
subject.display_meta_tags(site: 'someSite').tap do |meta|
expect(meta).to have_tag('meta', with: { content: "noindex", name: "some-noindex" })
expect(meta).to have_tag('meta', with: { content: "noindex", name: "another-noindex" })
end
end

it 'displays nothing by default' do
subject.display_meta_tags(site: 'someSite').tap do |meta|
expect(meta).not_to have_tag('meta', with: { content: "noindex" })
Expand Down Expand Up @@ -66,6 +74,14 @@
end
end

it 'accepts multiple custom nofollow robots in an array' do
subject.nofollow(['some-nofollow', 'another-nofollow'])
subject.display_meta_tags(site: 'someSite').tap do |meta|
expect(meta).to have_tag('meta', with: { content: "nofollow", name: "some-nofollow" })
expect(meta).to have_tag('meta', with: { content: "nofollow", name: "another-nofollow" })
end
end

it 'displays nothing by default' do
subject.display_meta_tags(site: 'someSite').tap do |meta|
expect(meta).not_to have_tag('meta', with: { content: "nofollow" })
Expand All @@ -89,6 +105,14 @@
end
end

it 'displays two meta tags when different names used with "set_meta_tags"' do
subject.set_meta_tags(noindex: 'robots', nofollow: 'googlebot')
subject.display_meta_tags(site: 'someSite').tap do |meta|
expect(meta).to have_tag('meta', with: { content: 'noindex', name: 'robots' })
expect(meta).to have_tag('meta', with: { content: 'nofollow', name: 'googlebot' })
end
end

it 'uses custom name if string is used' do
subject.noindex('some-name')
subject.nofollow('some-name')
Expand Down Expand Up @@ -179,4 +203,22 @@
end
end
end

it 'properly handles priorities and multiple robot names' do
subject.set_meta_tags(
noindex: true,
index: 'yahoo',
follow: ['google', 'yahoo', 'github'],
nofollow: ['yandex', :google],
noarchive: ['yahoo', 'bing'],
)
subject.display_meta_tags(site: 'someSite').tap do |meta|
expect(meta).to have_tag('meta', with: { name: "robots", content: "noindex" })
expect(meta).to have_tag('meta', with: { name: "yahoo", content: "index, follow, noarchive" })
expect(meta).to have_tag('meta', with: { name: "google", content: "follow" })
expect(meta).to have_tag('meta', with: { name: "github", content: "follow" })
expect(meta).to have_tag('meta', with: { name: "yandex", content: "nofollow" })
expect(meta).to have_tag('meta', with: { name: "bing", content: "noarchive" })
end
end
end
2 changes: 0 additions & 2 deletions spec/view_helper/title_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@
expect(title).to eq('TITLE')
end

# rubocop:disable Rails/OutputSafety
it 'uses custom separator when :separator specified' do
subject.title('someTitle')
subject.display_meta_tags(site: 'someSite', separator: '-').tap do |meta|
Expand All @@ -127,7 +126,6 @@
expect(meta).to eq('<title>someSite:someTitle</title>')
end
end
# rubocop:enable Rails/OutputSafety

it 'uses custom prefix and suffix if available' do
subject.display_meta_tags(site: 'someSite', title: 'someTitle', prefix: ' -', suffix: '- ').tap do |meta|
Expand Down