2016-01-17 13:07:52 +03:00
|
|
|
var _ = require('lodash'),
|
2017-12-14 05:01:23 +03:00
|
|
|
settingsCache = require('../../services/settings/cache');
|
2016-01-17 13:07:52 +03:00
|
|
|
|
2017-08-03 14:48:39 +03:00
|
|
|
function getTitle(data, root, options) {
|
2016-01-17 13:07:52 +03:00
|
|
|
var title = '',
|
|
|
|
context = root ? root.context : null,
|
2017-08-03 14:48:39 +03:00
|
|
|
postSdTitle,
|
2017-02-03 16:15:11 +03:00
|
|
|
blogTitle = settingsCache.get('title'),
|
2016-01-17 13:07:52 +03:00
|
|
|
pagination = root ? root.pagination : null,
|
|
|
|
pageString = '';
|
|
|
|
|
2017-08-03 14:48:39 +03:00
|
|
|
options = options ? options : {};
|
|
|
|
|
2016-01-17 13:07:52 +03:00
|
|
|
if (pagination && pagination.total > 1) {
|
2018-03-19 11:48:07 +03:00
|
|
|
pageString = _.has(options.hash, 'page') ? options.hash.page.replace('%', pagination.page) : ' (Page ' + pagination.page + ')';
|
2016-01-17 13:07:52 +03:00
|
|
|
}
|
2017-02-03 16:15:11 +03:00
|
|
|
|
2017-07-31 12:13:06 +03:00
|
|
|
// If there's a specific meta title
|
2016-01-17 13:07:52 +03:00
|
|
|
if (data.meta_title) {
|
|
|
|
title = data.meta_title;
|
2017-07-31 12:13:06 +03:00
|
|
|
// Home title
|
2016-06-11 21:23:27 +03:00
|
|
|
} else if (_.includes(context, 'home')) {
|
2017-02-03 16:15:11 +03:00
|
|
|
title = blogTitle;
|
2017-07-31 12:13:06 +03:00
|
|
|
// Author title, paged
|
|
|
|
} else if (_.includes(context, 'author') && data.author && _.includes(context, 'paged')) {
|
|
|
|
title = data.author.name + ' - ' + blogTitle + pageString;
|
|
|
|
// Author title, index
|
2016-06-11 21:23:27 +03:00
|
|
|
} else if (_.includes(context, 'author') && data.author) {
|
2017-07-31 12:13:06 +03:00
|
|
|
title = data.author.name + ' - ' + blogTitle;
|
|
|
|
// Tag title, paged
|
|
|
|
} else if (_.includes(context, 'tag') && data.tag && _.includes(context, 'paged')) {
|
|
|
|
title = data.tag.meta_title || data.tag.name + ' - ' + blogTitle + pageString;
|
|
|
|
// Tag title, index
|
2016-06-11 21:23:27 +03:00
|
|
|
} else if (_.includes(context, 'tag') && data.tag) {
|
2017-07-31 12:13:06 +03:00
|
|
|
title = data.tag.meta_title || data.tag.name + ' - ' + blogTitle;
|
|
|
|
// Post title
|
2016-06-11 21:23:27 +03:00
|
|
|
} else if ((_.includes(context, 'post') || _.includes(context, 'page')) && data.post) {
|
2017-08-03 14:48:39 +03:00
|
|
|
if (options && options.property) {
|
|
|
|
postSdTitle = options.property + '_title';
|
|
|
|
title = data.post[postSdTitle] || '';
|
|
|
|
} else {
|
|
|
|
title = data.post.meta_title || data.post.title;
|
|
|
|
}
|
2017-07-31 12:13:06 +03:00
|
|
|
// Fallback
|
2016-01-17 13:07:52 +03:00
|
|
|
} else {
|
2017-02-03 16:15:11 +03:00
|
|
|
title = blogTitle + pageString;
|
2016-01-17 13:07:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return (title || '').trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = getTitle;
|