Skip to content

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

FEATURE: solved schema markup for solution posts (#38768)

David Battersby567b03f8ce6a
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 51a5150376e..1aecf16575d 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -707,6 +707,38 @@ module ApplicationHelper
DiscoursePluginRegistry.build_html(name, controller, **kwargs) || ""
end
+ def crawler_topic_container_schema(topic)
+ tag.attributes(
+ DiscoursePluginRegistry.apply_modifier(
+ :topic_crawler_container_schema,
+ { itemscope: true, itemtype: "http://schema.org/DiscussionForumPosting" },
+ topic,
+ ),
+ )
+ end
+
+ def crawler_topic_main_entity_schema(topic)
+ tag.attributes(
+ DiscoursePluginRegistry.apply_modifier(:topic_crawler_main_entity_schema, {}, topic),
+ ).presence
+ end
+
+ def crawler_post_schema(post, topic)
+ default = {
+ itemprop: "comment",
+ itemscope: true,
+ itemtype: "http://schema.org/Comment",
+ } unless post.is_first_post?
+ tag.attributes(
+ DiscoursePluginRegistry.apply_modifier(
+ :topic_crawler_post_schema,
+ default || {},
+ post,
+ topic,
+ ),
+ )
+ end
+
# If there is plugin HTML return that, otherwise yield to the template
def replace_plugin_html(name)
if (html = build_plugin_html(name)).present?
diff --git a/app/views/topics/show.html.erb b/app/views/topics/show.html.erb
index f762e512ddc..9849ffdacac 100644
--- a/app/views/topics/show.html.erb
+++ b/app/views/topics/show.html.erb
@@ -34,22 +34,29 @@
<%= server_plugin_outlet "topic_header" %>
<%- if include_crawler_content? %>
- <div itemscope itemtype='http://schema.org/DiscussionForumPosting'>
- <meta itemprop='headline' content='<%= @topic_view.title %>'>
+ <% main_entity_schema = crawler_topic_main_entity_schema(@topic_view.topic) %>
+ <div <%= crawler_topic_container_schema(@topic_view.topic) %>>
+ <meta itemprop='<%= main_entity_schema ? "name" : "headline" %>' content='<%= @topic_view.title %>'>
<link itemprop='url' href='<%= @topic_view.absolute_url %>'>
<meta itemprop='datePublished' content='<%= @topic_view.topic.created_at.to_formatted_s(:iso8601) %>'>
- <% if @topic_view.topic.category.present? %>
- <meta itemprop='articleSection' content='<%= @topic_view.topic.category.name %>'>
- <% end %>
<meta itemprop='keywords' content='<%= @tags.map(&:name).join(', ') %>'>
- <div itemprop='publisher' itemscope itemtype="http://schema.org/Organization">
- <meta itemprop='name' content='<%= SiteSetting.company_name.presence || SiteSetting.title %>'>
- <% if application_logo_url.present? %>
- <div itemprop='logo' itemscope itemtype="http://schema.org/ImageObject">
- <meta itemprop='url' content='<%= application_logo_url %>'>
- </div>
+ <% unless main_entity_schema %>
+ <% if @topic_view.topic.category.present? %>
+ <meta itemprop='articleSection' content='<%= @topic_view.topic.category.name %>'>
<% end %>
- </div>
+ <div itemprop='publisher' itemscope itemtype="http://schema.org/Organization">
+ <meta itemprop='name' content='<%= SiteSetting.company_name.presence || SiteSetting.title %>'>
+ <% if application_logo_url.present? %>
+ <div itemprop='logo' itemscope itemtype="http://schema.org/ImageObject">
+ <meta itemprop='url' content='<%= application_logo_url %>'>
+ </div>
+ <% end %>
+ </div>
+ <% end %>
+
+ <% if main_entity_schema %>
+ <div <%= main_entity_schema %>>
+ <% end %>
<% if @topic_view.crawler_posts&.first && !@topic_view.crawler_posts.first.is_first_post? %>
<% if @topic_view.topic.user.present? %>
@@ -63,7 +70,7 @@
<% @topic_view.crawler_posts.each do |post| %>
<% if (u = post.user) && !post.hidden && post.cooked && !post.cooked.strip.empty? %>
- <div id='post_<%= post.post_number %>' <%= post.is_first_post? ? "" : "itemprop='comment' itemscope itemtype='http://schema.org/Comment'".html_safe %> class='topic-body crawler-post'>
+ <div id='post_<%= post.post_number %>' <%= crawler_post_schema(post, @topic_view.topic) %> class='topic-body crawler-post'>
<div class='crawler-post-meta'>
<span class="creator" itemprop="author" itemscope itemtype="http://schema.org/Person">
<a itemprop="url" rel='nofollow' href='<%= Discourse.base_url %>/u/<%= u.username %>'><span itemprop='name'><%= u.username %></span></a>
@@ -78,7 +85,7 @@
<% end %>
</span>
- <% if post.is_first_post? %>
+ <% if post.is_first_post? && !main_entity_schema %>
<link itemprop="mainEntityOfPage" href="<%= post.topic.url %>">
<% end %>
@@ -122,6 +129,10 @@
</div>
<% end %>
<% end %>
+
+ <% if main_entity_schema %>
+ </div>
+ <% end %>
</div>
<% if @topic_view.single_post_request? || @topic_view.prev_page || @topic_view.next_page %>
diff --git a/plugins/discourse-solved/app/serializers/discourse_solved/question_schema_serializer.rb b/plugins/discourse-solved/app/serializers/discourse_solved/question_schema_serializer.rb
index d85627e0312..281e8e31d41 100644
--- a/plugins/discourse-solved/app/serializers/discourse_solved/question_schema_serializer.rb
+++ b/plugins/discourse-solved/app/serializers/discourse_solved/question_schema_serializer.rb
@@ -12,6 +12,11 @@ class DiscourseSolved::QuestionSchemaSerializer < ApplicationSerializer
root: false,
).serializable_hash
end
+ if suggested_answers.present?
+ hash["suggestedAnswer"] = suggested_answers.map do |post|
+ DiscourseSolved::AnswerSchemaSerializer.new(post, root: false).serializable_hash
+ end
+ end
hash
end
@@ -30,7 +35,7 @@ class DiscourseSolved::QuestionSchemaSerializer < ApplicationSerializer
end
def answerCount
- accepted_answer.present? ? 1 : 0
+ (accepted_answer.present? ? 1 : 0) + suggested_answers.to_a.size
end
def datePublished
@@ -46,4 +51,8 @@ class DiscourseSolved::QuestionSchemaSerializer < ApplicationSerializer
def accepted_answer
options[:accepted_answer]
end
+
+ def suggested_answers
+ options[:suggested_answers]
+ end
end
diff --git a/plugins/discourse-solved/app/services/discourse_solved/build_schema_markup.rb b/plugins/discourse-solved/app/services/discourse_solved/build_schema_markup.rb
index e32d2869ec8..39396ab779b 100644
--- a/plugins/discourse-solved/app/services/discourse_solved/build_schema_markup.rb
+++ b/plugins/discourse-solved/app/services/discourse_solved/build_schema_markup.rb
@@ -4,14 +4,16 @@ class DiscourseSolved::BuildSchemaMarkup
include Service::Base
params do
+ attribute :post_ids, :array
attribute :topic_id, :integer
validates :topic_id, presence: true
end
model :topic
policy :accepted_answers_allowed
- model :accepted_answer, optional: true
policy :schema_markup_enabled
+ model :accepted_answer, optional: true
+ model :suggested_answers, optional: true
model :html
private
@@ -24,27 +26,29 @@ class DiscourseSolved::BuildSchemaMarkup
guardian.allow_accepted_answers?(topic)
end
+ def schema_markup_enabled(topic:)
+ DiscourseSolved::SchemaUtils.schema_markup_enabled?(topic)
+ end
+
def fetch_accepted_answer(topic:)
topic.solved&.answer_post
end
- def schema_markup_enabled(accepted_answer:)
- case SiteSetting.solved_add_schema_markup
- when "never"
- false
- when "answered only"
- accepted_answer.present?
- else
- true
- end
+ def fetch_suggested_answers(params:, topic:, accepted_answer:)
+ excluded_ids = [topic.first_post.id]
+ excluded_ids << accepted_answer.id if accepted_answer.present?
+ scope = topic.posts.where.not(id: excluded_ids)
+ scope = scope.where(id: params.post_ids) if params.post_ids.present?
+ scope.where(post_type: Post.types[:regular], hidden: false).order(:post_number).to_a
end
- def fetch_html(topic:, accepted_answer:)
+ def fetch_html(topic:, accepted_answer:, suggested_answers:)
question_json =
DiscourseSolved::QuestionSchemaSerializer.new(
topic,
root: false,
accepted_answer: accepted_answer,
+ suggested_answers: suggested_answers,
).serializable_hash
json =
diff --git a/plugins/discourse-solved/lib/discourse_solved/schema_utils.rb b/plugins/discourse-solved/lib/discourse_solved/schema_utils.rb
new file mode 100644
index 00000000000..d2e76f5069f
--- /dev/null
+++ b/plugins/discourse-solved/lib/discourse_solved/schema_utils.rb
@@ -0,0 +1,49 @@
+# frozen_string_literal: true
+
+module DiscourseSolved
+ module SchemaUtils
+ # Generates Schema.org microdata attributes for the crawler view of solved topics.
+ #
+ # Structure: QAPage > Question (mainEntity) > acceptedAnswer / suggestedAnswer
+ #
+ # - First post gets no schema (its content bubbles up to the Question scope)
+ # - Small action posts get an isolated itemscope to prevent leaking into Question
+ # - The solved post is marked as acceptedAnswer, other replies as suggestedAnswer
+ #
+ # Spec: https://schema.org/QAPage
+ # Validator: https://validator.schema.org/
+ def self.schema_markup_enabled?(topic)
+ return false unless Guardian.new.allow_accepted_answers?(topic)
+
+ case SiteSetting.solved_add_schema_markup
+ when "never"
+ false
+ when "answered only"
+ topic.solved&.answer_post_id.present?
+ else
+ true
+ end
+ end
+
+ def self.container_schema(topic)
+ return nil if !schema_markup_enabled?(topic)
+ { itemscope: true, itemtype: "https://schema.org/QAPage" }
+ end
+
+ def self.main_entity_schema(topic)
+ return nil if !schema_markup_enabled?(topic)
+ { itemprop: "mainEntity", itemscope: true, itemtype: "https://schema.org/Question" }
+ end
+
+ def self.post_schema(post, topic)
+ return nil if !schema_markup_enabled?(topic)
+ return {} if post.is_first_post?
+ return { itemscope: true } if post.post_type == Post.types[:small_action]
+ if topic.solved&.answer_post_id == post.id
+ { itemprop: "acceptedAnswer", itemscope: true, itemtype: "https://schema.org/Answer" }
+ else
+ { itemprop: "suggestedAnswer", itemscope: true, itemtype: "https://schema.org/Answer" }
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-solved/plugin.rb b/plugins/discourse-solved/plugin.rb
index c0badbc8feb..c2f9008d192 100644
--- a/plugins/discourse-solved/plugin.rb
+++ b/plugins/discourse-solved/plugin.rb
@@ -77,12 +77,25 @@ after_initialize do
{ answer: { actions: %w[discourse_solved/answer#accept discourse_solved/answer#unaccept] } },
)
+ register_modifier(:topic_crawler_container_schema) do |schema, topic|
+ DiscourseSolved::SchemaUtils.container_schema(topic) || schema
+ end
+
+ register_modifier(:topic_crawler_main_entity_schema) do |schema, topic|
+ DiscourseSolved::SchemaUtils.main_entity_schema(topic) || schema
+ end
+
+ register_modifier(:topic_crawler_post_schema) do |schema, post, topic|
+ DiscourseSolved::SchemaUtils.post_schema(post, topic) || schema
+ end
+
register_html_builder("server:before-head-close-crawler") do |controller|
- topic_id = controller.instance_variable_get(:@topic_view)&.topic&.id
+ topic_view = controller.instance_variable_get(:@topic_view)
result =
DiscourseSolved::BuildSchemaMarkup.call(
params: {
- topic_id: topic_id,
+ topic_id: topic_view&.topic&.id,
+ post_ids: topic_view&.posts&.ids,
},
guardian: controller.guardian,
)
@@ -90,11 +103,12 @@ after_initialize do
end
register_html_builder("server:before-head-close") do |controller|
- topic_id = controller.instance_variable_get(:@topic_view)&.topic&.id
+ topic_view = controller.instance_variable_get(:@topic_view)
result =
DiscourseSolved::BuildSchemaMarkup.call(
params: {
- topic_id: topic_id,
+ topic_id: topic_view&.topic&.id,
+ post_ids: topic_view&.posts&.ids,
},
guardian: controller.guardian,
)
diff --git a/plugins/discourse-solved/spec/requests/topics_controller_spec.rb b/plugins/discourse-solved/spec/requests/topics_controller_spec.rb
index 430b00f43b5..20a286fd663 100644
--- a/plugins/discourse-solved/spec/requests/topics_controller_spec.rb
+++ b/plugins/discourse-solved/spec/requests/topics_controller_spec.rb
@@ -97,6 +97,77 @@ RSpec.describe TopicsController do
end
end
+ describe "crawler schema modifiers" do
+ let(:crawler_env) { { "HTTP_USER_AGENT" => "Googlebot" } }
+
+ before { SiteSetting.allow_solved_on_all_topics = true }
+
+ def parsed_crawler_body
+ Nokogiri::HTML5.fragment(response.body)
+ end
+
+ it "uses Question schema instead of DiscussionForumPosting" do
+ get "/t/#{topic.slug}/#{topic.id}", env: crawler_env
+ doc = parsed_crawler_body
+
+ expect(doc.css('[itemtype*="Question"]').size).to eq(1)
+ expect(doc.css('[itemtype*="DiscussionForumPosting"]').size).to eq(0)
+ end
+
+ it "marks the solution post as acceptedAnswer and other replies as suggestedAnswer" do
+ p3 = Fabricate(:post, topic: topic, user: Fabricate(:user))
+ Fabricate(:solved_topic, topic: topic, answer_post: p2)
+
+ get "/t/#{topic.slug}/#{topic.id}", env: crawler_env
+ doc = parsed_crawler_body
+
+ expect(doc.at_css("#post_#{p2.post_number}")["itemprop"]).to eq("acceptedAnswer")
+ expect(doc.at_css("#post_#{p3.post_number}")["itemprop"]).to eq("suggestedAnswer")
+ end
+
+ it "does not modify schema for topics without solved enabled" do
+ SiteSetting.allow_solved_on_all_topics = false
+
+ get "/t/#{topic.slug}/#{topic.id}", env: crawler_env
+ doc = parsed_crawler_body
+
+ expect(doc.css('[itemtype*="DiscussionForumPosting"]').size).to eq(1)
+ expect(doc.css('[itemtype*="Question"]').size).to eq(0)
+ end
+
+ it "does not emit QAPage schema when schema markup is set to never" do
+ SiteSetting.solved_add_schema_markup = "never"
+
+ get "/t/#{topic.slug}/#{topic.id}", env: crawler_env
+ doc = parsed_crawler_body
+
+ expect(doc.css('[itemtype*="DiscussionForumPosting"]').size).to eq(1)
+ expect(doc.css('[itemtype*="QAPage"]').size).to eq(0)
+ expect(doc.css('[itemtype*="Question"]').size).to eq(0)
+ end
+
+ it "does not emit QAPage schema when set to 'answered only' without an accepted answer" do
+ SiteSetting.solved_add_schema_markup = "answered only"
+
+ get "/t/#{topic.slug}/#{topic.id}", env: crawler_env
+ doc = parsed_crawler_body
+
+ expect(doc.css('[itemtype*="DiscussionForumPosting"]').size).to eq(1)
+ expect(doc.css('[itemtype*="QAPage"]').size).to eq(0)
+ end
+
+ it "emits QAPage schema when set to 'answered only' with an accepted answer" do
+ SiteSetting.solved_add_schema_markup = "answered only"
+ Fabricate(:solved_topic, topic: topic, answer_post: p2)
+
+ get "/t/#{topic.slug}/#{topic.id}", env: crawler_env
+ doc = parsed_crawler_body
+
+ expect(doc.css('[itemtype*="QAPage"]').size).to eq(1)
+ expect(doc.css('[itemtype*="Question"]').size).to eq(1)
+ end
+ end
+
context "with solved enabled for topics with specific tags" do
let(:tag) { Fabricate(:tag) }
diff --git a/plugins/discourse-solved/spec/serializers/discourse_solved/question_schema_serializer_spec.rb b/plugins/discourse-solved/spec/serializers/discourse_solved/question_schema_serializer_spec.rb
index a7d10b4faba..067e42d7f07 100644
--- a/plugins/discourse-solved/spec/serializers/discourse_solved/question_schema_serializer_spec.rb
+++ b/plugins/discourse-solved/spec/serializers/discourse_solved/question_schema_serializer_spec.rb
@@ -26,8 +26,9 @@ RSpec.describe DiscourseSolved::QuestionSchemaSerializer do
)
end
- it "does not include acceptedAnswer" do
+ it "does not include acceptedAnswer or suggestedAnswer" do
expect(json).not_to have_key("acceptedAnswer")
+ expect(json).not_to have_key("suggestedAnswer")
end
end
@@ -55,4 +56,24 @@ RSpec.describe DiscourseSolved::QuestionSchemaSerializer do
)
end
end
+
+ context "with suggested answers" do
+ fab!(:answer_post) { Fabricate(:post, topic: topic) }
+ fab!(:suggested_post) { Fabricate(:post, topic: topic) }
+
+ subject(:json) do
+ described_class
+ .new(topic, root: false, accepted_answer: answer_post, suggested_answers: [suggested_post])
+ .serializable_hash
+ .deep_stringify_keys
+ end
+
+ it "sets answerCount to total of accepted and suggested" do
+ expect(json["answerCount"]).to eq(2)
+ end
+
+ it "includes suggestedAnswer as an array of Answer objects" do
+ expect(json["suggestedAnswer"].sole["@type"]).to eq("Answer")
+ end
+ end
end
diff --git a/plugins/discourse-solved/spec/services/discourse_solved/build_schema_markup_spec.rb b/plugins/discourse-solved/spec/services/discourse_solved/build_schema_markup_spec.rb
index 8c231d09ad7..513b33cc46f 100644
--- a/plugins/discourse-solved/spec/services/discourse_solved/build_schema_markup_spec.rb
+++ b/plugins/discourse-solved/spec/services/discourse_solved/build_schema_markup_spec.rb
@@ -52,10 +52,34 @@ RSpec.describe DiscourseSolved::BuildSchemaMarkup do
expect(html).to include('"@type":"QAPage"')
expect(html).to include('"answerCount":0')
expect(html).not_to include('"acceptedAnswer"')
+ expect(html).not_to include('"suggestedAnswer"')
+ end
+
+ context "when the topic has replies" do
+ fab!(:reply) { Fabricate(:post, topic: topic) }
+
+ it "includes replies as suggested answers" do
+ html = result[:html]
+ expect(html).to include('"suggestedAnswer"')
+ expect(html).to include('"answerCount":1')
+ expect(html).not_to include('"acceptedAnswer"')
+ end
+ end
+
+ context "when the topic has hidden replies" do
+ fab!(:hidden_reply) { Fabricate(:post, topic: topic, hidden: true) }
+ fab!(:visible_reply) { Fabricate(:post, topic: topic) }
+
+ it "excludes hidden posts from suggested answers" do
+ html = result[:html]
+ expect(html).to include('"answerCount":1')
+ expect(html).to include(visible_reply.user.username)
+ expect(html).not_to include(hidden_reply.user.username)
+ end
end
end
- context "when there is an accepted answer" do
+ context "when there is an accepted answer but no other answers" do
fab!(:answer_user, :user)
fab!(:answer_post) { Fabricate(:post, topic: topic, user: answer_user, like_count: 3) }
@@ -66,13 +90,37 @@ RSpec.describe DiscourseSolved::BuildSchemaMarkup do
it { is_expected.to run_successfully }
- it "builds QAPage markup with the accepted answer" do
+ it "builds QAPage markup with just the accepted answer" do
html = result[:html]
expect(html).to include('"@type":"QAPage"')
expect(html).to include('"answerCount":1')
expect(html).to include('"acceptedAnswer"')
expect(html).to include('"@type":"Answer"')
expect(html).to include(answer_user.username)
+ expect(html).not_to include('"suggestedAnswer"')
+ end
+ end
+
+ context "when there is an accepted answer and suggested answers" do
+ fab!(:answer_user, :user)
+ fab!(:answer_post) { Fabricate(:post, topic: topic, user: answer_user, like_count: 3) }
+ fab!(:suggested_user, :user)
+ fab!(:suggested_post) { Fabricate(:post, topic: topic, user: suggested_user, like_count: 1) }
+
+ before do
+ SiteSetting.solved_add_schema_markup = "always"
+ Fabricate(:solved_topic, topic: topic, answer_post: answer_post)
+ end
+
+ it { is_expected.to run_successfully }
+
+ it "builds QAPage markup with both accepted and suggested answers" do
+ html = result[:html]
+ expect(html).to include('"answerCount":2')
+ expect(html).to include('"acceptedAnswer"')
+ expect(html).to include('"suggestedAnswer"')
+ expect(html).to include(answer_user.username)
+ expect(html).to include(suggested_user.username)
end
end
end
diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb
index 2f0f50fa8ac..1188bc3ce9a 100644
--- a/spec/helpers/application_helper_spec.rb
+++ b/spec/helpers/application_helper_spec.rb
@@ -1269,4 +1269,31 @@ RSpec.describe ApplicationHelper do
end
end
end
+
+ describe "#crawler_topic_container_schema" do
+ fab!(:topic)
+
+ it "returns DiscussionForumPosting schema attributes by default" do
+ result = helper.crawler_topic_container_schema(topic)
+ expect(result).to include("itemscope")
+ expect(result).to include('itemtype="http://schema.org/DiscussionForumPosting"')
+ end
+ end
+
+ describe "#crawler_post_schema" do
+ fab!(:topic)
+ fab!(:first_post) { Fabricate(:post, topic: topic) }
+ fab!(:reply) { Fabricate(:post, topic: topic) }
+
+ it "returns empty string for the first post" do
+ expect(helper.crawler_post_schema(first_post, topic)).to eq("")
+ end
+
+ it "returns Comment schema attributes for reply posts" do
+ result = helper.crawler_post_schema(reply, topic)
+ expect(result).to include('itemprop="comment"')
+ expect(result).to include("itemscope")
+ expect(result).to include('itemtype="http://schema.org/Comment"')
+ end
+ end
end