2017-12-14 05:01:23 +03:00
|
|
|
var settingsCache = require('../../services/settings/cache'),
|
2017-02-03 16:15:11 +03:00
|
|
|
_ = require('lodash');
|
2016-03-30 12:41:41 +03:00
|
|
|
|
|
|
|
function getContextObject(data, context) {
|
2017-02-03 16:15:11 +03:00
|
|
|
/**
|
|
|
|
* If the data object does not contain the requested context, we return the fallback object.
|
|
|
|
*/
|
2019-03-12 14:13:47 +03:00
|
|
|
const blog = {
|
|
|
|
cover_image: settingsCache.get('cover_image'),
|
|
|
|
twitter: settingsCache.get('twitter'),
|
|
|
|
facebook: settingsCache.get('facebook')
|
|
|
|
};
|
2016-03-30 12:41:41 +03:00
|
|
|
|
2019-03-12 14:13:47 +03:00
|
|
|
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.
|
2019-03-13 13:13:52 +03:00
|
|
|
if ((_.includes(context, 'page') || _.includes(context, 'amp')) && data.post) {
|
2019-03-12 14:13:47 +03:00
|
|
|
chosenContext = data.post;
|
|
|
|
} else if (_.includes(context, 'post') && data.post) {
|
|
|
|
chosenContext = data.post;
|
2019-03-13 13:13:52 +03:00
|
|
|
} else if (_.includes(context, 'page') && data.page) {
|
|
|
|
chosenContext = data.page;
|
2019-03-12 14:13:47 +03:00
|
|
|
} else if (data[context]) {
|
|
|
|
// @NOTE: This is confusing as hell. It tries to get data[['author']], which works, but coincidence?
|
|
|
|
chosenContext = data[context];
|
2019-03-13 13:13:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Super fallback.
|
|
|
|
if (!chosenContext) {
|
2019-03-12 14:13:47 +03:00
|
|
|
chosenContext = blog;
|
|
|
|
}
|
|
|
|
|
|
|
|
return chosenContext;
|
2016-03-30 12:41:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = getContextObject;
|