FIX: Use basic meta description if other description tags are missing (#15356)

When attempting to Onebox a page if there is no `meta property="og:description"` tag but there is a  `meta name="description"` tag, Onebox should try to use that value.
This commit is contained in:
jbrw 2021-12-17 19:36:54 -05:00 committed by GitHub
parent b91ce192bf
commit 6e925fee6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 1 deletions

View File

@ -58,7 +58,12 @@ module Onebox
end end
favicon = get_favicon favicon = get_favicon
@raw["favicon".to_sym] = favicon unless Onebox::Helpers::blank?(favicon) @raw[:favicon] = favicon unless Onebox::Helpers::blank?(favicon)
unless @raw[:description]
description = get_description
@raw[:description] = description unless Onebox::Helpers::blank?(description)
end
@raw @raw
end end
@ -106,6 +111,15 @@ module Onebox
Onebox::Helpers::get_absolute_image_url(favicon, url) Onebox::Helpers::get_absolute_image_url(favicon, url)
end end
def get_description
return nil unless html_doc
description = html_doc.at("meta[name='description']").to_h['content']
description ||= html_doc.at("meta[name='Description']").to_h['content']
description
end
def get_json_response def get_json_response
oembed_url = get_oembed_url oembed_url = get_oembed_url

View File

@ -0,0 +1,17 @@
<html lang="en" data-ember-extension="1">
<head>
<title>My Page Title</title>
<meta name="description" content="basic meta description">
<link rel="canonical" href="https://www.example.com/content">
<meta property="og:site_name" content="My Site">
<meta property="twitter:site" content="@example">
<meta property="og:title" content="My Page Title">
<meta property="og:url" content="https://www.example.com/content">
<meta property="og:image" content="https://www.example.com/content/image.png">
<meta property="og:image:width" content="512">
<meta property="og:image:height" content="256">
<body>
<h1>Welcome</h1>
<p>Body content goes here</p>
</body>
</html>

View File

@ -184,6 +184,25 @@ describe Onebox::Engine::AllowlistedGenericOnebox do
expect(onebox.to_html).to include("People are fostering and adopting pets during the pandemic") expect(onebox.to_html).to include("People are fostering and adopting pets during the pandemic")
end end
end end
context 'uses basic meta description when necessary' do
before do
stub_request(:get, "https://www.reddit.com/r/colors/comments/b4d5xm/literally_nothing_black_edition/")
.to_return(status: 200, body: onebox_response('reddit_image'))
stub_request(:get, "https://www.example.com/content")
.to_return(status: 200, body: onebox_response('basic_description'))
end
it 'uses opengraph tags when present' do
onebox = described_class.new("https://www.reddit.com/r/colors/comments/b4d5xm/literally_nothing_black_edition/")
expect(onebox.to_html).to include("4 votes and 1 comment so far on Reddit")
end
it 'fallback to basic meta description if other description tags are missing' do
onebox = described_class.new("https://www.example.com/content")
expect(onebox.to_html).to include("basic meta description")
end
end
end end
describe 'article html hosts' do describe 'article html hosts' do