mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-16 20:43:01 +03:00
a5c29dfc34
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}}`
190 lines
5.7 KiB
JavaScript
190 lines
5.7 KiB
JavaScript
var config = require('../../config'),
|
|
hbs = require('express-hbs'),
|
|
socialUrls = require('../../utils/social-urls'),
|
|
escapeExpression = hbs.handlebars.Utils.escapeExpression,
|
|
_ = require('lodash');
|
|
|
|
function schemaImageObject(metaDataVal) {
|
|
var imageObject;
|
|
if (!metaDataVal) {
|
|
return null;
|
|
}
|
|
if (!metaDataVal.dimensions) {
|
|
return metaDataVal.url;
|
|
}
|
|
|
|
imageObject = {
|
|
'@type': 'ImageObject',
|
|
url: metaDataVal.url,
|
|
width: metaDataVal.dimensions.width,
|
|
height: metaDataVal.dimensions.height
|
|
};
|
|
|
|
return imageObject;
|
|
}
|
|
|
|
// Creates the final schema object with values that are not null
|
|
function trimSchema(schema) {
|
|
var schemaObject = {};
|
|
|
|
_.each(schema, function (value, key) {
|
|
if (value !== null && typeof value !== 'undefined') {
|
|
schemaObject[key] = value;
|
|
}
|
|
});
|
|
|
|
return schemaObject;
|
|
}
|
|
|
|
function trimSameAs(data, context) {
|
|
var sameAs = [];
|
|
|
|
if (context === 'post') {
|
|
if (data.post.author.website) {
|
|
sameAs.push(data.post.author.website);
|
|
}
|
|
if (data.post.author.facebook) {
|
|
sameAs.push(socialUrls.facebookUrl(data.post.author.facebook));
|
|
}
|
|
if (data.post.author.twitter) {
|
|
sameAs.push(socialUrls.twitterUrl(data.post.author.twitter));
|
|
}
|
|
} else if (context === 'author') {
|
|
if (data.author.website) {
|
|
sameAs.push(data.author.website);
|
|
}
|
|
if (data.author.facebook) {
|
|
sameAs.push(socialUrls.facebookUrl(data.author.facebook));
|
|
}
|
|
if (data.author.twitter) {
|
|
sameAs.push(socialUrls.twitterUrl(data.author.twitter));
|
|
}
|
|
}
|
|
|
|
return sameAs;
|
|
}
|
|
|
|
function getPostSchema(metaData, data) {
|
|
var description = metaData.metaDescription ? escapeExpression(metaData.metaDescription) :
|
|
(metaData.excerpt ? escapeExpression(metaData.excerpt) : null),
|
|
schema;
|
|
|
|
schema = {
|
|
'@context': 'https://schema.org',
|
|
'@type': 'Article',
|
|
publisher: {
|
|
'@type': 'Organization',
|
|
name: escapeExpression(metaData.blog.title),
|
|
logo: schemaImageObject(metaData.blog.logo) || null
|
|
},
|
|
author: {
|
|
'@type': 'Person',
|
|
name: escapeExpression(data.post.author.name),
|
|
image: schemaImageObject(metaData.authorImage),
|
|
url: metaData.authorUrl,
|
|
sameAs: trimSameAs(data, 'post'),
|
|
description: data.post.author.bio ?
|
|
escapeExpression(data.post.author.bio) :
|
|
null
|
|
},
|
|
headline: escapeExpression(metaData.metaTitle),
|
|
url: metaData.url,
|
|
datePublished: metaData.publishedDate,
|
|
dateModified: metaData.modifiedDate,
|
|
image: schemaImageObject(metaData.coverImage),
|
|
keywords: metaData.keywords && metaData.keywords.length > 0 ?
|
|
metaData.keywords.join(', ') : null,
|
|
description: description,
|
|
mainEntityOfPage: {
|
|
'@type': 'WebPage',
|
|
'@id': metaData.blog.url || null
|
|
}
|
|
};
|
|
schema.author = trimSchema(schema.author);
|
|
return trimSchema(schema);
|
|
}
|
|
|
|
function getHomeSchema(metaData) {
|
|
var schema = {
|
|
'@context': 'https://schema.org',
|
|
'@type': 'Website',
|
|
publisher: {
|
|
'@type': 'Organization',
|
|
name: escapeExpression(metaData.blog.title),
|
|
logo: schemaImageObject(metaData.blog.logo) || null
|
|
},
|
|
url: metaData.url,
|
|
image: schemaImageObject(metaData.coverImage),
|
|
mainEntityOfPage: {
|
|
'@type': 'WebPage',
|
|
'@id': metaData.blog.url || null
|
|
},
|
|
description: metaData.metaDescription ?
|
|
escapeExpression(metaData.metaDescription) :
|
|
null
|
|
};
|
|
return trimSchema(schema);
|
|
}
|
|
|
|
function getTagSchema(metaData, data) {
|
|
var schema = {
|
|
'@context': 'https://schema.org',
|
|
'@type': 'Series',
|
|
publisher: {
|
|
'@type': 'Organization',
|
|
name: escapeExpression(metaData.blog.title),
|
|
logo: schemaImageObject(metaData.blog.logo) || null
|
|
},
|
|
url: metaData.url,
|
|
image: schemaImageObject(metaData.coverImage),
|
|
name: data.tag.name,
|
|
mainEntityOfPage: {
|
|
'@type': 'WebPage',
|
|
'@id': metaData.blog.url || null
|
|
},
|
|
description: metaData.metaDescription ?
|
|
escapeExpression(metaData.metaDescription) :
|
|
null
|
|
};
|
|
|
|
return trimSchema(schema);
|
|
}
|
|
|
|
function getAuthorSchema(metaData, data) {
|
|
var schema = {
|
|
'@context': 'https://schema.org',
|
|
'@type': 'Person',
|
|
sameAs: trimSameAs(data, 'author'),
|
|
name: escapeExpression(data.author.name),
|
|
url: metaData.authorUrl,
|
|
image: schemaImageObject(metaData.coverImage),
|
|
mainEntityOfPage: {
|
|
'@type': 'WebPage',
|
|
'@id': metaData.blog.url || null
|
|
},
|
|
description: metaData.metaDescription ?
|
|
escapeExpression(metaData.metaDescription) :
|
|
null
|
|
};
|
|
|
|
return trimSchema(schema);
|
|
}
|
|
|
|
function getSchema(metaData, data) {
|
|
if (!config.isPrivacyDisabled('useStructuredData')) {
|
|
var context = data.context ? data.context : null;
|
|
if (_.includes(context, 'post') || _.includes(context, 'page') || _.includes(context, 'amp')) {
|
|
return getPostSchema(metaData, data);
|
|
} else if (_.includes(context, 'home')) {
|
|
return getHomeSchema(metaData);
|
|
} else if (_.includes(context, 'tag')) {
|
|
return getTagSchema(metaData, data);
|
|
} else if (_.includes(context, 'author')) {
|
|
return getAuthorSchema(metaData, data);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
module.exports = getSchema;
|