From e9e63ed203996a4e06e31a7220e9d8a2df71f67c Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Tue, 6 Aug 2024 09:52:47 +0200 Subject: [PATCH] Support JSON-LD named graph (#31288) --- app/lib/link_details_extractor.rb | 4 ++- spec/lib/link_details_extractor_spec.rb | 36 ++++++++++++++++++------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/app/lib/link_details_extractor.rb b/app/lib/link_details_extractor.rb index bd78aef7a9..6929fc1b0f 100644 --- a/app/lib/link_details_extractor.rb +++ b/app/lib/link_details_extractor.rb @@ -101,7 +101,9 @@ class LinkDetailsExtractor end def json - @json ||= root_array(Oj.load(@data)).compact.find { |obj| SUPPORTED_TYPES.include?(obj['@type']) } || {} + @json ||= root_array(Oj.load(@data)) + .map { |node| JSON::LD::API.compact(node, 'https://schema.org') } + .find { |node| SUPPORTED_TYPES.include?(node['type']) } || {} end end diff --git a/spec/lib/link_details_extractor_spec.rb b/spec/lib/link_details_extractor_spec.rb index b1e5cedced..7ceb6f511d 100644 --- a/spec/lib/link_details_extractor_spec.rb +++ b/spec/lib/link_details_extractor_spec.rb @@ -79,6 +79,16 @@ RSpec.describe LinkDetailsExtractor do }, }.to_json end + let(:html) { <<~HTML } + + + + + + + HTML shared_examples 'structured data' do it 'extracts the expected values from structured data' do @@ -224,21 +234,27 @@ RSpec.describe LinkDetailsExtractor do }, }.to_json end - let(:html) { <<~HTML } - - - - - - - HTML it 'joins author names' do expect(subject.author_name).to eq 'Author 1, Author 2' end end + + context 'with named graph' do + let(:ld_json) do + { + '@context' => 'https://schema.org', + '@graph' => [ + '@type' => 'NewsArticle', + 'headline' => "What's in a name", + ], + }.to_json + end + + it 'descends into @graph node' do + expect(subject.title).to eq "What's in a name" + end + end end context 'when Open Graph protocol data is present' do