Ghost/core/server/helpers/navigation.js
JT Turner e4c52a6915 Fix urlFor to handle secure correctly
issue #6270
- Exposed getBaseUrl on the config class.
- Fix formatting config index as array was more then 140 characters long.
- Updated getBaseUrl to handle secure by replacing http with https if true.
- Fixed ghost_head helper to output canonical base url no https.
- Fixed ghost_head helper to set secure correctly for the rss link.
- Fixed navigation helper to pass secure in each nav item, so that urlFor can u$
- Fixed {{url}} to pass secure correctly to config.urlFor.
- Fixed test to use urlSSL over https besides for canonical.
- Add tests for {{url}} and to make sure they output https for absolute and secure.
- Update twitter and og url to use the canonical url.
2016-01-11 19:40:30 -08:00

63 lines
1.9 KiB
JavaScript

// ### Navigation Helper
// `{{navigation}}`
// Outputs navigation menu of static urls
var _ = require('lodash'),
hbs = require('express-hbs'),
errors = require('../errors'),
template = require('./template'),
navigation;
navigation = function (options) {
/*jshint unused:false*/
var navigationData = options.data.blog.navigation,
currentUrl = options.data.root.relativeUrl,
self = this,
output,
context;
if (!_.isObject(navigationData) || _.isFunction(navigationData)) {
return errors.logAndThrowError('navigation data is not an object or is a function');
}
if (navigationData.filter(function (e) {
return (_.isUndefined(e.label) || _.isUndefined(e.url));
}).length > 0) {
return errors.logAndThrowError('All values must be defined for label, url and current');
}
// 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) {
return errors.logAndThrowError('Invalid value, Url and Label must be strings');
}
function _slugify(label) {
return label.toLowerCase().replace(/[^\w ]+/g, '').replace(/ +/g, '-');
}
// {{navigation}} should no-op if no data passed in
if (navigationData.length === 0) {
return new hbs.SafeString('');
}
output = navigationData.map(function (e) {
var out = {};
out.current = e.url === currentUrl;
out.label = e.label;
out.slug = _slugify(e.label);
out.url = hbs.handlebars.Utils.escapeExpression(e.url);
out.secure = self.secure;
return out;
});
context = _.merge({}, {navigation: output});
return template.execute('navigation', context, options);
};
module.exports = navigation;