Skip to content

Commit

Permalink
Fix broken anchor links on brexit child taxon pages
Browse files Browse the repository at this point in the history
The section headings need ID's so that the anchor links in the content
list can find them.
  • Loading branch information
hannako committed Jun 11, 2021
1 parent f7a7525 commit fa5b600
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
13 changes: 10 additions & 3 deletions app/lib/body_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def raw_title_and_link_sections
def parsed_links(section)
raw_links = section.css("a") if section.present?
if raw_links.any?
raw_links.map{ |link| parse_link(link) }
raw_links.map { |link| parse_link(link) }
else
[]
end
Expand All @@ -58,8 +58,15 @@ def parse_link(raw_link)
end

def parsed_title(section)
raw_title = section.present? ? section.at_css("h2") : ""
raw_title.present? ? raw_title.content : ""
parsed = { text: "", id: "" }

raw_header = section.at_css("h2") if section.present?
if raw_header.present?
id = raw_header.attribute("id")
parsed[:text] = raw_header.content
parsed[:id] = id.value if id.present?
end
parsed
end

def format_path(raw_url)
Expand Down
7 changes: 4 additions & 3 deletions app/views/content_items/detailed_guide/_brexit_links.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<% @content_item.title_and_link_sections.each do |section| %>
<% if section[:title].present? %>
<% if section[:title][:text].present? %>
<%= render "govuk_publishing_components/components/heading", {
text: section[:title],
text: section[:title][:text],
heading_level: 2,
font_size: "m",
margin_bottom: 6,
id: section[:title][:id],
} %>
<% end %>
<% if section[:links].present? %>
Expand All @@ -13,7 +14,7 @@
link_to(link[:text], link[:path], class: "govuk-link", data: {
track_action: link[:path],
track_category: track_category,
track_label: section[:title] || "",
track_label: section[:title][:text] || "",
module: 'gem-track-click',
})
end
Expand Down
15 changes: 12 additions & 3 deletions test/unit/body_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ def subject
test "#title_and_link_sections" do
expected = [
{
title: "Travel to the EU",
title: {
text: "Travel to the EU",
id: "travel-to-the-eu",
},
links: [
{
path: "/foreign-travel-advice",
Expand All @@ -57,7 +60,10 @@ def subject
],
},
{
title: "Travel to the UK",
title: {
text: "Travel to the UK",
id: "travel-to-the-uk",
},
links: [
{
path: "/local-travel-advice",
Expand All @@ -78,7 +84,10 @@ def subject
subject = BodyParser.new(html_missing_section_headings)
expected = [
{
title: "",
title: {
text: "",
id: "",
},
links: [
{
path: "/foreign-travel-advice",
Expand Down

0 comments on commit fa5b600

Please sign in to comment.