Comments (8)
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.
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.
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.
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.
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.
thanks for the gem! I will defintiely be using it for my future html truncating needs.
from html-truncator.
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.
Yup, it's now in the README.
from html-truncator.
Related Issues (15)
- The ellipsis should not be put in code tags HOT 2
- Strips iframe tags on ruby 2.1 HOT 1
- Is there any way to ignore specified tags? HOT 2
- Strips script and style
- Not work with russian language HOT 5
- HTML encoded strings are decoded HOT 2
- Truncations after tag boundary HOT 3
- Current implementation creates a Singleton class and prevents marshaling HOT 2
- Messing up Nokogiri HOT 1
- For speed, allow Nokogiri nodes to be passed in HOT 3
- Characters instead of Words HOT 4
- Pattern in words scan HOT 1
- Removing img tag HOT 3
- Comma after text HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from html-truncator.