From fa8e972722fb8fc056aa348dddaee4005b4a8ac4 Mon Sep 17 00:00:00 2001 From: David Roetzel Date: Mon, 8 Jul 2024 18:04:36 +0200 Subject: [PATCH] Fix author names as arrays in linked data. (#30957) --- app/lib/link_details_extractor.rb | 5 +++-- spec/lib/link_details_extractor_spec.rb | 29 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/lib/link_details_extractor.rb b/app/lib/link_details_extractor.rb index a62ede2bb1..72992d2f42 100644 --- a/app/lib/link_details_extractor.rb +++ b/app/lib/link_details_extractor.rb @@ -62,7 +62,8 @@ class LinkDetailsExtractor end def author_name - author['name'] + name = author['name'] + name.is_a?(Array) ? name.join(', ') : name end def author_url @@ -294,7 +295,7 @@ class LinkDetailsExtractor def html_entities_decode(string) return if string.nil? - unicode_string = string.encode('UTF-8') + unicode_string = string.to_s.encode('UTF-8') raise EncodingError, 'cannot convert string to valid UTF-8' unless unicode_string.valid_encoding? html_entities.decode(unicode_string) diff --git a/spec/lib/link_details_extractor_spec.rb b/spec/lib/link_details_extractor_spec.rb index 26d9d4e265..2a4df70a8b 100644 --- a/spec/lib/link_details_extractor_spec.rb +++ b/spec/lib/link_details_extractor_spec.rb @@ -192,6 +192,35 @@ RSpec.describe LinkDetailsExtractor do include_examples 'structured data' end + + context 'with author names as array' do + let(:ld_json) do + { + '@context' => 'https://schema.org', + '@type' => 'NewsArticle', + 'headline' => 'A lot of authors', + 'description' => 'But we decided to cram them into one', + 'author' => { + '@type' => 'Person', + 'name' => ['Author 1', 'Author 2'], + }, + }.to_json + end + let(:html) { <<~HTML } + + + + + + + HTML + + it 'joins author names' do + expect(subject.author_name).to eq 'Author 1, Author 2' + end + end end context 'when Open Graph protocol data is present' do