2014-10-10 15:54:07 +01:00
|
|
|
// # 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
|
|
|
|
|
2016-01-27 08:58:27 -08:00
|
|
|
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');
|
2014-10-10 15:54:07 +01:00
|
|
|
|
2015-11-04 14:20:05 +00:00
|
|
|
function getClient() {
|
2015-12-03 15:50:52 +00:00
|
|
|
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 {};
|
|
|
|
});
|
|
|
|
}
|
2016-01-27 08:58:27 -08:00
|
|
|
return Promise.resolve({});
|
2015-11-04 14:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function writeMetaTag(property, content, type) {
|
|
|
|
type = type || property.substring(0, 7) === 'twitter' ? 'name' : 'property';
|
|
|
|
return '<meta ' + type + '="' + property + '" content="' + content + '" />';
|
|
|
|
}
|
|
|
|
|
2016-01-27 08:58:27 -08:00
|
|
|
function finaliseStructuredData(metaData) {
|
|
|
|
var head = [];
|
|
|
|
_.each(metaData.structuredData, function (content, property) {
|
2015-02-05 13:06:36 +00:00
|
|
|
if (property === 'article:tag') {
|
2016-01-27 08:58:27 -08:00
|
|
|
_.each(metaData.keywords, function (keyword) {
|
|
|
|
if (keyword !== '') {
|
|
|
|
keyword = escapeExpression(keyword);
|
|
|
|
head.push(writeMetaTag(property,
|
|
|
|
escapeExpression(keyword)));
|
2015-02-05 13:06:36 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
head.push('');
|
|
|
|
} else if (content !== null && content !== undefined) {
|
2016-01-27 08:58:27 -08:00
|
|
|
head.push(writeMetaTag(property,
|
|
|
|
escapeExpression(content)));
|
2015-02-05 13:06:36 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
2015-12-10 08:46:58 -06:00
|
|
|
function getAjaxHelper(clientId, clientSecret) {
|
2015-12-15 10:41:53 +00:00
|
|
|
return '<script type="text/javascript" src="' +
|
|
|
|
assetHelper('shared/ghost-url.js', {hash: {minifyInProduction: true}}) + '"></script>\n' +
|
|
|
|
'<script type="text/javascript">\n' +
|
|
|
|
'ghost.init({\n' +
|
|
|
|
'\tclientId: "' + clientId + '",\n' +
|
|
|
|
'\tclientSecret: "' + clientSecret + '"\n' +
|
|
|
|
'});\n' +
|
|
|
|
'</script>';
|
2015-11-10 22:33:19 -06:00
|
|
|
}
|
|
|
|
|
2016-01-27 08:58:27 -08:00
|
|
|
function ghost_head(options) {
|
2016-01-05 23:31:46 -08:00
|
|
|
// if error page do nothing
|
2016-02-17 14:51:57 +00:00
|
|
|
if (this.statusCode >= 400) {
|
2016-01-05 23:31:46 -08:00
|
|
|
return;
|
|
|
|
}
|
2016-03-03 10:52:27 +02:00
|
|
|
|
2016-01-27 08:58:27 -08:00
|
|
|
var metaData = getMetaData(this, options.data.root),
|
2014-10-10 15:54:07 +01:00
|
|
|
head = [],
|
2016-01-27 08:58:27 -08:00
|
|
|
context = this.context ? this.context[0] : null,
|
|
|
|
useStructuredData = !config.isPrivacyDisabled('useStructuredData'),
|
2016-07-14 18:14:59 +02:00
|
|
|
safeVersion = this.safeVersion,
|
2016-08-22 11:20:56 +02:00
|
|
|
referrerPolicy = config.referrerPolicy ? config.referrerPolicy : 'no-referrer-when-downgrade';
|
2014-10-10 15:54:07 +01:00
|
|
|
|
2016-01-27 08:58:27 -08:00
|
|
|
return getClient().then(function (client) {
|
2015-04-16 16:48:46 +01:00
|
|
|
if (context) {
|
|
|
|
// head is our main array that holds our meta data
|
2016-01-27 08:58:27 -08:00
|
|
|
head.push('<link rel="canonical" href="' +
|
|
|
|
escapeExpression(metaData.canonicalUrl) + '" />');
|
2016-07-14 18:14:59 +02:00
|
|
|
head.push('<meta name="referrer" content="' + referrerPolicy + '" />');
|
2015-04-16 16:48:46 +01:00
|
|
|
|
2016-01-27 08:58:27 -08:00
|
|
|
if (metaData.previousUrl) {
|
|
|
|
head.push('<link rel="prev" href="' +
|
|
|
|
escapeExpression(metaData.previousUrl) + '" />');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (metaData.nextUrl) {
|
|
|
|
head.push('<link rel="next" href="' +
|
|
|
|
escapeExpression(metaData.nextUrl) + '" />');
|
2015-04-16 16:48:46 +01:00
|
|
|
}
|
|
|
|
|
2016-03-30 10:41:41 +01:00
|
|
|
if (context !== 'paged' && useStructuredData) {
|
2015-04-16 16:48:46 +01:00
|
|
|
head.push('');
|
2016-01-27 08:58:27 -08:00
|
|
|
head.push.apply(head, finaliseStructuredData(metaData));
|
2015-04-16 16:48:46 +01:00
|
|
|
head.push('');
|
2016-01-27 08:58:27 -08:00
|
|
|
|
2016-03-20 22:33:46 +00:00
|
|
|
if (metaData.schema) {
|
|
|
|
head.push('<script type="application/ld+json">\n' +
|
|
|
|
JSON.stringify(metaData.schema, null, ' ') +
|
|
|
|
'\n </script>\n');
|
|
|
|
}
|
2015-04-16 16:48:46 +01:00
|
|
|
}
|
2015-11-04 14:20:05 +00:00
|
|
|
|
2016-01-27 08:58:27 -08:00
|
|
|
if (client && client.id && client.secret) {
|
|
|
|
head.push(getAjaxHelper(client.id, client.secret));
|
2015-11-04 14:20:05 +00:00
|
|
|
}
|
2014-10-10 15:54:07 +01:00
|
|
|
}
|
2015-11-04 14:20:05 +00:00
|
|
|
|
2016-01-27 08:58:27 -08:00
|
|
|
head.push('<meta name="generator" content="Ghost ' +
|
|
|
|
escapeExpression(safeVersion) + '" />');
|
2014-10-10 15:54:07 +01:00
|
|
|
head.push('<link rel="alternate" type="application/rss+xml" title="' +
|
2016-01-27 08:58:27 -08:00
|
|
|
escapeExpression(metaData.blog.title) + '" href="' +
|
|
|
|
escapeExpression(metaData.rssUrl) + '" />');
|
|
|
|
|
2014-07-31 21:36:20 +02:00
|
|
|
return api.settings.read({key: 'ghost_head'});
|
|
|
|
}).then(function (response) {
|
|
|
|
head.push(response.settings[0].value);
|
2014-10-10 15:54:07 +01:00
|
|
|
return filters.doFilter('ghost_head', head);
|
|
|
|
}).then(function (head) {
|
2016-01-27 08:58:27 -08:00
|
|
|
return new SafeString(head.join('\n ').trim());
|
2014-10-10 15:54:07 +01:00
|
|
|
});
|
2016-01-27 08:58:27 -08:00
|
|
|
}
|
2014-10-10 15:54:07 +01:00
|
|
|
|
|
|
|
module.exports = ghost_head;
|