2014-10-10 18:54:07 +04:00
|
|
|
// # Ghost Head Helper
|
|
|
|
// Usage: `{{ghost_head}}`
|
|
|
|
//
|
|
|
|
// Outputs scripts and other assets at the top of a Ghost theme
|
2017-04-04 19:07:35 +03:00
|
|
|
var proxy = require('./proxy'),
|
2016-01-27 19:58:27 +03:00
|
|
|
_ = require('lodash'),
|
2017-09-07 13:59:02 +03:00
|
|
|
debug = require('ghost-ignition').debug('ghost_head'),
|
2017-04-04 19:07:35 +03:00
|
|
|
|
|
|
|
getMetaData = proxy.metaData.get,
|
2017-04-10 12:30:21 +03:00
|
|
|
getAssetUrl = proxy.metaData.getAssetUrl,
|
2017-04-04 19:07:35 +03:00
|
|
|
escapeExpression = proxy.escapeExpression,
|
|
|
|
SafeString = proxy.SafeString,
|
2017-09-07 10:29:44 +03:00
|
|
|
logging = proxy.logging,
|
2017-04-04 19:07:35 +03:00
|
|
|
settingsCache = proxy.settingsCache,
|
|
|
|
config = proxy.config,
|
2019-04-23 17:37:35 +03:00
|
|
|
blogIconUtils = proxy.blogIcon,
|
|
|
|
labs = proxy.labs;
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2015-11-04 17:20:05 +03:00
|
|
|
function writeMetaTag(property, content, type) {
|
|
|
|
type = type || property.substring(0, 7) === 'twitter' ? 'name' : 'property';
|
|
|
|
return '<meta ' + type + '="' + property + '" content="' + content + '" />';
|
|
|
|
}
|
|
|
|
|
2016-01-27 19:58:27 +03:00
|
|
|
function finaliseStructuredData(metaData) {
|
|
|
|
var head = [];
|
2017-02-03 16:15:11 +03:00
|
|
|
|
2016-01-27 19:58:27 +03:00
|
|
|
_.each(metaData.structuredData, function (content, property) {
|
2015-02-05 16:06:36 +03:00
|
|
|
if (property === 'article:tag') {
|
2016-01-27 19:58:27 +03:00
|
|
|
_.each(metaData.keywords, function (keyword) {
|
|
|
|
if (keyword !== '') {
|
|
|
|
keyword = escapeExpression(keyword);
|
|
|
|
head.push(writeMetaTag(property,
|
|
|
|
escapeExpression(keyword)));
|
2015-02-05 16:06:36 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
head.push('');
|
|
|
|
} else if (content !== null && content !== undefined) {
|
2016-01-27 19:58:27 +03:00
|
|
|
head.push(writeMetaTag(property,
|
|
|
|
escapeExpression(content)));
|
2015-02-05 16:06:36 +03:00
|
|
|
}
|
|
|
|
});
|
2017-02-03 16:15:11 +03:00
|
|
|
|
2015-02-05 16:06:36 +03:00
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
2015-12-10 17:46:58 +03:00
|
|
|
function getAjaxHelper(clientId, clientSecret) {
|
2018-04-18 15:33:31 +03:00
|
|
|
return '<script src="' +
|
2017-07-28 17:23:32 +03:00
|
|
|
getAssetUrl('public/ghost-sdk.js', true) +
|
2017-04-10 12:30:21 +03:00
|
|
|
'"></script>\n' +
|
2018-04-18 15:33:31 +03:00
|
|
|
'<script>\n' +
|
2015-12-15 13:41:53 +03:00
|
|
|
'ghost.init({\n' +
|
|
|
|
'\tclientId: "' + clientId + '",\n' +
|
|
|
|
'\tclientSecret: "' + clientSecret + '"\n' +
|
|
|
|
'});\n' +
|
|
|
|
'</script>';
|
2015-11-11 07:33:19 +03:00
|
|
|
}
|
|
|
|
|
2019-04-23 17:37:35 +03:00
|
|
|
function getMembersHelper() {
|
|
|
|
return `
|
|
|
|
<script src="${getAssetUrl('public/members-theme-bindings.js')}"></script>
|
|
|
|
<script defer src="${getAssetUrl('public/members.js')}"></script>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2017-10-26 13:03:53 +03:00
|
|
|
/**
|
|
|
|
* **NOTE**
|
|
|
|
* Express adds `_locals`, see https://github.com/expressjs/express/blob/4.15.4/lib/response.js#L962.
|
|
|
|
* But `options.data.root.context` is available next to `root._locals.context`, because
|
|
|
|
* Express creates a `renderOptions` object, see https://github.com/expressjs/express/blob/4.15.4/lib/application.js#L554
|
|
|
|
* and merges all locals to the root of the object. Very confusing, because the data is available in different layers.
|
|
|
|
*
|
|
|
|
* Express forwards the data like this to the hbs engine:
|
|
|
|
* {
|
|
|
|
* post: {}, - res.render('view', databaseResponse)
|
|
|
|
* context: ['post'], - from res.locals
|
|
|
|
* safeVersion: '1.x', - from res.locals
|
|
|
|
* _locals: {
|
|
|
|
* context: ['post'],
|
|
|
|
* safeVersion: '1.x'
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* hbs forwards the data to any hbs helper like this
|
|
|
|
* {
|
|
|
|
* data: {
|
|
|
|
* blog: {},
|
|
|
|
* labs: {},
|
|
|
|
* config: {},
|
|
|
|
* root: {
|
|
|
|
* post: {},
|
|
|
|
* context: ['post'],
|
|
|
|
* locals: {...}
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* `blog`, `labs` and `config` are the templateOptions, search for `hbs.updateTemplateOptions` in the code base.
|
2018-09-17 12:29:47 +03:00
|
|
|
* Also see how the root object gets created, https://github.com/wycats/handlebars.js/blob/v4.0.6/lib/handlebars/runtime.js#L259
|
2017-10-26 13:03:53 +03:00
|
|
|
*/
|
2017-11-01 16:44:54 +03:00
|
|
|
// We use the name ghost_head to match the helper for consistency:
|
|
|
|
module.exports = function ghost_head(options) { // eslint-disable-line camelcase
|
2017-09-07 13:59:02 +03:00
|
|
|
debug('begin');
|
2017-10-13 16:26:42 +03:00
|
|
|
|
2017-03-14 12:06:42 +03:00
|
|
|
// if server error page do nothing
|
2017-10-26 13:03:53 +03:00
|
|
|
if (options.data.root.statusCode >= 500) {
|
2016-01-06 10:31:46 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-03-03 11:52:27 +03:00
|
|
|
|
2017-09-07 13:59:02 +03:00
|
|
|
var head = [],
|
2017-10-26 13:03:53 +03:00
|
|
|
dataRoot = options.data.root,
|
|
|
|
context = dataRoot._locals.context ? dataRoot._locals.context : null,
|
|
|
|
client = dataRoot._locals.client,
|
|
|
|
safeVersion = dataRoot._locals.safeVersion,
|
|
|
|
postCodeInjection = dataRoot && dataRoot.post ? dataRoot.post.codeinjection_head : null,
|
2017-08-02 12:38:19 +03:00
|
|
|
globalCodeinjection = settingsCache.get('ghost_head'),
|
2016-01-27 19:58:27 +03:00
|
|
|
useStructuredData = !config.isPrivacyDisabled('useStructuredData'),
|
2016-09-13 18:41:14 +03:00
|
|
|
referrerPolicy = config.get('referrerPolicy') ? config.get('referrerPolicy') : 'no-referrer-when-downgrade',
|
🙇 Blog icon utils and publisher.logo for JSON-LD (#8297)
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
2017-04-11 19:32:06 +03:00
|
|
|
favicon = blogIconUtils.getIconUrl(),
|
|
|
|
iconType = blogIconUtils.getIconType(favicon);
|
✨ [FEATURE] AMP⚡ (#7229)
closes #6588, #7095
* `ImageObject` with image dimensions (#7152, #7151, #7153)
- Returns meta data as promise
- returns a new Promise from meta data
- uses `Promise.props()` to resolve `getClient()` and `getMetaData()`
- Adds 'image-size' util
The util returns an object like this
```
{
height: 50,
url: 'http://myblog.com/images/cat.jpg',
width: 50
};
```
if the dimensions can be fetched and rejects with error, if not.
In case we get a locally stored image or a not complete url (like `//www.gravatar.com/andsoon`), we add the protocol to the incomplete one and use `urlFor()` to get the absolute URL. If the request fails or `image-size` is not able to read the file, we reject with error.
- adds 'image-size' module to dependencies
- adds `getImageSizeFromUrl` function that returns image dimensions
- In preparation of AMP support and to improve our schema.org JSON-LD and structured data, I made the following changes:
- Changes the following properties to be `Objects`, which have a `url` property by default and a `dimensions` property, if `width` and `height` are available:
- `metaData.coverImage`
- `metaData.authorImage`
- `metaData.blog.logo`
- Checks cache by calling `getCachedImageSizeFromUrl`. If image dimensions were fetched already, returns them from cache instead of fetching them again.
- If we have image dimensions on hand, the output in our JSON-LD changes from normal urls to be full `ImageObjects`. Applies to all images and logos.
- Special case for `publisher.logo` as it has size restrictions: if the image doesn't fulfil the restrictions (<=600 width and <=60 height), we simply output the url instead, so like before.
- Adds new property for schema.org JSON-LD: `mainEntityOfPage` as an Object.
- Adds additional Open Graph data (if we have the image size): `og:image:width` and `og:image:height`
- Adds/updates tests
* AMP router and controller (#7171, #7157)
Implements AMP in `/apps/`:
- renders `amp.hbs` if route is `/:slug/amp/`
- updates `setResponseContext` to set context to `['amp', 'post']` for a amp post and `['amp', 'page']` for a page, but will not render amp template for a page
- updates `context_spec`
- registers 'amp' as new internal app
- adds the `amp.hbs` template to `core/server/apps/amp` which will be the default template for AMP posts.
- adds `isAmpURL` to `post-lookup`
* 🎨 Use `context` in meta as array (#7205)
Instead of reading the first value of the context array, we're checking if it includes certain context values.
This is a preparation change for AMP, where the context will be delivered as `['amp', 'post']`.
* ✨ AMP helpers (#7174, #7216, #7215, #7223)
- Adds AMP helpers `{{amp_content}}`, `{{amp_component}}` and `{{amp_ghost_head}}` to support AMP:
- `{{amp_content}}`:
- Adds `Amperize` as dependency
- AMP app uses new helper `{{amp_content}}` to render AMP HTML
- `Amperize` transforms regular HTML into AMP HTML
- Adds test for `{{amp_content}}` helper
- Adds 'Sanitize-HTML` as dependendy
- After the HTML get 'amperized' we still might have some HTML tags, which are prohibited in AMP HTML, so we use `sanitize-html` to remove those. With every update, `Amperize` gets and it is able to transform more HTML tags, they valid AMP HTML tags (e. g. `video` and `amp-video`) and will therefore not be removed.
- `{{amp_ghost_head}}`:
- registers `{{amp_ghost_head}}` helper, but uses `{{ghost_head}}` code
- uses `{{amp_ghost_head}}` in `amp.hbs` instead of `{{ghost_head}}`
- `{{ghost_head}}`:
- Render `amphtml` link in metadata for post, which links to the amp post (`getAmpUrl`)
- Updates all test in metadata to support `amp` context
- Changes context conditionals to work with full array instead of first array value
- Adds conditionals, so no additional javascript gets rendered in `{{ghost_head}}`
- Removes trailing `/amp/` in URLs, so only `amphtml` link on regular post renders it
- Adds a conditional, so no code injection will be included, for an `amp` context.
- `{{amp_components}}`:
- AMP app uses new helper `{{amp_components}}` to render necessary script tags for AMP extended components as `amp-iframe`, `amp-anime` and `amp-form`
- Adds test for `{{amp_components}}`
2016-08-22 19:49:27 +03:00
|
|
|
|
2017-09-07 13:59:02 +03:00
|
|
|
debug('preparation complete, begin fetch');
|
2017-10-26 13:03:53 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @TODO:
|
|
|
|
* - getMetaData(dataRoot, dataRoot) -> yes that looks confusing!
|
|
|
|
* - there is a very mixed usage of `data.context` vs. `root.context` vs `root._locals.context` vs. `this.context`
|
|
|
|
* - NOTE: getMetaData won't live here anymore soon, see https://github.com/TryGhost/Ghost/issues/8995
|
|
|
|
* - therefor we get rid of using `getMetaData(this, dataRoot)`
|
|
|
|
* - dataRoot has access to *ALL* locals, see function description
|
|
|
|
* - it should not break anything
|
|
|
|
*/
|
|
|
|
return getMetaData(dataRoot, dataRoot)
|
|
|
|
.then(function handleMetaData(metaData) {
|
2017-09-07 13:59:02 +03:00
|
|
|
debug('end fetch');
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2017-09-07 13:59:02 +03:00
|
|
|
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) + '" />');
|
|
|
|
}
|
2017-03-14 19:50:35 +03:00
|
|
|
|
2017-09-07 13:59:02 +03:00
|
|
|
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 + '" />');
|
2015-04-16 18:48:46 +03:00
|
|
|
|
2018-09-17 12:29:47 +03:00
|
|
|
// don't allow indexing of preview URLs!
|
|
|
|
if (_.includes(context, 'preview')) {
|
|
|
|
head.push(writeMetaTag('robots', 'noindex,nofollow', 'name'));
|
|
|
|
}
|
|
|
|
|
2017-09-07 13:59:02 +03:00
|
|
|
// 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) + '" />');
|
|
|
|
}
|
✨ [FEATURE] AMP⚡ (#7229)
closes #6588, #7095
* `ImageObject` with image dimensions (#7152, #7151, #7153)
- Returns meta data as promise
- returns a new Promise from meta data
- uses `Promise.props()` to resolve `getClient()` and `getMetaData()`
- Adds 'image-size' util
The util returns an object like this
```
{
height: 50,
url: 'http://myblog.com/images/cat.jpg',
width: 50
};
```
if the dimensions can be fetched and rejects with error, if not.
In case we get a locally stored image or a not complete url (like `//www.gravatar.com/andsoon`), we add the protocol to the incomplete one and use `urlFor()` to get the absolute URL. If the request fails or `image-size` is not able to read the file, we reject with error.
- adds 'image-size' module to dependencies
- adds `getImageSizeFromUrl` function that returns image dimensions
- In preparation of AMP support and to improve our schema.org JSON-LD and structured data, I made the following changes:
- Changes the following properties to be `Objects`, which have a `url` property by default and a `dimensions` property, if `width` and `height` are available:
- `metaData.coverImage`
- `metaData.authorImage`
- `metaData.blog.logo`
- Checks cache by calling `getCachedImageSizeFromUrl`. If image dimensions were fetched already, returns them from cache instead of fetching them again.
- If we have image dimensions on hand, the output in our JSON-LD changes from normal urls to be full `ImageObjects`. Applies to all images and logos.
- Special case for `publisher.logo` as it has size restrictions: if the image doesn't fulfil the restrictions (<=600 width and <=60 height), we simply output the url instead, so like before.
- Adds new property for schema.org JSON-LD: `mainEntityOfPage` as an Object.
- Adds additional Open Graph data (if we have the image size): `og:image:width` and `og:image:height`
- Adds/updates tests
* AMP router and controller (#7171, #7157)
Implements AMP in `/apps/`:
- renders `amp.hbs` if route is `/:slug/amp/`
- updates `setResponseContext` to set context to `['amp', 'post']` for a amp post and `['amp', 'page']` for a page, but will not render amp template for a page
- updates `context_spec`
- registers 'amp' as new internal app
- adds the `amp.hbs` template to `core/server/apps/amp` which will be the default template for AMP posts.
- adds `isAmpURL` to `post-lookup`
* 🎨 Use `context` in meta as array (#7205)
Instead of reading the first value of the context array, we're checking if it includes certain context values.
This is a preparation change for AMP, where the context will be delivered as `['amp', 'post']`.
* ✨ AMP helpers (#7174, #7216, #7215, #7223)
- Adds AMP helpers `{{amp_content}}`, `{{amp_component}}` and `{{amp_ghost_head}}` to support AMP:
- `{{amp_content}}`:
- Adds `Amperize` as dependency
- AMP app uses new helper `{{amp_content}}` to render AMP HTML
- `Amperize` transforms regular HTML into AMP HTML
- Adds test for `{{amp_content}}` helper
- Adds 'Sanitize-HTML` as dependendy
- After the HTML get 'amperized' we still might have some HTML tags, which are prohibited in AMP HTML, so we use `sanitize-html` to remove those. With every update, `Amperize` gets and it is able to transform more HTML tags, they valid AMP HTML tags (e. g. `video` and `amp-video`) and will therefore not be removed.
- `{{amp_ghost_head}}`:
- registers `{{amp_ghost_head}}` helper, but uses `{{ghost_head}}` code
- uses `{{amp_ghost_head}}` in `amp.hbs` instead of `{{ghost_head}}`
- `{{ghost_head}}`:
- Render `amphtml` link in metadata for post, which links to the amp post (`getAmpUrl`)
- Updates all test in metadata to support `amp` context
- Changes context conditionals to work with full array instead of first array value
- Adds conditionals, so no additional javascript gets rendered in `{{ghost_head}}`
- Removes trailing `/amp/` in URLs, so only `amphtml` link on regular post renders it
- Adds a conditional, so no code injection will be included, for an `amp` context.
- `{{amp_components}}`:
- AMP app uses new helper `{{amp_components}}` to render necessary script tags for AMP extended components as `amp-iframe`, `amp-anime` and `amp-form`
- Adds test for `{{amp_components}}`
2016-08-22 19:49:27 +03:00
|
|
|
|
2017-09-07 13:59:02 +03:00
|
|
|
if (metaData.previousUrl) {
|
|
|
|
head.push('<link rel="prev" href="' +
|
|
|
|
escapeExpression(metaData.previousUrl) + '" />');
|
|
|
|
}
|
2016-01-27 19:58:27 +03:00
|
|
|
|
2017-09-07 13:59:02 +03:00
|
|
|
if (metaData.nextUrl) {
|
|
|
|
head.push('<link rel="next" href="' +
|
|
|
|
escapeExpression(metaData.nextUrl) + '" />');
|
|
|
|
}
|
2015-04-16 18:48:46 +03:00
|
|
|
|
2017-09-07 13:59:02 +03:00
|
|
|
if (!_.includes(context, 'paged') && useStructuredData) {
|
|
|
|
head.push('');
|
|
|
|
head.push.apply(head, finaliseStructuredData(metaData));
|
|
|
|
head.push('');
|
2016-01-27 19:58:27 +03:00
|
|
|
|
2017-09-07 13:59:02 +03:00
|
|
|
if (metaData.schema) {
|
|
|
|
head.push('<script type="application/ld+json">\n' +
|
|
|
|
JSON.stringify(metaData.schema, null, ' ') +
|
|
|
|
'\n </script>\n');
|
|
|
|
}
|
2016-03-21 01:33:46 +03:00
|
|
|
}
|
2015-11-04 17:20:05 +03:00
|
|
|
|
2017-09-07 13:59:02 +03:00
|
|
|
if (client && client.id && client.secret && !_.includes(context, 'amp')) {
|
|
|
|
head.push(getAjaxHelper(client.id, client.secret));
|
|
|
|
}
|
2019-04-23 17:37:35 +03:00
|
|
|
|
|
|
|
if (!_.includes(context, 'amp') && labs.isSet('members')) {
|
|
|
|
head.push(getMembersHelper());
|
|
|
|
}
|
2015-11-04 17:20:05 +03:00
|
|
|
}
|
|
|
|
|
2017-09-07 13:59:02 +03:00
|
|
|
head.push('<meta name="generator" content="Ghost ' +
|
|
|
|
escapeExpression(safeVersion) + '" />');
|
2017-02-03 16:15:11 +03:00
|
|
|
|
2017-09-07 13:59:02 +03:00
|
|
|
head.push('<link rel="alternate" type="application/rss+xml" title="' +
|
2017-12-12 00:47:46 +03:00
|
|
|
escapeExpression(metaData.blog.title) + '" href="' +
|
2017-09-07 13:59:02 +03:00
|
|
|
escapeExpression(metaData.rssUrl) + '" />');
|
2016-01-27 19:58:27 +03:00
|
|
|
|
2017-09-07 13:59:02 +03:00
|
|
|
// no code injection for amp context!!!
|
|
|
|
if (!_.includes(context, 'amp')) {
|
|
|
|
if (!_.isEmpty(globalCodeinjection)) {
|
|
|
|
head.push(globalCodeinjection);
|
|
|
|
}
|
2017-08-02 14:06:51 +03:00
|
|
|
|
2017-09-07 13:59:02 +03:00
|
|
|
if (!_.isEmpty(postCodeInjection)) {
|
|
|
|
head.push(postCodeInjection);
|
|
|
|
}
|
2017-08-02 14:06:51 +03:00
|
|
|
}
|
2017-09-07 13:59:02 +03:00
|
|
|
debug('end');
|
|
|
|
return new SafeString(head.join('\n ').trim());
|
|
|
|
})
|
|
|
|
.catch(function handleError(err) {
|
|
|
|
logging.error(err);
|
|
|
|
|
|
|
|
// Return what we have so far (currently nothing)
|
|
|
|
return new SafeString(head.join('\n ').trim());
|
|
|
|
});
|
2017-04-04 19:07:35 +03:00
|
|
|
};
|