[openstreetmap/openstreetmap-website] Provide a truncated view of recent diaries (PR #5121)
Anton Khorev
notifications at github.com
Tue Jan 28 10:57:49 UTC 2025
@AntonKhorev 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
> Images now take size of 1000 character.
Any empty tag costs 1000 characters. For example, empty paragraph costs 1000 characters. Something like this becomes empty:
```
<p></p>
<p></p>
hello?
```
Some tags are naturally empty, for example `<br>`. In this example "3" is truncated:
```
1
2
3
```
(note spaces in markdown after "1" and "2")
--
Reply to this email directly or view it on GitHub:
https://github.com/openstreetmap/openstreetmap-website/pull/5121#pullrequestreview-2577864858
You are receiving this because you are subscribed to this thread.
Message ID: <openstreetmap/openstreetmap-website/pull/5121/review/2577864858 at github.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstreetmap.org/pipermail/rails-dev/attachments/20250128/2351d03f/attachment.htm>
More information about the rails-dev
mailing list