Skip to content

Commit

Permalink
Supports custom robots tags
Browse files Browse the repository at this point in the history
  • Loading branch information
dlackty committed Sep 28, 2021
1 parent 60ebf51 commit 8e9ab54
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ Use these options to customize the title format:
| `:og` | add Open Graph tags (Hash) |
| `:twitter` | add Twitter tags (Hash) |
| `:refresh` | refresh interval and optionally url to redirect to |
| `:robots` | add custom robots tags (Hash) |

And here are a few examples to give you ideas.

Expand Down Expand Up @@ -749,6 +750,23 @@ Further reading:

- [App Links Documentation](https://developers.facebook.com/docs/applinks)

### Robots

Besides using noindex, there's still other custom robots met tags options to instruct search engine to serve crawled site content in specific ways.

```ruby
set_meta_tags robots: {
"max-snippet": -1,
"max-video-preview": -1
}

# <meta name="robots" content="max-snippet:-1, max-video-preview:-1">
```

Further reading:

* [Robots meta tag, data-nosnippet, and X-Robots-Tag specifications](https://developers.google.com/search/reference/robots_meta_tag)

### Custom meta tags

Starting from version 1.3.1, you can specify arbitrary meta tags, and they will
Expand Down
5 changes: 5 additions & 0 deletions lib/meta_tags/meta_tags_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ def extract_robots
calculate_robots_attributes(result, attributes)
end

robots = extract(:robots).presence
if robots
result['robots'].concat(robots.map { |k, v| "#{k}:#{v}" })
end

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

Expand Down
1 change: 1 addition & 0 deletions lib/meta_tags/view_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def refresh(refresh)
# @option default [String, Integer] :refresh (nil) meta refresh tag;
# @option default [Hash] :open_graph ({}) add Open Graph meta tags.
# @option default [Hash] :open_search ({}) add Open Search link tag.
# @option default [Hash] :robots ({}) add robots meta tags.
# @return [String] HTML meta tags to render in HEAD section of the
# HTML document.
#
Expand Down
24 changes: 24 additions & 0 deletions spec/view_helper/robots_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe MetaTags::ViewHelper, "displaying robots meta tags" do
it "displays meta tags specified with :robots" do
subject.display_meta_tags(robots: { "max-snippet": -1 }).tap do |meta|
expect(meta).to have_tag("meta", with: { content: "max-snippet:-1", name: "robots" })
end
end

it "displays multiple custom robots tags in an array" do
subject.display_meta_tags(robots: { "max-snippet": -1, "max-video-preview": -1 }).tap do |meta|
expect(meta).to have_tag("meta", with: { content: "max-snippet:-1, max-video-preview:-1", name: "robots" })
end
end

it "displays custom robots tags alng with noindex" do
subject.noindex(true)
subject.display_meta_tags(robots: { "max-snippet": -1, "max-video-preview": -1 }).tap do |meta|
expect(meta).to have_tag("meta", with: { content: "noindex, max-snippet:-1, max-video-preview:-1", name: "robots" })
end
end
end

0 comments on commit 8e9ab54

Please sign in to comment.