mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
Fix facebook/twitter/schema description
refs #6534 - this is an initial fix for having no description at all unless a meta description is provided - we may need to tweak the lengths / provide different lengths for different values in future
This commit is contained in:
parent
d7d3c681e8
commit
d7b9eb6176
6 changed files with 36 additions and 8 deletions
|
@ -1,6 +1,7 @@
|
||||||
var downsize = require('downsize');
|
var downsize = require('downsize');
|
||||||
|
|
||||||
function getExcerpt(html, truncateOptions) {
|
function getExcerpt(html, truncateOptions) {
|
||||||
|
truncateOptions = truncateOptions || {};
|
||||||
// Strip inline and bottom footnotes
|
// Strip inline and bottom footnotes
|
||||||
var excerpt = html.replace(/<a href="#fn.*?rel="footnote">.*?<\/a>/gi, '');
|
var excerpt = html.replace(/<a href="#fn.*?rel="footnote">.*?<\/a>/gi, '');
|
||||||
excerpt = excerpt.replace(/<div class="footnotes"><ol>.*?<\/ol><\/div>/, '');
|
excerpt = excerpt.replace(/<div class="footnotes"><ol>.*?<\/ol><\/div>/, '');
|
||||||
|
|
|
@ -14,7 +14,8 @@ var config = require('../../config'),
|
||||||
getModifiedDate = require('./modified_date'),
|
getModifiedDate = require('./modified_date'),
|
||||||
getOgType = require('./og_type'),
|
getOgType = require('./og_type'),
|
||||||
getStructuredData = require('./structured_data'),
|
getStructuredData = require('./structured_data'),
|
||||||
getPostSchema = require('./schema');
|
getPostSchema = require('./schema'),
|
||||||
|
getExcerpt = require('./excerpt');
|
||||||
|
|
||||||
function getMetaData(data, root) {
|
function getMetaData(data, root) {
|
||||||
var blog = config.theme, metaData;
|
var blog = config.theme, metaData;
|
||||||
|
@ -37,6 +38,10 @@ function getMetaData(data, root) {
|
||||||
blog: blog
|
blog: blog
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (data.post && data.post.html) {
|
||||||
|
metaData.excerpt = getExcerpt(data.post.html, {words: 50});
|
||||||
|
}
|
||||||
|
|
||||||
metaData.structuredData = getStructuredData(metaData);
|
metaData.structuredData = getStructuredData(metaData);
|
||||||
metaData.schema = getPostSchema(metaData, data);
|
metaData.schema = getPostSchema(metaData, data);
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,11 @@ function trimSchema(schema) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPostSchema(metaData, data) {
|
function getPostSchema(metaData, data) {
|
||||||
var schema = {
|
var description = metaData.metaDescription ? escapeExpression(metaData.metaDescription) :
|
||||||
|
(metaData.excerpt ? escapeExpression(metaData.excerpt) : null),
|
||||||
|
schema;
|
||||||
|
|
||||||
|
schema = {
|
||||||
'@context': 'http://schema.org',
|
'@context': 'http://schema.org',
|
||||||
'@type': 'Article',
|
'@type': 'Article',
|
||||||
publisher: metaData.blog.title,
|
publisher: metaData.blog.title,
|
||||||
|
@ -37,9 +41,7 @@ function getPostSchema(metaData, data) {
|
||||||
image: metaData.coverImage,
|
image: metaData.coverImage,
|
||||||
keywords: metaData.keywords && metaData.keywords.length > 0 ?
|
keywords: metaData.keywords && metaData.keywords.length > 0 ?
|
||||||
metaData.keywords.join(', ') : null,
|
metaData.keywords.join(', ') : null,
|
||||||
description: metaData.metaDescription ?
|
description: description
|
||||||
escapeExpression(metaData.metaDescription) :
|
|
||||||
null
|
|
||||||
};
|
};
|
||||||
schema.author = trimSchema(schema.author);
|
schema.author = trimSchema(schema.author);
|
||||||
return trimSchema(schema);
|
return trimSchema(schema);
|
||||||
|
|
|
@ -10,7 +10,7 @@ function getStructuredData(metaData) {
|
||||||
'og:site_name': metaData.blog.title,
|
'og:site_name': metaData.blog.title,
|
||||||
'og:type': metaData.ogType,
|
'og:type': metaData.ogType,
|
||||||
'og:title': metaData.metaTitle,
|
'og:title': metaData.metaTitle,
|
||||||
'og:description': metaData.metaDescription,
|
'og:description': metaData.metaDescription || metaData.excerpt,
|
||||||
'og:url': metaData.canonicalUrl,
|
'og:url': metaData.canonicalUrl,
|
||||||
'og:image': metaData.coverImage,
|
'og:image': metaData.coverImage,
|
||||||
'article:published_time': metaData.publishedDate,
|
'article:published_time': metaData.publishedDate,
|
||||||
|
@ -18,7 +18,7 @@ function getStructuredData(metaData) {
|
||||||
'article:tag': metaData.keywords,
|
'article:tag': metaData.keywords,
|
||||||
'twitter:card': card,
|
'twitter:card': card,
|
||||||
'twitter:title': metaData.metaTitle,
|
'twitter:title': metaData.metaTitle,
|
||||||
'twitter:description': metaData.metaDescription,
|
'twitter:description': metaData.metaDescription || metaData.excerpt,
|
||||||
'twitter:url': metaData.canonicalUrl,
|
'twitter:url': metaData.canonicalUrl,
|
||||||
'twitter:image:src': metaData.coverImage
|
'twitter:image:src': metaData.coverImage
|
||||||
};
|
};
|
||||||
|
|
|
@ -11,7 +11,6 @@ var getMetaData = require('../data/meta'),
|
||||||
escapeExpression = hbs.handlebars.Utils.escapeExpression,
|
escapeExpression = hbs.handlebars.Utils.escapeExpression,
|
||||||
SafeString = hbs.handlebars.SafeString,
|
SafeString = hbs.handlebars.SafeString,
|
||||||
_ = require('lodash'),
|
_ = require('lodash'),
|
||||||
api = require('../api'),
|
|
||||||
filters = require('../filters'),
|
filters = require('../filters'),
|
||||||
assetHelper = require('./asset'),
|
assetHelper = require('./asset'),
|
||||||
config = require('../config'),
|
config = require('../config'),
|
||||||
|
|
|
@ -595,6 +595,27 @@ describe('{{ghost_head}} helper', function () {
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('returns twitter and facebook descriptions if no meta description available', function (done) {
|
||||||
|
var post = {
|
||||||
|
title: 'Welcome to Ghost',
|
||||||
|
html: '<p>This is a short post</p>',
|
||||||
|
author: {
|
||||||
|
name: 'Author name'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
helpers.ghost_head.call(
|
||||||
|
{relativeUrl: '/post/', safeVersion: '0.3', context: ['post'], post: post},
|
||||||
|
{data: {root: {context: ['post']}}}
|
||||||
|
).then(function (rendered) {
|
||||||
|
should.exist(rendered);
|
||||||
|
rendered.string.should.match(/<meta property="og:description" content="This is a short post" \/>/);
|
||||||
|
rendered.string.should.match(/<meta name="twitter:description" content="This is a short post" \/>/);
|
||||||
|
|
||||||
|
done();
|
||||||
|
}).catch(done);
|
||||||
|
});
|
||||||
|
|
||||||
it('returns canonical URL', function (done) {
|
it('returns canonical URL', function (done) {
|
||||||
helpers.ghost_head.call(
|
helpers.ghost_head.call(
|
||||||
{safeVersion: '0.3', relativeUrl: '/about/', context: ['page']},
|
{safeVersion: '0.3', relativeUrl: '/about/', context: ['page']},
|
||||||
|
|
Loading…
Add table
Reference in a new issue