Skip to content

Commit

Permalink
Merge pull request #3516 from alphagov/faster-manuals
Browse files Browse the repository at this point in the history
Memoize manual section presenter
  • Loading branch information
richardTowers authored Jan 15, 2025
2 parents 749b29c + 5e06f8b commit 2eeac75
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions app/presenters/manual_section_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ def base_path
def intro
return nil unless details["body"]

intro = Nokogiri::HTML::DocumentFragment.parse(details["body"])
@intro ||= begin
intro = Nokogiri::HTML::DocumentFragment.parse(details["body"])

# Strip all content following and including h2
intro.css("h2").xpath("following-sibling::*").remove
intro.css("h2").remove
# Strip all content following and including h2
intro.css("h2").xpath("following-sibling::*").remove
intro.css("h2").remove

intro.text.squeeze == "\n" ? "" : intro
intro.text.squeeze == "\n" ? "" : intro
end
end

def visually_expanded?
Expand All @@ -29,28 +31,30 @@ def visually_expanded?
def main
return nil unless details["body"]

document = Nokogiri::HTML::DocumentFragment.parse(details["body"])
@main ||= begin
document = Nokogiri::HTML::DocumentFragment.parse(details["body"])

# Identifies all h2's and creates an array of objects from the heading and
# its proceeding content up to the next heading. This is so that it can be
# consumed by accordion components in the template.
document.css("h2").map do |heading|
content = []
heading.xpath("following-sibling::*").each do |element|
if element.name == "h2"
break
else
content << element.to_html
# Identifies all h2's and creates an array of objects from the heading and
# its proceeding content up to the next heading. This is so that it can be
# consumed by accordion components in the template.
document.css("h2").map do |heading|
content = []
heading.xpath("following-sibling::*").each do |element|
if element.name == "h2"
break
else
content << element.to_html
end
end
end

{
heading: {
text: heading.text,
id: heading[:id],
},
content: content.join,
}
{
heading: {
text: heading.text,
id: heading[:id],
},
content: content.join,
}
end
end
end

Expand Down

0 comments on commit 2eeac75

Please sign in to comment.