|
4 | 4 | // const excerpt = require('html-excerpt');
|
5 | 5 | const trunc = require('trunc-html');
|
6 | 6 |
|
7 |
| -module.exports = function (options = {}) { // eslint-disable-line no-unused-vars |
| 7 | +module.exports = function (options = { |
| 8 | + field: 'content', |
| 9 | + length: 120 |
| 10 | +}) { // eslint-disable-line no-unused-vars |
8 | 11 | return function (hook) {
|
9 |
| - if(!hook.data || !hook.data.content) { |
| 12 | + console.log('TRY TO CREATE EXCERPT'); |
| 13 | + console.log('field', options.field); |
| 14 | + if(!options.field || !hook.data[options.field]) return hook; |
| 15 | + |
| 16 | + if(!hook.data || !hook.data[options.field]) { |
10 | 17 | return hook;
|
11 | 18 | }
|
12 | 19 | /* eslint no-use-before-define: 0 */ // --> OFF
|
13 |
| - const content = hook.data.content |
| 20 | + const text = hook.data[options.field] |
14 | 21 | .replace(/\<br\>|\<\/br\>|\<\/ br\>|\<br\>|\<br\\\>|\<p\>|\<\/p\>/ig, "\n")
|
15 | 22 | .replace(/\<(strong|b|i|blockquote|pre|em|u|h[1-6])>|\<\/(strong|b|i|blockquote|pre|em|u|h[1-6])>/ig, '')
|
16 | 23 | .replace(/\<p\>\<br\>\<\/p\>/ig, ' ')
|
17 | 24 | .replace(/(\ )[2,]/ig, ' ')
|
18 | 25 | .trim();
|
19 |
| - hook.data.contentExcerpt = trunc(content, 120, { |
| 26 | + hook.data[`${options.field}Excerpt`] = trunc(text, options.length, { |
20 | 27 | ignoreTags: ['img', 'script']
|
21 | 28 | }).html;
|
22 | 29 |
|
23 |
| - hook.data.content = hook.data.content |
| 30 | + hook.data[options.field] = hook.data[options.field] |
24 | 31 | .replace(/(\ )[2,]/ig, ' ')
|
25 | 32 | return Promise.resolve(hook);
|
26 | 33 | };
|
|
0 commit comments