diff --git a/app/liquid_tags/open_graph_tag.rb b/app/liquid_tags/open_graph_tag.rb index 33146da06..aea7bd844 100644 --- a/app/liquid_tags/open_graph_tag.rb +++ b/app/liquid_tags/open_graph_tag.rb @@ -6,6 +6,7 @@ class OpenGraphTag < LiquidTagBase def initialize(_tag_name, url, _parse_context) super + @url = url @page = OpenGraph.new url @url_domain = URI.parse(url).host.delete_prefix("www.") end @@ -15,6 +16,7 @@ class OpenGraphTag < LiquidTagBase partial: PARTIAL, locals: { page: @page, + url: @url, url_domain: @url_domain }, ) diff --git a/app/services/open_graph.rb b/app/services/open_graph.rb index 93062121d..3e0849c65 100644 --- a/app/services/open_graph.rb +++ b/app/services/open_graph.rb @@ -48,6 +48,7 @@ class OpenGraph end def main_properties + # QUESTION: If we don't have `og:url` could we infer the url based on what was passed? %w[og:title og:url] end diff --git a/app/views/liquids/_open_graph.html.erb b/app/views/liquids/_open_graph.html.erb index 0dbd7c847..1c787a07d 100644 --- a/app/views/liquids/_open_graph.html.erb +++ b/app/views/liquids/_open_graph.html.erb @@ -30,7 +30,7 @@ <% else %> - + <%= url_domain %> <% end %> diff --git a/spec/fixtures/files/guides.rubyonrails.org-routing.html b/spec/fixtures/files/guides.rubyonrails.org-routing.html new file mode 100644 index 000000000..5becc8e71 --- /dev/null +++ b/spec/fixtures/files/guides.rubyonrails.org-routing.html @@ -0,0 +1,18 @@ + + + + + + Rails Routing from the Outside In — Ruby on Rails Guides + + + + + + + + + +

The Guide

+ + diff --git a/spec/liquid_tags/unified_embed/tag_spec.rb b/spec/liquid_tags/unified_embed/tag_spec.rb index 6536cfea6..2238da9fd 100644 --- a/spec/liquid_tags/unified_embed/tag_spec.rb +++ b/spec/liquid_tags/unified_embed/tag_spec.rb @@ -3,6 +3,40 @@ require "rails_helper" RSpec.describe UnifiedEmbed::Tag, type: :liquid_tag do let(:listing) { create(:listing) } + # See https://github.com/forem/forem/issues/17679; Note the document has `og:title` but not + # `og:url`; should we fallback to the given URL instead? + # rubocop:disable RSpec/ExampleLength + it "handles https://guides.rubyonrails.org" do + link = "https://guides.rubyonrails.org/routing.html" + stub_request(:head, link) + .with( + headers: { + "Accept" => "*/*", + "User-Agent" => "DEV(local) (#{URL.url})" + }, + ) + .to_return(status: 200, body: "", headers: {}) + + stub_request(:get, link) + .with( + headers: { + "Accept" => "*/*", + "Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3", + "Host" => "guides.rubyonrails.org", + "User-Agent" => "Ruby" + }, + ) + .to_return( + status: 200, + body: Rails.root.join("spec/fixtures/files/guides.rubyonrails.org-routing.html").read, + headers: {}, + ) + + parsed_tag = Liquid::Template.parse("{% embed #{link} %}") + expect(parsed_tag.render).to include("