Ghost/core/server/data/meta/schema.js
JT Turner 1f4c01d207 Started moving meta data fetching to functions.
issue #6186
- Moved asset helper logic to a asset url function.
- Created author image function to be used in ghost_head helper.
- Created author url function to be used in the ghost_head helper.
- Created canonical url function to be used in the ghost_head helper.
- Moved meta_description helper logic to a function.
- Moved excerpt helper logic to a function.
- Created an index in data/meta to be used in ghost_head helper to get all data.
- Created keyword function to be used in the ghost_head helper.
- Created modified data function to be used in the ghost_head helper.
- Created next url function to be used in the ghost_head helper.
- Created ogType function to be used in the ghost_head helper.
- Created previous url function to be used in the ghost_head helper.
- Created published data function to be used in the ghost_head helper.
- Created rss url function to be used in the ghost_head helper.
- Created schema function to be used in the ghost_head helper.
- Created structured data function to be used in the ghost_head helper.
- Moved meta_title helper logic to a title function.
- Moved url helper logic to a url function.
- Wrote tests for all the new functions

This is just the first step. I plan on refactoring the ghost head to use these new functions.
2016-01-23 13:58:21 -08:00

112 lines
3.4 KiB
JavaScript

var config = require('../../config'),
hbs = require('express-hbs'),
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;
}
function getPostSchema(metaData, data) {
var schema = {
'@context': 'http://schema.org',
'@type': 'Article',
publisher: metaData.blog.title,
author: {
'@type': 'Person',
name: escapeExpression(data.post.author.name),
image: metaData.authorImage,
url: metaData.authorUrl,
sameAs: data.post.author.website || null,
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,
description: metaData.metaDescription ?
escapeExpression(metaData.metaDescription) :
null
};
schema.author = trimSchema(schema.author);
return trimSchema(schema);
}
function getHomeSchema(metaData) {
var schema = {
'@context': 'http://schema.org',
'@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 = {
'@context': 'http://schema.org',
'@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 = {
'@context': 'http://schema.org',
'@type': 'Person',
sameAs: data.author.website || null,
publisher: escapeExpression(metaData.blog.title),
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;
if (context === 'post') {
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;