2
1
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2023-12-13 21:00:40 +01: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:
Hannah Wolfe 2016-02-18 11:47:08 +00:00
parent d7d3c681e8
commit d7b9eb6176
6 changed files with 36 additions and 8 deletions

View file

@ -1,6 +1,7 @@
var downsize = require('downsize');
function getExcerpt(html, truncateOptions) {
truncateOptions = truncateOptions || {};
// Strip inline and bottom footnotes
var excerpt = html.replace(/<a href="#fn.*?rel="footnote">.*?<\/a>/gi, '');
excerpt = excerpt.replace(/<div class="footnotes"><ol>.*?<\/ol><\/div>/, '');

View file

@ -14,7 +14,8 @@ var config = require('../../config'),
getModifiedDate = require('./modified_date'),
getOgType = require('./og_type'),
getStructuredData = require('./structured_data'),
getPostSchema = require('./schema');
getPostSchema = require('./schema'),
getExcerpt = require('./excerpt');
function getMetaData(data, root) {
var blog = config.theme, metaData;
@ -37,6 +38,10 @@ function getMetaData(data, root) {
blog: blog
};
if (data.post && data.post.html) {
metaData.excerpt = getExcerpt(data.post.html, {words: 50});
}
metaData.structuredData = getStructuredData(metaData);
metaData.schema = getPostSchema(metaData, data);

View file

@ -16,7 +16,11 @@ function trimSchema(schema) {
}
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',
'@type': 'Article',
publisher: metaData.blog.title,
@ -37,9 +41,7 @@ function getPostSchema(metaData, data) {
image: metaData.coverImage,
keywords: metaData.keywords && metaData.keywords.length > 0 ?
metaData.keywords.join(', ') : null,
description: metaData.metaDescription ?
escapeExpression(metaData.metaDescription) :
null
description: description
};
schema.author = trimSchema(schema.author);
return trimSchema(schema);

View file

@ -10,7 +10,7 @@ function getStructuredData(metaData) {
'og:site_name': metaData.blog.title,
'og:type': metaData.ogType,
'og:title': metaData.metaTitle,
'og:description': metaData.metaDescription,
'og:description': metaData.metaDescription || metaData.excerpt,
'og:url': metaData.canonicalUrl,
'og:image': metaData.coverImage,
'article:published_time': metaData.publishedDate,
@ -18,7 +18,7 @@ function getStructuredData(metaData) {
'article:tag': metaData.keywords,
'twitter:card': card,
'twitter:title': metaData.metaTitle,
'twitter:description': metaData.metaDescription,
'twitter:description': metaData.metaDescription || metaData.excerpt,
'twitter:url': metaData.canonicalUrl,
'twitter:image:src': metaData.coverImage
};

View file

@ -11,7 +11,6 @@ var getMetaData = require('../data/meta'),
escapeExpression = hbs.handlebars.Utils.escapeExpression,
SafeString = hbs.handlebars.SafeString,
_ = require('lodash'),
api = require('../api'),
filters = require('../filters'),
assetHelper = require('./asset'),
config = require('../config'),

View file

@ -595,6 +595,27 @@ describe('{{ghost_head}} helper', function () {
}).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) {
helpers.ghost_head.call(
{safeVersion: '0.3', relativeUrl: '/about/', context: ['page']},