Comparing version 1.0.2 to 1.1.0
21
index.js
@@ -17,2 +17,3 @@ 'use strict'; | ||
var ignoreTags = (o.ignoreTags || []).reduce(intoHashMap, Object.create(null)); | ||
var altBuffer = ''; | ||
var plain = ''; | ||
@@ -23,4 +24,6 @@ var insaneDefaults = { | ||
}; | ||
var html = insane(input, assign(o.sanitizer || {}, insaneDefaults)); | ||
return { html: html, text: plain }; | ||
var sanitizer = o.sanitizer || {}; | ||
var opts = assign({}, sanitizer, insaneDefaults); | ||
var html = insane(input, opts); | ||
return { html: html, text: plain + altBuffer }; | ||
function filter (token) { | ||
@@ -30,3 +33,12 @@ if (token.tag in ignoreTags) { | ||
} | ||
return remainder > 0; | ||
if (remainder <= 0) { | ||
return false; | ||
} | ||
if (sanitizer.filter && !sanitizer.filter(token)) { | ||
return false; | ||
} | ||
if (o.imageAltText && token.tag === 'img') { | ||
altBuffer += token.attrs.alt; | ||
} | ||
return true; | ||
} | ||
@@ -43,3 +55,4 @@ function transformText (text) { | ||
} | ||
plain += truncated; | ||
plain += altBuffer + truncated; | ||
altBuffer = ''; | ||
return truncated; | ||
@@ -46,0 +59,0 @@ } |
{ | ||
"name": "trunc-html", | ||
"description": "truncate html by text length", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"homepage": "https://github.com/bevacqua/trunc-html", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -39,7 +39,11 @@ # trunc-html | ||
If you'd like to supress certain HTML tags from being output you can provide an array of tags to be excluded, e.g `['img']`. | ||
If you'd like to supress certain HTML tags from being output you can provide an array of tags to be excluded, set `ignoreTags` to something like `['strong']`. | ||
## `options.imageAltText` | ||
If you'd like to use the `alt` attribute for the text portion of the result, instead of dropping images entirely in text flavor, set `imageAltText` to `true`. | ||
## `options.sanitizer` | ||
Options passed to [`insane`][1]. Note that these options are overridden by the options provided by `trunc-html`, so you can't use `filter` and `transformText`, which `trunc-html` reserves for itself. | ||
Options passed to [`insane`][1]. Note that these options are overridden by the options provided by `trunc-html`, so you can't use `transformText`, which `trunc-html` reserves for itself. | ||
@@ -46,0 +50,0 @@ # related |
12
test.js
@@ -25,1 +25,13 @@ var test = require('ava'); | ||
}); | ||
test(t => { | ||
t.same(trunc('some sort of tada <img class="tj-emoji" alt="🎉" src="https://twemoji.maxcdn.com/2/72x72/1f389.png"/>', 30, { imageAltText: true }), { | ||
html: 'some sort of tada <img alt="🎉" src="https://twemoji.maxcdn.com/2/72x72/1f389.png"/>', | ||
text: 'some sort of tada 🎉' | ||
}); | ||
t.same(trunc('some <img class="tj-emoji" alt="🎉" src="https://twemoji.maxcdn.com/2/72x72/1f389.png"/> sort of tada <img class="tj-emoji" alt="🎉" src="https://twemoji.maxcdn.com/2/72x72/1f389.png"/>', 30, { imageAltText: true }), { | ||
html: 'some <img alt="🎉" src="https://twemoji.maxcdn.com/2/72x72/1f389.png"/> sort of tada <img alt="🎉" src="https://twemoji.maxcdn.com/2/72x72/1f389.png"/>', | ||
text: 'some 🎉 sort of tada 🎉' | ||
}); | ||
}); |
6183
85
58