-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (50 loc) · 1.46 KB
/
index.js
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
55
56
57
const { convert } = require("html-to-text");
const mergeWith = require("lodash.mergewith");
const DEFAULTS = {
phraseRegex: /(\p{Terminal_Punctuation}\p{White_Space})/gu,
lengthCutoff: 200,
terminator: "...",
htmlToTextOptions: {
wordwrap: false,
selectors: [
{ selector: "a", options: { ignoreHref: true } },
{ selector: "img", format: "skip" },
...["h1", "h2", "h3", "h4", "h5", "h6"].map((selector) => ({
selector,
options: { uppercase: false },
})),
],
},
};
function concatArrays(dst, src) {
if (Array.isArray(dst)) {
return dst.concat(src);
}
}
function description(templateContent, overrides) {
const phraseRegex = overrides.phraseRegex ?? DEFAULTS.phraseRegex;
const lengthCutoff = overrides.lengthCutoff ?? DEFAULTS.lengthCutoff;
const terminator = overrides.terminator ?? DEFAULTS.terminator;
const htmlToTextOptions = mergeWith(
{},
DEFAULTS.htmlToTextOptions,
overrides.htmlToTextOptions,
concatArrays,
);
let content = convert(templateContent, htmlToTextOptions);
let phrases = content.split(phraseRegex);
let description = "";
while (phrases.length > 0 && description.length < lengthCutoff) {
description += phrases.shift();
}
description += terminator;
return description;
}
module.exports = {
description,
configFunction(eleventyConfig, overrides) {
eleventyConfig.addFilter("description", (text) =>
description(text, overrides),
);
},
};