govuk_publishing_components 8.0.1 → 8.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/views/govuk_publishing_components/components/_machine_readable_metadata.html.erb +5 -0
- data/app/views/govuk_publishing_components/components/docs/machine_readable_metadata.yml +33 -0
- data/lib/govuk_publishing_components.rb +1 -0
- data/lib/govuk_publishing_components/presenters/schema_org.rb +113 -0
- data/lib/govuk_publishing_components/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 575de4ef4fef97453f60afc3d3ab388997efd80f10d76c9b7ee5860ca6423278
|
4
|
+
data.tar.gz: 2a418743b25fa77ef0d553025f0d757642d4d2cf69ad6817402e77087f638894
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 619fc4f95918f3f296d8981e9cbbb7ed3bccc7b56faa402e5b844c3b1057044ca5274b6ca7b5e684adc091e86f8a5a416eb50a4d86cb90e52f48b7f554b49f4b
|
7
|
+
data.tar.gz: a46ec8e3951691469c222b91047967ee2c2822367ba04aae67dea3b68b34fe7d6b0cb65f0b6f3ce91db50c77cd1266eee94624e36b429f480c7fe82c7ee3392d
|
@@ -0,0 +1,33 @@
|
|
1
|
+
name: Machine readable metadata
|
2
|
+
description: Generates schema.org JSON-LD for use by search engines
|
3
|
+
body: |
|
4
|
+
This component doesn't output anything visible. It generates "structured data"
|
5
|
+
in JSON-LD format, with [schema.org](http://schema.org) schemas.
|
6
|
+
accessibility_criteria: |
|
7
|
+
The HTML should not be visible.
|
8
|
+
display_html: true
|
9
|
+
examples:
|
10
|
+
default:
|
11
|
+
data:
|
12
|
+
content_item:
|
13
|
+
title: "A title"
|
14
|
+
base_path: "/foo"
|
15
|
+
details: {}
|
16
|
+
schema: :article
|
17
|
+
with_canonical_url:
|
18
|
+
data:
|
19
|
+
content_item:
|
20
|
+
title: "A title"
|
21
|
+
base_path: "/foo"
|
22
|
+
details: {}
|
23
|
+
schema: :article
|
24
|
+
canonical_url: "https://www.gov.uk/foreign-travel-advice/andorra/health"
|
25
|
+
|
26
|
+
with_body:
|
27
|
+
data:
|
28
|
+
content_item:
|
29
|
+
title: "A title"
|
30
|
+
base_path: "/foo"
|
31
|
+
details: {}
|
32
|
+
schema: :article
|
33
|
+
body: "Some other body"
|
@@ -15,6 +15,7 @@ require "govuk_publishing_components/presenters/curated_taxonomy_sidebar_links"
|
|
15
15
|
require "govuk_publishing_components/presenters/content_item"
|
16
16
|
require "govuk_publishing_components/presenters/translation_nav_helper"
|
17
17
|
require "govuk_publishing_components/presenters/subscription_links_helper"
|
18
|
+
require "govuk_publishing_components/presenters/schema_org"
|
18
19
|
require "govuk_publishing_components/presenters/heading_helper"
|
19
20
|
|
20
21
|
require "govuk_publishing_components/app_helpers/taxon_breadcrumbs"
|
@@ -0,0 +1,113 @@
|
|
1
|
+
module GovukPublishingComponents
|
2
|
+
module Presenters
|
3
|
+
class SchemaOrg
|
4
|
+
def initialize(local_assigns)
|
5
|
+
@local_assigns = local_assigns
|
6
|
+
end
|
7
|
+
|
8
|
+
def structured_data
|
9
|
+
if @local_assigns.fetch(:schema) == :article
|
10
|
+
ArticleSchema.new(@local_assigns).structured_data
|
11
|
+
elsif @local_assigns.fetch(:schema) == :news_article
|
12
|
+
NewsArticleSchema.new(@local_assigns).structured_data
|
13
|
+
else
|
14
|
+
raise "#{@local_assigns.fetch(:schema)} is not supported"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class NewsArticleSchema
|
19
|
+
def initialize(local_assigns)
|
20
|
+
@local_assigns = local_assigns
|
21
|
+
end
|
22
|
+
|
23
|
+
def structured_data
|
24
|
+
# http://schema.org/NewsArticle
|
25
|
+
data = ArticleSchema.new(@local_assigns).structured_data
|
26
|
+
data["@type"] = "NewsArticle"
|
27
|
+
data
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class ArticleSchema
|
32
|
+
attr_reader :content_item, :canonical_url, :local_assigns
|
33
|
+
|
34
|
+
def initialize(local_assigns)
|
35
|
+
@local_assigns = local_assigns
|
36
|
+
|
37
|
+
@content_item = local_assigns[:content_item]
|
38
|
+
@canonical_url = local_assigns[:canonical_url]
|
39
|
+
@custom_body = local_assigns[:body]
|
40
|
+
end
|
41
|
+
|
42
|
+
def structured_data
|
43
|
+
# http://schema.org/Article
|
44
|
+
{
|
45
|
+
"@context" => "http://schema.org",
|
46
|
+
"@type" => "Article",
|
47
|
+
"mainEntityOfPage" => {
|
48
|
+
"@type" => "WebPage",
|
49
|
+
"@id" => canonical_url || page_url_from_content_item,
|
50
|
+
},
|
51
|
+
"headline" => local_assigns[:title] || content_item["title"],
|
52
|
+
"datePublished" => content_item["first_published_at"],
|
53
|
+
"dateModified" => content_item["public_updated_at"],
|
54
|
+
"description" => content_item["description"],
|
55
|
+
"publisher" => {
|
56
|
+
"@type" => "Organization",
|
57
|
+
"name" => "GOV.UK",
|
58
|
+
"url" => "https://www.gov.uk",
|
59
|
+
}
|
60
|
+
}.merge(image_schema).merge(author_schema).merge(body)
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
attr_reader :presenter
|
66
|
+
|
67
|
+
# Not all formats have a `body` - some have their content split over
|
68
|
+
# multiple fields. In this case we'll skip the `articleBody` field
|
69
|
+
def body
|
70
|
+
return {} unless @custom_body || content_item["details"]["body"]
|
71
|
+
|
72
|
+
{
|
73
|
+
"articleBody" => @custom_body || content_item["details"]["body"]
|
74
|
+
}
|
75
|
+
end
|
76
|
+
|
77
|
+
def image_schema
|
78
|
+
return {} unless image
|
79
|
+
|
80
|
+
{
|
81
|
+
"image" => [
|
82
|
+
image["url"],
|
83
|
+
]
|
84
|
+
}
|
85
|
+
end
|
86
|
+
|
87
|
+
def author_schema
|
88
|
+
return {} unless publishing_organisation
|
89
|
+
|
90
|
+
{
|
91
|
+
"author" => {
|
92
|
+
"@type" => "Organization",
|
93
|
+
"name" => publishing_organisation["title"],
|
94
|
+
"url" => Plek.current.website_root + publishing_organisation["base_path"],
|
95
|
+
}
|
96
|
+
}
|
97
|
+
end
|
98
|
+
|
99
|
+
def publishing_organisation
|
100
|
+
content_item.dig("links", "primary_publishing_organisation").to_a.first
|
101
|
+
end
|
102
|
+
|
103
|
+
def page_url_from_content_item
|
104
|
+
Plek.current.website_root + content_item["base_path"]
|
105
|
+
end
|
106
|
+
|
107
|
+
def image
|
108
|
+
content_item.dig("details", "image")
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: govuk_publishing_components
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 8.0
|
4
|
+
version: 8.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- GOV.UK Dev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-05-
|
11
|
+
date: 2018-05-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: govuk_app_config
|
@@ -385,6 +385,7 @@ files:
|
|
385
385
|
- app/views/govuk_publishing_components/components/_input.html.erb
|
386
386
|
- app/views/govuk_publishing_components/components/_inverse_header.html.erb
|
387
387
|
- app/views/govuk_publishing_components/components/_label.html.erb
|
388
|
+
- app/views/govuk_publishing_components/components/_machine_readable_metadata.html.erb
|
388
389
|
- app/views/govuk_publishing_components/components/_meta_tags.html.erb
|
389
390
|
- app/views/govuk_publishing_components/components/_radio.html.erb
|
390
391
|
- app/views/govuk_publishing_components/components/_related_navigation.html.erb
|
@@ -410,6 +411,7 @@ files:
|
|
410
411
|
- app/views/govuk_publishing_components/components/docs/input.yml
|
411
412
|
- app/views/govuk_publishing_components/components/docs/inverse_header.yml
|
412
413
|
- app/views/govuk_publishing_components/components/docs/label.yml
|
414
|
+
- app/views/govuk_publishing_components/components/docs/machine_readable_metadata.yml
|
413
415
|
- app/views/govuk_publishing_components/components/docs/meta_tags.yml
|
414
416
|
- app/views/govuk_publishing_components/components/docs/radio.yml
|
415
417
|
- app/views/govuk_publishing_components/components/docs/related_navigation.yml
|
@@ -450,6 +452,7 @@ files:
|
|
450
452
|
- lib/govuk_publishing_components/presenters/page_with_step_by_step_navigation.rb
|
451
453
|
- lib/govuk_publishing_components/presenters/related_navigation_helper.rb
|
452
454
|
- lib/govuk_publishing_components/presenters/rummager_taxonomy_sidebar_links.rb
|
455
|
+
- lib/govuk_publishing_components/presenters/schema_org.rb
|
453
456
|
- lib/govuk_publishing_components/presenters/services.rb
|
454
457
|
- lib/govuk_publishing_components/presenters/step_by_step_nav_helper.rb
|
455
458
|
- lib/govuk_publishing_components/presenters/subscription_links_helper.rb
|