mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-22 18:31:57 +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
93 lines
3.0 KiB
JavaScript
93 lines
3.0 KiB
JavaScript
// ### Navigation Helper
|
|
// `{{navigation}}`
|
|
// Outputs navigation menu of static urls
|
|
const {SafeString, templates, hbs} = require('../services/proxy');
|
|
|
|
const errors = require('@tryghost/errors');
|
|
const tpl = require('@tryghost/tpl');
|
|
const {slugify} = require('@tryghost/string');
|
|
const _ = require('lodash');
|
|
|
|
const messages = {
|
|
invalidData: 'navigation data is not an object or is a function',
|
|
valuesMustBeDefined: 'All values must be defined for label, url and current',
|
|
valuesMustBeString: 'Invalid value, Url and Label must be strings'
|
|
};
|
|
|
|
const createFrame = hbs.handlebars.createFrame;
|
|
|
|
module.exports = function navigation(options) {
|
|
options = options || {};
|
|
options.hash = options.hash || {};
|
|
options.data = options.data || {};
|
|
|
|
const key = options.hash.type && options.hash.type === 'secondary' ? 'secondary_navigation' : 'navigation';
|
|
// Set isSecondary so we can compare in the template
|
|
options.hash.isSecondary = !!(options.hash.type && options.hash.type === 'secondary');
|
|
// Remove type, so it's not accessible
|
|
delete options.hash.type;
|
|
|
|
const navigationData = options.data.site[key];
|
|
const currentUrl = options.data.root.relativeUrl;
|
|
const self = this;
|
|
let output;
|
|
|
|
if (!_.isObject(navigationData) || _.isFunction(navigationData)) {
|
|
throw new errors.IncorrectUsageError({
|
|
message: tpl(messages.invalidData)
|
|
});
|
|
}
|
|
|
|
if (navigationData.filter(function (e) {
|
|
return (_.isUndefined(e.label) || _.isUndefined(e.url));
|
|
}).length > 0) {
|
|
throw new errors.IncorrectUsageError({
|
|
message: tpl(messages.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: tpl(messages.valuesMustBeString)
|
|
});
|
|
}
|
|
|
|
// strips trailing slashes and compares urls
|
|
function _isCurrentUrl(href, url) {
|
|
if (!url) {
|
|
return false;
|
|
}
|
|
|
|
const strippedHref = href.replace(/\/+$/, '');
|
|
const strippedCurrentUrl = url.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) {
|
|
const 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;
|
|
});
|
|
|
|
// CASE: The navigation helper should have access to the navigation items at the top level.
|
|
this.navigation = output;
|
|
// CASE: The navigation helper will forward attributes passed to it.
|
|
_.merge(this, options.hash);
|
|
const data = createFrame(options.data);
|
|
|
|
return templates.execute('navigation', this, {data});
|
|
};
|