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
|
2021-01-28 21:07:45 +03:00
|
|
|
const {metaData, escapeExpression, SafeString, logging, settingsCache, config, blogIcon, urlUtils} = require('../services/proxy');
|
2020-03-30 23:23:02 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const debug = require('ghost-ignition').debug('ghost_head');
|
2020-09-08 10:19:36 +03:00
|
|
|
const templateStyles = require('./tpl/styles');
|
2017-04-04 19:07:35 +03:00
|
|
|
|
2020-03-30 23:23:02 +03:00
|
|
|
const getMetaData = metaData.get;
|
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 + '" />';
|
|
|
|
}
|
|
|
|
|
2020-10-20 02:02:56 +03:00
|
|
|
function finaliseStructuredData(meta) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const head = [];
|
2017-02-03 16:15:11 +03:00
|
|
|
|
2020-10-20 02:02:56 +03:00
|
|
|
_.each(meta.structuredData, function (content, property) {
|
2015-02-05 16:06:36 +03:00
|
|
|
if (property === 'article:tag') {
|
2020-10-20 02:02:56 +03:00
|
|
|
_.each(meta.keywords, function (keyword) {
|
2016-01-27 19:58:27 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-02-15 13:25:57 +03:00
|
|
|
function getMembersHelper(data) {
|
2021-04-19 19:27:37 +03:00
|
|
|
if (settingsCache.get('members_signup_access') === 'none') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:22:42 +03:00
|
|
|
const stripeDirectSecretKey = settingsCache.get('stripe_secret_key');
|
|
|
|
const stripeDirectPublishableKey = settingsCache.get('stripe_publishable_key');
|
|
|
|
const stripeConnectAccountId = settingsCache.get('stripe_connect_account_id');
|
2021-02-15 13:25:57 +03:00
|
|
|
const colorString = _.has(data, 'site._preview') && data.site.accent_color ? ` data-accent-color="${data.site.accent_color}"` : '';
|
2021-02-12 20:31:41 +03:00
|
|
|
const portalUrl = config.get('portal:url');
|
2021-02-15 13:25:57 +03:00
|
|
|
let membersHelper = `<script defer src="${portalUrl}" data-ghost="${urlUtils.getSiteUrl()}"${colorString}></script>`;
|
2021-04-19 19:27:37 +03:00
|
|
|
membersHelper += (`<style id="gh-members-styles">${templateStyles}</style>`);
|
2020-06-29 17:22:42 +03:00
|
|
|
if ((!!stripeDirectSecretKey && !!stripeDirectPublishableKey) || !!stripeConnectAccountId) {
|
2020-08-17 07:08:14 +03:00
|
|
|
membersHelper += '<script async src="https://js.stripe.com/v3/"></script>';
|
2020-01-17 07:57:29 +03:00
|
|
|
}
|
|
|
|
return membersHelper;
|
2019-04-23 17:37:35 +03:00
|
|
|
}
|
|
|
|
|
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: {
|
2019-09-10 12:37:04 +03:00
|
|
|
* site: {},
|
2017-10-26 13:03:53 +03:00
|
|
|
* labs: {},
|
|
|
|
* config: {},
|
|
|
|
* root: {
|
|
|
|
* post: {},
|
|
|
|
* context: ['post'],
|
|
|
|
* locals: {...}
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
2019-09-10 12:37:04 +03:00
|
|
|
* `site`, `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
|
|
|
|
2020-04-29 18:44:27 +03:00
|
|
|
const head = [];
|
|
|
|
const dataRoot = options.data.root;
|
|
|
|
const context = dataRoot._locals.context ? dataRoot._locals.context : null;
|
|
|
|
const safeVersion = dataRoot._locals.safeVersion;
|
|
|
|
const postCodeInjection = dataRoot && dataRoot.post ? dataRoot.post.codeinjection_head : null;
|
2020-07-10 15:20:22 +03:00
|
|
|
const tagCodeInjection = dataRoot && dataRoot.tag ? dataRoot.tag.codeinjection_head : null;
|
2020-07-01 19:44:59 +03:00
|
|
|
const globalCodeinjection = settingsCache.get('codeinjection_head');
|
2020-04-29 18:44:27 +03:00
|
|
|
const useStructuredData = !config.isPrivacyDisabled('useStructuredData');
|
|
|
|
const referrerPolicy = config.get('referrerPolicy') ? config.get('referrerPolicy') : 'no-referrer-when-downgrade';
|
|
|
|
const favicon = blogIcon.getIconUrl();
|
|
|
|
const iconType = blogIcon.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)
|
2020-10-20 02:02:56 +03:00
|
|
|
.then(function handleMetaData(meta) {
|
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
|
2020-10-20 02:02:56 +03:00
|
|
|
if (meta.metaDescription && meta.metaDescription.length > 0) {
|
|
|
|
head.push('<meta name="description" content="' + escapeExpression(meta.metaDescription) + '" />');
|
2017-09-07 13:59:02 +03:00
|
|
|
}
|
2020-07-01 19:44:59 +03:00
|
|
|
|
2020-06-30 17:07:08 +03:00
|
|
|
// no output in head if a publication icon is not set
|
|
|
|
if (settingsCache.get('icon')) {
|
|
|
|
head.push('<link rel="icon" href="' + favicon + '" type="image/' + iconType + '" />');
|
|
|
|
}
|
2017-03-14 19:50:35 +03:00
|
|
|
|
2017-09-07 13:59:02 +03:00
|
|
|
head.push('<link rel="canonical" href="' +
|
2020-10-20 02:02:56 +03:00
|
|
|
escapeExpression(meta.canonicalUrl) + '" />');
|
2017-09-07 13:59:02 +03:00
|
|
|
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="' +
|
2020-10-20 02:02:56 +03:00
|
|
|
escapeExpression(meta.ampUrl) + '" />');
|
2017-09-07 13:59:02 +03:00
|
|
|
}
|
✨ [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
|
|
|
|
2020-10-20 02:02:56 +03:00
|
|
|
if (meta.previousUrl) {
|
2017-09-07 13:59:02 +03:00
|
|
|
head.push('<link rel="prev" href="' +
|
2020-10-20 02:02:56 +03:00
|
|
|
escapeExpression(meta.previousUrl) + '" />');
|
2017-09-07 13:59:02 +03:00
|
|
|
}
|
2016-01-27 19:58:27 +03:00
|
|
|
|
2020-10-20 02:02:56 +03:00
|
|
|
if (meta.nextUrl) {
|
2017-09-07 13:59:02 +03:00
|
|
|
head.push('<link rel="next" href="' +
|
2020-10-20 02:02:56 +03:00
|
|
|
escapeExpression(meta.nextUrl) + '" />');
|
2017-09-07 13:59:02 +03:00
|
|
|
}
|
2015-04-16 18:48:46 +03:00
|
|
|
|
2017-09-07 13:59:02 +03:00
|
|
|
if (!_.includes(context, 'paged') && useStructuredData) {
|
|
|
|
head.push('');
|
2020-10-20 02:02:56 +03:00
|
|
|
head.push.apply(head, finaliseStructuredData(meta));
|
2017-09-07 13:59:02 +03:00
|
|
|
head.push('');
|
2016-01-27 19:58:27 +03:00
|
|
|
|
2020-10-20 02:02:56 +03:00
|
|
|
if (meta.schema) {
|
2017-09-07 13:59:02 +03:00
|
|
|
head.push('<script type="application/ld+json">\n' +
|
2020-10-20 02:02:56 +03:00
|
|
|
JSON.stringify(meta.schema, null, ' ') +
|
2017-09-07 13:59:02 +03:00
|
|
|
'\n </script>\n');
|
|
|
|
}
|
2016-03-21 01:33:46 +03:00
|
|
|
}
|
2021-03-02 14:40:45 +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="' +
|
2020-10-20 02:02:56 +03:00
|
|
|
escapeExpression(meta.site.title) + '" href="' +
|
|
|
|
escapeExpression(meta.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')) {
|
2021-03-05 00:38:11 +03:00
|
|
|
head.push(getMembersHelper(options.data));
|
|
|
|
|
2017-09-07 13:59:02 +03:00
|
|
|
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);
|
|
|
|
}
|
2020-07-10 15:20:22 +03:00
|
|
|
|
|
|
|
if (!_.isEmpty(tagCodeInjection)) {
|
|
|
|
head.push(tagCodeInjection);
|
|
|
|
}
|
2017-08-02 14:06:51 +03:00
|
|
|
}
|
2021-03-05 00:38:11 +03:00
|
|
|
|
2021-03-17 14:49:42 +03:00
|
|
|
// AMP template has style injected directly because there can only be one <style amp-custom> tag
|
|
|
|
if (options.data.site.accent_color && !_.includes(context, 'amp')) {
|
2021-03-05 00:38:11 +03:00
|
|
|
const accentColor = escapeExpression(options.data.site.accent_color);
|
|
|
|
const styleTag = `<style>:root {--ghost-accent-color: ${accentColor};}</style>`;
|
|
|
|
const existingScriptIndex = _.findLastIndex(head, str => str.match(/<\/(style|script)>/));
|
|
|
|
|
|
|
|
if (existingScriptIndex !== -1) {
|
|
|
|
head[existingScriptIndex] = head[existingScriptIndex] + styleTag;
|
|
|
|
} else {
|
|
|
|
head.push(styleTag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|