Is there an HTML safe truncate method in Rails? -
i have string of html in rails. i'd truncate string after number of characters not including html markup. also, if split happens fall in middle of opening , closing tag, i'd close open tag/s. example;
html = "123<a href='#'>456</a>7890" truncate_markup(html, :length => 5) --> "123<a href='#'>45</a>"
there 2 different solutions both same name: truncate_html
- https://github.com/ianwhite/truncate_html : gem , uses html parser (nokogiri)
- https://github.com/hgmnz/truncate_html : file put in helpers directory. uses regular expressions , has no dependencies.
Comments
Post a Comment