Skip to content
This repository was archived by the owner on Jun 27, 2019. It is now read-only.

Commit 947d078

Browse files
improve excerpt logic
1 parent 48490af commit 947d078

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

server/hooks/create-excerpt.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ const { getByDot, setByDot } = require('feathers-hooks-common');
88
const { isEmpty } = require('lodash');
99

1010
const sanitizeOptions = {
11-
allowedTags: [ 'br', 'a', 'p' ]
11+
allowedTags: ['p', 'br', 'a', 'span', 'blockquote'],
12+
allowedAttributes: {
13+
a: ['href', 'class', 'target', 'data-*' , 'contenteditable'],
14+
span: ['contenteditable', 'class', 'data-*']
15+
},
1216
};
1317

1418
module.exports = function (options = {}) { // eslint-disable-line no-unused-vars
@@ -29,14 +33,19 @@ module.exports = function (options = {}) { // eslint-disable-line no-unused-vars
2933
.replace(/(\ ){2,}/ig, ' ')
3034
.trim();
3135

32-
contentSanitized = trunc(contentSanitized, options.length).html;
36+
contentBefore = trunc(content, 9999999999);
37+
const contentTruncated = trunc(contentSanitized, options.length);
38+
hook.app.debug('contentBefore');
39+
hook.app.debug(contentBefore.text.length);
40+
hook.app.debug('contentTruncated');
41+
hook.app.debug(contentTruncated.text.length);
3342

34-
setByDot(hook.data, 'hasMore', contentSanitized.length < content.length);
43+
const hasMore = contentBefore.text.length > (contentTruncated.text.length + 20);
44+
setByDot(hook.data, 'hasMore', hasMore);
3545

3646
// set excerpt
37-
setByDot(hook.data, `${options.field}Excerpt`, contentSanitized)
47+
setByDot(hook.data, `${options.field}Excerpt`, hasMore ? contentTruncated.html : content.replace(/(\ ){2,}/ig, ' '))
3848
} catch (err) {
39-
hook.app.error(err);
4049
throw new Error(err);
4150
}
4251
// trim content

0 commit comments

Comments
 (0)