[openstreetmap/openstreetmap-website] Provide a truncated view of recent diaries (PR #5121)

David Tsiklauri notifications at github.com
Thu Nov 14 07:48:20 UTC 2024


@nertc commented on this pull request.



> +  def truncate_html(html, max_length, empty_tag_length = 500)
+    doc = Nokogiri::HTML::DocumentFragment.parse(html)
+    accumulated_length = 0
+    truncated_node = nil
+
+    doc.traverse do |node|
+      if accumulated_length >= max_length
+        node.remove unless truncated_node.ancestors.include?(node)
+        next
+      end
+
+      next unless node.children.empty?
+
+      content_length = node.text? ? node.text.length : empty_tag_length
+      if accumulated_length + content_length >= max_length
+        node.content = node.text.truncate(max_length - accumulated_length) if node.text?

If we discard it as a whole, diary entry of someone who created only one huge paragraph, won't be shown at all.
If we keep it fully, diary entry of someone who created only one huge paragraph, will overload diary entries list.
That's why I think truncation is the best method in this case.

-- 
Reply to this email directly or view it on GitHub:
https://github.com/openstreetmap/openstreetmap-website/pull/5121#discussion_r1841712743
You are receiving this because you are subscribed to this thread.

Message ID: <openstreetmap/openstreetmap-website/pull/5121/review/2435275034 at github.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstreetmap.org/pipermail/rails-dev/attachments/20241113/c2e9ad2e/attachment.htm>


More information about the rails-dev mailing list