2015-10-10 20:51:38 +03:00
|
|
|
/**
|
|
|
|
* # Response context
|
|
|
|
*
|
|
|
|
* Figures out which context we are currently serving. The biggest challenge with determining this
|
|
|
|
* is that the only way to determine whether or not we are a post, or a page, is with data after all the
|
|
|
|
* data for the template has been retrieved.
|
|
|
|
*
|
|
|
|
* Contexts are determined based on 3 pieces of information
|
|
|
|
* 1. res.locals.relativeUrl - which never includes the subdirectory
|
2019-09-02 13:34:13 +03:00
|
|
|
* 2. req.params.page - always has the page parameter, regardless of if the URL contains a keyword
|
2015-10-10 20:51:38 +03:00
|
|
|
* 3. data - used for telling the difference between posts and pages
|
|
|
|
*/
|
2020-04-29 18:44:27 +03:00
|
|
|
// @TODO: fix this!! These regexes are app specific and should be dynamic. They should not belong here....
|
|
|
|
// routeKeywords.private: 'private'
|
|
|
|
const privatePattern = new RegExp('^\\/private\\/');
|
|
|
|
|
|
|
|
// routeKeywords.amp: 'amp'
|
|
|
|
const ampPattern = new RegExp('\\/amp\\/$');
|
|
|
|
|
|
|
|
const homePattern = new RegExp('^\\/$');
|
2015-10-10 20:51:38 +03:00
|
|
|
|
|
|
|
function setResponseContext(req, res, data) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const pageParam = req.params && req.params.page !== undefined ? parseInt(req.params.page, 10) : 1;
|
2015-10-10 20:51:38 +03:00
|
|
|
|
|
|
|
res.locals = res.locals || {};
|
|
|
|
res.locals.context = [];
|
|
|
|
|
|
|
|
// If we don't have a relativeUrl, we can't detect the context, so return
|
2020-04-21 20:46:52 +03:00
|
|
|
// See web/parent/middleware/ghost-locals
|
2015-10-10 20:51:38 +03:00
|
|
|
if (!res.locals.relativeUrl) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-20 15:44:46 +03:00
|
|
|
// Paged context - special rule
|
2015-10-10 20:51:38 +03:00
|
|
|
if (!isNaN(pageParam) && pageParam > 1) {
|
|
|
|
res.locals.context.push('paged');
|
|
|
|
}
|
|
|
|
|
2016-03-20 15:44:46 +03:00
|
|
|
// Home context - special rule
|
|
|
|
if (homePattern.test(res.locals.relativeUrl)) {
|
2015-10-10 20:51:38 +03:00
|
|
|
res.locals.context.push('home');
|
2016-03-20 15:44:46 +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
|
|
|
// Add context 'amp' to either post or page, if we have an `*/amp` route
|
2019-09-10 12:41:42 +03:00
|
|
|
if (ampPattern.test(res.locals.relativeUrl) && (data.post || data.page)) {
|
✨ [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
|
|
|
res.locals.context.push('amp');
|
|
|
|
}
|
|
|
|
|
2016-03-20 15:44:46 +03:00
|
|
|
// Each page can only have at most one of these
|
2018-06-26 02:12:50 +03:00
|
|
|
if (res.routerOptions && res.routerOptions.context) {
|
|
|
|
res.locals.context = res.locals.context.concat(res.routerOptions.context);
|
2019-01-21 13:48:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (privatePattern.test(res.locals.relativeUrl)) {
|
|
|
|
if (!res.locals.context.includes('private')) {
|
|
|
|
res.locals.context.push('private');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 19:06:45 +03:00
|
|
|
// @TODO: remove first if condition when only page key is returned
|
|
|
|
// ref.: https://github.com/TryGhost/Ghost/issues/10042
|
|
|
|
if (data && data.post && data.post.page) {
|
|
|
|
if (!res.locals.context.includes('page')) {
|
|
|
|
res.locals.context.push('page');
|
|
|
|
}
|
|
|
|
} else if (data && data.post) {
|
2019-01-21 13:48:18 +03:00
|
|
|
if (!res.locals.context.includes('post')) {
|
|
|
|
res.locals.context.push('post');
|
|
|
|
}
|
2019-03-12 03:16:02 +03:00
|
|
|
} else if (data && data.page) {
|
|
|
|
if (!res.locals.context.includes('page')) {
|
|
|
|
res.locals.context.push('page');
|
|
|
|
}
|
2020-08-17 17:21:09 +03:00
|
|
|
} else if (data && data.tag) {
|
|
|
|
if (!res.locals.context.includes('tag')) {
|
|
|
|
res.locals.context.push('tag');
|
|
|
|
}
|
2015-10-10 20:51:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = setResponseContext;
|