forked from nodejs/nodejs.org
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsummary.js
More file actions
54 lines (45 loc) · 1.39 KB
/
Copy pathsummary.js
File metadata and controls
54 lines (45 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict';
const cheerio = require('cheerio');
const SUMMARY_MAX_LENGTH = 300;
const IGNORE_SELECTORS = [
'.blogpost-header',
'.anchor',
'h1',
'h2',
'h3',
'blockquote'
];
/**
* Due to the nature of metalsmith and
* how the metalsmith-paginate plugin operates,
* this helper has to handle two different types of
* HTML contents:
* - clean blog posts converted from markdown to HTML,
* seen on the first page of blog posts
* - the remaining paginated pages has gone
* through the handlebars process and therefore has the
* entire page layout (w/<html>, <head> and <body> etc)
* wrapped around the actual blog contents :(
*/
module.exports = (contents, locale, path) => {
const $ = cheerio.load(contents);
const $body = $('body');
const hasBody = $body.length > 0;
const $elements = hasBody ? $body.find('article > *') : $('*');
let summary = '';
$elements
.not((i, elem) => IGNORE_SELECTORS.some((selector) => $(elem).is(selector)))
.each((i, elem) => {
if (summary.length > SUMMARY_MAX_LENGTH) {
summary += `<p><a href="/${locale}/${path}/">Read more...</a></p>`;
return false;
}
// Don't re-add nested elements when extracting summary
// from blog posts not contained in a complete HTML document
if (!hasBody && elem.parent) {
return;
}
summary += $.html(elem);
});
return summary;
};