mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-24 19:33:02 +03:00
243b387063
refs #8126, #8221, #8223 ✨ New 'Proxy' for all helper requires - this is not currently enforced, but could be, much like apps - the proxy object is HUGE - changed date to use SafeString, this should have been there anyway - use the proxy for all helpers, including those in apps 😁 ✨ 🎨 Single instance of hbs for theme + for errors - we now have theme/engine instead of requiring express-hbs everywhere - only error-handler still also requires express-hbs, this is so that we can render errors without extra crud - TODO: remove the asset helper after #8126 IF it is not needed, or else remove the TODO 🎨 Cleanup visibility utils 🎨 Clean up the proxy a little bit 🚨 Unskip test as it now works! 🎨 Minor amends as per comments
78 lines
2.3 KiB
JavaScript
78 lines
2.3 KiB
JavaScript
// ### Navigation Helper
|
|
// `{{navigation}}`
|
|
// Outputs navigation menu of static urls
|
|
|
|
var proxy = require('./proxy'),
|
|
_ = require('lodash'),
|
|
SafeString = proxy.SafeString,
|
|
i18n = proxy.i18n,
|
|
errors = proxy.errors,
|
|
templates = proxy.templates;
|
|
|
|
module.exports = function navigation(options) {
|
|
var navigationData = options.data.blog.navigation,
|
|
currentUrl = options.data.root.relativeUrl,
|
|
self = this,
|
|
output,
|
|
data;
|
|
|
|
if (!_.isObject(navigationData) || _.isFunction(navigationData)) {
|
|
throw new errors.IncorrectUsageError({
|
|
message: i18n.t('warnings.helpers.navigation.invalidData')
|
|
});
|
|
}
|
|
|
|
if (navigationData.filter(function (e) {
|
|
return (_.isUndefined(e.label) || _.isUndefined(e.url));
|
|
}).length > 0) {
|
|
throw new errors.IncorrectUsageError({
|
|
message: i18n.t('warnings.helpers.navigation.valuesMustBeDefined')
|
|
});
|
|
}
|
|
|
|
// check for non-null string values
|
|
if (navigationData.filter(function (e) {
|
|
return ((!_.isNull(e.label) && !_.isString(e.label)) ||
|
|
(!_.isNull(e.url) && !_.isString(e.url)));
|
|
}).length > 0) {
|
|
throw new errors.IncorrectUsageError({
|
|
message: i18n.t('warnings.helpers.navigation.valuesMustBeString')
|
|
});
|
|
}
|
|
|
|
function _slugify(label) {
|
|
return label.toLowerCase().replace(/[^\w ]+/g, '').replace(/ +/g, '-');
|
|
}
|
|
|
|
// strips trailing slashes and compares urls
|
|
function _isCurrentUrl(href, currentUrl) {
|
|
if (!currentUrl) {
|
|
return false;
|
|
}
|
|
|
|
var strippedHref = href.replace(/\/+$/, ''),
|
|
strippedCurrentUrl = currentUrl.replace(/\/+$/, '');
|
|
return strippedHref === strippedCurrentUrl;
|
|
}
|
|
|
|
// {{navigation}} should no-op if no data passed in
|
|
if (navigationData.length === 0) {
|
|
return new SafeString('');
|
|
}
|
|
|
|
output = navigationData.map(function (e) {
|
|
var out = {};
|
|
out.current = _isCurrentUrl(e.url, currentUrl);
|
|
out.label = e.label;
|
|
out.slug = _slugify(e.label);
|
|
out.url = e.url;
|
|
out.secure = self.secure;
|
|
return out;
|
|
});
|
|
|
|
data = _.merge({}, {navigation: output});
|
|
|
|
return templates.execute('navigation', data, options);
|
|
};
|
|
|