0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-04-08 02:52:39 -05:00

Added fallbacks when the site title is undefined

refs https://github.com/TryGhost/Team/issues/1180

- An undefined site title was causing the `null` string to show in the html title tag on tag and author pages
This commit is contained in:
Thibaut Patel 2021-10-27 12:18:24 +02:00
parent 297e173544
commit 6e0bd7e7b5
2 changed files with 67 additions and 5 deletions

View file

@ -1,9 +1,16 @@
const _ = require('lodash');
const settingsCache = require('../../shared/settings-cache');
function optionalString(test, string) {
if (test) {
return string;
}
return '';
}
function getTitle(data, root, options = {}) {
const context = root ? root.context : null;
const siteTitle = settingsCache.get('title');
const siteTitle = settingsCache.get('title') || '';
const pagination = root ? root.pagination : null;
// options.property = null/'og'/'twitter'
@ -16,6 +23,9 @@ function getTitle(data, root, options = {}) {
pageString = _.has(options.hash, 'page') ? options.hash.page.replace('%', pagination.page) : ' (Page ' + pagination.page + ')';
}
const dashSiteTitle = optionalString(siteTitle, ' - ' + siteTitle);
const dashSiteTitlePage = optionalString(siteTitle || pageString, ' -' + optionalString(siteTitle, ' ' + siteTitle) + pageString);
// If there's a specific meta title
if (data.meta_title) {
title = data.meta_title;
@ -28,16 +38,16 @@ function getTitle(data, root, options = {}) {
}
// Author title, paged
} else if (_.includes(context, 'author') && data.author && _.includes(context, 'paged')) {
title = data.author.name + ' - ' + siteTitle + pageString;
title = data.author.name + dashSiteTitlePage;
// Author title, index
} else if (_.includes(context, 'author') && data.author) {
title = data.author.name + ' - ' + siteTitle;
title = data.author.name + dashSiteTitle;
// Tag title, paged
} else if (_.includes(context, 'tag') && data.tag && _.includes(context, 'paged')) {
title = data.tag.meta_title || data.tag.name + ' - ' + siteTitle + pageString;
title = data.tag.meta_title || data.tag.name + dashSiteTitlePage;
// Tag title, index
} else if (_.includes(context, 'tag') && data.tag) {
title = data.tag[optionsPropertyName] || data.tag.meta_title || data.tag.name + ' - ' + siteTitle;
title = data.tag[optionsPropertyName] || data.tag.meta_title || data.tag.name + dashSiteTitle;
// Post title
} else if (_.includes(context, 'post') && data.post) {
title = data.post[optionsPropertyName] || data.post.meta_title || data.post.title;

View file

@ -449,4 +449,56 @@ describe('getTitle', function () {
title.should.equal('My site title 4 (Page 35)');
});
it('should not display "null" for an undefined site title', function () {
localSettingsCache.title = null;
var title = getTitle({
tag: {
name: 'My tag'
}
}, {
context: ['tag']
});
title.should.equal('My tag');
title = getTitle({
tag: {
name: 'My tag'
}
}, {
context: ['tag', 'paged'],
pagination: {
total: 40,
page: 35
}
});
title.should.equal('My tag - (Page 35)');
title = getTitle({
author: {
name: 'My name'
}
}, {
context: ['author']
});
title.should.equal('My name');
title = getTitle({
author: {
name: 'My name'
}
}, {
context: ['author', 'paged'],
pagination: {
total: 40,
page: 35
}
});
title.should.equal('My name - (Page 35)');
});
});