[ActionView]Add :strip_tags option to truncate helper

Hello everybody! I made a little improvement to ActionView::Helpers::TextHelper#truncate – added strip_tags: true option, to strip HTML tags in truncated text.

For example:

truncate(“

Once upon a time in a world far far away

”)

=> “

Once upon a time in a wo…”

truncate(“

Once upon a time in a world far far away

”, strip_tags: true)

=> “Once upon a time in a wo…”

I think this may be useful when truncating rich text, particularly when there are only annoying opening tags left after truncation.

Please, let me know what you think about it! Should I open pull request? I’m new to open source, this may be my first pr ever :slight_smile:

Thanks

I think the proper way to do what you want is…

content_tag(:p, truncate(“Once upon a time in a world far far away”))

Hey, thanks for answer! But it doesn’t really do what I meant with :strip_tags option.

The idea is to strip HTML tags before text gets truncated. Of course, you can just pass stripped text to truncate, like truncate(strip_tags(content)), but i think it will be nice to have this as an option in truncate method, since it’s already have :escape anyway.

My idea is kind of inspired by ActionText in Rails 6, I was thinking about real life use cases for truncate method, and realized that most of the time it will be dealing with rich text in some way: blog posts, articles, etc