2019-07-25 12:08:29 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const settingsCache = require('../../server/services/settings/cache');
|
2019-11-21 16:08:00 +03:00
|
|
|
const getExcerpt = require('./excerpt');
|
2016-01-17 13:07:52 +03:00
|
|
|
|
2019-11-21 16:08:00 +03:00
|
|
|
function getDescription(data, root, options = {}) {
|
2019-07-25 12:08:29 +03:00
|
|
|
const context = root ? root.context : null;
|
|
|
|
|
|
|
|
let description = '';
|
2017-08-03 14:48:39 +03:00
|
|
|
|
2019-07-25 12:08:29 +03:00
|
|
|
// We only return meta_description if provided
|
2016-01-17 13:07:52 +03:00
|
|
|
if (data.meta_description) {
|
|
|
|
description = data.meta_description;
|
2016-06-11 21:23:27 +03:00
|
|
|
} else if (_.includes(context, 'home')) {
|
2019-11-21 16:08:00 +03:00
|
|
|
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 || '';
|
2019-07-25 12:08:29 +03:00
|
|
|
} else {
|
|
|
|
description = siteDescription;
|
|
|
|
}
|
2016-06-11 21:23:27 +03:00
|
|
|
} else if (_.includes(context, 'author') && data.author) {
|
2019-11-21 16:08:00 +03:00
|
|
|
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') : '')
|
|
|
|
|| '';
|
|
|
|
}
|
2016-06-11 21:23:27 +03:00
|
|
|
} else if (_.includes(context, 'tag') && data.tag) {
|
2019-11-21 16:08:00 +03:00
|
|
|
if (!options.property && _.includes(context, 'paged')) {
|
|
|
|
description = '';
|
|
|
|
} else {
|
|
|
|
description = data.tag.meta_description
|
|
|
|
|| data.tag.description
|
|
|
|
|| (options.property ? settingsCache.get('meta_description') : '')
|
|
|
|
|| '';
|
|
|
|
}
|
2019-03-12 03:16:02 +03:00
|
|
|
} else if (_.includes(context, 'post') && data.post) {
|
2019-11-21 16:08:00 +03:00
|
|
|
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')
|
|
|
|
|| '';
|
2017-08-03 14:48:39 +03:00
|
|
|
} else {
|
|
|
|
description = data.post.meta_description || '';
|
|
|
|
}
|
2019-11-11 12:40:53 +03:00
|
|
|
} else if (_.includes(context, 'page') && data.post) {
|
2019-11-21 16:08:00 +03:00
|
|
|
// 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')
|
|
|
|
|| '';
|
2019-11-11 12:40:53 +03:00
|
|
|
} else {
|
|
|
|
description = data.post.meta_description || '';
|
|
|
|
}
|
2019-03-12 03:16:02 +03:00
|
|
|
} else if (_.includes(context, 'page') && data.page) {
|
2019-11-21 16:08:00 +03:00
|
|
|
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')
|
|
|
|
|| '';
|
2019-03-12 03:16:02 +03:00
|
|
|
} else {
|
|
|
|
description = data.page.meta_description || '';
|
|
|
|
}
|
2016-01-17 13:07:52 +03:00
|
|
|
}
|
|
|
|
|
2019-11-21 16:08:00 +03:00
|
|
|
return (description || '').trim() || null;
|
2016-01-17 13:07:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = getDescription;
|