-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.js
155 lines (122 loc) · 3.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* [trimHtml description]
* @param {String} html
* @param {Object} options
* @return {Object}
*/
function trimHtml(html, options) {
options = options || {};
var limit = options.limit || 100,
preserveTags = (typeof options.preserveTags !== 'undefined') ? options.preserveTags : true,
wordBreak = (typeof options.wordBreak !== 'undefined') ? options.wordBreak : false,
suffix = options.suffix || '...',
moreLink = options.moreLink || '',
moreText = options.moreText || '»',
preserveWhiteSpace = options.preserveWhiteSpace || false;
var arr = html.replace(/</g, "\n<")
.replace(/>/g, ">\n")
.replace(/\n\n/g, "\n")
.replace(/^\n/g, "")
.replace(/\n$/g, "")
.split("\n");
var sum = 0,
row, cut, add, rowCut,
tagMatch,
tagName,
tagStack = [],
more = false;
for (var i = 0; i < arr.length; i++) {
row = arr[i];
// count multiple spaces as one character
if(!preserveWhiteSpace) {
rowCut = row.replace(/[ ]+/g, ' ');
} else {
rowCut = row;
}
if (!row.length) {
continue;
}
var charArr = getCharArr(rowCut);
if (row[0] !== "<") {
if (sum >= limit) {
row = "";
} else if ((sum + charArr.length) >= limit) {
cut = limit - sum;
if (charArr[cut - 1] === ' ') {
while(cut){
cut -= 1;
if(charArr[cut - 1] !== ' '){
break;
}
}
} else {
add = charArr.slice(cut).indexOf(' ');
// break on halh of word
if(!wordBreak) {
if (add !== -1) {
cut += add;
} else {
cut = row.length;
}
}
}
row = charArr.slice(0, cut).join('') + suffix;
if (moreLink) {
row += '<a href="' + moreLink + '" style="display:inline">' + moreText + '</a>';
}
sum = limit;
more = true;
} else {
sum += charArr.length;
}
} else if (!preserveTags) {
row = '';
} else if (sum >= limit) {
tagMatch = row.match(/[a-zA-Z]+/);
tagName = tagMatch ? tagMatch[0] : '';
if (tagName) {
if (row.substring(0, 2) !== '</') {
tagStack.push(tagName);
row = '';
} else {
while (tagStack[tagStack.length - 1] !== tagName && tagStack.length) {
tagStack.pop();
}
if (tagStack.length) {
row = '';
}
tagStack.pop();
}
} else {
row = '';
}
}
arr[i] = row;
}
return {
html: arr.join("\n").replace(/\n/g, ""),
more: more
};
}
// count symbols like one char
function getCharArr(rowCut) {
var charArr = [],
subRow,
match,
char;
for (var i = 0; i < rowCut.length; i++) {
subRow = rowCut.substring(i);
match = subRow.match(/^&[a-z0-9#]+;/);
if (match) {
char = match[0];
charArr.push(char);
i += (char.length - 1);
} else {
charArr.push(rowCut[i]);
}
}
return charArr;
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = trimHtml;
}