mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-30 14:22:07 +03:00
be4a5a84d9
refs https://github.com/TryGhost/Ghost/issues/10921, closes https://github.com/TryGhost/Ghost/issues/11357, closes https://github.com/TryGhost/Ghost/issues/11403 - updates the sources and fallbacks for the output of `{{ghost_head}}` meta/structured data - re-works related tests to better show the fallback chains for different scenarios - fixes `{{ghost_head}}` tests to use `before/afterEach` so that tests are not interdependent
84 lines
3.4 KiB
JavaScript
84 lines
3.4 KiB
JavaScript
const _ = require('lodash');
|
|
const settingsCache = require('../../server/services/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.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;
|