mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-22 18:31:57 +03:00
40d0a745df
no issue This PR adds the server side logic for multiple authors. This adds the ability to add multiple authors per post. We keep and support single authors (maybe till the next major - this is still in discussion) ### key notes - `authors` are not fetched by default, only if we need them - the migration script iterates over all posts and figures out if an author_id is valid and exists (in master we can add invalid author_id's) and then adds the relation (falls back to owner if invalid) - ~~i had to push a fork of bookshelf to npm because we currently can't bump bookshelf + the two bugs i discovered are anyway not yet merged (https://github.com/kirrg001/bookshelf/commits/master)~~ replaced by new bookshelf release - the implementation of single & multiple authors lives in a single place (introduction of a new concept: model relation) - if you destroy an author, we keep the behaviour for now -> remove all posts where the primary author id matches. furthermore, remove all relations in posts_authors (e.g. secondary author) - we make re-use of the `excludeAttrs` concept which was invented in the contributors PR (to protect editing authors as author/contributor role) -> i've added a clear todo that we need a logic to make a diff of the target relation -> both for tags and authors - `authors` helper available (same as `tags` helper) - `primary_author` computed field available - `primary_author` functionality available (same as `primary_tag` e.g. permalinks, prev/next helper etc)
190 lines
5.9 KiB
JavaScript
190 lines
5.9 KiB
JavaScript
var config = require('../../config'),
|
|
escapeExpression = require('../../services/themes/engine').escapeExpression,
|
|
social = require('../../lib/social'),
|
|
_ = require('lodash');
|
|
|
|
function schemaImageObject(metaDataVal) {
|
|
var imageObject;
|
|
if (!metaDataVal) {
|
|
return null;
|
|
}
|
|
if (!metaDataVal.dimensions) {
|
|
return metaDataVal.url;
|
|
}
|
|
|
|
imageObject = {
|
|
'@type': 'ImageObject',
|
|
url: metaDataVal.url,
|
|
width: metaDataVal.dimensions.width,
|
|
height: metaDataVal.dimensions.height
|
|
};
|
|
|
|
return imageObject;
|
|
}
|
|
|
|
// Creates the final schema object with values that are not null
|
|
function trimSchema(schema) {
|
|
var schemaObject = {};
|
|
|
|
_.each(schema, function (value, key) {
|
|
if (value !== null && typeof value !== 'undefined') {
|
|
schemaObject[key] = value;
|
|
}
|
|
});
|
|
|
|
return schemaObject;
|
|
}
|
|
|
|
function trimSameAs(data, context) {
|
|
var sameAs = [];
|
|
|
|
if (context === 'post') {
|
|
if (data.post.primary_author.website) {
|
|
sameAs.push(escapeExpression(data.post.primary_author.website));
|
|
}
|
|
if (data.post.primary_author.facebook) {
|
|
sameAs.push(social.urls.facebook(data.post.primary_author.facebook));
|
|
}
|
|
if (data.post.primary_author.twitter) {
|
|
sameAs.push(social.urls.twitter(data.post.primary_author.twitter));
|
|
}
|
|
} else if (context === 'author') {
|
|
if (data.author.website) {
|
|
sameAs.push(escapeExpression(data.author.website));
|
|
}
|
|
if (data.author.facebook) {
|
|
sameAs.push(social.urls.facebook(data.author.facebook));
|
|
}
|
|
if (data.author.twitter) {
|
|
sameAs.push(social.urls.twitter(data.author.twitter));
|
|
}
|
|
}
|
|
|
|
return sameAs;
|
|
}
|
|
|
|
function getPostSchema(metaData, data) {
|
|
// CASE: metaData.excerpt for post context is populated by either the custom excerpt, the meta description,
|
|
// or the automated excerpt of 50 words. It is empty for any other context.
|
|
var description = metaData.excerpt ? escapeExpression(metaData.excerpt) : null,
|
|
schema;
|
|
|
|
schema = {
|
|
'@context': 'https://schema.org',
|
|
'@type': 'Article',
|
|
publisher: {
|
|
'@type': 'Organization',
|
|
name: escapeExpression(metaData.blog.title),
|
|
logo: schemaImageObject(metaData.blog.logo) || null
|
|
},
|
|
author: {
|
|
'@type': 'Person',
|
|
name: escapeExpression(data.post.primary_author.name),
|
|
image: schemaImageObject(metaData.authorImage),
|
|
url: metaData.authorUrl,
|
|
sameAs: trimSameAs(data, 'post'),
|
|
description: data.post.primary_author.metaDescription ?
|
|
escapeExpression(data.post.primary_author.metaDescription) :
|
|
null
|
|
},
|
|
headline: escapeExpression(metaData.metaTitle),
|
|
url: metaData.url,
|
|
datePublished: metaData.publishedDate,
|
|
dateModified: metaData.modifiedDate,
|
|
image: schemaImageObject(metaData.coverImage),
|
|
keywords: metaData.keywords && metaData.keywords.length > 0 ?
|
|
metaData.keywords.join(', ') : null,
|
|
description: description,
|
|
mainEntityOfPage: {
|
|
'@type': 'WebPage',
|
|
'@id': metaData.blog.url || null
|
|
}
|
|
};
|
|
schema.author = trimSchema(schema.author);
|
|
return trimSchema(schema);
|
|
}
|
|
|
|
function getHomeSchema(metaData) {
|
|
var schema = {
|
|
'@context': 'https://schema.org',
|
|
'@type': 'WebSite',
|
|
publisher: {
|
|
'@type': 'Organization',
|
|
name: escapeExpression(metaData.blog.title),
|
|
logo: schemaImageObject(metaData.blog.logo) || null
|
|
},
|
|
url: metaData.url,
|
|
image: schemaImageObject(metaData.coverImage),
|
|
mainEntityOfPage: {
|
|
'@type': 'WebPage',
|
|
'@id': metaData.blog.url || null
|
|
},
|
|
description: metaData.metaDescription ?
|
|
escapeExpression(metaData.metaDescription) :
|
|
null
|
|
};
|
|
return trimSchema(schema);
|
|
}
|
|
|
|
function getTagSchema(metaData, data) {
|
|
var schema = {
|
|
'@context': 'https://schema.org',
|
|
'@type': 'Series',
|
|
publisher: {
|
|
'@type': 'Organization',
|
|
name: escapeExpression(metaData.blog.title),
|
|
logo: schemaImageObject(metaData.blog.logo) || null
|
|
},
|
|
url: metaData.url,
|
|
image: schemaImageObject(metaData.coverImage),
|
|
name: data.tag.name,
|
|
mainEntityOfPage: {
|
|
'@type': 'WebPage',
|
|
'@id': metaData.blog.url || null
|
|
},
|
|
description: metaData.metaDescription ?
|
|
escapeExpression(metaData.metaDescription) :
|
|
null
|
|
};
|
|
|
|
return trimSchema(schema);
|
|
}
|
|
|
|
function getAuthorSchema(metaData, data) {
|
|
var schema = {
|
|
'@context': 'https://schema.org',
|
|
'@type': 'Person',
|
|
sameAs: trimSameAs(data, 'author'),
|
|
name: escapeExpression(data.author.name),
|
|
url: metaData.authorUrl,
|
|
image: schemaImageObject(metaData.coverImage),
|
|
mainEntityOfPage: {
|
|
'@type': 'WebPage',
|
|
'@id': metaData.blog.url || null
|
|
},
|
|
description: metaData.metaDescription ?
|
|
escapeExpression(metaData.metaDescription) :
|
|
null
|
|
};
|
|
|
|
return trimSchema(schema);
|
|
}
|
|
|
|
function getSchema(metaData, data) {
|
|
if (!config.isPrivacyDisabled('useStructuredData')) {
|
|
var context = data.context ? data.context : null;
|
|
if (_.includes(context, 'post') || _.includes(context, 'page') || _.includes(context, 'amp')) {
|
|
return getPostSchema(metaData, data);
|
|
} else if (_.includes(context, 'home')) {
|
|
return getHomeSchema(metaData);
|
|
} else if (_.includes(context, 'tag')) {
|
|
return getTagSchema(metaData, data);
|
|
} else if (_.includes(context, 'author')) {
|
|
return getAuthorSchema(metaData, data);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
module.exports = getSchema;
|