-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
41 lines (33 loc) · 1.46 KB
/
index.js
File metadata and controls
41 lines (33 loc) · 1.46 KB
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
'use strict';
var minidom = require('minidom'),
htmlencode = require('node-htmlencode'),
mergeLineBreaksAndConvertToTags = function (htmlEncodedContent) {
return htmlEncodedContent.replace(/ {2,}/gm, '$1<br />');
},
makeSummary = function (content, maxLength, prefix, suffix) {
var summary = content
.substr(0, content.lastIndexOf(' ', maxLength));
return prefix + mergeLineBreaksAndConvertToTags(htmlencode.htmlEncode(summary)) + suffix;
},
makeMetaDescription = function (content) {
var minimumMetaDescriptionLengthToNextSpace = 180; // http://webdesign.about.com/od/metatags/qt/meta_descriptio.htm
return content
.replace(/\"/g, '\'')
.substr(0, content.indexOf(' ', minimumMetaDescriptionLengthToNextSpace))
.concat('...');
};
exports.maxLength = 300;
exports.prefix = '<p>';
exports.suffix = ' ...<p>';
exports.init = function (engine) {
var original = engine.loadContent;
engine.loadContent = function (entry, content, callback) {
original(entry, content, function (transformedContent) {
var dom = minidom('<div>' + transformedContent + '</div>'),
text = dom.children[0].textContent;
entry.summary = makeSummary(text, exports.maxLength, exports.prefix, exports.suffix);
entry.metaDescription = makeMetaDescription(text);
callback(transformedContent);
});
};
};