Skip to content

Commit

Permalink
Update structured data for fb & twitter usernames
Browse files Browse the repository at this point in the history
refs TryGhost#6534

- twitter & facebook fields are changing to store usernames only
- use the new social url util to generate urls where necessary
- update tests
  • Loading branch information
ErisDS authored and geekhuyang committed Nov 20, 2016
1 parent 65d42ba commit 616909e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 34 deletions.
9 changes: 5 additions & 4 deletions core/server/data/meta/schema.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var config = require('../../config'),
hbs = require('express-hbs'),
socialUrls = require('../../utils/social-urls'),
escapeExpression = hbs.handlebars.Utils.escapeExpression,
_ = require('lodash');

Expand All @@ -23,20 +24,20 @@ function trimSameAs(data, context) {
sameAs.push(data.post.author.website);
}
if (data.post.author.facebook) {
sameAs.push(data.post.author.facebook);
sameAs.push(socialUrls.facebookUrl(data.post.author.facebook));
}
if (data.post.author.twitter) {
sameAs.push(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(data.author.facebook);
sameAs.push(socialUrls.facebookUrl(data.author.facebook));
}
if (data.author.twitter) {
sameAs.push(data.author.twitter);
sameAs.push(socialUrls.twitterUrl(data.author.twitter));
}
}

Expand Down
15 changes: 6 additions & 9 deletions core/server/data/meta/structured_data.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
var socialUrls = require('../../utils/social-urls');

