Skip to content

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

FIX: Use the full post text in Solved's QAPage JSON-LD (#45460)

Arpit Jalaned6a1b80ef3a
diff --git a/plugins/discourse-solved/app/serializers/discourse_solved/answer_schema_serializer.rb b/plugins/discourse-solved/app/serializers/discourse_solved/answer_schema_serializer.rb
index 4990965b9c4..1ca61b4b930 100644
--- a/plugins/discourse-solved/app/serializers/discourse_solved/answer_schema_serializer.rb
+++ b/plugins/discourse-solved/app/serializers/discourse_solved/answer_schema_serializer.rb
@@ -9,8 +9,7 @@ class DiscourseSolved::AnswerSchemaSerializer < ApplicationSerializer
end
def text
- object.excerpt(nil, keep_onebox_body: true).presence ||
- object.excerpt(nil, keep_onebox_body: true, keep_quotes: true)
+ DiscourseSolved::SchemaUtils.post_text(object)
end
def upvoteCount
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 446a20b7a5b..2adefe95b1e 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
@@ -24,9 +24,7 @@ class DiscourseSolved::QuestionSchemaSerializer < ApplicationSerializer
end
def text
- first_post = object.first_post
- first_post.excerpt(nil, keep_onebox_body: true).presence ||
- first_post.excerpt(nil, keep_onebox_body: true, keep_quotes: true)
+ DiscourseSolved::SchemaUtils.post_text(object.first_post)
end
def include_text?
diff --git a/plugins/discourse-solved/lib/discourse_solved/schema_utils.rb b/plugins/discourse-solved/lib/discourse_solved/schema_utils.rb
index aef78302f33..68ebd108eec 100644
--- a/plugins/discourse-solved/lib/discourse_solved/schema_utils.rb
+++ b/plugins/discourse-solved/lib/discourse_solved/schema_utils.rb
@@ -83,6 +83,41 @@ module DiscourseSolved
post.cooked.present? && Nokogiri::HTML5.fragment(post.cooked).text.strip.present?
end
+ # The full plain text of a post, for the Question and Answer `text` properties. Google's
+ # Q&A guidelines ask for the complete question and answer, so unlike the excerpts used
+ # elsewhere this has no length limit and nothing is folded away. Quotes are left out
+ # unless the post is nothing but a quote, and onebox bodies are kept, as the excerpt did
+ # before.
+ def self.post_text(post)
+ cooked = post.cooked.to_s
+ return "" if cooked.blank?
+
+ # ExcerptParser keeps only the contents of an [excerpt] block and only the summary of a
+ # [details] block. The full text needs both, so unwrap them before parsing. The summary
+ # becomes a paragraph so it stays separated from the body it introduces.
+ doc = Nokogiri::HTML5.fragment(cooked)
+ doc.css(".excerpt").each { |node| node.remove_class("excerpt") }
+ doc
+ .css("details")
+ .each do |details|
+ details.css("summary").each { |summary| summary.name = "p" }
+ details.replace(details.children)
+ end
+ html = doc.to_html
+
+ options = {
+ strip_links: true,
+ text_entities: true,
+ plain_hashtags: true,
+ keep_onebox_body: true,
+ post:,
+ }
+ # The plain text is never longer than the HTML it comes from, so the HTML's length acts
+ # as "no limit", the same way ExcerptParser treats custom excerpts.
+ Post.excerpt(html, html.length, options).presence ||
+ Post.excerpt(html, html.length, options.merge(keep_quotes: true))
+ end
+
private_class_method def self.accepted_answer_visible?(topic)
topic.solved&.answer_posts&.any? { |post| Guardian.new.can_see_post?(post) }
end
diff --git a/plugins/discourse-solved/spec/lib/discourse_solved/schema_utils_spec.rb b/plugins/discourse-solved/spec/lib/discourse_solved/schema_utils_spec.rb
index 5fc85689829..707b912a042 100644
--- a/plugins/discourse-solved/spec/lib/discourse_solved/schema_utils_spec.rb
+++ b/plugins/discourse-solved/spec/lib/discourse_solved/schema_utils_spec.rb
@@ -29,4 +29,61 @@ RSpec.describe DiscourseSolved::SchemaUtils do
expect(described_class.qa_page_schema?(topic)).to eq(false)
end
end
+
+ describe ".post_text" do
+ fab!(:topic, :topic_with_op)
+
+ it "returns the whole post rather than an excerpt" do
+ words = Array.new(120) { |i| "word#{i}" }.join(" ")
+ post = Fabricate(:post, topic:, raw: words)
+
+ expect(words.length).to be > SiteSetting.post_excerpt_maxlength
+ expect(described_class.post_text(post)).to eq(words)
+ end
+
+ it "returns plain text, with links, entities and code blocks flattened" do
+ post =
+ Fabricate(
+ :post,
+ topic:,
+ raw: "See [the docs](https://example.com/docs) & ask\n\n```\nputs 1 < 2\n```",
+ )
+
+ expect(described_class.post_text(post)).to eq("See the docs & ask\n\nputs 1 < 2")
+ end
+
+ it "leaves quotes out unless the post is nothing but a quote" do
+ quoting_post = Fabricate(:post, topic:, raw: "[quote]\nquoted words\n[/quote]\n\nown words")
+ quote_only_post = Fabricate(:post, topic:, raw: "[quote]\nquoted words\n[/quote]")
+
+ expect(described_class.post_text(quoting_post)).to eq("own words")
+ expect(described_class.post_text(quote_only_post)).to eq("quoted words")
+ end
+
+ it "keeps the text around an [excerpt] block" do
+ post =
+ Fabricate(
+ :post,
+ topic:,
+ raw: "Context before.\n\n[excerpt]Summary[/excerpt]\n\nActual question here.",
+ )
+
+ expect(described_class.post_text(post)).to eq(
+ "Context before.\n\nSummary\n\nActual question here.",
+ )
+ end
+
+ it "keeps the body of a details block, not just its summary" do
+ post =
+ Fabricate(
+ :post,
+ topic:,
+ raw: "[details=\"Logs\"]\nhidden lines\n[/details]\n\nVisible after.",
+ cooked:
+ "<details>\n<summary>Logs</summary>\n<p>hidden lines</p>\n</details>\n<p>Visible after.</p>",
+ )
+
+ expect(described_class.post_text(post)).to eq("Logs\n\nhidden lines\n\nVisible after.")
+ end
+ end
end
diff --git a/plugins/discourse-solved/spec/serializers/discourse_solved/answer_schema_serializer_spec.rb b/plugins/discourse-solved/spec/serializers/discourse_solved/answer_schema_serializer_spec.rb
index 358c1706a5e..31ac0ad3ab1 100644
--- a/plugins/discourse-solved/spec/serializers/discourse_solved/answer_schema_serializer_spec.rb
+++ b/plugins/discourse-solved/spec/serializers/discourse_solved/answer_schema_serializer_spec.rb
@@ -21,4 +21,17 @@ RSpec.describe DiscourseSolved::AnswerSchemaSerializer do
{ "@type" => "Person", "name" => post.user.username, "url" => post.user.full_url },
)
end
+
+ context "with a post longer than the excerpt limit" do
+ fab!(:long_post) { Fabricate(:post, raw: Array.new(120) { |i| "word#{i}" }.join(" ")) }
+
+ subject(:json) do
+ described_class.new(long_post, root: false).serializable_hash.deep_stringify_keys
+ end
+
+ it "uses the full text of the post" do
+ expect(long_post.raw.length).to be > SiteSetting.post_excerpt_maxlength
+ expect(json["text"]).to eq(long_post.raw)
+ end
+ end
end
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 21941343398..8567c21b220 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
@@ -90,4 +90,20 @@ RSpec.describe DiscourseSolved::QuestionSchemaSerializer do
expect(suggested["upvoteCount"]).to eq(0)
end
end
+
+ context "with a first post longer than the excerpt limit" do
+ fab!(:long_topic, :topic)
+ fab!(:long_post) do
+ Fabricate(:post, topic: long_topic, raw: Array.new(120) { |i| "word#{i}" }.join(" "))
+ end
+
+ subject(:json) do
+ described_class.new(long_topic, root: false).serializable_hash.deep_stringify_keys
+ end
+
+ it "uses the full text of the first post" do
+ expect(long_post.raw.length).to be > SiteSetting.post_excerpt_maxlength
+ expect(json["text"]).to eq(long_post.raw)
+ 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 b7a041fba28..c15c8404ff5 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
@@ -315,5 +315,35 @@ RSpec.describe DiscourseSolved::BuildSchemaMarkup do
expect(html).to include(suggested_user.username)
end
end
+
+ context "when the question and the accepted answer are longer than the excerpt limit" do
+ fab!(:long_topic) { Fabricate(:topic, user:) }
+ fab!(:question_post) do
+ Fabricate(
+ :post,
+ topic: long_topic,
+ user:,
+ raw: Array.new(120) { |i| "question#{i}" }.join(" "),
+ )
+ end
+ fab!(:answer_post) do
+ Fabricate(:post, topic: long_topic, raw: Array.new(120) { |i| "answer#{i}" }.join(" "))
+ end
+
+ let(:params) { { topic_id: long_topic.id } }
+
+ before do
+ SiteSetting.solved_add_schema_markup = "always"
+ Fabricate(:solved_topic, topic: long_topic, answer_post:)
+ end
+
+ it "includes the full text of the question and the answer" do
+ fragment = Nokogiri::HTML5.fragment(result[:html])
+ schema = JSON.parse(fragment.at_css('script[type="application/ld+json"]').content)
+
+ expect(schema.dig("mainEntity", "text")).to eq(question_post.raw)
+ expect(schema.dig("mainEntity", "acceptedAnswer", 0, "text")).to eq(answer_post.raw)
+ end
+ end
end
end