Skip to content

Commit 32d1c90

Browse files
merged master in
2 parents 9fc8ef2 + 6f0c61a commit 32d1c90

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

server/hooks/create-excerpt.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
/* eslint-disable */
2-
32
// https://github.com/yangsibai/node-html-excerpt
43
// const excerpt = require('html-excerpt');
4+
5+
const sanitizeHtml = require('sanitize-html');
56
const trunc = require('trunc-html');
67

8+
const sanitizeOptions = {
9+
allowedTags: [ 'br' ]
10+
};
11+
712
module.exports = function (options = {}) { // eslint-disable-line no-unused-vars
813
return function (hook) {
914

@@ -15,12 +20,10 @@ module.exports = function (options = {}) { // eslint-disable-line no-unused-vars
1520

1621
try {
1722
/* eslint no-use-before-define: 0 */ // --> OFF
18-
const content = hook.data[options.field]
19-
// TODO: use html-sanatize package
20-
.replace(/\<br\>|\<\/br\>|\<\/ br\>|\<br\>|\<br\\\>|\<p\>|\<\/p\>/ig, "\n")
21-
.replace(/\<(strong|b|i|blockquote|pre|em|u|h[1-6]|a)>|\<\/(strong|b|i|blockquote|pre|em|u|h[1-6]|a)>/ig, '')
23+
const content = sanitizeHtml(hook.data[options.field], sanitizeOptions)
24+
.replace(/\<br\>|\<\/br\>|\<\/ br\>|\<br\>|\<br\\\>/ig, "\n")
2225
.replace(/\<p\>\<br\>\<\/p\>/ig, ' ')
23-
.replace(/(\ )[2,]/ig, ' ')
26+
.replace(/(\ ){2,}/ig, ' ')
2427
.trim();
2528
hook.data[`${options.field}Excerpt`] = trunc(content, options.length, {
2629
ignoreTags: ['img', 'script', 'iframe']
@@ -33,7 +36,7 @@ module.exports = function (options = {}) { // eslint-disable-line no-unused-vars
3336
}
3437
}
3538
hook.data[options.field] = hook.data[options.field]
36-
.replace(/(\ )[2,]/ig, ' ')
39+
.replace(/(\ ){2,}/ig, ' ')
3740

3841
return Promise.resolve(hook);
3942
};

0 commit comments

Comments
 (0)