diff --git a/app/presenters/content_item/updatable.rb b/app/presenters/content_item/updatable.rb index d434687c0..bd43cdec3 100644 --- a/app/presenters/content_item/updatable.rb +++ b/app/presenters/content_item/updatable.rb @@ -13,6 +13,14 @@ def history reverse_chronological_change_history end + def first_public_at + content_item["details"]["first_public_at"] + end + + def public_updated_at + content_item["public_updated_at"] + end + private def change_history @@ -39,13 +47,5 @@ def any_updates? false end end - - def first_public_at - content_item["details"]["first_public_at"] - end - - def public_updated_at - content_item["public_updated_at"] - end end end diff --git a/app/presenters/news_article_presenter.rb b/app/presenters/news_article_presenter.rb index b9507ed38..72e2316f1 100644 --- a/app/presenters/news_article_presenter.rb +++ b/app/presenters/news_article_presenter.rb @@ -10,4 +10,8 @@ class NewsArticlePresenter < ContentItemPresenter def image content_item["details"]["image"] end + + def structured_data + NewsArticleStructured.new(self).structured_data + end end diff --git a/app/presenters/news_article_structured.rb b/app/presenters/news_article_structured.rb new file mode 100644 index 000000000..d3c9c61e4 --- /dev/null +++ b/app/presenters/news_article_structured.rb @@ -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 diff --git a/app/views/content_items/news_article.html.erb b/app/views/content_items/news_article.html.erb index 3c201cac8..018a00e4d 100644 --- a/app/views/content_items/news_article.html.erb +++ b/app/views/content_items/news_article.html.erb @@ -1,3 +1,7 @@ + +