2014-10-10 18:54:07 +04:00
|
|
|
// # Is Helper
|
|
|
|
// Usage: `{{#is "paged"}}`, `{{#is "index, paged"}}`
|
|
|
|
// Checks whether we're in a given context.
|
2021-09-26 23:01:13 +03:00
|
|
|
const logging = require('@tryghost/logging');
|
|
|
|
const tpl = require('@tryghost/tpl');
|
2020-03-30 23:23:02 +03:00
|
|
|
const _ = require('lodash');
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2021-09-23 22:46:22 +03:00
|
|
|
const messages = {
|
|
|
|
invalidAttribute: 'Invalid or no attribute given to is helper'
|
|
|
|
};
|
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
module.exports = function is(context, options) {
|
2014-10-10 18:54:07 +04:00
|
|
|
options = options || {};
|
|
|
|
|
2020-04-29 18:44:27 +03:00
|
|
|
const currentContext = options.data.root.context;
|
2014-10-10 18:54:07 +04:00
|
|
|
|
|
|
|
if (!_.isString(context)) {
|
2021-09-23 22:46:22 +03:00
|
|
|
logging.warn(tpl(messages.invalidAttribute));
|
2014-10-10 18:54:07 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function evaluateContext(expr) {
|
|
|
|
return expr.split(',').map(function (v) {
|
|
|
|
return v.trim();
|
|
|
|
}).reduce(function (p, c) {
|
2016-06-11 21:23:27 +03:00
|
|
|
return p || _.includes(currentContext, c);
|
2014-10-10 18:54:07 +04:00
|
|
|
}, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (evaluateContext(context)) {
|
|
|
|
return options.fn(this);
|
|
|
|
}
|
|
|
|
return options.inverse(this);
|
|
|
|
};
|