Skip to content

Instantly share code, notes, and snippets.

@andyhd
Created May 23, 2012 16:01
Show Gist options
  • Save andyhd/2776097 to your computer and use it in GitHub Desktop.
Save andyhd/2776097 to your computer and use it in GitHub Desktop.
Tag aware truncation of HTML by words
function truncate_html(html, words_left, depth) {
if (typeof html == 'string') {
html = html.replace(/</g, ' <').replace(/>/g, '> ').trim().split(/\s+/);
}
if (!depth) { depth = 0; }
var opening_tag = /^<\s*\w+[^>]*>/;
var closing_tag = /^<\/\s*\w+[^>]*>/;
var void_tag = /^<\s*(area|base|br|col|command|embed|hr|img|input|keygen|link|meta|param|source|track|wbr)[^>]*>/;
var word = html[0];
if (!word) { return []; }
var html = html.slice(1);
var is_void_tag = word.match(void_tag);
var is_opening_tag = !is_void_tag && word.match(opening_tag);
var is_closing_tag = word.match(closing_tag);
if (is_opening_tag) { depth++; }
if (is_closing_tag) { depth--; }
if (words_left) {
if (!(is_opening_tag || is_void_tag || is_closing_tag)) {
words_left--;
}
return [word].concat(truncate_html(html, words_left, depth));
}
if (is_closing_tag) {
if (depth == 0) {
return [word];
} else {
return [word].concat(truncate_html(html, words_left, depth));
}
}
return truncate_html(html, words_left, depth);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment