Giter VIP home page Giter VIP logo

Comments (8)

nono avatar nono commented on July 20, 2024 1

Is this what you want?

HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 12, :length_in_chars => true)
# => "<p>Lorem ipsum …</p>"

from html-truncator.

jrochkind avatar jrochkind commented on July 20, 2024

Looks like it, yeah! That's an option already? I did not realize that, didn't notice it in docs anywhere.

I'll play around with it and see if it does what I want, and what Rails truncate does (truncate on word boundary most immediately prior to X chars -- not just force on exactly X chars even in the middle of a word!)

from html-truncator.

jrochkind avatar jrochkind commented on July 20, 2024

Yeah, it does look like that does exactly what I want!

Suggest advertising that api on the README, it wasn't clear to me it was available. (The api is also a little bit confusing, in that it's not clear it's going to go back to the immediately preceding word boundary (great), instead of just mechanically truncating exactly on X chars (would not be so useful)).

from html-truncator.

jrochkind avatar jrochkind commented on July 20, 2024

PPS: Rails goes further to let you specify the seperator, so you could truncate on "." instead to try to get the most sentances you can fit under X chars, etc. Although I can't recall if i've ever actually used that.

from html-truncator.

nono avatar nono commented on July 20, 2024

The example above is taken from the README, but you are right: I should explain that it won't truncate inside a word.

For the separator option, I don't think it's really useful. Maybe I'll add it if someone has good use case for it.

from html-truncator.

jrochkind avatar jrochkind commented on July 20, 2024

thanks for the gem! I will defintiely be using it for my future html truncating needs.

from html-truncator.

jrochkind avatar jrochkind commented on July 20, 2024

The example is actually not quite right in it's demo output, in a way that potentially encourages the wrong idea about the not-truncate-inside-a-word thing:

HTML_Truncator.truncate("<p>Lorem ipsum dolor sit amet.</p>", 12, :length_in_chars => true)
# => "<p>Lorem ipsum …</p>"

In the example there's a space after the ipsum but before the -- in fact, HTML_Truncator will not include that space (great!) -- the example out is also exactly 12 characters, including the (incorrect) space -- this gives the impression that it really is truncating at exactly 12 chars, instead of the nearest preceding word boundary, as the sample output is consistent with that, not with what it actually does. A better example case might help, but a sentence explicitly saying it'll use nearest word boundary is prob still a good idea.

Thanks!

from html-truncator.

nono avatar nono commented on July 20, 2024

Yup, it's now in the README.

from html-truncator.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.