可以截取HTML字符串,并不破坏打断HTML标签。
注* 网站内容大多基于HTML文本,传统的字符串截取会破坏HTML标签的结构,此库对于自动生成网站内容摘要十分有帮助。
npm install trim-html
var trimHtml = require('trim-html');
var trimmed = trimHtml(html);
var html = '<div><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. </p><p>Utenim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquipex ea commodo consequat. </p><p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. </p><p>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p></div>';
var trim = trimHtml(html, { limit: 200 });
// 返回对象
{
html: '<div><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. </p><p>Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut...
</p></div>',
more: true // 标识是否还有更多内容
}
项目地址: https://github.com/brankosekulic/trimHtml