[openstreetmap/openstreetmap-website] Provide a truncated view of recent diaries (PR #5121)
David Tsiklauri
notifications at github.com
Fri Jan 31 12:34:52 UTC 2025
@nertc commented on this pull request.
> + 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
Maybe changing checking if the tag is empty to checking if the tag is `<img>` will be better. Therefore, tags like `<br>` or `<p></p>` will take 1 symbol space, while `<img>` will take for example 1000 symbol space. If we want any other tags to be treated in a specific way, feel free to suggest.
--
Reply to this email directly or view it on GitHub:
https://github.com/openstreetmap/openstreetmap-website/pull/5121#discussion_r1937179354
You are receiving this because you are subscribed to this thread.
Message ID: <openstreetmap/openstreetmap-website/pull/5121/review/2586492825 at github.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstreetmap.org/pipermail/rails-dev/attachments/20250131/b3e3b9f8/attachment.htm>
More information about the rails-dev
mailing list