Skip to content

Commit 8856252

Browse files
added better excerpts
1 parent 7abffee commit 8856252

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ node_modules
3232
.project
3333
.classpath
3434
.c9/
35+
.vscode
3536
*.launch
3637
.settings/
3738
*.sublime-workspace

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
"serve-favicon": "~2.4.5",
9696
"shortid": "~2.2.8",
9797
"slug": "~0.9.1",
98+
"trunc-html": "^1.1.2",
9899
"winston": "~3.0.0-rc1"
99100
},
100101
"devDependencies": {

server/hooks/create-excerpt.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/* eslint-disable */
22

33
// https://github.com/yangsibai/node-html-excerpt
4-
const excerpt = require('html-excerpt');
4+
// const excerpt = require('html-excerpt');
5+
const trunc = require('trunc-html');
56

67
module.exports = function (options = {}) { // eslint-disable-line no-unused-vars
78
return function (hook) {
@@ -10,9 +11,17 @@ module.exports = function (options = {}) { // eslint-disable-line no-unused-vars
1011
}
1112
/* eslint no-use-before-define: 0 */ // --> OFF
1213
const content = hook.data.content
13-
.replace(/\<br\>|\<\/br\>|\<\/ br\>|\<\/(strong|b|h[1-6])>/ig, "\n")
14-
.replace(/(<([^>]+)>)/ig, '');
15-
hook.data.contentExcerpt = excerpt.text(content, 120, '...');
14+
.replace(/\<br\>|\<\/br\>|\<\/ br\>|\<br\>|\<br\\\>|\<p\>|\<\/p\>/ig, "\n")
15+
.replace(/\<(strong|b|i|blockquote|pre|em|u|h[1-6])>|\<\/(strong|b|i|blockquote|pre|em|u|h[1-6])>/ig, '')
16+
.replace(/\<p\>\<br\>\<\/p\>/ig, ' ')
17+
.replace(/(\ )[2,]/ig, ' ')
18+
.trim();
19+
hook.data.contentExcerpt = trunc(content, 120, {
20+
ignoreTags: ['img', 'script']
21+
}).html;
22+
23+
hook.data.content = hook.data.content
24+
.replace(/(\ )[2,]/ig, ' ')
1625
return Promise.resolve(hook);
1726
};
1827
};

yarn.lock

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,14 @@ assign-symbols@^1.0.0:
279279
version "1.0.0"
280280
resolved "https://registry.yarnpkg.com/assign-symbols/-/assign-symbols-1.0.0.tgz#59667f41fadd4f20ccbc2bb96b8d4f7f78ec0367"
281281

282+
283+
version "2.0.0"
284+
resolved "https://registry.yarnpkg.com/assignment/-/assignment-2.0.0.tgz#ffd17b21bf5d6b22e777b989681a815456a3dd3e"
285+
286+
287+
version "2.2.0"
288+
resolved "https://registry.yarnpkg.com/assignment/-/assignment-2.2.0.tgz#f5b5bc2d160d69986e8700cd38f567c0aabe101e"
289+
282290
async-each@^1.0.0:
283291
version "1.0.1"
284292
resolved "https://registry.yarnpkg.com/async-each/-/async-each-1.0.1.tgz#19d386a1d9edc6e7c1c85d388aedbcc56d33602d"
@@ -2269,6 +2277,10 @@ hawk@~6.0.2:
22692277
hoek "4.x.x"
22702278
sntp "2.x.x"
22712279

2280+
2281+
version "0.5.0"
2282+
resolved "https://registry.yarnpkg.com/he/-/he-0.5.0.tgz#2c05ffaef90b68e860f3fd2b54ef580989277ee2"
2283+
22722284
22732285
version "1.1.1"
22742286
resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd"
@@ -2456,6 +2468,13 @@ inquirer@^3.0.6:
24562468
strip-ansi "^4.0.0"
24572469
through "^2.3.6"
24582470

2471+
2472+
version "2.6.1"
2473+
resolved "https://registry.yarnpkg.com/insane/-/insane-2.6.1.tgz#c7dcae7b51c20346883b71078fad6ce0483c198f"
2474+
dependencies:
2475+
assignment "2.0.0"
2476+
he "0.5.0"
2477+
24592478
interpret@^1.0.0:
24602479
version "1.1.0"
24612480
resolved "https://registry.yarnpkg.com/interpret/-/interpret-1.1.0.tgz#7ed1b1410c6a0e0f78cf95d3b8440c63f78b8614"
@@ -4783,6 +4802,18 @@ triple-beam@^1.0.1:
47834802
dependencies:
47844803
glob "^6.0.4"
47854804

4805+
trunc-html@^1.1.2:
4806+
version "1.1.2"
4807+
resolved "https://registry.yarnpkg.com/trunc-html/-/trunc-html-1.1.2.tgz#1e97d51f67d470b67662b1a670e6d0ea7a8edafe"
4808+
dependencies:
4809+
assignment "2.2.0"
4810+
insane "2.6.1"
4811+
trunc-text "1.0.1"
4812+
4813+
4814+
version "1.0.1"
4815+
resolved "https://registry.yarnpkg.com/trunc-text/-/trunc-text-1.0.1.tgz#58f876d8ac59b224b79834bb478b8656e69622b5"
4816+
47864817
tunnel-agent@^0.6.0:
47874818
version "0.6.0"
47884819
resolved "https://registry.yarnpkg.com/tunnel-agent/-/tunnel-agent-0.6.0.tgz#27a5dea06b36b04a0a9966774b290868f0fc40fd"

0 commit comments

Comments
 (0)