Skip to content

Commit

Permalink
FIX: make JSON_LD schema consistent with our microdata in core (#269)
Browse files Browse the repository at this point in the history
* FIX: make JSON_LD schema consistent with our microdata in core

https://meta.discourse.org/t/alignment-of-schema-org-markup-between-qa-solved-dfp-core/279510
  • Loading branch information
Arpit Jalan committed Nov 22, 2023
1 parent bc9b8c4 commit ad1b7c9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 5 additions & 3 deletions plugin.rb
Expand Up @@ -303,10 +303,11 @@ def before_head_close_meta(controller)
"text" => get_schema_text(first_post),
"upvoteCount" => first_post.like_count,
"answerCount" => 0,
"dateCreated" => topic.created_at,
"datePublished" => topic.created_at,
"author" => {
"@type" => "Person",
"name" => topic.user&.name,
"name" => topic.user&.username,
"url" => topic.user&.full_url,
},
}

Expand All @@ -319,11 +320,12 @@ def before_head_close_meta(controller)
"@type" => "Answer",
"text" => get_schema_text(accepted_answer),
"upvoteCount" => accepted_answer.like_count,
"dateCreated" => accepted_answer.created_at,
"datePublished" => accepted_answer.created_at,
"url" => accepted_answer.full_url,
"author" => {
"@type" => "Person",
"name" => accepted_answer.user&.username,
"url" => accepted_answer.user&.full_url,
},
}
else
Expand Down
8 changes: 5 additions & 3 deletions spec/requests/topics_controller_spec.rb
Expand Up @@ -10,28 +10,30 @@
def schema_json(answerCount)
if answerCount > 0
answer_json =
',"acceptedAnswer":{"@type":"Answer","text":"%{answer_text}","upvoteCount":%{answer_likes},"dateCreated":"%{answered_at}","url":"%{answer_url}","author":{"@type":"Person","name":"%{username2}"}}' %
',"acceptedAnswer":{"@type":"Answer","text":"%{answer_text}","upvoteCount":%{answer_likes},"datePublished":"%{answered_at}","url":"%{answer_url}","author":{"@type":"Person","name":"%{username2}","url":"%{user2_url}"}}' %
{
answer_text: p2.excerpt,
answer_likes: p2.like_count,
answered_at: p2.created_at.as_json,
answer_url: p2.full_url,
username2: p2.user&.username,
user2_url: p2.user&.full_url,
}
else
answer_json = ""
end

# rubocop:todo Layout/LineLength
'<script type="application/ld+json">{"@context":"http://schema.org","@type":"QAPage","name":"%{title}","mainEntity":{"@type":"Question","name":"%{title}","text":"%{question_text}","upvoteCount":%{question_likes},"answerCount":%{answerCount},"dateCreated":"%{created_at}","author":{"@type":"Person","name":"%{username1}"}%{answer_json}}}</script>' %
'<script type="application/ld+json">{"@context":"http://schema.org","@type":"QAPage","name":"%{title}","mainEntity":{"@type":"Question","name":"%{title}","text":"%{question_text}","upvoteCount":%{question_likes},"answerCount":%{answerCount},"datePublished":"%{created_at}","author":{"@type":"Person","name":"%{username1}","url":"%{user1_url}"}%{answer_json}}}</script>' %
# rubocop:enable Layout/LineLength
{
title: topic.title,
question_text: p1.excerpt,
question_likes: p1.like_count,
answerCount: answerCount,
created_at: topic.created_at.as_json,
username1: topic.user&.name,
username1: topic.user&.username,
user1_url: topic.user&.full_url,
answer_json: answer_json,
}
end
Expand Down

0 comments on commit ad1b7c9

Please sign in to comment.