truncate_html
truncate_html copied to clipboard
Helper method to get the rest of given html
Sometimes there is a requirement to toggle the rest of given html text using Javascript(show more/show less functionality). In order to implement this without duplicating truncated html I need the rest of it. I tried to get the rest of html as following:
truncated_content = truncate_html(item.content, length: 300, omission: '')
trunc_length = truncated_content.length
rest = item.content[trunc_length, item.content.length]
But when there are extra consecutive whitespace in the given html, above code won't produce correct output. This is because in TruncateHtml::HtmlString#html_tokens method consecutive whitespace are being reduced to single whitespace.
Therefore I propose to create new helper "slice_html" that returns both truncated html and the rest as following:
truncated_content, rest = slice_html(item.content, length: 300, omission: '')