Skip to content

Commit

Permalink
Do not merge title and site title for OpenGraph.
Browse files Browse the repository at this point in the history
Site title is available for reference as `:site`, and full title as `:full_title`.
Closes #119.
  • Loading branch information
kpumuk committed Aug 24, 2017
1 parent 1d237e6 commit 0fd073f
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ Style/ParallelAssignment:
matches on both sides of the assignment.
This also provides performance benefits
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#parallel-assignment'
Enabled: true
Enabled: false

Style/ParenthesesAroundCondition:
Description: >-
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Features:

Bugfixes:
- When title limit reached with `reverse` set to `true`, properly truncate the last item of the title array instead of the first one.
- Do not merge title and site title for OpenGraph, site title is available for reference as `:site`, and full title as `:full_title`.

Changes:
- Removed Google "author" and "publisher" links, as Google deprecated these options (https://support.google.com/webmasters/answer/6083347?hl=en)
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ Say, you have the following in your application layout:

```ruby
display_meta_tags og: {
title: :title
title: :title,
site_name: :site,
}
```

Expand All @@ -214,6 +215,9 @@ You get this open graph meta tag for free:
<meta property="og:title" content="my great view"></meta>
```

Please note, that title does not include site. If you need to reference the exact
value rendered in the `<title>` meta tag, use `:full_title`.

### Using with Turbolinks

[Turbolinks](https://github.com/turbolinks/turbolinks) is a simple solution for getting
Expand Down
11 changes: 11 additions & 0 deletions lib/meta_tags/meta_tags_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ def full_title(defaults = {})
with_defaults(defaults) { extract_full_title }
end

# Constructs the title without site title (for normalized parameters).
#
# @return [String] page title.
#
def page_title(defaults = {})
old_site, @meta_tags[:site] = @meta_tags[:site], nil
with_defaults(defaults) { extract_full_title }
ensure
@meta_tags[:site] = old_site
end

# Deletes and returns a meta tag value by name.
#
# @param [String, Symbol] name meta tag name.
Expand Down
4 changes: 3 additions & 1 deletion lib/meta_tags/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ def render_charset(tags)
# @param [Array<Tag>] tags a buffer object to store tag in.
#
def render_title(tags)
normalized_meta_tags[:title] = meta_tags.page_title
normalized_meta_tags[:site] = meta_tags[:site]
title = meta_tags.extract_full_title
normalized_meta_tags[:title] = title
normalized_meta_tags[:full_title] = title
tags << ContentTag.new(:title, content: title) if title.present?
end

Expand Down
9 changes: 9 additions & 0 deletions spec/view_helper/open_graph_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@
end
end

it "should properly handle title and site title in mirrored content" do
subject.set_meta_tags(title: 'someTitle', site: 'someSite')
subject.display_meta_tags(open_graph: { title: :title, site_name: :site, full_title: :full_title }).tap do |meta|
expect(meta).to have_tag('meta', with: { content: "someTitle", property: "og:title" })
expect(meta).to have_tag('meta', with: { content: "someSite", property: "og:site_name" })
expect(meta).to have_tag('meta', with: { content: "someSite | someTitle", property: "og:full_title" })
end
end

it "should display open graph meta tags with an array of images" do
subject.set_meta_tags(open_graph: {
title: 'someTitle',
Expand Down

0 comments on commit 0fd073f

Please sign in to comment.