diff --git a/app/presenters/manual_section_presenter.rb b/app/presenters/manual_section_presenter.rb index 97e6a1499..27324a54d 100644 --- a/app/presenters/manual_section_presenter.rb +++ b/app/presenters/manual_section_presenter.rb @@ -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? @@ -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