Skip to content

Commit

Permalink
added a new option keepWhitespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
oe committed Jul 21, 2016
1 parent 027360a commit e6bb155
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 27 deletions.
71 changes: 59 additions & 12 deletions lib/truncate.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ extend = (obj, dft)->
* @param {String} html html string to truncate
* @param {Object|number} length
* @param {Object|null} options
* {
* stripTags: false, // remove all tags, default false
* ellipsis: '...', // ellipsis sign, default '...'
* decodeEntities: false, // decode html entities before counting length, default false
* excludes: '', // elements' selector you want ignore, default none
* length: 10, // how many letters you want reserve, default none
* keepWhitespaces: false // keep whitespaces, by default continuous spaces will be replace one space, default false
* }
* @return {String}
* @example
* truncate('<p>wweeweewewwe</p>', 10)
Expand All @@ -35,7 +43,7 @@ truncate = (html, length, options)->

options = extend options, truncate.defaultOptions

if typeof options.length isnt 'number' or options.length <= 0 then return html
if !html or isNaN( options.length ) or options.length <= 0 then return html

if typeof html is 'object'
html = $(html).html()
Expand All @@ -62,34 +70,69 @@ truncate = (html, length, options)->
else
return text.substr( 0, options.length ) + options.ellipsis

len = options.length
length = options.length

travelChildren = ($ele, length)->
keepWhitespaces = options.keepWhitespaces

travelChildren = ($ele)->
$ele.contents().each ->
switch this.type
when 'text'
if len <= 0
if length <= 0
$(this).remove()
return
text = $(this).text().replace /\s+/g, ' '
if text.length <= len
text = $(this).text()
if keepWhitespaces
textLength = 0
subLength = 0
# count none spaces & spaces
# continuous spaces will be treat as one
text.replace /(\S*)(\s*)/g, ($0, $1, $2)->
console.log $1 + ' ~~~ ' + $2
console.log $1.length + ' ~~~ ' + $2.length
return if textLength > length
if $1.length >= length
subLength += length
textLength += $1.length
length = 0
return
textLength += $1.length
subLength += $1.length
length -= $1.length

$2Len = !!($2.length)
if $2Len >= length
subLength += $2Len
textLength += $2.length
length = 0
return

textLength += $2Len
subLength += $2.length
length -= $2Len
return
else
text = text.replace /\s+/g, ' '
textLength = text.length
subLength = Math.min textLength, length
if textLength <= length
this.data = text
len -= text.length
length -= textLength
else
this.data = text.substr(0, len) + options.ellipsis
len = 0
this.data = text.substr(0, subLength) + options.ellipsis
length = 0

when 'tag'
if len <= 0
if length <= 0
$(this).remove()
else
travelChildren $(this), len
travelChildren $(this)

# for comments
else
$(this).remove()

travelChildren $html, len
travelChildren $html

$html.html()

Expand All @@ -103,6 +146,10 @@ truncate.defaultOptions =
# decode html entities
decodeEntities: false
# excludes: img
# # truncate by words, set to true keep words
# # set to number then truncate by word count
# words: false
# length: 0
# keepWhitespaces: false

module.exports = truncate
71 changes: 58 additions & 13 deletions lib/truncate.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "truncate-html",
"version": "0.0.6",
"version": "0.1.0",
"description": "truncate html and keep tags in safe",
"main": "lib/truncate.js",
"scripts": {
Expand Down
8 changes: 7 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ truncate(html, [length], [options])
ellipsis: String, custom ellipsis sign, set it to empty string to remove the ellipsis postfix
excludes: String or Array, the selectors of the elements you want to ignore
decodeEntities: Boolean, auto decode html entities in the html string
keepWhitespaces: Boolean, keep whitespaces, whether to replace continuous spaces to one space
}
```

Expand All @@ -24,7 +25,8 @@ truncate(html, [length], [options])
truncate.defaultOptions = {
stripTags: false,
ellipsis: '...',
decodeEntities: false
decodeEntities: false,
keepWhitespaces: false
};
```

Expand All @@ -51,6 +53,10 @@ var html = '<p><img src="abc.png">This is a string</p> for test.';
truncate(html, 10, {stripTags: true});
// returns: This is a ...

// with options, keep whitespaces
var html = '<p> <img src="abc.png">This is a string</p> for test.';
truncate(html, 10, {keepWhitespaces: true});
// returns: <p> <img src="abc.png">This is a ...</p>


// combine length and options
Expand Down

0 comments on commit e6bb155

Please sign in to comment.