Skip to content

Commit

Permalink
Use the page title as the default FAQPage question
Browse files Browse the repository at this point in the history
Previously we used "Summary" to name a section of html that didn't have
an h2 tag preceding it.  Unfortunately, Google appears to not include
sections in rich results unless all the text actually appears on the page.

We still get rich snippets, we're just missing the first section.  Hopefully
this will be remedied by using the page title text instead.

This wasn't flagged by the rich text testing tool.
  • Loading branch information
sihugh committed Sep 26, 2019
1 parent f8db0a0 commit dd2a678
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

## Unreleased

* Fix some minor issues with the FAQPage schema ([PR #1142](https://github.com/alphagov/govuk_publishing_components/pull/1142))
* Add error state to select component ([PR #1141](https://github.com/alphagov/govuk_publishing_components/pull/1141))
* Add size option to label ([PR #1140](https://github.com/alphagov/govuk_publishing_components/pull/1140))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def questions_and_answers_markup

# Generates a hash of questions and associated information:
# - question: the text in the h2 tag preceding other markup. Questions are
# used to key the hash. "Summary" is set as the default, as
# used to key the hash. The page title is set as the default, as
# there is often a preamble in guides before any h2 is set.
#
# - :answer: the markup that is not an h2 tag. It is associated with the
Expand All @@ -55,7 +55,7 @@ def questions_and_answers_markup
def question_and_answers(html)
doc = Nokogiri::HTML(html)

question = "Summary"
question = page.title

# rubocop:disable Style/IfInsideElse
doc.xpath("html/body").children.each_with_object({}) do |element, q_and_as|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
expect(q_and_a.second["acceptedAnswer"]["text"].strip).to eq("<p>Give it a treat</p>")
end

it "handles missing h2s at the start of the body" do
it "handles missing h2s at the start of the body by using the page title" do
part_body = "<p>First catch your dragon</p>
<h2 id='step-two'>Step two</h2>
<p>Give it a treat</p>
Expand All @@ -109,7 +109,7 @@
body: part_body
).structured_data['mainEntity']

expect(q_and_a.first["name"]).to eq("Summary")
expect(q_and_a.first["name"]).to eq("How to train your dragon")
expect(q_and_a.first["url"]).to eq("http://www.dev.gov.uk/how-to-train-your-dragon")
expect(q_and_a.first["acceptedAnswer"]["text"].strip).to eq("<p>First catch your dragon</p>")
end
Expand Down Expand Up @@ -151,6 +151,7 @@ def dragon_guide
canonical_url: "http://www.dev.gov.uk/how-to-train-your-dragon/insurance"
) do |random_item|
random_item.merge(
"title" => "How to train your dragon",
"base_path" => "/how-to-train-your-dragon"
)
end
Expand Down

0 comments on commit dd2a678

Please sign in to comment.