function getStructuredData(metaData) {
var structuredData,
card = 'summary',
twitterUser;
card = 'summary';

if (metaData.coverImage) {
card = 'summary_large_image';
}

if (metaData.creatorTwitter) {
twitterUser = '@' + metaData.creatorTwitter.match(/(?:https:\/\/)(?:twitter\.com)\/(?:#!\/)?@?([^\/]*)/)[1];
}

structuredData = {
'og:site_name': metaData.blog.title,
'og:type': metaData.ogType,
Expand All @@ -21,8 +18,8 @@ function getStructuredData(metaData) {
'article:published_time': metaData.publishedDate,
'article:modified_time': metaData.modifiedDate,
'article:tag': metaData.keywords,
'article:publisher': metaData.blog.facebook || undefined,
'article:author': metaData.authorFacebook || undefined,
'article:publisher': metaData.blog.facebook ? socialUrls.facebookUrl(metaData.blog.facebook) : undefined,
'article:author': metaData.authorFacebook ? socialUrls.facebookUrl(metaData.authorFacebook) : undefined,
'twitter:card': card,
'twitter:title': metaData.metaTitle,
'twitter:description': metaData.metaDescription || metaData.excerpt,
Expand All @@ -33,7 +30,7 @@ function getStructuredData(metaData) {
'twitter:label2': metaData.keywords ? 'Filed under' : undefined,
'twitter:data2': metaData.keywords ? metaData.keywords.join(', ') : undefined,
'twitter:site': metaData.blog.twitter || undefined,
'twitter:creator': twitterUser || undefined
'twitter:creator': metaData.creatorTwitter || undefined
};

// return structured data removing null or undefined keys
Expand Down
8 changes: 4 additions & 4 deletions core/test/unit/metadata/schema_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('getSchema', function () {
logo: 'http://mysite.com/author/image/url/logo.jpg'
},
authorImage: 'http://mysite.com/author/image/url/me.jpg',
authorFacebook: 'https://facebook.com/testuser',
authorFacebook: 'testuser',
creatorTwitter: '@testuser',
authorUrl: 'http://mysite.com/author/me/',
metaTitle: 'Post Title',
Expand All @@ -27,8 +27,8 @@ describe('getSchema', function () {
name: 'Post Author',
website: 'http://myblogsite.com/',
bio: 'My author bio.',
facebook: 'https://www.facebook.com/testuser',
twitter: 'https://twitter.com/testuser'
facebook: 'testuser',
twitter: '@testuser'
}
}
}, schema = getSchema(metadata, data);
Expand Down Expand Up @@ -175,7 +175,7 @@ describe('getSchema', function () {
author: {
name: 'Author Name',
website: 'http://myblogsite.com/',
twitter: 'https://twitter.com/testuser'
twitter: '@testuser'
}
}, schema = getSchema(metadata, data);

Expand Down
6 changes: 3 additions & 3 deletions core/test/unit/metadata/structured_data_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('getStructuredData', function () {
var metadata = {
blog: {
title: 'Blog Title',
facebook: 'https://www.facebook.com/testuser',
facebook: 'testuser',
twitter: '@testuser'
},
authorName: 'Test User',
Expand All @@ -17,8 +17,8 @@ describe('getStructuredData', function () {
publishedDate: '2015-12-25T05:35:01.234Z',
modifiedDate: '2016-01-21T22:13:05.412Z',
coverImage: 'http://mysite.com/content/image/mypostcoverimage.jpg',
authorFacebook: 'https://www.facebook.com/testpage',
creatorTwitter: 'https://twitter.com/twitterpage',
authorFacebook: 'testpage',
creatorTwitter: '@twitterpage',
keywords: ['one', 'two', 'tag'],
metaDescription: 'Post meta description'
}, structuredData = getStructuredData(metadata);
Expand Down
28 changes: 14 additions & 14 deletions core/test/unit/server_helpers/ghost_head_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ describe('{{ghost_head}} helper', function () {
slug: 'Author',
image: '/content/images/test-author-image.png',
website: 'http://authorwebsite.com',
facebook: 'https://www.facebook.com/testuser',
twitter: 'https://twitter.com/testuser',
facebook: 'testuser',
twitter: '@testuser',
bio: 'Author bio'
}
};
Expand Down Expand Up @@ -301,8 +301,8 @@ describe('{{ghost_head}} helper', function () {
image: '/content/images/test-author-image.png',
cover: '/content/images/author-cover-image.png',
website: 'http://authorwebsite.com',
facebook: 'https://www.facebook.com/testuser',
twitter: 'https://twitter.com/testuser'
facebook: 'testuser',
twitter: '@testuser'
}, authorBk = _.cloneDeep(author);

helpers.ghost_head.call(
Expand Down Expand Up @@ -393,8 +393,8 @@ describe('{{ghost_head}} helper', function () {
image: '/content/images/test-author-image.png',
website: 'http://authorwebsite.com',
bio: 'Author bio',
facebook: 'https://www.facebook.com/testuser',
twitter: 'https://twitter.com/testuser'
facebook: 'testuser',
twitter: '@testuser'
}
}, postBk = _.cloneDeep(post);

Expand Down Expand Up @@ -470,8 +470,8 @@ describe('{{ghost_head}} helper', function () {
slug: 'Author',
image: '/content/images/test-author-image.png',
website: 'http://authorwebsite.com',
facebook: 'https://www.facebook.com/testuser',
twitter: 'https://twitter.com/testuser'
facebook: 'testuser',
twitter: '@testuser'
}
};

Expand Down Expand Up @@ -546,8 +546,8 @@ describe('{{ghost_head}} helper', function () {
slug: 'Author',
image: '/content/images/test-author-image.png',
website: 'http://authorwebsite.com',
facebook: 'https://www.facebook.com/testuser',
twitter: 'https://twitter.com/testuser'
facebook: 'testuser',
twitter: '@testuser'
}
};

Expand Down Expand Up @@ -618,8 +618,8 @@ describe('{{ghost_head}} helper', function () {
slug: 'Author',
image: null,
website: 'http://authorwebsite.com',
facebook: 'https://www.facebook.com/testuser',
twitter: 'https://twitter.com/testuser'
facebook: 'testuser',
twitter: '@testuser'
}
};

Expand Down Expand Up @@ -833,8 +833,8 @@ describe('{{ghost_head}} helper', function () {
slug: 'Author',
image: 'content/images/test-author-image.png',
website: 'http://authorwebsite.com',
facebook: 'https://www.facebook.com/testuser',
twitter: 'https://twitter.com/testuser'
facebook: 'testuser',
twitter: '@testuser'
}
};

Expand Down

0 comments on commit 616909e

Please sign in to comment.