Skip to content

Commit 521705e

Browse files
longer comment excerpts
1 parent 19cb109 commit 521705e

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

server/hooks/create-excerpt.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,32 @@ const trunc = require('trunc-html');
66

77
module.exports = function (options = {}) { // eslint-disable-line no-unused-vars
88
return function (hook) {
9-
if(!hook.data || !hook.data.content) {
9+
10+
options = Object.assign({ length: 120, field: 'content' }, options);
11+
12+
if(!hook.data || !hook.data[options.field]) {
1013
return hook;
1114
}
1215

1316
try {
1417
/* eslint no-use-before-define: 0 */ // --> OFF
15-
const content = hook.data.content
18+
const content = hook.data[options.field]
1619
.replace(/\<br\>|\<\/br\>|\<\/ br\>|\<br\>|\<br\\\>|\<p\>|\<\/p\>/ig, "\n")
17-
.replace(/\<(strong|b|i|blockquote|pre|em|u|h[1-6])>|\<\/(strong|b|i|blockquote|pre|em|u|h[1-6])>/ig, '')
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, '')
1821
.replace(/\<p\>\<br\>\<\/p\>/ig, ' ')
1922
.replace(/(\ )[2,]/ig, ' ')
2023
.trim();
21-
hook.data.contentExcerpt = trunc(content, 120, {
22-
ignoreTags: ['img', 'script']
24+
hook.data[`${options.field}Excerpt`] = trunc(content, options.length, {
25+
ignoreTags: ['img', 'script', 'iframe']
2326
}).html;
2427
} catch (err) {
2528
if (hook.data.teaserImg) {
26-
hook.data.contentExcerpt = '-----';
29+
hook.data[`${options.field}Excerpt`] = '-----';
2730
} else {
28-
throw new Error('Content Needed!');
31+
throw new Error('Text content needed!');
2932
}
3033
}
31-
hook.data.content = hook.data.content
34+
hook.data[options.field] = hook.data[options.field]
3235
.replace(/(\ )[2,]/ig, ' ')
3336

3437
return Promise.resolve(hook);

server/services/comments/comments.hooks.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ module.exports = {
3434
isVerified()
3535
),
3636
associateCurrentUser(),
37-
createExcerpt()
37+
createExcerpt({ length: 180 })
3838
],
3939
update: [
4040
authenticate('jwt'),
4141
isVerified(),
4242
unless(isProvider('server'),
4343
restrictToOwner()
4444
),
45-
createExcerpt()
45+
createExcerpt({ length: 180 })
4646
],
4747
patch: [
4848
authenticate('jwt'),
@@ -58,7 +58,7 @@ module.exports = {
5858
(!_.difference(_.values(valid.$inc), _.values(hook.data.$inc)).length);
5959
}, restrictToOwner())
6060
),
61-
createExcerpt()
61+
createExcerpt({ length: 180 })
6262
],
6363
remove: [
6464
authenticate('jwt'),

0 commit comments

Comments
 (0)