mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
Add helpers for facebook & twitter urls
refs #6534 - this PR assumes that we are now saving usernames only in the database for twitter & facebook - adds a new social links utility which can generate twitter & facebook urls from the username - adds a {{twitter_url}} and {{facebook_url}} helper which uses these - adds a full suite of tests for the helpers & utils
This commit is contained in:
parent
06a2920333
commit
e96b60b850
8 changed files with 213 additions and 1 deletions
26
core/server/helpers/facebook_url.js
Normal file
26
core/server/helpers/facebook_url.js
Normal file
|
@ -0,0 +1,26 @@
|
|||
// # Facebook URL Helper
|
||||
// Usage: `{{facebook_url}}` or `{{facebook_url author.facebook}}`
|
||||
//
|
||||
// Output a url for a twitter username
|
||||
//
|
||||
// We use the name facebook_url to match the helper for consistency:
|
||||
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
|
||||
|
||||
var socialUrls = require('../utils/social-urls'),
|
||||
findKey = require('./utils').findKey,
|
||||
facebook_url;
|
||||
|
||||
facebook_url = function (username, options) {
|
||||
if (!options) {
|
||||
options = username;
|
||||
username = findKey('facebook', this, options.data.blog);
|
||||
}
|
||||
|
||||
if (username) {
|
||||
return socialUrls.facebookUrl(username);
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
module.exports = facebook_url;
|
|
@ -17,6 +17,7 @@ coreHelpers.content = require('./content');
|
|||
coreHelpers.date = require('./date');
|
||||
coreHelpers.encode = require('./encode');
|
||||
coreHelpers.excerpt = require('./excerpt');
|
||||
coreHelpers.facebook_url = require('./facebook_url');
|
||||
coreHelpers.foreach = require('./foreach');
|
||||
coreHelpers.get = require('./get');
|
||||
coreHelpers.ghost_foot = require('./ghost_foot');
|
||||
|
@ -34,6 +35,7 @@ coreHelpers.prev_post = require('./prev_next');
|
|||
coreHelpers.next_post = require('./prev_next');
|
||||
coreHelpers.tags = require('./tags');
|
||||
coreHelpers.title = require('./title');
|
||||
coreHelpers.twitter_url = require('./twitter_url');
|
||||
coreHelpers.url = require('./url');
|
||||
|
||||
// Specialist helpers for certain templates
|
||||
|
@ -110,6 +112,8 @@ registerHelpers = function (adminHbs) {
|
|||
registerThemeHelper('post_class', coreHelpers.post_class);
|
||||
registerThemeHelper('tags', coreHelpers.tags);
|
||||
registerThemeHelper('title', coreHelpers.title);
|
||||
registerThemeHelper('twitter_url', coreHelpers.twitter_url);
|
||||
registerThemeHelper('facebook_url', coreHelpers.facebook_url);
|
||||
registerThemeHelper('url', coreHelpers.url);
|
||||
|
||||
// Async theme helpers
|
||||
|
|
26
core/server/helpers/twitter_url.js
Normal file
26
core/server/helpers/twitter_url.js
Normal file
|
@ -0,0 +1,26 @@
|
|||
// # Twitter URL Helper
|
||||
// Usage: `{{twitter_url}}` or `{{twitter_url author.twitter}}`
|
||||
//
|
||||
// Output a url for a twitter username
|
||||
//
|
||||
// We use the name twitter_url to match the helper for consistency:
|
||||
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
|
||||
|
||||
var socialUrls = require('../utils/social-urls'),
|
||||
findKey = require('./utils').findKey,
|
||||
twitter_url;
|
||||
|
||||
twitter_url = function twitter_url(username, options) {
|
||||
if (!options) {
|
||||
options = username;
|
||||
username = findKey('twitter', this, options.data.blog);
|
||||
}
|
||||
|
||||
if (username) {
|
||||
return socialUrls.twitterUrl(username);
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
module.exports = twitter_url;
|
|
@ -6,7 +6,19 @@ utils = {
|
|||
linkTemplate: _.template('<a href="<%= url %>"><%= text %></a>'),
|
||||
scriptTemplate: _.template('<script src="<%= source %>?v=<%= version %>"></script>'),
|
||||
inputTemplate: _.template('<input class="<%= className %>" type="<%= type %>" name="<%= name %>" <%= extras %> />'),
|
||||
isProduction: process.env.NODE_ENV === 'production'
|
||||
isProduction: process.env.NODE_ENV === 'production',
|
||||
// @TODO this can probably be made more generic and used in more places
|
||||
findKey: function findKey(key, object, data) {
|
||||
if (object && _.has(object, key) && !_.isEmpty(object[key])) {
|
||||
return object[key];
|
||||
}
|
||||
|
||||
if (data && _.has(data, key) && !_.isEmpty(data[key])) {
|
||||
return data[key];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = utils;
|
||||
|
|
9
core/server/utils/social-urls.js
Normal file
9
core/server/utils/social-urls.js
Normal file
|
@ -0,0 +1,9 @@
|
|||
module.exports.twitterUrl = function twitterUrl(username) {
|
||||
// Creates the canonical twitter URL without the '@'
|
||||
return 'https://twitter.com/' + username.replace(/^@/, '');
|
||||
};
|
||||
|
||||
module.exports.facebookUrl = function facebookUrl(username) {
|
||||
// Handles a starting slash, this shouldn't happen, but just in case
|
||||
return 'https://www.facebook.com/' + username.replace(/^\//, '');
|
||||
};
|
48
core/test/unit/server_helpers/facebook_url_spec.js
Normal file
48
core/test/unit/server_helpers/facebook_url_spec.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*globals describe, before, beforeEach, it*/
|
||||
var should = require('should'),
|
||||
hbs = require('express-hbs'),
|
||||
utils = require('./utils'),
|
||||
|
||||
// Stuff we are testing
|
||||
handlebars = hbs.handlebars,
|
||||
helpers = require('../../../server/helpers');
|
||||
|
||||
describe('{{facebook_url}} helper', function () {
|
||||
var options = {data: {blog: {}}};
|
||||
|
||||
before(function () {
|
||||
utils.loadHelpers();
|
||||
});
|
||||
|
||||
beforeEach(function () {
|
||||
options.data.blog = {facebook: ''};
|
||||
});
|
||||
|
||||
it('has loaded facebook_url helper', function () {
|
||||
should.exist(handlebars.helpers.facebook_url);
|
||||
});
|
||||
|
||||
it('should output the facebook url for @blog, if no other facebook username is provided', function () {
|
||||
options.data.blog = {facebook: 'hey'};
|
||||
|
||||
helpers.facebook_url.call({}, options).should.equal('https://www.facebook.com/hey');
|
||||
});
|
||||
|
||||
it('should output the facebook url for the local object, if it has one', function () {
|
||||
options.data.blog = {facebook: 'hey'};
|
||||
|
||||
helpers.facebook_url.call({facebook: 'you/there'}, options).should.equal('https://www.facebook.com/you/there');
|
||||
});
|
||||
|
||||
it('should output the facebook url for the provided username when it is explicitly passed in', function () {
|
||||
options.data.blog = {facebook: 'hey'};
|
||||
|
||||
helpers.facebook_url.call({facebook: 'you/there'}, 'i/see/you/over/there', options)
|
||||
.should.equal('https://www.facebook.com/i/see/you/over/there');
|
||||
});
|
||||
|
||||
it('should return null if there are no facebook usernames', function () {
|
||||
should.equal(helpers.facebook_url(options), null);
|
||||
});
|
||||
});
|
||||
|
48
core/test/unit/server_helpers/twitter_url_spec.js
Normal file
48
core/test/unit/server_helpers/twitter_url_spec.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*globals describe, before, beforeEach, it*/
|
||||
var should = require('should'),
|
||||
hbs = require('express-hbs'),
|
||||
utils = require('./utils'),
|
||||
|
||||
// Stuff we are testing
|
||||
handlebars = hbs.handlebars,
|
||||
helpers = require('../../../server/helpers');
|
||||
|
||||
describe('{{twitter_url}} helper', function () {
|
||||
var options = {data: {blog: {}}};
|
||||
|
||||
before(function () {
|
||||
utils.loadHelpers();
|
||||
});
|
||||
|
||||
beforeEach(function () {
|
||||
options.data.blog = {twitter: ''};
|
||||
});
|
||||
|
||||
it('has loaded twitter_url helper', function () {
|
||||
should.exist(handlebars.helpers.twitter_url);
|
||||
});
|
||||
|
||||
it('should output the twitter url for @blog, if no other twitter username is provided', function () {
|
||||
options.data.blog = {twitter: '@hey'};
|
||||
|
||||
helpers.twitter_url.call({}, options).should.equal('https://twitter.com/hey');
|
||||
});
|
||||
|
||||
it('should output the twitter url for the local object, if it has one', function () {
|
||||
options.data.blog = {twitter: '@hey'};
|
||||
|
||||
helpers.twitter_url.call({twitter: '@youthere'}, options).should.equal('https://twitter.com/youthere');
|
||||
});
|
||||
|
||||
it('should output the twitter url for the provided username when it is explicitly passed in', function () {
|
||||
options.data.blog = {twitter: '@hey'};
|
||||
|
||||
helpers.twitter_url.call({twitter: '@youthere'}, '@iseeyouoverthere', options)
|
||||
.should.equal('https://twitter.com/iseeyouoverthere');
|
||||
});
|
||||
|
||||
it('should return null if there are no twitter usernames', function () {
|
||||
should.equal(helpers.twitter_url(options), null);
|
||||
});
|
||||
});
|
||||
|
39
core/test/unit/social-urls_spec.js
Normal file
39
core/test/unit/social-urls_spec.js
Normal file
|
@ -0,0 +1,39 @@
|
|||
/*globals describe, it*/
|
||||
var should = require('should'),
|
||||
|
||||
// Stuff we are testing
|
||||
socialUrls = require('../../server/utils/social-urls');
|
||||
|
||||
describe('Social Urls', function () {
|
||||
it('should have a twitter url function', function () {
|
||||
should.exist(socialUrls.twitterUrl);
|
||||
});
|
||||
|
||||
it('should have a facebook url function', function () {
|
||||
should.exist(socialUrls.facebookUrl);
|
||||
});
|
||||
|
||||
describe('twitter', function () {
|
||||
it('should return a correct concatenated URL', function () {
|
||||
socialUrls.twitterUrl('myusername').should.eql('https://twitter.com/myusername');
|
||||
});
|
||||
|
||||
it('should return a url without an @ sign if one is provided', function () {
|
||||
socialUrls.twitterUrl('@myusername').should.eql('https://twitter.com/myusername');
|
||||
});
|
||||
});
|
||||
|
||||
describe('facebook', function () {
|
||||
it('should return a correct concatenated URL', function () {
|
||||
socialUrls.facebookUrl('myusername').should.eql('https://www.facebook.com/myusername');
|
||||
});
|
||||
|
||||
it('should return a correct concatenated URL for usernames with slashes', function () {
|
||||
socialUrls.facebookUrl('page/xxx/123').should.eql('https://www.facebook.com/page/xxx/123');
|
||||
});
|
||||
|
||||
it('should return a correct concatenated URL for usernames which start with a slash', function () {
|
||||
socialUrls.facebookUrl('/page/xxx/123').should.eql('https://www.facebook.com/page/xxx/123');
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue