// # Ghost Head Helper
// Usage: `{{ghost_head}}`
//
// Outputs scripts and other assets at the top of a Ghost theme
//
// We use the name ghost_head to match the helper for consistency:
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
var getMetaData = require('../data/meta'),
hbs = require('express-hbs'),
escapeExpression = hbs.handlebars.Utils.escapeExpression,
SafeString = hbs.handlebars.SafeString,
_ = require('lodash'),
filters = require('../filters'),
assetHelper = require('./asset'),
config = require('../config'),
Promise = require('bluebird'),
labs = require('../utils/labs'),
api = require('../api');
function getClient() {
if (labs.isSet('publicAPI') === true) {
return api.clients.read({slug: 'ghost-frontend'}).then(function (client) {
client = client.clients[0];
if (client.status === 'enabled') {
return {
id: client.slug,
secret: client.secret
};
}
return {};
});
}
return Promise.resolve({});
}
function writeMetaTag(property, content, type) {
type = type || property.substring(0, 7) === 'twitter' ? 'name' : 'property';
return '';
}
function finaliseStructuredData(metaData) {
var head = [];
_.each(metaData.structuredData, function (content, property) {
if (property === 'article:tag') {
_.each(metaData.keywords, function (keyword) {
if (keyword !== '') {
keyword = escapeExpression(keyword);
head.push(writeMetaTag(property,
escapeExpression(keyword)));
}
});
head.push('');
} else if (content !== null && content !== undefined) {
head.push(writeMetaTag(property,
escapeExpression(content)));
}
});
return head;
}
function getAjaxHelper(clientId, clientSecret) {
return '\n' +
'';
}
function ghost_head(options) {
// if error page do nothing
if (this.statusCode >= 400) {
return;
}
var metaData = getMetaData(this, options.data.root),
head = [],
context = this.context ? this.context[0] : null,
useStructuredData = !config.isPrivacyDisabled('useStructuredData'),
safeVersion = this.safeVersion;
return getClient().then(function (client) {
if (context) {
// head is our main array that holds our meta data
head.push('');
head.push('');
if (metaData.previousUrl) {
head.push('');
}
if (metaData.nextUrl) {
head.push('');
}
if (context !== 'paged' && useStructuredData) {
head.push('');
head.push.apply(head, finaliseStructuredData(metaData));
head.push('');
if (metaData.schema) {
head.push('\n');
}
}
if (client && client.id && client.secret) {
head.push(getAjaxHelper(client.id, client.secret));
}
}
head.push('');
head.push('');
return api.settings.read({key: 'ghost_head'});
}).then(function (response) {
head.push(response.settings[0].value);
return filters.doFilter('ghost_head', head);
}).then(function (head) {
return new SafeString(head.join('\n ').trim());
});
}
module.exports = ghost_head;