Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated meta/structured data sources & fallbacks (#11068)
refs #10921, closes #11357, closes #11403 - updates the sources and fallbacks for the output of `{{ghost_head}}` meta/structured data - re-works related tests to better show the fallback chains for different scenarios - fixes `{{ghost_head}}` tests to use `before/afterEach` so that tests are not interdependent
- Loading branch information
1 parent
193c179
commit be4a5a8
Showing
15 changed files
with
1,383 additions
and
668 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,83 @@ | ||
const _ = require('lodash'); | ||
const settingsCache = require('../../server/services/settings/cache'); | ||
const getExcerpt = require('./excerpt'); | ||
|
||
function getDescription(data, root, options) { | ||
function getDescription(data, root, options = {}) { | ||
const context = root ? root.context : null; | ||
const siteDescription = settingsCache.get('meta_description') || settingsCache.get('description'); | ||
|
||
let description = ''; | ||
let postSdDescription; | ||
|
||
options = options ? options : {}; | ||
|
||
// We only return meta_description if provided | ||
if (data.meta_description) { | ||
description = data.meta_description; | ||
} else if (_.includes(context, 'paged')) { | ||
description = ''; | ||
} else if (_.includes(context, 'home')) { | ||
if (options && options.property) { | ||
const siteSdDescription = options.property + '_description'; | ||
description = settingsCache.get(siteSdDescription) || ''; | ||
const siteDescription = settingsCache.get('meta_description') || settingsCache.get('description'); | ||
|
||
if (options.property) { | ||
// options.property = null/'og'/'twitter' | ||
const optionsPropertyName = `${options.property || 'meta'}_description`; | ||
description = settingsCache.get(optionsPropertyName) || siteDescription || ''; | ||
} else { | ||
description = siteDescription; | ||
} | ||
} else if (_.includes(context, 'author') && data.author) { | ||
// The usage of meta data fields for author is currently not implemented. | ||
// We do have meta_description and meta_title fields | ||
// in the users table, but there's no UI to populate those. | ||
description = data.author.meta_description || ''; | ||
if (!options.property && _.includes(context, 'paged')) { | ||
description = ''; | ||
} else { | ||
// The usage of meta data fields for author is currently not implemented. | ||
// We do have meta_description and meta_title fields | ||
// in the users table, but there's no UI to populate those. | ||
description = data.author.meta_description | ||
|| data.author.bio | ||
|| (options.property ? settingsCache.get('meta_description') : '') | ||
|| ''; | ||
} | ||
} else if (_.includes(context, 'tag') && data.tag) { | ||
description = data.tag.meta_description || ''; | ||
if (!options.property && _.includes(context, 'paged')) { | ||
description = ''; | ||
} else { | ||
description = data.tag.meta_description | ||
|| data.tag.description | ||
|| (options.property ? settingsCache.get('meta_description') : '') | ||
|| ''; | ||
} | ||
} else if (_.includes(context, 'post') && data.post) { | ||
if (options && options.property) { | ||
postSdDescription = options.property + '_description'; | ||
description = data.post[postSdDescription] || ''; | ||
if (options.property) { | ||
description = data.post[`${options.property}_description`] | ||
|| data.post.custom_excerpt | ||
|| data.post.meta_description | ||
|| getExcerpt(data.post.html || '', {words: 50}) | ||
|| settingsCache.get('description') | ||
|| ''; | ||
} else { | ||
description = data.post.meta_description || ''; | ||
} | ||
} else if (_.includes(context, 'page') && data.post) { | ||
// Page title dependent on legacy object formatting (https://github.com/TryGhost/Ghost/issues/10042) | ||
if (options && options.property) { | ||
postSdDescription = options.property + '_description'; | ||
description = data.post[postSdDescription] || ''; | ||
// Page description dependent on legacy object formatting (https://github.com/TryGhost/Ghost/issues/10042) | ||
if (options.property) { | ||
description = data.post[`${options.property}_description`] | ||
|| data.post.custom_excerpt | ||
|| data.post.meta_description | ||
|| getExcerpt(data.post.html || '', {words: 50}) | ||
|| settingsCache.get('description') | ||
|| ''; | ||
} else { | ||
description = data.post.meta_description || ''; | ||
} | ||
} else if (_.includes(context, 'page') && data.page) { | ||
if (options && options.property) { | ||
postSdDescription = options.property + '_description'; | ||
description = data.page[postSdDescription] || ''; | ||
if (options.property) { | ||
description = data.page[`${options.property}_description`] | ||
|| data.page.custom_excerpt | ||
|| data.page.meta_description | ||
|| getExcerpt(data.page.html || '', {words: 50}) | ||
|| settingsCache.get('description') | ||
|| ''; | ||
} else { | ||
description = data.page.meta_description || ''; | ||
} | ||
} | ||
|
||
return (description || '').trim(); | ||
return (description || '').trim() || null; | ||
} | ||
|
||
module.exports = getDescription; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.