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