Skip to content

A platform for community discussion. Free, open, simple.

FIX: remove ItemList schema from linkbacks in crawler view (#36608)

Régis Hanol58d298cae1cd
diff --git a/app/views/topics/show.html.erb b/app/views/topics/show.html.erb
index 9a7ee5dba38..8b47cabe4ed 100644
--- a/app/views/topics/show.html.erb
+++ b/app/views/topics/show.html.erb
@@ -108,18 +108,15 @@
<span class='post-likes'><%= post.like_count > 0 ? t('post.has_likes', count: post.like_count) : '' %></span>
</div>
- <% if @topic_view.link_counts[post.id] && @topic_view.link_counts[post.id].filter { |l| l[:reflection] }.length > 0 %>
- <div class='crawler-linkback-list' itemscope itemtype='http://schema.org/ItemList'>
- <% @topic_view.link_counts[post.id].each_with_index do |link, i| %>
- <% if link[:reflection] && link[:title].present? %>
- <div itemprop='itemListElement' itemscope itemtype='http://schema.org/ListItem'>
- <a itemprop='url' href="<%=link[:url]%>"><%=link[:title]%></a>
- <meta itemprop='position' content='<%= i+1 %>'>
- </div>
- <% end %>
+ <% if (linkbacks = @topic_view.linkbacks_for(post)).present? %>
+ <div class='crawler-linkback-list'>
+ <% linkbacks.each do |link| %>
+ <div>
+ <a href="<%=link[:url]%>"><%=link[:title]%></a>
+ </div>
<% end %>
</div>
- <% end %>
+ <% end %>
<%= build_plugin_html "server:topic-show-crawler-post-end", post: post %>
</div>
diff --git a/lib/topic_view.rb b/lib/topic_view.rb
index cecea060c3f..8151f465f06 100644
--- a/lib/topic_view.rb
+++ b/lib/topic_view.rb
@@ -714,6 +714,10 @@ class TopicView
)
end
+ def linkbacks_for(post)
+ link_counts[post.id]&.select { |l| l[:reflection] && l[:title].present? }
+ end
+
def pm_params
@pm_params ||= TopicQuery.new(@user).get_pm_params(topic)
end
diff --git a/spec/fixtures/onebox/discourse_topic.response b/spec/fixtures/onebox/discourse_topic.response
index 52eb3247403..35b223685f5 100644
--- a/spec/fixtures/onebox/discourse_topic.response
+++ b/spec/fixtures/onebox/discourse_topic.response
@@ -280,7 +280,7 @@ And that too in just over an year, way to go! [boom]">
<meta itemprop="userInteractionCount" content="0" />
</div>
- <div class='crawler-linkback-list' itemscope itemtype='http://schema.org/ItemList'>
+ <div class='crawler-linkback-list'>
</div>
</div>
diff --git a/spec/fixtures/onebox/discourse_topic_reply.response b/spec/fixtures/onebox/discourse_topic_reply.response
index 52bde5760c7..b06d2c49505 100644
--- a/spec/fixtures/onebox/discourse_topic_reply.response
+++ b/spec/fixtures/onebox/discourse_topic_reply.response
@@ -272,7 +272,7 @@ And that too in just over an year, way to go! [boom]">
<meta itemprop="userInteractionCount" content="0" />
</div>
- <div class='crawler-linkback-list' itemscope itemtype='http://schema.org/ItemList'>
+ <div class='crawler-linkback-list'>
</div>
</div>
diff --git a/spec/views/topics/show.html.erb_spec.rb b/spec/views/topics/show.html.erb_spec.rb
index bdd60e431f4..abfffb5e3ad 100644
--- a/spec/views/topics/show.html.erb_spec.rb
+++ b/spec/views/topics/show.html.erb_spec.rb
@@ -1,34 +1,31 @@
# frozen_string_literal: true
-require "ostruct"
-
RSpec.describe "topics/show.html.erb" do
- fab!(:category)
- fab!(:topic) { Fabricate(:topic, category: category) }
+ fab!(:topic) { Fabricate(:topic, category: Fabricate(:category)) }
- it "add nofollow to RSS alternate link for topic" do
+ it "adds nofollow to RSS alternate link" do
topic_view = OpenStruct.new(topic: topic, posts: [], crawler_posts: [])
topic_view.stubs(:summary).returns("")
view.stubs(:crawler_layout?).returns(false)
- view.stubs(:url_for).returns("https://www.example.com/test.rss")
+ view.stubs(:url_for).returns("https://example.com/test.rss")
view.instance_variable_set("@topic_view", topic_view)
assign(:tags, [])
render template: "topics/show", formats: [:html]
expect(view.content_for(:head)).to match(
- %r{<link rel="alternate nofollow" type="application/rss\+xml" title="[^"]+" href="https://www.example.com/test\.rss" />},
+ %r{<link rel="alternate nofollow" type="application/rss\+xml"},
)
end
- it "adds structured data" do
+ it "renders linkbacks as plain links without ItemList schema" do
view.stubs(:include_crawler_content?).returns(true)
post = Fabricate(:post, topic: topic)
TopicLink.create!(
topic_id: post.topic_id,
post_id: post.id,
user_id: post.user_id,
- url: "https://example.com/",
+ url: "https://example.com/linked",
domain: "example.com",
link_topic_id: Fabricate(:topic).id,
reflection: true,
@@ -38,27 +35,25 @@ RSpec.describe "topics/show.html.erb" do
render template: "topics/show", formats: [:html]
- links_list = Nokogiri::HTML5.fragment(rendered).css(".crawler-linkback-list")
- first_item = links_list.css('[itemprop="itemListElement"]')
- expect(first_item.css('[itemprop="position"]')[0]["content"]).to eq("1")
- expect(first_item.css('[itemprop="url"]')[0]["href"]).to eq("https://example.com/")
+ doc = Nokogiri::HTML5.fragment(rendered)
+ linkbacks = doc.css(".crawler-linkback-list")
+ expect(linkbacks.css("a[href='https://example.com/linked']")).to be_present
+ expect(linkbacks.css('[itemtype*="ItemList"]')).to be_empty
end
- it "uses comment scheme type for replies" do
+ it "uses DiscussionForumPosting with Comment schema for replies" do
view.stubs(:crawler_layout?).returns(true)
view.stubs(:include_crawler_content?).returns(true)
- Fabricate(:post, topic: topic)
- Fabricate(:post, topic: topic)
- Fabricate(:post, topic: topic)
+ 3.times { Fabricate(:post, topic: topic) }
assign(:topic_view, TopicView.new(topic))
assign(:tags, [])
render template: "topics/show", formats: [:html]
doc = Nokogiri::HTML5.fragment(rendered)
- topic_schema = doc.css('[itemtype="http://schema.org/DiscussionForumPosting"]')
- expect(topic_schema.size).to eq(1)
- expect(topic_schema.css('[itemtype="http://schema.org/Comment"]').size).to eq(2)
- expect(topic_schema.css('[itemprop="articleSection"]')[0]["content"]).to eq(topic.category.name)
+ posting = doc.css('[itemtype*="DiscussionForumPosting"]')
+ expect(posting.size).to eq(1)
+ expect(posting.css('[itemtype*="Comment"]').size).to eq(2) # replies only, not OP
+ expect(posting.css('[itemprop="articleSection"]').first["content"]).to eq(topic.category.name)
end
end