Created
December 18, 2013 21:07
-
-
Save designfrontier/8029846 to your computer and use it in GitHub Desktop.
creates a 200 char summary with html tags intact
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + '…'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment