This commit is contained in:
David Tsiklauri 2025-03-10 21:20:46 +04:00 committed by GitHub
commit bc46db7077
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 64 additions and 9 deletions

View file

@ -2,7 +2,7 @@
<%= render :partial => "diary_entry_heading", :object => diary_entry, :as => "diary_entry" %>
<div class="richtext text-break" xml:lang="<%= diary_entry.language_code %>" lang="<%= diary_entry.language_code %>">
<%= diary_entry.body.to_html %>
<%= truncated ? diary_entry.body.to_html(2000) : diary_entry.body.to_html %>
</div>
<% if diary_entry.latitude and diary_entry.longitude %>
@ -12,6 +12,7 @@
<nav class='secondary-actions'>
<ul class='clearfix'>
<% if params[:action] == 'index' %>
<li><%= link_to t(".full_entry"), diary_entry_path(diary_entry.user, diary_entry) %></li>
<li><%= link_to t(".comment_link"), diary_entry_path(diary_entry.user, diary_entry, :anchor => "newcomment") %></li>
<li><%= link_to t(".reply_link"), new_message_path(diary_entry.user, :message => { :title => "Re: #{diary_entry.title}" }) %></li>
<li>

View file

@ -1,7 +1,7 @@
<turbo-frame id="pagination" target="_top" data-turbo="false">
<h4><%= t ".recent_entries" %></h4>
<%= render @entries %>
<%= render @entries, :truncated => true %>
<%= render "shared/pagination",
:newer_id => @newer_entries_id,

View file

@ -14,7 +14,7 @@
</div>
<% end %>
<%= render @entry %>
<%= render @entry, :truncated => false %>
<%= social_share_buttons(:title => @entry.title, :url => diary_entry_url(@entry.user, @entry)) %>
<div id="comments" class="comments mb-3 overflow-hidden">

View file

@ -606,6 +606,7 @@ en:
diary_entry:
posted_by_html: "Posted by %{link_user} on %{created} in %{language_link}."
updated_at_html: "Last updated on %{updated}."
full_entry: See full entry
comment_link: Comment on this entry
reply_link: Send a message to the author
comment_count:

View file

@ -67,6 +67,32 @@ module RichText
protected
def truncate_html(html_doc, max_length = nil, empty_tag_length = 1000)
return html_doc if max_length.nil?
doc = Nokogiri::HTML::DocumentFragment.parse(html_doc)
accumulated_length = 0
last_child = nil
doc.traverse do |node|
if accumulated_length >= max_length
node.remove unless !last_child.nil? && last_child.ancestors.include?(node)
next
end
next unless node.children.empty?
accumulated_length += node.text? ? node.text.length : empty_tag_length
if accumulated_length < max_length
last_child = node
else
node.remove
end
end
doc
end
def simple_format(text)
SimpleFormat.new.simple_format(text)
end
@ -85,8 +111,8 @@ module RichText
end
class HTML < Base
def to_html
linkify(sanitize(simple_format(self)))
def to_html(truncation_length = nil)
linkify(sanitize(truncate_html(simple_format(self), truncation_length)))
end
def to_text
@ -95,8 +121,8 @@ module RichText
end
class Markdown < Base
def to_html
linkify(sanitize(document.to_html), :all)
def to_html(truncation_length = nil)
linkify(sanitize(truncate_html(document.to_html, truncation_length)), :all)
end
def to_text
@ -175,8 +201,8 @@ module RichText
end
class Text < Base
def to_html
linkify(simple_format(ERB::Util.html_escape(self)))
def to_html(truncation_length = nil)
linkify(sanitize(truncate_html(simple_format(ERB::Util.html_escape(self)), truncation_length)))
end
def to_text

View file

@ -74,4 +74,31 @@ class DiaryEntrySystemTest < ApplicationSystemTestCase
assert_link "Diary Entries in Portuguese", :href => "/diary/pt"
assert_no_link "Diary Entries in Russian"
end
test "should not be hidden on the list page" do
body = SecureRandom.alphanumeric(1999)
create(:diary_entry, :body => body)
visit diary_entries_path
assert_content body
end
test "should be hidden on the list page" do
body = SecureRandom.alphanumeric(2001)
create(:diary_entry, :body => body)
visit diary_entries_path
assert_no_content body
end
test "should not be hidden on the show page" do
body = SecureRandom.alphanumeric(2001)
diary_entry = create(:diary_entry, :body => body)
visit diary_entry_path(diary_entry.user, diary_entry)
assert_content body
end
end