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 getTitle(data, root, options) {
|
2019-07-25 12:08:29 +03:00
|
|
|
const context = root ? root.context : null;
|
|
|
|
const siteTitle = settingsCache.get('meta_title') || settingsCache.get('title');
|
|
|
|
const pagination = root ? root.pagination : null;
|
|
|
|
|
|
|
|
let title = '';
|
|
|
|
let postSdTitle;
|
|
|
|
let pageString = '';
|
2016-01-17 13:07:52 +03:00
|
|
|
|
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')) {
|
2019-07-25 12:08:29 +03:00
|
|
|
if (options && options.property) {
|
|
|
|
const siteSdTitle = options.property + '_title';
|
|
|
|
title = settingsCache.get(siteSdTitle) || '';
|
|
|
|
} else {
|
|
|
|
title = siteTitle;
|
|
|
|
}
|
2017-07-31 12:13:06 +03:00
|
|
|
// Author title, paged
|
|
|
|
} else if (_.includes(context, 'author') && data.author && _.includes(context, 'paged')) {
|
2019-07-25 12:08:29 +03:00
|
|
|
title = data.author.name + ' - ' + siteTitle + pageString;
|
2017-07-31 12:13:06 +03:00
|
|
|
// Author title, index
|
2016-06-11 21:23:27 +03:00
|
|
|
} else if (_.includes(context, 'author') && data.author) {
|
2019-07-25 12:08:29 +03:00
|
|
|
title = data.author.name + ' - ' + siteTitle;
|
2017-07-31 12:13:06 +03:00
|
|
|
// Tag title, paged
|
|
|
|
} else if (_.includes(context, 'tag') && data.tag && _.includes(context, 'paged')) {
|
2019-07-25 12:08:29 +03:00
|
|
|
title = data.tag.meta_title || data.tag.name + ' - ' + siteTitle + pageString;
|
2017-07-31 12:13:06 +03:00
|
|
|
// Tag title, index
|
2016-06-11 21:23:27 +03:00
|
|
|
} else if (_.includes(context, 'tag') && data.tag) {
|
2019-07-25 12:08:29 +03:00
|
|
|
title = data.tag.meta_title || data.tag.name + ' - ' + siteTitle;
|
2017-07-31 12:13:06 +03:00
|
|
|
// Post title
|
2019-03-12 03:17:27 +03:00
|
|
|
} else if (_.includes(context, 'post') && 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;
|
|
|
|
}
|
2019-03-12 03:17:27 +03:00
|
|
|
// Page title v2
|
|
|
|
} else if (_.includes(context, 'page') && data.page) {
|
|
|
|
if (options && options.property) {
|
|
|
|
postSdTitle = options.property + '_title';
|
|
|
|
title = data.page[postSdTitle] || '';
|
|
|
|
} else {
|
|
|
|
title = data.page.meta_title || data.page.title;
|
|
|
|
}
|
2017-07-31 12:13:06 +03:00
|
|
|
// Fallback
|
2016-01-17 13:07:52 +03:00
|
|
|
} else {
|
2019-07-25 12:08:29 +03:00
|
|
|
title = siteTitle + pageString;
|
2016-01-17 13:07:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return (title || '').trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = getTitle;
|