huang47 / nodejs-html-truncate

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

truncate fails on plain text without HTML #23

Open MortenHofft opened 7 years ago

MortenHofft commented 7 years ago

Thank you for a nice library. I've stumbled upon a bug the other day.

I cannot figure out exactly when this happens, but I have an example: https://runkit.com/hofft/5877633764cddc00146690a8

var truncate = require("html-truncate");
var str = 'abc (http://doi.org/10.3897/zookeys.555.6173) The dataset contains close to 2.5 million occurrences, recorded by 101 GPS trackers mounted on 75 Lesser Black-backed Gulls and 26 Herring Gulls breeding at the Belgian and Dutch coast (see https://inbo.cartodb.com/u/lifewatch/viz/da04f120-ea70-11e4-a3f2-0e853d047bba/public_map for a visualization of the data). (UvA-BiTS, http://www.uva-bits.nl). ';

//not being html I would expect it to have length ~3
console.log(truncate(str, 3)); 

/not being html I would expect it to have length ~13
console.log(truncate(str, 13)); 

result

"abc..."
and
"abc (http://doi.org/10.3897/zookeys.555.6173) The dataset contains close to 2.5 million occurrences, recorded by 101 GPS trackers mounted on 75 Lesser Black-backed Gulls and 26 Herring Gulls breeding at the Belgian and Dutch coast (see https://inbo.cartodb.com/u/lifewatch/viz/da04f120-ea70-11e4-a3f2-0e853d047bba/public_map..."

basically it seems that having multiple urls in the text breaks the truncating.

Thanks