2020-04-29 18:44:27 +03:00
|
|
|
const Promise = require('bluebird');
|
|
|
|
const settingsCache = require('../../server/services/settings/cache');
|
2020-05-28 13:57:02 +03:00
|
|
|
const urlUtils = require('../../shared/url-utils');
|
2020-05-28 21:30:23 +03:00
|
|
|
const logging = require('../../shared/logging');
|
2020-04-29 18:44:27 +03:00
|
|
|
const getUrl = require('./url');
|
|
|
|
const getImageDimensions = require('./image-dimensions');
|
|
|
|
const getCanonicalUrl = require('./canonical_url');
|
|
|
|
const getAmpUrl = require('./amp_url');
|
|
|
|
const getPaginatedUrl = require('./paginated_url');
|
|
|
|
const getAuthorUrl = require('./author_url');
|
|
|
|
const getBlogLogo = require('./blog_logo');
|
|
|
|
const getRssUrl = require('./rss_url');
|
|
|
|
const getTitle = require('./title');
|
|
|
|
const getDescription = require('./description');
|
|
|
|
const getCoverImage = require('./cover_image');
|
|
|
|
const getAuthorImage = require('./author_image');
|
|
|
|
const getAuthorFacebook = require('./author_fb_url');
|
|
|
|
const getCreatorTwitter = require('./creator_url');
|
|
|
|
const getKeywords = require('./keywords');
|
|
|
|
const getPublishedDate = require('./published_date');
|
|
|
|
const getModifiedDate = require('./modified_date');
|
|
|
|
const getOgType = require('./og_type');
|
|
|
|
const getOgImage = require('./og_image');
|
|
|
|
const getTwitterImage = require('./twitter_image');
|
|
|
|
const getStructuredData = require('./structured_data');
|
|
|
|
const getSchema = require('./schema');
|
|
|
|
const getExcerpt = require('./excerpt');
|
2016-01-17 13:07:52 +03:00
|
|
|
|
|
|
|
function getMetaData(data, root) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const metaData = {
|
|
|
|
url: getUrl(data, true),
|
|
|
|
canonicalUrl: getCanonicalUrl(data),
|
|
|
|
ampUrl: getAmpUrl(data),
|
|
|
|
previousUrl: getPaginatedUrl('prev', data, true),
|
|
|
|
nextUrl: getPaginatedUrl('next', data, true),
|
|
|
|
authorUrl: getAuthorUrl(data, true),
|
|
|
|
rssUrl: getRssUrl(data, true),
|
|
|
|
metaTitle: getTitle(data, root),
|
|
|
|
metaDescription: getDescription(data, root) || null,
|
|
|
|
coverImage: {
|
|
|
|
url: getCoverImage(data, true)
|
✨ [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-04-29 18:44:27 +03:00
|
|
|
authorImage: {
|
|
|
|
url: getAuthorImage(data, true)
|
|
|
|
},
|
|
|
|
ogImage: {
|
|
|
|
url: getOgImage(data)
|
|
|
|
},
|
|
|
|
ogTitle: getTitle(data, root, {property: 'og'}),
|
|
|
|
ogDescription: getDescription(data, root, {property: 'og'}),
|
|
|
|
twitterImage: getTwitterImage(data, true),
|
|
|
|
twitterTitle: getTitle(data, root, {property: 'twitter'}),
|
|
|
|
twitterDescription: getDescription(data, root, {property: 'twitter'}),
|
|
|
|
authorFacebook: getAuthorFacebook(data),
|
|
|
|
creatorTwitter: getCreatorTwitter(data),
|
|
|
|
keywords: getKeywords(data),
|
|
|
|
publishedDate: getPublishedDate(data),
|
|
|
|
modifiedDate: getModifiedDate(data),
|
|
|
|
ogType: getOgType(data),
|
|
|
|
// @TODO: pass into each meta helper - wrap each helper
|
|
|
|
site: {
|
|
|
|
title: settingsCache.get('title'),
|
|
|
|
description: settingsCache.get('description'),
|
|
|
|
url: urlUtils.urlFor('home', true),
|
|
|
|
facebook: settingsCache.get('facebook'),
|
|
|
|
twitter: settingsCache.get('twitter'),
|
2020-06-22 14:21:00 +03:00
|
|
|
timezone: settingsCache.get('timezone'),
|
2020-04-29 18:44:27 +03:00
|
|
|
navigation: settingsCache.get('navigation'),
|
|
|
|
icon: settingsCache.get('icon'),
|
|
|
|
cover_image: settingsCache.get('cover_image'),
|
|
|
|
logo: getBlogLogo(),
|
|
|
|
amp: settingsCache.get('amp')
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let customExcerpt;
|
|
|
|
let metaDescription;
|
|
|
|
let fallbackExcerpt;
|
2016-01-17 13:07:52 +03:00
|
|
|
|
2017-09-05 15:13:22 +03:00
|
|
|
// TODO: cleanup these if statements
|
2019-11-11 13:36:00 +03:00
|
|
|
// NOTE: should use 'post' OR 'page' once https://github.com/TryGhost/Ghost/issues/10042 is resolved
|
|
|
|
if (data.post) {
|
2017-09-05 15:13:22 +03:00
|
|
|
// There's a specific order for description fields (not <meta name="description" /> !!) in structured data
|
|
|
|
// and schema.org which is used the description fields (see https://github.com/TryGhost/Ghost/issues/8793):
|
|
|
|
// 1. CASE: custom_excerpt is populated via the UI
|
|
|
|
// 2. CASE: no custom_excerpt, but meta_description is poplated via the UI
|
|
|
|
// 3. CASE: fall back to automated excerpt of 50 words if neither custom_excerpt nor meta_description is provided
|
2019-09-10 12:37:04 +03:00
|
|
|
// @TODO: https://github.com/TryGhost/Ghost/issues/10062
|
2019-11-11 13:36:00 +03:00
|
|
|
customExcerpt = data.post.excerpt || data.post.custom_excerpt;
|
|
|
|
metaDescription = data.post.meta_description;
|
|
|
|
fallbackExcerpt = data.post.html ? getExcerpt(data.post.html, {words: 50}) : '';
|
2017-02-03 16:15:11 +03:00
|
|
|
|
2017-09-05 15:13:22 +03:00
|
|
|
metaData.excerpt = customExcerpt ? customExcerpt : metaDescription ? metaDescription : fallbackExcerpt;
|
|
|
|
}
|
2017-08-01 11:39:34 +03:00
|
|
|
|
2018-03-27 17:16:15 +03:00
|
|
|
if (data.post && data.post.primary_author && data.post.primary_author.name) {
|
|
|
|
metaData.authorName = data.post.primary_author.name;
|
2017-09-05 15:13:22 +03:00
|
|
|
}
|
2016-02-20 18:55:35 +03:00
|
|
|
|
2017-09-05 15:13:22 +03:00
|
|
|
return Promise.props(getImageDimensions(metaData)).then(function () {
|
|
|
|
metaData.structuredData = getStructuredData(metaData);
|
|
|
|
metaData.schema = getSchema(metaData, data);
|
2016-01-17 13:07:52 +03:00
|
|
|
|
2017-09-05 15:13:22 +03:00
|
|
|
return metaData;
|
🙇 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
|
|
|
}).catch(function (err) {
|
2020-05-22 21:22:20 +03:00
|
|
|
logging.error(err);
|
✨ [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
|
|
|
return metaData;
|
|
|
|
});
|
2016-01-17 13:07:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = getMetaData;
|