-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add structured data for news articles
This implements a minimum of structured data for news articles. As per Google: > Structured data is a standardized format for providing information about a page and classifying the page content; for example, on a recipe page, what are the ingredients, the cooking time and temperature, the calories, and so on. https://developers.google.com/search/docs/guides/intro-structured-data We use the JSON-LD (JSON "Linked Data", https://json-ld.org) here. It's Google's recommended way of implementing this and avoids a lot of the hassle involved with Microdata, which interleaves the structured data with HTML. The output of this is tested with the Structured data testing tool: https://search.google.com/structured-data/testing-tool Unfortunately, the "publisher" attribute needs a logo. I've chosen the Opengraph image for this because it's not likely to change and will probably stay around for a long time. If we're going to implement more structured data, we'll have to come up with an alternative (and a better image).
- Loading branch information
Showing
5 changed files
with
96 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
class NewsArticleStructured | ||
def initialize(presenter) | ||
@presenter = presenter | ||
end | ||
|
||
def structured_data | ||
return {} unless enough_structured_data? | ||
|
||
# http://schema.org/NewsArticle | ||
{ | ||
"@context" => "http://schema.org", | ||
"@type" => "NewsArticle", | ||
"mainEntityOfPage" => { | ||
"@type" => "WebPage", | ||
"@id" => page_url, | ||
}, | ||
"headline" => presenter.title, | ||
"datePublished" => presenter.first_public_at, | ||
"dateModified" => presenter.public_updated_at, | ||
"description" => presenter.description, | ||
"publisher" => { | ||
"@type" => "Organization", | ||
"name" => "GOV.UK", | ||
"url" => "https://www.gov.uk", | ||
"logo" => { | ||
"@type" => "ImageObject", | ||
# TODO: change this to a better image, without the URL hard coded. | ||
"url" => "https://assets.publishing.service.gov.uk/static/opengraph-image-a1f7d89ffd0782738b1aeb0da37842d8bd0addbd724b8e58c3edbc7287cc11de.png", | ||
}, | ||
}, | ||
"image" => [ | ||
image["url"], | ||
], | ||
"author" => { | ||
"@type" => "Organization", | ||
"name" => publishing_organisation["title"], | ||
"url" => Plek.current.website_root + publishing_organisation["base_path"], | ||
}, | ||
} | ||
end | ||
|
||
private | ||
|
||
attr_reader :presenter | ||
|
||
def enough_structured_data? | ||
# The author (for which we use the publishing org) and image are required | ||
# fields. If the news article doesn't have them, don't use structured data | ||
# at all. | ||
publishing_organisation && image | ||
end | ||
|
||
def publishing_organisation | ||
presenter.content_item.dig("links", "primary_publishing_organisation").to_a.first | ||
end | ||
|
||
def page_url | ||
Plek.current.website_root + presenter.content_item["base_path"] | ||
end | ||
|
||
def image | ||
presenter.image | ||
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