// # 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 proxy = require('./proxy'),
_ = require('lodash'),
Promise = require('bluebird'),
getMetaData = proxy.metaData.get,
escapeExpression = proxy.escapeExpression,
SafeString = proxy.SafeString,
filters = proxy.filters,
labs = proxy.labs,
api = proxy.api,
settingsCache = proxy.settingsCache,
config = proxy.config,
url = proxy.url;
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) {
// @TODO: swap this for a direct utility, rather than using the helper? see #8221
// Note: this is here because the asset helper isn't registered when this file is first loaded
var assetHelper = proxy.hbs.handlebars.helpers.asset;
return '\n' +
'';
}
module.exports = function ghost_head(options) {
// if server error page do nothing
if (this.statusCode >= 500) {
return;
}
var metaData,
client,
head = [],
codeInjection = settingsCache.get('ghost_head'),
context = this.context ? this.context : null,
useStructuredData = !config.isPrivacyDisabled('useStructuredData'),
safeVersion = this.safeVersion,
referrerPolicy = config.get('referrerPolicy') ? config.get('referrerPolicy') : 'no-referrer-when-downgrade',
fetch = {
metaData: getMetaData(this, options.data.root),
client: getClient()
},
blogIcon = settingsCache.get('icon'),
// CASE: blog icon is not set in config, we serve the default
iconType = !blogIcon ? 'x-icon' : blogIcon.match(/\/favicon\.ico$/i) ? 'x-icon' : 'png',
favicon = !blogIcon ? '/favicon.ico' : url.urlFor('image', {image: blogIcon});
return Promise.props(fetch).then(function (response) {
client = response.client;
metaData = response.metaData;
if (context) {
// head is our main array that holds our meta data
if (metaData.metaDescription && metaData.metaDescription.length > 0) {
head.push('');
}
head.push('');
head.push('');
head.push('');
// show amp link in post when 1. we are not on the amp page and 2. amp is enabled
if (_.includes(context, 'post') && !_.includes(context, 'amp') && settingsCache.get('amp')) {
head.push('');
}
if (metaData.previousUrl) {
head.push('');
}
if (metaData.nextUrl) {
head.push('');
}
if (!_.includes(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 && !_.includes(context, 'amp')) {
head.push(getAjaxHelper(client.id, client.secret));
}
}
head.push('');
head.push('');
// no code injection for amp context!!!
if (!_.includes(context, 'amp') && !_.isEmpty(codeInjection)) {
head.push(codeInjection);
}
return filters.doFilter('ghost_head', head);
}).then(function (head) {
return new SafeString(head.join('\n ').trim());
});
};