2014-10-10 18:54:07 +04:00
|
|
|
// # Content Helper
|
|
|
|
// Usage: `{{content}}`, `{{content words="20"}}`, `{{content characters="256"}}`
|
|
|
|
//
|
|
|
|
// Turns content html into a safestring so that the user doesn't have to
|
|
|
|
// escape it or tell handlebars to leave it alone with a triple-brace.
|
|
|
|
//
|
2021-02-09 09:46:43 +03:00
|
|
|
// Shows default or custom CTA when trying to see content without access
|
|
|
|
//
|
2014-10-10 18:54:07 +04:00
|
|
|
// Enables tag-safe truncation of content by characters or words.
|
2020-09-08 10:19:36 +03:00
|
|
|
//
|
|
|
|
// Dev flag feature: In case of restricted content access for member-only posts, shows CTA box
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2020-11-03 12:06:21 +03:00
|
|
|
const {templates, hbs, SafeString} = require('../services/proxy');
|
2019-04-02 09:36:13 +03:00
|
|
|
const downsize = require('downsize');
|
2020-09-08 10:19:36 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const createFrame = hbs.handlebars.createFrame;
|
|
|
|
|
|
|
|
function restrictedCta(options) {
|
|
|
|
options = options || {};
|
|
|
|
options.data = options.data || {};
|
|
|
|
_.merge(this, {
|
2021-03-24 15:55:26 +03:00
|
|
|
accentColor: (options.data.site && options.data.site.accent_color)
|
2020-09-08 10:19:36 +03:00
|
|
|
});
|
|
|
|
const data = createFrame(options.data);
|
2021-02-09 09:46:43 +03:00
|
|
|
return templates.execute('content-cta', this, {data});
|
2020-09-08 10:19:36 +03:00
|
|
|
}
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2019-04-02 09:36:13 +03:00
|
|
|
module.exports = function content(options = {}) {
|
2020-09-08 10:19:36 +03:00
|
|
|
let self = this;
|
|
|
|
let args = arguments;
|
|
|
|
|
2019-04-02 09:36:13 +03:00
|
|
|
const hash = options.hash || {};
|
|
|
|
const truncateOptions = {};
|
|
|
|
let runTruncate = false;
|
|
|
|
|
|
|
|
for (const key of ['words', 'characters']) {
|
2019-08-08 11:47:13 +03:00
|
|
|
if (Object.prototype.hasOwnProperty.call(hash, key)) {
|
2019-04-02 09:36:13 +03:00
|
|
|
runTruncate = true;
|
|
|
|
truncateOptions[key] = parseInt(hash[key], 10);
|
|
|
|
}
|
|
|
|
}
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2019-03-18 14:46:59 +03:00
|
|
|
if (this.html === null) {
|
|
|
|
this.html = '';
|
|
|
|
}
|
|
|
|
|
2020-11-03 12:06:21 +03:00
|
|
|
if (!_.isUndefined(this.access) && !this.access) {
|
2020-09-08 10:19:36 +03:00
|
|
|
return restrictedCta.apply(self, args);
|
|
|
|
}
|
|
|
|
|
2019-04-02 09:36:13 +03:00
|
|
|
if (runTruncate) {
|
2017-04-04 19:07:35 +03:00
|
|
|
return new SafeString(
|
2014-10-10 18:54:07 +04:00
|
|
|
downsize(this.html, truncateOptions)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
return new SafeString(this.html);
|
2014-10-10 18:54:07 +04:00
|
|
|
};
|