Skip to content

Commit

Permalink
Memoize ManualSectionPresenter#intro
Browse files Browse the repository at this point in the history
This saves about 70ms re-parsing the body, as the intro method is called
twice.
  • Loading branch information
richardTowers committed Jan 15, 2025
1 parent ba8e38f commit 0821ecc
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions app/presenters/manual_section_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ def base_path
end

def intro
return nil unless details["body"]
@intro ||= begin
return nil unless details["body"]

intro = Nokogiri::HTML::DocumentFragment.parse(details["body"])
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 Down

0 comments on commit 0821ecc

Please sign in to comment.