-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds all the appropriate meta tags to pages. This makes will improve SEO and improve previews in services like Slack, which uses both Twitter and Facebook/OpenGraph data for its unfurling.
- Loading branch information
Showing
8 changed files
with
154 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
module GovukTechDocs | ||
class MetaTags | ||
def initialize(config, current_page) | ||
@config = config | ||
@current_page = current_page | ||
end | ||
|
||
def tags | ||
all_tags = { | ||
'description' => page_description, | ||
'og:description' => page_description, | ||
'og:image' => page_image, | ||
'og:site_name' => site_name, | ||
'og:title' => page_title, | ||
'og:type' => 'object', | ||
'og:url' => canonical_url, | ||
'twitter:card' => 'summary', | ||
'twitter:domain' => URI.parse(host).host, | ||
'twitter:image' => page_image, | ||
'twitter:title' => browser_title, | ||
'twitter:url' => canonical_url, | ||
} | ||
|
||
Hash[all_tags.select { |_k, v| v }] | ||
end | ||
|
||
def browser_title | ||
"#{page_title} | #{site_name}" | ||
end | ||
|
||
def canonical_url | ||
"#{host}#{current_page.url}" | ||
end | ||
|
||
private | ||
|
||
attr_reader :config, :current_page | ||
|
||
def page_image | ||
"#{host}/images/govuk-large.png" | ||
end | ||
|
||
def site_name | ||
config[:tech_docs][:service_name] | ||
end | ||
|
||
def page_description | ||
locals[:description] || frontmatter.description | ||
end | ||
|
||
def page_title | ||
locals[:title] || frontmatter.title | ||
end | ||
|
||
def host | ||
config[:tech_docs][:host] | ||
end | ||
|
||
def locals | ||
current_page.metadata[:locals] | ||
end | ||
|
||
def frontmatter | ||
current_page.data | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
RSpec.describe GovukTechDocs::MetaTags do | ||
describe '#tags' do | ||
it 'returns all the extra meta tags' do | ||
config = generate_config( | ||
host: "https://www.example.org", | ||
service_name: "Test Site", | ||
) | ||
|
||
current_page = double("current_page", | ||
data: double("page_frontmatter", description: "The description.", title: "The Title"), | ||
url: "/foo.html", | ||
metadata: { locals: {} }) | ||
|
||
tags = GovukTechDocs::MetaTags.new(config, current_page).tags | ||
|
||
expect(tags).to eql("description" => "The description.", | ||
"og:description" => "The description.", | ||
"og:image" => "https://www.example.org/images/govuk-large.png", | ||
"og:site_name" => "Test Site", | ||
"og:title" => "The Title", | ||
"og:type" => "object", | ||
"og:url" => "https://www.example.org/foo.html", | ||
"twitter:card" => "summary", | ||
"twitter:domain" => "www.example.org", | ||
"twitter:image" => "https://www.example.org/images/govuk-large.png", | ||
"twitter:title" => "The Title | Test Site", | ||
"twitter:url" => "https://www.example.org/foo.html") | ||
end | ||
|
||
it 'uses the local variable as page description for proxied pages' do | ||
current_page = double("current_page", | ||
data: double("page_frontmatter", description: "The description.", title: "The Title"), | ||
url: "/foo.html", | ||
metadata: { locals: { description: "The local variable description." } }) | ||
|
||
tags = GovukTechDocs::MetaTags.new(generate_config, current_page).tags | ||
|
||
expect(tags["description"]).to eql("The local variable description.") | ||
end | ||
|
||
it 'uses the local variable as page title for proxied pages' do | ||
current_page = double("current_page", | ||
data: double("page_frontmatter", description: "The description.", title: "The Title"), | ||
url: "/foo.html", | ||
metadata: { locals: { title: "The local variable title." } }) | ||
|
||
tags = GovukTechDocs::MetaTags.new(generate_config, current_page).tags | ||
|
||
expect(tags["og:title"]).to eql("The local variable title.") | ||
end | ||
|
||
def generate_config(config = {}) | ||
{ | ||
tech_docs: { | ||
host: "https://www.example.org", | ||
service_name: "Test Site", | ||
}.merge(config) | ||
} | ||
end | ||
end | ||
end |