Skip to content

Commit 6f0c61a

Browse files
try to fix broken excerpts
1 parent 30ae07b commit 6f0c61a

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

server/hooks/create-excerpt.js

Lines changed: 8 additions & 4 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,9 +20,8 @@ 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-
.replace(/\<br\>|\<\/br\>|\<\/ br\>|\<br\>|\<br\\\>|\<p\>|\<\/p\>/ig, "\n")
20-
.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")
2125
.replace(/\<p\>\<br\>\<\/p\>/ig, ' ')
2226
.replace(/(\ ){2,}/ig, ' ')
2327
.trim();

0 commit comments

Comments
 (0)