Skip to content

Commit

Permalink
- Use Nokogiri to parse truncated HTML content and re-format as valid…
Browse files Browse the repository at this point in the history
… HTML to avoid unclosed tags in truncated content
  • Loading branch information
j-corry committed Aug 29, 2024
1 parent 589f86e commit 8c1372b
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 143 deletions.
4 changes: 4 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ module ApplicationHelper
# ## We set the date display format.
DATE_DISPLAY_FORMAT = '%A, %e %B %Y'

def format_html(html, truncate_words)
Nokogiri::HTML::DocumentFragment.parse(html.truncate_words(truncate_words)).to_html
end

def boolean_yes_no(boolean)
# outputs 'Yes' or 'No' strings from a boolean (an instance of Ruby TrueClass or FalseClass)
# for presenting output of methods such as WrittenQuestion transferred?
Expand Down
3 changes: 1 addition & 2 deletions app/views/search/results/item_details/_content.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<% unless content.blank? %>
<div class="item-details">
<span>
<%= raw content[:value].truncate_words(50) %>
<!-- Nokogiri::HTML::DocumentFragment.parse(html).to_html-->
<%= raw format_html(content[:value], 50) %>
</span>
</div>
<% end %>
2 changes: 1 addition & 1 deletion coverage/.last_run.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"result": {
"line": 83.02
"line": 83.04
}
}
6 changes: 5 additions & 1 deletion coverage/.resultset.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
1,
null,
1,
2,
null,
null,
1,
null,
null,
null,
Expand Down Expand Up @@ -3661,6 +3665,6 @@
]
}
},
"timestamp": 1724855850
"timestamp": 1724915616
}
}
Loading

0 comments on commit 8c1372b

Please sign in to comment.