Ghost/core/frontend/meta/title.js
Hannah Wolfe bd597db829
Moved settings/cache to shared/settings-cache
- This is part of the quest to separate the frontend and server & get rid of all the places where there are cross-requires
- At the moment the settings cache is one big shared cache used by the frontend and server liberally
- This change doesn't really solve the fundamental problems, as we still depend on events, and requires from inside frontend
- However it allows us to control the misuse slightly better by getting rid of restricted requires and turning on that eslint ruleset
2021-06-30 15:49:10 +01:00

59 lines
2.4 KiB
JavaScript

const _ = require('lodash');
const settingsCache = require('../../shared/settings-cache');
function getTitle(data, root, options = {}) {
const context = root ? root.context : null;
const siteTitle = settingsCache.get('title');
const pagination = root ? root.pagination : null;
// options.property = null/'og'/'twitter'
const optionsPropertyName = `${options.property || 'meta'}_title`;
let title = '';
let pageString = '';
if (pagination && pagination.total > 1) {
pageString = _.has(options.hash, 'page') ? options.hash.page.replace('%', pagination.page) : ' (Page ' + pagination.page + ')';
}
// If there's a specific meta title
if (data.meta_title) {
title = data.meta_title;
// Home title
} else if (_.includes(context, 'home')) {
if (options.property) {
title = settingsCache.get(optionsPropertyName) || siteTitle;
} else {
title = settingsCache.get('meta_title') || siteTitle;
}
// Author title, paged
} else if (_.includes(context, 'author') && data.author && _.includes(context, 'paged')) {
title = data.author.name + ' - ' + siteTitle + pageString;
// Author title, index
} else if (_.includes(context, 'author') && data.author) {
title = data.author.name + ' - ' + siteTitle;
// Tag title, paged
} else if (_.includes(context, 'tag') && data.tag && _.includes(context, 'paged')) {
title = data.tag.meta_title || data.tag.name + ' - ' + siteTitle + pageString;
// Tag title, index
} else if (_.includes(context, 'tag') && data.tag) {
title = data.tag[optionsPropertyName] || data.tag.meta_title || data.tag.name + ' - ' + siteTitle;
// Post title
} else if (_.includes(context, 'post') && data.post) {
title = data.post[optionsPropertyName] || data.post.meta_title || data.post.title;
// Page title dependent on legacy object formatting (https://github.com/TryGhost/Ghost/issues/10042)
} else if (_.includes(context, 'page') && data.post) {
title = data.post[optionsPropertyName] || data.post.meta_title || data.post.title;
// Page title v2
} else if (_.includes(context, 'page') && data.page) {
title = data.page[optionsPropertyName] || data.page.meta_title || data.page.title;
// Fallback
} else {
title = siteTitle + pageString;
}
return (title || '').trim();
}
module.exports = getTitle;