[Feature][ActionView]Add :strip_tags option to truncate helper

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[Feature][ActionView]Add :strip_tags option to truncate helper

arsen oganesian
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("<p>Once upon a time in a world far far away</p>")
# => "<p>Once upon a time in a wo..."

truncate("<p>Once upon a time in a world far far away</p>", 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 :)
Thanks

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActionView]Add :strip_tags option to truncate helper

Andrew Kaspick
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"))

On Thu, Oct 25, 2018 at 7:16 PM arsen oganesian <[hidden email]> wrote:
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("<p>Once upon a time in a world far far away</p>")
# => "<p>Once upon a time in a wo..."

truncate("<p>Once upon a time in a world far far away</p>", 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 :)
Thanks

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActionView]Add :strip_tags option to truncate helper

arsen oganesian
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

On Friday, October 26, 2018 at 2:27:24 AM UTC+3, Andrew Kaspick wrote:
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"))

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.