1
+ const { getByDot, setByDot, getItems, replaceItems } = require ( 'feathers-hooks-common' ) ;
1
2
const sanitizeHtml = require ( 'sanitize-html' ) ;
2
3
// const embedToAnchor = require('quill-url-embeds/dist/embed-to-anchor');
3
- const _ = require ( 'lodash' ) ;
4
+ const { isEmpty } = require ( 'lodash' ) ;
4
5
const cheerio = require ( 'cheerio' ) ;
5
6
6
7
const embedToAnchor = ( content ) => {
@@ -14,6 +15,10 @@ const embedToAnchor = (content) => {
14
15
} ;
15
16
16
17
function clean ( dirty ) {
18
+ if ( ! dirty ) {
19
+ return dirty ;
20
+ }
21
+
17
22
// Convert embeds to a-tags
18
23
dirty = embedToAnchor ( dirty ) ;
19
24
dirty = sanitizeHtml ( dirty , {
@@ -42,7 +47,7 @@ function clean (dirty) {
42
47
s : 'strike'
43
48
// 'img': function (tagName, attribs) {
44
49
// let src = attribs.src;
45
- // if (_. isEmpty(hook.result)) {
50
+ // if (isEmpty(hook.result)) {
46
51
// const config = hook.app.get('thumbor');
47
52
// if (config && src.indexOf(config < 0)) {
48
53
// // download image
@@ -74,26 +79,37 @@ function clean (dirty) {
74
79
return dirty ;
75
80
}
76
81
82
+ // iterate through all fields and clean the values
83
+ function cleanAllFields ( items , fields ) {
84
+ if ( ! items ) {
85
+ return items ;
86
+ }
87
+
88
+ if ( Array . isArray ( items ) ) {
89
+ // items is an array so fall this function for all items
90
+ items . forEach ( ( item , key ) => {
91
+ items [ key ] = cleanAllFields ( items [ key ] , fields ) ;
92
+ } ) ;
93
+ } else {
94
+ // clean value for all fields on the single given item
95
+ fields . forEach ( ( field ) => {
96
+ // get item by dot notation
97
+ const value = getByDot ( items , field ) ;
98
+ // set cleaned item by dot notation
99
+ setByDot ( items , field , clean ( value ) ) ;
100
+ } ) ;
101
+ }
102
+ return items ;
103
+ }
104
+
77
105
module . exports = function ( options = { fields : [ ] } ) {
78
106
return function ( hook ) {
79
107
return new Promise ( resolve => {
80
- options . fields . forEach ( field => {
81
- try {
82
- if ( ! _ . isEmpty ( hook . result ) && ! _ . isEmpty ( hook . result [ field ] ) ) {
83
- hook . result [ field ] = clean ( hook . result [ field ] , hook ) ;
84
- } else if ( ! _ . isEmpty ( hook . result ) && ! _ . isEmpty ( hook . result . data ) ) {
85
- hook . result . data . forEach ( ( result , i ) => {
86
- if ( ! _ . isEmpty ( hook . result . data [ i ] [ field ] ) ) {
87
- hook . result . data [ i ] [ field ] = clean ( hook . result . data [ i ] [ field ] , hook ) ;
88
- }
89
- } ) ;
90
- } else if ( ! _ . isEmpty ( hook . data ) && ! _ . isEmpty ( hook . data [ field ] ) ) {
91
- hook . data [ field ] = clean ( hook . data [ field ] ) ;
92
- }
93
- } catch ( err ) {
94
- hook . app . error ( err ) ;
95
- }
96
- } ) ;
108
+ const isFindOrGet = [ 'find' , 'get' ] . includes ( hook . method ) ;
109
+ const items = getItems ( hook ) ;
110
+ if ( ! isEmpty ( items ) && ! ( isFindOrGet && hook . type === 'before' ) ) {
111
+ replaceItems ( hook , cleanAllFields ( items , options . fields ) ) ;
112
+ }
97
113
resolve ( hook ) ;
98
114
} ) ;
99
115
} ;
0 commit comments