2019-07-25 12:08:29 +03:00
|
|
|
const _ = require('lodash');
|
2021-06-30 16:56:57 +03:00
|
|
|
const settingsCache = require('../../shared/settings-cache');
|
2016-01-17 13:07:52 +03:00
|
|
|
|
2021-10-27 13:18:24 +03:00
|
|
|
function optionalString(test, string) {
|
|
|
|
if (test) {
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2019-11-21 16:08:00 +03:00
|
|
|
function getTitle(data, root, options = {}) {
|
2019-07-25 12:08:29 +03:00
|
|
|
const context = root ? root.context : null;
|
2021-10-27 13:18:24 +03:00
|
|
|
const siteTitle = settingsCache.get('title') || '';
|
2019-07-25 12:08:29 +03:00
|
|
|
const pagination = root ? root.pagination : null;
|
|
|
|
|
2019-11-21 16:08:00 +03:00
|
|
|
// options.property = null/'og'/'twitter'
|
|
|
|
const optionsPropertyName = `${options.property || 'meta'}_title`;
|
|
|
|
|
2019-07-25 12:08:29 +03:00
|
|
|
let title = '';
|
|
|
|
let pageString = '';
|
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
|
|
|
|
2021-10-27 13:18:24 +03:00
|
|
|
const dashSiteTitle = optionalString(siteTitle, ' - ' + siteTitle);
|
|
|
|
const dashSiteTitlePage = optionalString(siteTitle || pageString, ' -' + optionalString(siteTitle, ' ' + siteTitle) + pageString);
|
|
|
|
|
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-11-21 16:08:00 +03:00
|
|
|
if (options.property) {
|
|
|
|
title = settingsCache.get(optionsPropertyName) || siteTitle;
|
2019-07-25 12:08:29 +03:00
|
|
|
} else {
|
2019-11-21 16:08:00 +03:00
|
|
|
title = settingsCache.get('meta_title') || siteTitle;
|
2019-07-25 12:08:29 +03:00
|
|
|
}
|
2017-07-31 12:13:06 +03:00
|
|
|
// Author title, paged
|
|
|
|
} else if (_.includes(context, 'author') && data.author && _.includes(context, 'paged')) {
|
2021-10-27 13:18:24 +03:00
|
|
|
title = data.author.name + dashSiteTitlePage;
|
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) {
|
2021-10-27 13:18:24 +03:00
|
|
|
title = data.author.name + dashSiteTitle;
|
2017-07-31 12:13:06 +03:00
|
|
|
// Tag title, paged
|
|
|
|
} else if (_.includes(context, 'tag') && data.tag && _.includes(context, 'paged')) {
|
2021-10-27 13:18:24 +03:00
|
|
|
title = data.tag.meta_title || data.tag.name + dashSiteTitlePage;
|
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) {
|
2021-10-27 13:18:24 +03:00
|
|
|
title = data.tag[optionsPropertyName] || data.tag.meta_title || data.tag.name + dashSiteTitle;
|
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) {
|
2019-11-21 16:08:00 +03:00
|
|
|
title = data.post[optionsPropertyName] || data.post.meta_title || data.post.title;
|
2019-11-08 08:58:11 +03:00
|
|
|
// Page title dependent on legacy object formatting (https://github.com/TryGhost/Ghost/issues/10042)
|
|
|
|
} else if (_.includes(context, 'page') && data.post) {
|
2019-11-21 16:08:00 +03:00
|
|
|
title = data.post[optionsPropertyName] || 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) {
|
2019-11-21 16:08:00 +03:00
|
|
|
title = data.page[optionsPropertyName] || 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;
|