huang47 / nodejs-html-truncate

truncate html text and keep tag safe
77 stars 30 forks source link

Library does not truncate text of links #21

Open damianwojakowski opened 8 years ago

damianwojakowski commented 8 years ago

Hi there!

I started using this library some time ago and I've noticed that it is not truncating links.

If I have a link, e.g.

<a href="someUrl">SomeLongTextOfALinkEvenLongerThanThis</a>

then this library is always going to display the whole text (I mean the "SomeLongTextOfALinkEvenLongerThanThis"), it does not truncate it.

Another thing - If you have a clickable block of text from a source that you don't control (e.g. RSS channel), then there's no way to simply truncate it with this library - you need to do additional work.

Please, have a look at this problem.

Regards,

sshaw commented 8 years ago

Do you have an example? I just tried this, and it worked:

~/code/js/html-trunc >node
> var truncate = require('html-truncate');
undefined
> truncate('<a href="someUrl">SomeLongTextOfALinkEvenLongerThanThis</a>', 4)
'<a href="someUrl">Some...</a>'
> truncate('<p><div><a href="someUrl">SomeLongTextOfALinkEvenLongerThanThis</a>hello world</div></p>', 4)
'<p><div><a href="someUrl">Some...</a></div></p>'
damianwojakowski commented 8 years ago

Hi,

I gave you just a simple example (not real) to describe what's the problem.

This library has problems with links. I used it at work (for a project similar to facebook), we had ton's of links there and some business rules for truncating links on many "components" (e.g. profiles, chatbox), this library had problems with truncating quite a lot of links.

Finally we replaced it with another lib that handled the problems.

I don't remember what kind of liks were they and I don't have time to test it now. Just try a few possibilities.

Maybe when there's a link in tag (instead of text), or maybe when the words in tag are separated. It definatelly did not work for long links (like I described it above, e.g. when you got a lot of text from RSS source and you wanted to make it clickable.

Regards, Damian Wojakowski.

sshaw commented 8 years ago

Have you seen/tried this?

damianwojakowski commented 8 years ago

No, I haven't. I tried other libarary.

Noushad-web commented 8 months ago

I encountered a similar issue where the library fails to function properly with hyperlinks. In my experience, this resulted in unexpected behavior, including triggering an infinite loop that caused browser hang-ups.