Skip to content

Instantly share code, notes, and snippets.

@jnbt
Created February 16, 2015 10:16
Show Gist options
  • Save jnbt/e0ec48521efe5eece200 to your computer and use it in GitHub Desktop.
Save jnbt/e0ec48521efe5eece200 to your computer and use it in GitHub Desktop.
Nicer truncation for Batman.js using jquery.truncate
// Truncates (HTML) content
// Requirements: jquery.truncate (https://github.com/pathable/truncate/blob/master/jquery.truncate.js)
// Usage:
// <p data-bind="message.content | truncateHTML 200 | raw"></p>
Batman.mixin(Batman.Filters, {
niceTruncate: function(content, length, stripTags, words, noBreak, ellipsis){
if(typeof(content) === "undefined" || content === null)
return undefined;
if(length == null) length = Infinity;
if(stripTags == null) stripTags = false;
if(words == null) words = true;
if(noBreak == null) noBreak = false;
if(ellipsis == null) ellipsis = '\u2026';
return $.truncate(content,{
stripTags: stripTags,
length: length,
words: words,
noBreak: noBreak,
ellipsis: ellipsis
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment