1
1
/* eslint-disable */
2
-
3
2
// https://github.com/yangsibai/node-html-excerpt
4
3
// const excerpt = require('html-excerpt');
4
+
5
+ const sanitizeHtml = require ( 'sanitize-html' ) ;
5
6
const trunc = require ( 'trunc-html' ) ;
6
7
8
+ const sanitizeOptions = {
9
+ allowedTags : [ 'br' ]
10
+ } ;
11
+
7
12
module . exports = function ( options = { } ) { // eslint-disable-line no-unused-vars
8
13
return function ( hook ) {
9
14
@@ -15,12 +20,10 @@ module.exports = function (options = {}) { // eslint-disable-line no-unused-vars
15
20
16
21
try {
17
22
/* eslint no-use-before-define: 0 */ // --> OFF
18
- const content = hook . data [ options . field ]
19
- // TODO: use html-sanatize package
20
- . replace ( / \< b r \> | \< \/ b r \> | \< \/ b r \> | \< b r \> | \< b r \\ \> | \< p \> | \< \/ p \> / ig, "\n" )
21
- . replace ( / \< ( s t r o n g | b | i | b l o c k q u o t e | p r e | e m | u | h [ 1 - 6 ] | a ) > | \< \/ ( s t r o n g | b | i | b l o c k q u o t e | p r e | e m | u | h [ 1 - 6 ] | a ) > / ig, '' )
23
+ const content = sanitizeHtml ( hook . data [ options . field ] , sanitizeOptions )
24
+ . replace ( / \< b r \> | \< \/ b r \> | \< \/ b r \> | \< b r \> | \< b r \\ \> / ig, "\n" )
22
25
. replace ( / \< p \> \< b r \> \< \/ p \> / ig, ' ' )
23
- . replace ( / ( \ ) [ 2 , ] / ig, ' ' )
26
+ . replace ( / ( \ ) { 2 , } / ig, ' ' )
24
27
. trim ( ) ;
25
28
hook . data [ `${ options . field } Excerpt` ] = trunc ( content , options . length , {
26
29
ignoreTags : [ 'img' , 'script' , 'iframe' ]
@@ -33,7 +36,7 @@ module.exports = function (options = {}) { // eslint-disable-line no-unused-vars
33
36
}
34
37
}
35
38
hook . data [ options . field ] = hook . data [ options . field ]
36
- . replace ( / ( \ ) [ 2 , ] / ig, ' ' )
39
+ . replace ( / ( \ ) { 2 , } / ig, ' ' )
37
40
38
41
return Promise . resolve ( hook ) ;
39
42
} ;
0 commit comments