Skip to content

Commit

Permalink
Fix FAQPage schemas with empty answers
Browse files Browse the repository at this point in the history
Some pages have adjacent h2 tags which means that we will be generating
question and answer pairs that don't actually have an answer.

(An answer in an FAQPage schema is the html that resides after an h2 tag,
the "question" is the text of the h2 tag)

An example page is https://www.gov.uk/check-a-university-is-officially-recognised/listed-bodies
where there is an A-Z list with no content between the V and W h2 titles.
  • Loading branch information
sihugh committed Sep 26, 2019
1 parent 25c6a0c commit f8db0a0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def main_entity
end

def questions_and_answers_markup
question_and_answers(page.body).map do |question, value|
question_and_answers(page.body)
.select { |_, value| value[:answer].present? }
.map do |question, value|
q_and_a_url = section_url(value[:anchor])

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@

content_item = dragon_guide


q_and_a = generate_structured_data(
content_item: content_item,
schema: :faq,
Expand All @@ -115,6 +114,24 @@
expect(q_and_a.first["acceptedAnswer"]["text"].strip).to eq("<p>First catch your dragon</p>")
end

it "does not include questions when there is no associated answer" do
part_body = "<p>First catch your dragon</p>
<h2 id='step-two'>Step two</h2>
<h2 id='step-three'>Step three</h2>
<p>Give it a pat (wear gloves)</p>"

content_item = dragon_guide

q_and_a = generate_structured_data(
content_item: content_item,
schema: :faq,
body: part_body
).structured_data['mainEntity']

expect(q_and_a.count).to eq(2)
expect(q_and_a.map { |faq| faq["name"] }).to_not include("Step two")
end

it "handles an empty body to ensure that preview works OK" do
empty_part_body = ""
content_item = dragon_guide
Expand Down

0 comments on commit f8db0a0

Please sign in to comment.