I have a string of HTML in Rails. I\'d like to truncate the string after a certain number of characters not including the HTML markup. Also, if the split happens to fall in
There are two completely different solutions both with the same name: truncate_html
We can do that with the help of simple_format http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format
html = "123<a href='#'>456</a>7890"
simle_format(truncate_markup(html, :length => 5))
=> "123 456 7890"
We had this need in zendone.com. The problem was that the existing solutions were very slow when truncating long HTML documents (MBs) into shorter ones (KBs). I ended up coding a library based in Nokogiri called truncato. The library includes some benchmarks comparing its performance with other libs.
your_tagged_string.truncate(60).html_safe