2022-05-13 18:23:30 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
const mergeSettings = (extraSettings) => {
|
|
|
|
return _.mergeWith({}, baseSettings, extraSettings, function customizer(objValue, srcValue) {
|
|
|
|
if (_.isArray(objValue)) {
|
|
|
|
return objValue.concat(srcValue);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-05-13 16:42:26 +03:00
|
|
|
const baseSettings = {
|
|
|
|
wordwrap: false,
|
|
|
|
preserveNewlines: true,
|
|
|
|
|
|
|
|
// equiv returnDomByDefault: true,
|
|
|
|
baseElements: {returnDomByDefault: true},
|
|
|
|
selectors: [
|
|
|
|
// Ignore images, equiv ignoreImage: true
|
|
|
|
{selector: 'img', format: 'skip'},
|
|
|
|
|
|
|
|
// disable uppercase headings, equiv uppercaseHeadings: false
|
|
|
|
{selector: 'h1', options: {uppercase: false}},
|
|
|
|
{selector: 'h2', options: {uppercase: false}},
|
|
|
|
{selector: 'h3', options: {uppercase: false}},
|
|
|
|
{selector: 'h4', options: {uppercase: false}},
|
|
|
|
{selector: 'h5', options: {uppercase: false}},
|
|
|
|
{selector: 'h6', options: {uppercase: false}},
|
|
|
|
{selector: 'table', options: {uppercaseHeaderCells: false}},
|
|
|
|
|
|
|
|
// Backwards compatibility with html-to-text 5.1.1
|
|
|
|
{selector: 'div', format: 'inline'}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
let excerptConverter;
|
|
|
|
let emailConverter;
|
2022-08-02 18:44:00 +03:00
|
|
|
let commentConverter;
|
2022-05-13 16:42:26 +03:00
|
|
|
|
|
|
|
const loadConverters = () => {
|
|
|
|
if (excerptConverter && emailConverter) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const {compile} = require('html-to-text');
|
|
|
|
|
2022-05-13 18:23:30 +03:00
|
|
|
const excerptSettings = mergeSettings({
|
|
|
|
selectors: [
|
|
|
|
{selector: 'a', options: {ignoreHref: true}},
|
2022-05-16 17:43:46 +03:00
|
|
|
{selector: 'figcaption', format: 'skip'},
|
|
|
|
// Strip inline and bottom footnotes
|
|
|
|
{selector: 'a[rel=footnote]', format: 'skip'},
|
2022-05-23 21:49:06 +03:00
|
|
|
{selector: 'div.footnotes', format: 'skip'},
|
|
|
|
// Don't output hrs
|
|
|
|
{selector: 'hr', format: 'skip'},
|
|
|
|
// Don't output > in blockquotes
|
2023-06-29 15:45:30 +03:00
|
|
|
{selector: 'blockquote', format: 'block'},
|
|
|
|
// Don't include signup cards in excerpts
|
|
|
|
{selector: '.kg-signup-card', format: 'skip'}
|
2022-05-13 18:23:30 +03:00
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
const emailSettings = mergeSettings({
|
|
|
|
selectors: [
|
|
|
|
// equiv hideLinkHrefIfSameAsText: true
|
2023-06-29 13:47:17 +03:00
|
|
|
{selector: 'a', options: {hideLinkHrefIfSameAsText: true}},
|
|
|
|
// Don't include html .preheader in email
|
|
|
|
{selector: '.preheader', format: 'skip'}
|
2022-05-13 18:23:30 +03:00
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2022-08-02 18:44:00 +03:00
|
|
|
const commentSettings = mergeSettings({
|
|
|
|
preserveNewlines: false,
|
|
|
|
selectors: [
|
|
|
|
// equiv hideLinkHrefIfSameAsText: true
|
|
|
|
{selector: 'a', options: {hideLinkHrefIfSameAsText: true}},
|
|
|
|
// No space between <p> tags. An empty <p> is needed
|
|
|
|
{selector: 'p', options: {leadingLineBreaks: 1, trailingLineBreaks: 1}}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2022-05-13 18:23:30 +03:00
|
|
|
excerptConverter = compile(excerptSettings);
|
|
|
|
emailConverter = compile(emailSettings);
|
2022-08-02 18:44:00 +03:00
|
|
|
commentConverter = compile(commentSettings);
|
2022-05-13 16:42:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.excerpt = (html) => {
|
|
|
|
loadConverters();
|
|
|
|
|
|
|
|
return excerptConverter(html);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.email = (html) => {
|
|
|
|
loadConverters();
|
|
|
|
|
|
|
|
return emailConverter(html);
|
2021-02-18 02:00:26 +03:00
|
|
|
};
|
2022-08-02 18:44:00 +03:00
|
|
|
|
|
|
|
module.exports.comment = (html) => {
|
|
|
|
loadConverters();
|
|
|
|
|
|
|
|
return commentConverter(html);
|
|
|
|
};
|