Skip to content

Instantly share code, notes, and snippets.

@designfrontier
Created December 18, 2013 21:07
Show Gist options
  • Save designfrontier/8029846 to your computer and use it in GitHub Desktop.
Save designfrontier/8029846 to your computer and use it in GitHub Desktop.
creates a 200 char summary with html tags intact
var createSummary = function(str){
var str = str
//tag replacement stuff
, tagRegex = /<[^>]*>?/g
, markerRegexGlobal = /~/g
, markerRegex = /~/
, marker = '~'
//other vars that we will need
, refString = str.replace(tagRegex, marker) //replace out tags
, i = 0
, max = 0
, formattingArray = str.match(tagRegex)
, tempString;
if(refString.length <= (200 + formattingArray.length)){
//short enough already... return that thing
return str;
}else{
//this is a longer string... time for magic!
//slice then check for tags and reslice as needed
tempString = refString.slice(0,200 + refString.slice(0,200).match(markerRegexGlobal).length);
max = tempString.match(markerRegexGlobal).length;
for(i=0; i < max; i++){
tempString = tempString.replace(markerRegex, formattingArray[i]);
}
//add the elipses and return this thing
return tempString + '&hellip;';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment