Skip to content

Commit fdd8e92

Browse files
more content cleanup
1 parent e4993a1 commit fdd8e92

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

server/hooks/xss.js

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const sanitizeHtml = require('sanitize-html');
22
const _ = require('lodash');
33

44
function clean (dirty, hook) {
5-
return sanitizeHtml(dirty, {
5+
dirty = sanitizeHtml(dirty, {
66
allowedTags: ['iframe', 'img', 'p', 'br', 'b', 'i', 'em', 'strong', 'a', 'pre', 'ul', 'li', 'ol'],
77
allowedAttributes: {
88
a: ['href', 'data-*'],
@@ -12,22 +12,23 @@ function clean (dirty, hook) {
1212
allowedIframeHostnames: ['www.youtube.com', 'player.vimeo.com'],
1313
parser: {
1414
lowerCaseTags: true
15-
}
16-
// transformTags: {
15+
},
16+
transformTags: {
17+
i: 'em',
18+
b: 'strong'
1719
// 'img': function (tagName, attribs) {
1820
// let src = attribs.src;
19-
20-
// const config = hook.app.get('thumbor');
21-
// if (config && src.indexOf(config < 0)) {
22-
// // download image
23-
24-
// // make thumbnail
25-
26-
// // const ThumborUrlHelper = require('../helper/thumbor-helper');
27-
// // const Thumbor = new ThumborUrlHelper(config.key || null, config.url || null);
28-
// // src = Thumbor
29-
// // .setImagePath(src)
30-
// // .buildUrl('740x0');
21+
// if (_.isEmpty(hook.result)) {
22+
// const config = hook.app.get('thumbor');
23+
// if (config && src.indexOf(config < 0)) {
24+
// // download image
25+
//
26+
// // const ThumborUrlHelper = require('../helper/thumbor-helper');
27+
// // const Thumbor = new ThumborUrlHelper(config.key || null, config.url || null);
28+
// // src = Thumbor
29+
// // .setImagePath(src)
30+
// // .buildUrl('740x0');
31+
// }
3132
// }
3233
// return {
3334
// tagName: 'img',
@@ -36,8 +37,17 @@ function clean (dirty, hook) {
3637
// }
3738
// };
3839
// }
39-
// }
40+
}
4041
});
42+
43+
// remove empty html tags and duplicated returns
44+
dirty = dirty
45+
.replace(/<[a-z]>[\s]*<\/[a-z]>/igm, '')
46+
.replace(/(<iframe(?!.*?src=(['"]).*?\2)[^>]*)(>)[^>]*\/*>/igm, '')
47+
.replace(/<p>[\s]*(<br ?\/?>)+[\s]*<\/p>/igm, '<br />')
48+
.replace(/(<br ?\/?>){2,}/igm, '<br />')
49+
.replace(/[\n]{3,}/igm, '\n\n');
50+
return dirty;
4151
}
4252

4353
module.exports = function (options = { fields: [] }) {
@@ -60,7 +70,6 @@ module.exports = function (options = { fields: [] }) {
6070
hook.app.error(err);
6171
}
6272
});
63-
6473
resolve(hook);
6574
});
6675
};

server/services/contributions/contributions.hooks.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ module.exports = {
9292
isVerified()
9393
),
9494
associateCurrentUser(),
95-
// xss({ fields: ['content'] }),
95+
// xss({ fields: ['content', 'contentExcerpt'] }),
9696
createSlug({ field: 'title' }),
9797
metascraper(),
9898
saveRemoteImages(['teaserImg']),
@@ -107,7 +107,7 @@ module.exports = {
107107
excludeDisabled(),
108108
restrictToOwner()
109109
),
110-
// xss({ fields: ['content'] }),
110+
// xss({ fields: ['content', 'contentExcerpt'] }),
111111
metascraper(),
112112
saveRemoteImages(['teaserImg']),
113113
createExcerpt()
@@ -121,7 +121,7 @@ module.exports = {
121121
excludeDisabled(),
122122
restrictToOwner()
123123
),
124-
// xss({ fields: ['content'] }),
124+
// xss({ fields: ['content', 'contentExcerpt'] }),
125125
metascraper(),
126126
saveRemoteImages(['teaserImg']),
127127
createExcerpt()
@@ -147,7 +147,7 @@ module.exports = {
147147
when(isSingleItem(),
148148
getAssociatedCanDos()
149149
),
150-
xss({ fields: ['content'] }),
150+
xss({ fields: ['content', 'contentExcerpt'] }),
151151
thumbnails({
152152
teaserImg: {
153153
cardS: '300x0',
@@ -162,7 +162,7 @@ module.exports = {
162162
],
163163
get: [
164164
getAssociatedCanDos(),
165-
xss({ fields: ['content'] }),
165+
xss({ fields: ['content', 'contentExcerpt'] }),
166166
thumbnails({
167167
teaserImg: {
168168
cardS: '300x0',
@@ -177,7 +177,7 @@ module.exports = {
177177
],
178178
create: [
179179
createMentionNotifications(),
180-
xss({ fields: ['content'] }),
180+
xss({ fields: ['content', 'contentExcerpt'] }),
181181
thumbnails({
182182
teaserImg: {
183183
cardS: '300x0',
@@ -192,7 +192,7 @@ module.exports = {
192192
],
193193
update: [
194194
createMentionNotifications(),
195-
xss({ fields: ['content'] }),
195+
xss({ fields: ['content', 'contentExcerpt'] }),
196196
thumbnails({
197197
teaserImg: {
198198
cardS: '300x0',
@@ -207,7 +207,7 @@ module.exports = {
207207
],
208208
patch: [
209209
createMentionNotifications(),
210-
xss({ fields: ['content'] }),
210+
xss({ fields: ['content', 'contentExcerpt'] }),
211211
thumbnails({
212212
teaserImg: {
213213
cardS: '300x0',

0 commit comments

Comments
 (0)