2016-01-17 13:07:52 +03:00
|
|
|
var _ = require('lodash'),
|
2019-06-19 12:30:28 +03:00
|
|
|
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) {
|
2016-01-17 13:07:52 +03:00
|
|
|
var description = '',
|
2017-08-03 14:48:39 +03:00
|
|
|
postSdDescription,
|
2017-02-03 16:15:11 +03:00
|
|
|
context = root ? root.context : null,
|
|
|
|
blogDescription = settingsCache.get('description');
|
2016-01-17 13:07:52 +03:00
|
|
|
|
2017-08-03 14:48:39 +03:00
|
|
|
options = options ? options : {};
|
|
|
|
|
2017-08-01 11:39:34 +03:00
|
|
|
// We only return meta_description if provided. Only exception is the Blog
|
|
|
|
// description, which doesn't rely on meta_description.
|
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')) {
|
2017-02-03 16:15:11 +03:00
|
|
|
description = blogDescription;
|
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;
|