mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-21 09:52:06 +03:00
52b924638d
- The frontend proxy is meant to be a way to pass critical internal pieces of Ghost core into the frontend - These fundamental @tryghost packages are shared and can be required directly, hence there's no need to pass them via the proxy - Reducing the surface area of the proxy reduces the proxies API - This makes it easier to see what's left in terms of decoupling the frontend, and what will always need to be passed (e.g. api) Note on @tryghost/social-urls: - this is a small utility that helps create URLs for social profiles, it's a util for working with data on the frontend aka part of the sdk - I think there should be many of these small helpers and we'll probably want to bundle them for the frontend at some point - for now, I'm leaving these as part of the proxy, as need to figure out where they belong
109 lines
2.9 KiB
JavaScript
109 lines
2.9 KiB
JavaScript
const {SafeString, labs} = require('../services/proxy');
|
|
|
|
const logging = require('@tryghost/logging');
|
|
const tpl = require('@tryghost/tpl');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const messages = {
|
|
invalidAttribute: 'Invalid or no attribute given to match helper'
|
|
};
|
|
|
|
/**
|
|
* This is identical to the built-in if helper, except inverse/fn calls are replaced with false/true
|
|
* https://github.com/handlebars-lang/handlebars.js/blob/19bdace85a8d0bc5ed3a4dec4071cb08c8d003f2/lib/handlebars/helpers/if.js#L9-L20
|
|
*/
|
|
function isEmptyValue(value) {
|
|
if (!value && value !== 0) {
|
|
return true;
|
|
} else if (Array.isArray(value) && value.length === 0) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
const handleConditional = (conditional, options) => {
|
|
if (_.isFunction(conditional)) {
|
|
conditional = conditional.call(this);
|
|
}
|
|
|
|
if (conditional instanceof SafeString) {
|
|
conditional = conditional.string;
|
|
}
|
|
|
|
// Default behavior is to render the positive path if the value is truthy and not empty.
|
|
// The `includeZero` option may be set to treat the condtional as purely not empty based on the
|
|
// behavior of isEmpty. Effectively this determines if 0 is handled by the positive path or negative.
|
|
if ((!options.hash.includeZero && !conditional) || isEmptyValue(conditional)) {
|
|
return false;
|
|
} else {
|
|
return true;
|
|
}
|
|
};
|
|
|
|
const handleMatch = (data, operator, value) => {
|
|
let result;
|
|
|
|
switch (operator) {
|
|
case '!=':
|
|
result = data !== value;
|
|
break;
|
|
default:
|
|
result = data === value;
|
|
}
|
|
|
|
return result;
|
|
};
|
|
|
|
function match(...attrs) {
|
|
// options = options || {};
|
|
// options.hash = options.hash || {};
|
|
// options.data = options.data || {};
|
|
|
|
const options = attrs.pop();
|
|
const isBlock = _.has(options, 'fn');
|
|
let result;
|
|
|
|
if (_.isEmpty(attrs)) {
|
|
logging.warn(tpl(messages.invalidAttribute));
|
|
return;
|
|
}
|
|
|
|
if (attrs.length === 1) {
|
|
// If we only have one attribute, treat it as simple true/false (like the if helper)
|
|
result = handleConditional(attrs[0], options);
|
|
} else if (attrs.length === 3) {
|
|
result = handleMatch(attrs[0], attrs[1], attrs[2], options);
|
|
} else {
|
|
logging.warn(tpl(messages.invalidAttribute));
|
|
return;
|
|
}
|
|
|
|
// If we're in block mode, return the outcome from the fn/inverse functions
|
|
if (isBlock) {
|
|
if (result) {
|
|
return options.fn(this);
|
|
}
|
|
|
|
return options.inverse(this);
|
|
}
|
|
|
|
// Else return the result as a string
|
|
return new SafeString(result);
|
|
}
|
|
|
|
module.exports = function matchLabsWrapper() {
|
|
let self = this;
|
|
let args = arguments;
|
|
|
|
return labs.enabledHelper({
|
|
flagKey: 'matchHelper',
|
|
flagName: 'Match helper',
|
|
helperName: 'match',
|
|
helpUrl: 'https://ghost.org/docs/themes/'
|
|
}, () => {
|
|
return match.apply(self, args);
|
|
});
|
|
};
|