Ghost/core/frontend/meta/context_object.js
Naz Gargol df7e64fafa
Extracted frontend folder (#10780)
refs #10790

- Moved /core/apps into core/frontend
- Moved /core/server/helpers to /core/frontend/helpers along with /core/server/services/themes
- Changed helper location in overrides
- Moved /core/server/services/routing to /core/frontend/services
- Moved /core/server/services/url to /core/frontend/services
- Moved /core/server/data/meta to /core/frontend/meta
- Moved /core/server/services/rss to /core/frontend/services
- Moved /core/server/data/xml to /core/frontend/services
2019-06-19 11:30:28 +02:00

39 lines
1.4 KiB
JavaScript

var settingsCache = require('../../server/services/settings/cache'),
_ = require('lodash');
function getContextObject(data, context) {
/**
* If the data object does not contain the requested context, we return the fallback object.
*/
const blog = {
cover_image: settingsCache.get('cover_image'),
twitter: settingsCache.get('twitter'),
facebook: settingsCache.get('facebook')
};
let chosenContext;
// @TODO: meta layer is very broken, it's really hard to understand what it's doing
// The problem is that handlebars root object is structured differently. Sometimes the object is flat on data
// and sometimes the object is part of a key e.g. data.post. This needs to be prepared at the very first stage and not in each helper.
if ((_.includes(context, 'page') || _.includes(context, 'amp')) && data.post) {
chosenContext = data.post;
} else if (_.includes(context, 'post') && data.post) {
chosenContext = data.post;
} else if (_.includes(context, 'page') && data.page) {
chosenContext = data.page;
} else if (data[context]) {
// @NOTE: This is confusing as hell. It tries to get data[['author']], which works, but coincidence?
chosenContext = data[context];
}
// Super fallback.
if (!chosenContext) {
chosenContext = blog;
}
return chosenContext;
}
module.exports = getContextObject;