Skip to content

Commit d7b9eb6

Browse files
committed
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
1 parent d7d3c68 commit d7b9eb6

File tree

6 files changed

+36
-8
lines changed

6 files changed

+36
-8
lines changed

core/server/data/meta/excerpt.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var downsize = require('downsize');
22

33
function getExcerpt(html, truncateOptions) {
4+
truncateOptions = truncateOptions || {};
45
// Strip inline and bottom footnotes
56
var excerpt = html.replace(/<a href="#fn.*?rel="footnote">.*?<\/a>/gi, '');
67
excerpt = excerpt.replace(/<div class="footnotes"><ol>.*?<\/ol><\/div>/, '');

core/server/data/meta/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ var config = require('../../config'),
1414
getModifiedDate = require('./modified_date'),
1515
getOgType = require('./og_type'),
1616
getStructuredData = require('./structured_data'),
17-
getPostSchema = require('./schema');
17+
getPostSchema = require('./schema'),
18+
getExcerpt = require('./excerpt');
1819

1920
function getMetaData(data, root) {
2021
var blog = config.theme, metaData;
@@ -37,6 +38,10 @@ function getMetaData(data, root) {
3738
blog: blog
3839
};
3940

41+
if (data.post && data.post.html) {
42+
metaData.excerpt = getExcerpt(data.post.html, {words: 50});
43+
}
44+
4045
metaData.structuredData = getStructuredData(metaData);
4146
metaData.schema = getPostSchema(metaData, data);
4247

core/server/data/meta/schema.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ function trimSchema(schema) {
1616
}
1717

1818
function getPostSchema(metaData, data) {
19-
var schema = {
19+
var description = metaData.metaDescription ? escapeExpression(metaData.metaDescription) :
20+
(metaData.excerpt ? escapeExpression(metaData.excerpt) : null),
21+
schema;
22+
23+
schema = {
2024
'@context': 'http://schema.org',
2125
'@type': 'Article',
2226
publisher: metaData.blog.title,
@@ -37,9 +41,7 @@ function getPostSchema(metaData, data) {
3741
image: metaData.coverImage,
3842
keywords: metaData.keywords && metaData.keywords.length > 0 ?
3943
metaData.keywords.join(', ') : null,
40-
description: metaData.metaDescription ?
41-
escapeExpression(metaData.metaDescription) :
42-
null
44+
description: description
4345
};
4446
schema.author = trimSchema(schema.author);
4547
return trimSchema(schema);

core/server/data/meta/structured_data.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ function getStructuredData(metaData) {
1010
'og:site_name': metaData.blog.title,
1111
'og:type': metaData.ogType,
1212
'og:title': metaData.metaTitle,
13-
'og:description': metaData.metaDescription,
13+
'og:description': metaData.metaDescription || metaData.excerpt,
1414
'og:url': metaData.canonicalUrl,
1515
'og:image': metaData.coverImage,
1616
'article:published_time': metaData.publishedDate,
1717
'article:modified_time': metaData.modifiedDate,
1818
'article:tag': metaData.keywords,
1919
'twitter:card': card,
2020
'twitter:title': metaData.metaTitle,
21-
'twitter:description': metaData.metaDescription,
21+
'twitter:description': metaData.metaDescription || metaData.excerpt,
2222
'twitter:url': metaData.canonicalUrl,
2323
'twitter:image:src': metaData.coverImage
2424
};

core/server/helpers/ghost_head.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ var getMetaData = require('../data/meta'),
1111
escapeExpression = hbs.handlebars.Utils.escapeExpression,
1212
SafeString = hbs.handlebars.SafeString,
1313
_ = require('lodash'),
14-
api = require('../api'),
1514
filters = require('../filters'),
1615
assetHelper = require('./asset'),
1716
config = require('../config'),

core/test/unit/server_helpers/ghost_head_spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,27 @@ describe('{{ghost_head}} helper', function () {
595595
}).catch(done);
596596
});
597597

598+
it('returns twitter and facebook descriptions if no meta description available', function (done) {
599+
var post = {
600+
title: 'Welcome to Ghost',
601+
html: '<p>This is a short post</p>',
602+
author: {
603+
name: 'Author name'
604+
}
605+
};
606+
607+
helpers.ghost_head.call(
608+
{relativeUrl: '/post/', safeVersion: '0.3', context: ['post'], post: post},
609+
{data: {root: {context: ['post']}}}
610+
).then(function (rendered) {
611+
should.exist(rendered);
612+
rendered.string.should.match(/<meta property="og:description" content="This is a short post" \/>/);
613+
rendered.string.should.match(/<meta name="twitter:description" content="This is a short post" \/>/);
614+
615+
done();
616+
}).catch(done);
617+
});
618+
598619
it('returns canonical URL', function (done) {
599620
helpers.ghost_head.call(
600621
{safeVersion: '0.3', relativeUrl: '/about/', context: ['page']},

0 commit comments

Comments
 (0)