From 423d0ca875fccbcd02ed4201999cec1625dee9e6 Mon Sep 17 00:00:00 2001 From: zunda Date: Mon, 1 Jun 2026 05:10:08 -1000 Subject: [PATCH] Extract text from language tagged strings for link preview (#39190) --- app/lib/link_details_extractor.rb | 8 ++++-- spec/lib/link_details_extractor_spec.rb | 35 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/app/lib/link_details_extractor.rb b/app/lib/link_details_extractor.rb index 5dba1350ef..fb38f7f5fe 100644 --- a/app/lib/link_details_extractor.rb +++ b/app/lib/link_details_extractor.rb @@ -28,11 +28,11 @@ class LinkDetailsExtractor end def headline - json['headline'] + text_or_language_tagged_string(json['headline']) end def description - json['description'] + text_or_language_tagged_string(json['description']) end def language @@ -96,6 +96,10 @@ class LinkDetailsExtractor arr.is_a?(Array) ? arr.flatten.find { |item| item.is_a?(Hash) } : arr end + def text_or_language_tagged_string(obj) + obj.is_a?(Hash) && obj['@value'] && obj['@language'] ? obj['@value'] : obj + end + def root_array(root) root.is_a?(Array) ? root : [root] end diff --git a/spec/lib/link_details_extractor_spec.rb b/spec/lib/link_details_extractor_spec.rb index 019a57cac5..24f55620aa 100644 --- a/spec/lib/link_details_extractor_spec.rb +++ b/spec/lib/link_details_extractor_spec.rb @@ -287,6 +287,41 @@ RSpec.describe LinkDetailsExtractor do expect(subject.provider_name).to eq 'Pet News' end end + + context 'with headline and description as language tagged strings' do + let(:ld_json) do + { + '@context' => 'https://schema.org', + '@type' => 'NewsArticle', + 'headline' => { + '@value' => 'Title in English', + '@language' => 'en', + }, + 'description' => { + '@value' => 'Text in English.', + '@language' => 'en', + }, + }.to_json + end + let(:html) { <<~HTML } + + + + + + + HTML + + it 'gives correct title' do + expect(subject.title).to eq 'Title in English' + end + + it 'gives correct description' do + expect(subject.description).to eq 'Text in English.' + end + end end context 'when Open Graph protocol data is present' do