mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-28 05:37:34 +03:00
1272968848
no issue
refs e3a0bb535f
Previously, {{content}} helper was updated use default or custom template to show a CTA when trying to use helper without access to content.
While the change was expected to not affect any existing themes as `{{content}}` helper is not supposed to be used on member sites without `access` check, we found quite a few themes use a pattern of using `content.hbs` with `{{content}}` helper inside them as default, which causes infinite loop in how content helper works in certain cases.
Whilst this followed the pattern of the other helpers using a partial with the same name, there are 2 key differences:
- this partial template pattern is being introduced way after the inception of themes with a fairly generic name “content”
- this partial template isn’t used to render the helper all the time - just the CTA part under certain circumstances.
This change updates the template name to `content-cta.hbs` to which makes it less likely to clash, and makes more sense as to what it is.
59 lines
1.7 KiB
JavaScript
59 lines
1.7 KiB
JavaScript
// # 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.
|
|
//
|
|
// Shows default or custom CTA when trying to see content without access
|
|
//
|
|
// Enables tag-safe truncation of content by characters or words.
|
|
//
|
|
// Dev flag feature: In case of restricted content access for member-only posts, shows CTA box
|
|
|
|
const {templates, hbs, SafeString} = require('../services/proxy');
|
|
const downsize = require('downsize');
|
|
const _ = require('lodash');
|
|
const createFrame = hbs.handlebars.createFrame;
|
|
|
|
function restrictedCta(options) {
|
|
options = options || {};
|
|
options.data = options.data || {};
|
|
_.merge(this, {
|
|
accentColor: (options.data.site && options.data.site.accent_color) || '#3db0ef'
|
|
});
|
|
const data = createFrame(options.data);
|
|
return templates.execute('content-cta', this, {data});
|
|
}
|
|
|
|
module.exports = function content(options = {}) {
|
|
let self = this;
|
|
let args = arguments;
|
|
|
|
const hash = options.hash || {};
|
|
const truncateOptions = {};
|
|
let runTruncate = false;
|
|
|
|
for (const key of ['words', 'characters']) {
|
|
if (Object.prototype.hasOwnProperty.call(hash, key)) {
|
|
runTruncate = true;
|
|
truncateOptions[key] = parseInt(hash[key], 10);
|
|
}
|
|
}
|
|
|
|
if (this.html === null) {
|
|
this.html = '';
|
|
}
|
|
|
|
if (!_.isUndefined(this.access) && !this.access) {
|
|
return restrictedCta.apply(self, args);
|
|
}
|
|
|
|
if (runTruncate) {
|
|
return new SafeString(
|
|
downsize(this.html, truncateOptions)
|
|
);
|
|
}
|
|
|
|
return new SafeString(this.html);
|
|
};
|