Ghost/core/frontend/meta/og_image.js
Hannah Wolfe bd597db829
Moved settings/cache to shared/settings-cache
- This is part of the quest to separate the frontend and server & get rid of all the places where there are cross-requires
- At the moment the settings cache is one big shared cache used by the frontend and server liberally
- This change doesn't really solve the fundamental problems, as we still depend on events, and requires from inside frontend
- However it allows us to control the misuse slightly better by getting rid of restricted requires and turning on that eslint ruleset
2021-06-30 15:49:10 +01:00

45 lines
1.8 KiB
JavaScript

const _ = require('lodash');
const getContextObject = require('./context_object.js');
const urlUtils = require('../../shared/url-utils');
const settingsCache = require('../../shared/settings-cache');
function getOgImage(data) {
const context = data.context ? data.context : null;
const contextObject = getContextObject(data, context, false);
if (_.includes(context, 'home')) {
const imgUrl = settingsCache.get('og_image') || settingsCache.get('cover_image');
return (imgUrl && urlUtils.relativeToAbsolute(imgUrl)) || null;
}
if (_.includes(context, 'post') || _.includes(context, 'page') || _.includes(context, 'amp')) {
if (contextObject.og_image) {
return urlUtils.relativeToAbsolute(contextObject.og_image);
} else if (contextObject.feature_image) {
return urlUtils.relativeToAbsolute(contextObject.feature_image);
} else if (settingsCache.get('og_image')) {
return urlUtils.relativeToAbsolute(settingsCache.get('og_image'));
} else if (settingsCache.get('cover_image')) {
return urlUtils.relativeToAbsolute(settingsCache.get('cover_image'));
}
}
if (_.includes(context, 'author') && contextObject.cover_image) {
return urlUtils.relativeToAbsolute(contextObject.cover_image);
}
if (_.includes(context, 'tag')) {
if (contextObject.og_image) {
return urlUtils.relativeToAbsolute(contextObject.og_image);
} else if (contextObject.feature_image) {
return urlUtils.relativeToAbsolute(contextObject.feature_image);
} else if (settingsCache.get('cover_image')) {
return urlUtils.relativeToAbsolute(settingsCache.get('cover_image'));
}
}
return null;
}
module.exports = getOgImage;