Extract text from language tagged strings for link preview (#39190)

This commit is contained in:
zunda 2026-06-01 05:10:08 -10:00 committed by GitHub
parent 8d4f9a97fe
commit 423d0ca875
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 2 deletions

View File

@ -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

View File

@ -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 }
<!doctype html>
<html>
<body>
<script type="application/ld+json">
#{ld_json}
</script>
</body>
</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