-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (39 loc) · 1.11 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
(function() {
// Allow this to be used with node or the browser:
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = excerpt;
}
exports.excerpt = excerpt;
} else {
this.excerpt = excerpt;
}
function excerpt(text, phrase, radius, ending) {
var append, prepend, phraseLen, pos, startPos, endPos, excerptText, textLen;
if (!text || !phrase) {
return text;
}
radius = radius || '100';
ending = ending || '...';
append = prepend = ending;
phraseLen = phrase.length;
textLen = text.length;
pos = text.toLowerCase().indexOf(phrase.toLowerCase());
if (pos === false) {
return text.substr(0, radius) + ending;
}
startPos = pos - radius;
if (startPos <= 0) {
startPos = 0;
prepend = '';
}
endPos = pos + phraseLen + radius;
if (endPos >= textLen) {
endPos = textLen;
append = '';
}
excerptText = text.substr(startPos, endPos - startPos);
excerptText = prepend + excerptText + append;
return excerptText;
}
}).call(this);