Extract text from language tagged strings for link preview (#39190)
This commit is contained in:
parent
8d4f9a97fe
commit
423d0ca875
@ -28,11 +28,11 @@ class LinkDetailsExtractor
|
|||||||
end
|
end
|
||||||
|
|
||||||
def headline
|
def headline
|
||||||
json['headline']
|
text_or_language_tagged_string(json['headline'])
|
||||||
end
|
end
|
||||||
|
|
||||||
def description
|
def description
|
||||||
json['description']
|
text_or_language_tagged_string(json['description'])
|
||||||
end
|
end
|
||||||
|
|
||||||
def language
|
def language
|
||||||
@ -96,6 +96,10 @@ class LinkDetailsExtractor
|
|||||||
arr.is_a?(Array) ? arr.flatten.find { |item| item.is_a?(Hash) } : arr
|
arr.is_a?(Array) ? arr.flatten.find { |item| item.is_a?(Hash) } : arr
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def text_or_language_tagged_string(obj)
|
||||||
|
obj.is_a?(Hash) && obj['@value'] && obj['@language'] ? obj['@value'] : obj
|
||||||
|
end
|
||||||
|
|
||||||
def root_array(root)
|
def root_array(root)
|
||||||
root.is_a?(Array) ? root : [root]
|
root.is_a?(Array) ? root : [root]
|
||||||
end
|
end
|
||||||
|
|||||||
@ -287,6 +287,41 @@ RSpec.describe LinkDetailsExtractor do
|
|||||||
expect(subject.provider_name).to eq 'Pet News'
|
expect(subject.provider_name).to eq 'Pet News'
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
context 'when Open Graph protocol data is present' do
|
context 'when Open Graph protocol data is present' do
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user