2019-07-25 04:08:29 -05:00
|
|
|
const _ = require('lodash');
|
2021-06-30 08:56:57 -05:00
|
|
|
const settingsCache = require('../../shared/settings-cache');
|
2021-07-02 03:35:07 -05:00
|
|
|
const generateExcerpt = require('./generate-excerpt');
|
2016-01-17 05:07:52 -05:00
|
|
|
|
2019-11-21 08:08:00 -05:00
|
|
|
function getDescription(data, root, options = {}) {
|
2019-07-25 04:08:29 -05:00
|
|
|
const context = root ? root.context : null;
|
|
|
|
|
|
|
|
let description = '';
|
2017-08-03 06:48:39 -05:00
|
|
|
|
2019-07-25 04:08:29 -05:00
|
|
|
// We only return meta_description if provided
|
2016-01-17 05:07:52 -05:00
|
|
|
if (data.meta_description) {
|
|
|
|
description = data.meta_description;
|
2016-06-11 13:23:27 -05:00
|
|
|
} else if (_.includes(context, 'home')) {
|
2019-11-21 08:08:00 -05: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 04:08:29 -05:00
|
|
|
} else {
|
|
|
|
description = siteDescription;
|
|
|
|
}
|
2016-06-11 13:23:27 -05:00
|
|
|
} else if (_.includes(context, 'author') && data.author) {
|
2019-11-21 08:08:00 -05: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 13:23:27 -05:00
|
|
|
} else if (_.includes(context, 'tag') && data.tag) {
|
2019-11-21 08:08:00 -05:00
|
|
|
if (!options.property && _.includes(context, 'paged')) {
|
|
|
|
description = '';
|
|
|
|
} else {
|
2020-07-10 06:52:48 -05:00
|
|
|
description = data.tag[`${options.property}_description`]
|
|
|
|
|| data.tag.meta_description
|
2019-11-21 08:08:00 -05:00
|
|
|
|| data.tag.description
|
|
|
|
|| (options.property ? settingsCache.get('meta_description') : '')
|
|
|
|
|| '';
|
|
|
|
}
|
2019-03-11 19:16:02 -05:00
|
|
|
} else if (_.includes(context, 'post') && data.post) {
|
2019-11-21 08:08:00 -05:00
|
|
|
if (options.property) {
|
|
|
|
description = data.post[`${options.property}_description`]
|
|
|
|
|| data.post.custom_excerpt
|
|
|
|
|| data.post.meta_description
|
2022-05-16 09:43:46 -05:00
|
|
|
|| generateExcerpt(data.post.excerpt || '', {words: 50})
|
2019-11-21 08:08:00 -05:00
|
|
|
|| settingsCache.get('description')
|
|
|
|
|| '';
|
2017-08-03 06:48:39 -05:00
|
|
|
} else {
|
2022-01-03 14:09:03 -05:00
|
|
|
description = data.post.meta_description || data.post.custom_excerpt || '';
|
2017-08-03 06:48:39 -05:00
|
|
|
}
|
2019-11-11 04:40:53 -05:00
|
|
|
} else if (_.includes(context, 'page') && data.post) {
|
2019-11-21 08:08:00 -05: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
|
2022-05-16 09:43:46 -05:00
|
|
|
|| generateExcerpt(data.post.excerpt || '', {words: 50})
|
2019-11-21 08:08:00 -05:00
|
|
|
|| settingsCache.get('description')
|
|
|
|
|| '';
|
2019-11-11 04:40:53 -05:00
|
|
|
} else {
|
2022-01-03 14:09:03 -05:00
|
|
|
description = data.post.meta_description || data.post.custom_excerpt || '';
|
2019-11-11 04:40:53 -05:00
|
|
|
}
|
2019-03-11 19:16:02 -05:00
|
|
|
} else if (_.includes(context, 'page') && data.page) {
|
2019-11-21 08:08:00 -05:00
|
|
|
if (options.property) {
|
|
|
|
description = data.page[`${options.property}_description`]
|
|
|
|
|| data.page.custom_excerpt
|
|
|
|
|| data.page.meta_description
|
2022-05-16 09:43:46 -05:00
|
|
|
|| generateExcerpt(data.page.excerpt || '', {words: 50})
|
2019-11-21 08:08:00 -05:00
|
|
|
|| settingsCache.get('description')
|
|
|
|
|| '';
|
2019-03-11 19:16:02 -05:00
|
|
|
} else {
|
2022-01-03 14:09:03 -05:00
|
|
|
description = data.page.meta_description || data.page.custom_excerpt || '';
|
2019-03-11 19:16:02 -05:00
|
|
|
}
|
2016-01-17 05:07:52 -05:00
|
|
|
}
|
|
|
|
|
2019-11-21 08:08:00 -05:00
|
|
|
return (description || '').trim() || null;
|
2016-01-17 05:07:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = getDescription;
|