Ghost/core/frontend/meta/description.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

85 lines
3.5 KiB
JavaScript

const _ = require('lodash');
const settingsCache = require('../../shared/settings-cache');
const getExcerpt = require('./excerpt');
function getDescription(data, root, options = {}) {
const context = root ? root.context : null;
let description = '';
// We only return meta_description if provided
if (data.meta_description) {
description = data.meta_description;
} else if (_.includes(context, 'home')) {
const siteDescription = settingsCache.get('meta_description') || settingsCache.get('description');
if (options.property) {
// options.property = null/'og'/'twitter'
const optionsPropertyName = `${options.property || 'meta'}_description`;
description = settingsCache.get(optionsPropertyName) || siteDescription || '';
} else {
description = siteDescription;
}
} else if (_.includes(context, 'author') && data.author) {
if (!options.property && _.includes(context, 'paged')) {
description = '';
} else {
// The usage of meta data fields for author is currently not implemented.
// We do have meta_description and meta_title fields
// in the users table, but there's no UI to populate those.
description = data.author.meta_description
|| data.author.bio
|| (options.property ? settingsCache.get('meta_description') : '')
|| '';
}
} else if (_.includes(context, 'tag') && data.tag) {
if (!options.property && _.includes(context, 'paged')) {
description = '';
} else {
description = data.tag[`${options.property}_description`]
|| data.tag.meta_description
|| data.tag.description
|| (options.property ? settingsCache.get('meta_description') : '')
|| '';
}
} else if (_.includes(context, 'post') && data.post) {
if (options.property) {
description = data.post[`${options.property}_description`]
|| data.post.custom_excerpt
|| data.post.meta_description
|| getExcerpt(data.post.html || '', {words: 50})
|| settingsCache.get('description')
|| '';
} else {
description = data.post.meta_description || '';
}
} else if (_.includes(context, 'page') && data.post) {
// Page description dependent on legacy object formatting (https://github.com/TryGhost/Ghost/issues/10042)
if (options.property) {
description = data.post[`${options.property}_description`]
|| data.post.custom_excerpt
|| data.post.meta_description
|| getExcerpt(data.post.html || '', {words: 50})
|| settingsCache.get('description')
|| '';
} else {
description = data.post.meta_description || '';
}
} else if (_.includes(context, 'page') && data.page) {
if (options.property) {
description = data.page[`${options.property}_description`]
|| data.page.custom_excerpt
|| data.page.meta_description
|| getExcerpt(data.page.html || '', {words: 50})
|| settingsCache.get('description')
|| '';
} else {
description = data.page.meta_description || '';
}
}
return (description || '').trim() || null;
}
module.exports = getDescription;