2016-01-17 13:07:52 +03:00
|
|
|
var config = require('../../config'),
|
|
|
|
hbs = require('express-hbs'),
|
2016-05-17 17:06:09 +03:00
|
|
|
socialUrls = require('../../utils/social-urls'),
|
2016-01-17 13:07:52 +03:00
|
|
|
escapeExpression = hbs.handlebars.Utils.escapeExpression,
|
|
|
|
_ = require('lodash');
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2016-05-13 14:43:51 +03:00
|
|
|
function trimSameAs(data, context) {
|
|
|
|
var sameAs = [];
|
|
|
|
|
|
|
|
if (context === 'post') {
|
|
|
|
if (data.post.author.website) {
|
|
|
|
sameAs.push(data.post.author.website);
|
|
|
|
}
|
|
|
|
if (data.post.author.facebook) {
|
2016-05-17 17:06:09 +03:00
|
|
|
sameAs.push(socialUrls.facebookUrl(data.post.author.facebook));
|
2016-05-13 14:43:51 +03:00
|
|
|
}
|
|
|
|
if (data.post.author.twitter) {
|
2016-05-17 17:06:09 +03:00
|
|
|
sameAs.push(socialUrls.twitterUrl(data.post.author.twitter));
|
2016-05-13 14:43:51 +03:00
|
|
|
}
|
|
|
|
} else if (context === 'author') {
|
|
|
|
if (data.author.website) {
|
|
|
|
sameAs.push(data.author.website);
|
|
|
|
}
|
|
|
|
if (data.author.facebook) {
|
2016-05-17 17:06:09 +03:00
|
|
|
sameAs.push(socialUrls.facebookUrl(data.author.facebook));
|
2016-05-13 14:43:51 +03:00
|
|
|
}
|
|
|
|
if (data.author.twitter) {
|
2016-05-17 17:06:09 +03:00
|
|
|
sameAs.push(socialUrls.twitterUrl(data.author.twitter));
|
2016-05-13 14:43:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sameAs;
|
|
|
|
}
|
|
|
|
|
2016-01-17 13:07:52 +03:00
|
|
|
function getPostSchema(metaData, data) {
|
2016-02-18 14:47:08 +03:00
|
|
|
var description = metaData.metaDescription ? escapeExpression(metaData.metaDescription) :
|
|
|
|
(metaData.excerpt ? escapeExpression(metaData.excerpt) : null),
|
|
|
|
schema;
|
|
|
|
|
|
|
|
schema = {
|
2016-05-17 21:19:32 +03:00
|
|
|
'@context': 'https://schema.org',
|
2016-01-17 13:07:52 +03:00
|
|
|
'@type': 'Article',
|
2016-05-13 15:09:32 +03:00
|
|
|
publisher: {
|
|
|
|
'@type': 'Organization',
|
|
|
|
name: escapeExpression(metaData.blog.title),
|
|
|
|
logo: metaData.blog.logo || null
|
|
|
|
},
|
2016-01-17 13:07:52 +03:00
|
|
|
author: {
|
|
|
|
'@type': 'Person',
|
|
|
|
name: escapeExpression(data.post.author.name),
|
|
|
|
image: metaData.authorImage,
|
|
|
|
url: metaData.authorUrl,
|
2016-05-13 14:43:51 +03:00
|
|
|
sameAs: trimSameAs(data, 'post'),
|
2016-01-17 13:07:52 +03:00
|
|
|
description: data.post.author.bio ?
|
|
|
|
escapeExpression(data.post.author.bio) :
|
|
|
|
null
|
|
|
|
},
|
|
|
|
headline: escapeExpression(metaData.metaTitle),
|
|
|
|
url: metaData.url,
|
|
|
|
datePublished: metaData.publishedDate,
|
|
|
|
dateModified: metaData.modifiedDate,
|
|
|
|
image: metaData.coverImage,
|
|
|
|
keywords: metaData.keywords && metaData.keywords.length > 0 ?
|
|
|
|
metaData.keywords.join(', ') : null,
|
2016-02-18 14:47:08 +03:00
|
|
|
description: description
|
2016-01-17 13:07:52 +03:00
|
|
|
};
|
|
|
|
schema.author = trimSchema(schema.author);
|
|
|
|
return trimSchema(schema);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getHomeSchema(metaData) {
|
|
|
|
var schema = {
|
2016-05-17 21:19:32 +03:00
|
|
|
'@context': 'https://schema.org',
|
2016-01-17 13:07:52 +03:00
|
|
|
'@type': 'Website',
|
|
|
|
publisher: escapeExpression(metaData.blog.title),
|
|
|
|
url: metaData.url,
|
|
|
|
image: metaData.coverImage,
|
|
|
|
description: metaData.metaDescription ?
|
|
|
|
escapeExpression(metaData.metaDescription) :
|
|
|
|
null
|
|
|
|
};
|
|
|
|
return trimSchema(schema);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTagSchema(metaData, data) {
|
|
|
|
var schema = {
|
2016-05-17 21:19:32 +03:00
|
|
|
'@context': 'https://schema.org',
|
2016-01-17 13:07:52 +03:00
|
|
|
'@type': 'Series',
|
|
|
|
publisher: escapeExpression(metaData.blog.title),
|
|
|
|
url: metaData.url,
|
|
|
|
image: metaData.coverImage,
|
|
|
|
name: data.tag.name,
|
|
|
|
description: metaData.metaDescription ?
|
|
|
|
escapeExpression(metaData.metaDescription) :
|
|
|
|
null
|
|
|
|
};
|
|
|
|
|
|
|
|
return trimSchema(schema);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAuthorSchema(metaData, data) {
|
|
|
|
var schema = {
|
2016-05-17 21:19:32 +03:00
|
|
|
'@context': 'https://schema.org',
|
2016-01-17 13:07:52 +03:00
|
|
|
'@type': 'Person',
|
2016-05-13 14:43:51 +03:00
|
|
|
sameAs: trimSameAs(data, 'author'),
|
2016-01-17 13:07:52 +03:00
|
|
|
name: escapeExpression(data.author.name),
|
|
|
|
url: metaData.authorUrl,
|
|
|
|
image: metaData.coverImage,
|
|
|
|
description: metaData.metaDescription ?
|
|
|
|
escapeExpression(metaData.metaDescription) :
|
|
|
|
null
|
|
|
|
};
|
|
|
|
|
|
|
|
return trimSchema(schema);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSchema(metaData, data) {
|
|
|
|
if (!config.isPrivacyDisabled('useStructuredData')) {
|
|
|
|
var context = data.context ? data.context[0] : null;
|
2016-03-30 12:41:41 +03:00
|
|
|
if (context === 'post' || context === 'page') {
|
2016-01-17 13:07:52 +03:00
|
|
|
return getPostSchema(metaData, data);
|
|
|
|
} else if (context === 'home') {
|
|
|
|
return getHomeSchema(metaData);
|
|
|
|
} else if (context === 'tag') {
|
|
|
|
return getTagSchema(metaData, data);
|
|
|
|
} else if (context === 'author') {
|
|
|
|
return getAuthorSchema(metaData, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = getSchema;
|