mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-22 18:31:57 +03:00
e19e91044d
refs #8221, closes #7688, refs #7558 🙇 Improve meta data publisher logo behaviour This is a follow-up PR for #8285. Reasons: The code changes of #8285 caused error messages when falling back to the default `favicon.ico`, as the `image-size` tool doesn't support `ico` files. This PR takes the logic to decide which logo needs to be listed in our schema into a new fn `blog_logo.js`. There we have now three decisions: 1. If we have a publication **logo**, we'll take that one 2. If we have no publication logo, but an **icon** we'll use this one. 3. If we have none of the above things, we fall back to our default `favicon.ico` Additional, we're hard coding image dimensions for whenever the logo is an `.ico` file and built and extra decision to not call `image-size` when the dimension are already given. I will create another follow-up PR, which checks the extension type for the file and offers it as a util. 🛠 Blog icon util refs #7688 Serve functionality around the blog icon in its own util: - getIconDimensions -> async function that takes the filepath of on ico file and returns its dimensions - isIcoImageType -> returns true if file has `.ico` extension - getIconType -> returns icon-type (`x-icon` or `png`) - getIconUrl -> returns the absolut or relativ URL for the favicon: `[subdirectory or not]favicon.[ico or png]` 📖 Get .ico sizes for meta data & logo improvement refs #7558 refs #8221 Use the new `blogIconUtil` in meta data to fetch the dimensions of `.ico` files. Improvements for `publisher.logo`: We're now returning a hard-coded 'faked' image dimensions value to render an `imageObject` and prevent error our schema (Google structured data). As soon as an image (`.ico` or non-`.ico`) is too large, but - in case of non-`.ico` - a square format, be set the image-dimensions to 60px width and height. This reduces the chances of getting constantly error messages from Googles' webmaster tools. - add getIconPath util
166 lines
5.8 KiB
JavaScript
166 lines
5.8 KiB
JavaScript
// # 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,
|
|
getAssetUrl = proxy.metaData.getAssetUrl,
|
|
escapeExpression = proxy.escapeExpression,
|
|
SafeString = proxy.SafeString,
|
|
filters = proxy.filters,
|
|
labs = proxy.labs,
|
|
api = proxy.api,
|
|
settingsCache = proxy.settingsCache,
|
|
config = proxy.config,
|
|
blogIconUtils = proxy.blogIcon;
|
|
|
|
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 '<meta ' + type + '="' + property + '" content="' + content + '" />';
|
|
}
|
|
|
|
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 '<script type="text/javascript" src="' +
|
|
getAssetUrl('public/ghost-url.js', true) +
|
|
'"></script>\n' +
|
|
'<script type="text/javascript">\n' +
|
|
'ghost.init({\n' +
|
|
'\tclientId: "' + clientId + '",\n' +
|
|
'\tclientSecret: "' + clientSecret + '"\n' +
|
|
'});\n' +
|
|
'</script>';
|
|
}
|
|
|
|
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()
|
|
},
|
|
favicon = blogIconUtils.getIconUrl(),
|
|
iconType = blogIconUtils.getIconType(favicon);
|
|
|
|
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('<meta name="description" content="' + escapeExpression(metaData.metaDescription) + '" />');
|
|
}
|
|
|
|
head.push('<link rel="shortcut icon" href="' + favicon + '" type="image/' + iconType + '" />');
|
|
head.push('<link rel="canonical" href="' +
|
|
escapeExpression(metaData.canonicalUrl) + '" />');
|
|
head.push('<meta name="referrer" content="' + referrerPolicy + '" />');
|
|
|
|
// 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('<link rel="amphtml" href="' +
|
|
escapeExpression(metaData.ampUrl) + '" />');
|
|
}
|
|
|
|
if (metaData.previousUrl) {
|
|
head.push('<link rel="prev" href="' +
|
|
escapeExpression(metaData.previousUrl) + '" />');
|
|
}
|
|
|
|
if (metaData.nextUrl) {
|
|
head.push('<link rel="next" href="' +
|
|
escapeExpression(metaData.nextUrl) + '" />');
|
|
}
|
|
|
|
if (!_.includes(context, 'paged') && useStructuredData) {
|
|
head.push('');
|
|
head.push.apply(head, finaliseStructuredData(metaData));
|
|
head.push('');
|
|
|
|
if (metaData.schema) {
|
|
head.push('<script type="application/ld+json">\n' +
|
|
JSON.stringify(metaData.schema, null, ' ') +
|
|
'\n </script>\n');
|
|
}
|
|
}
|
|
|
|
if (client && client.id && client.secret && !_.includes(context, 'amp')) {
|
|
head.push(getAjaxHelper(client.id, client.secret));
|
|
}
|
|
}
|
|
|
|
head.push('<meta name="generator" content="Ghost ' +
|
|
escapeExpression(safeVersion) + '" />');
|
|
|
|
head.push('<link rel="alternate" type="application/rss+xml" title="' +
|
|
escapeExpression(metaData.blog.title) + '" href="' +
|
|
escapeExpression(metaData.rssUrl) + '" />');
|
|
|
|
// 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());
|
|
});
|
|
};
|