0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Author helpers

closes #3077

- expend urlFor to handle /author/ urls
- update author helper to output a link
This commit is contained in:
Hannah Wolfe 2014-07-20 17:41:59 +01:00
parent 2d01e15a18
commit b7aa09f439
4 changed files with 60 additions and 19 deletions

View file

@ -102,7 +102,7 @@ function urlPathForPost(post, permalinks) {
function urlFor(context, data, absolute) {
var urlPath = '/',
secure,
knownObjects = ['post', 'tag', 'user'],
knownObjects = ['post', 'tag', 'author'],
// this will become really big
knownPaths = {
@ -130,6 +130,9 @@ function urlFor(context, data, absolute) {
} else if (context === 'tag' && data.tag) {
urlPath = '/tag/' + data.tag.slug + '/';
secure = data.tag.secure;
} else if (context === 'author' && data.author) {
urlPath = '/author/' + data.author.slug + '/';
secure = data.author.secure;
}
// other objects are recognised but not yet supported
} else if (_.isString(context) && _.indexOf(_.keys(knownPaths), context) !== -1) {

View file

@ -193,8 +193,14 @@ function isTag(jsonData) {
jsonData.hasOwnProperty('description') && jsonData.hasOwnProperty('parent');
}
function isUser(jsonData) {
return jsonData.hasOwnProperty('bio') && jsonData.hasOwnProperty('website') &&
jsonData.hasOwnProperty('status') && jsonData.hasOwnProperty('location');
}
module.exports.tables = db;
module.exports.checks = {
isPost: isPost,
isTag: isTag
isTag: isTag,
isUser: isUser
};

View file

@ -154,6 +154,11 @@ coreHelpers.url = function (options) {
return when(config.urlFor('tag', {tag: this}, absolute));
}
if (schema.isUser(this)) {
return when(config.urlFor('author', {author: this}, absolute));
}
return when(config.urlFor(this, absolute));
};
@ -200,9 +205,26 @@ coreHelpers.asset = function (context, options) {
// Returns the full name of the author of a given post, or a blank string
// if the author could not be determined.
//
coreHelpers.author = function (context, options) {
coreHelpers.author = function (options) {
options = options || {};
options.hash = options.hash || {};
/*jshint unused:false*/
return this.author ? this.author.name : '';
var autolink = _.isString(options.hash.autolink) && options.hash.autolink === 'false' ? false : true,
output = '';
if (this.author && this.author.name) {
if (autolink) {
output = linkTemplate({
url: config.urlFor('author', {author: this.author}),
text: _.escape(this.author.name)
});
} else {
output = _.escape(this.author.name);
}
}
return new hbs.handlebars.SafeString(output);
};
// ### Tags Helper
@ -217,10 +239,13 @@ coreHelpers.author = function (context, options) {
// Note that the standard {{#each tags}} implementation is unaffected by this helper
// and can be used for more complex templates.
coreHelpers.tags = function (options) {
var autolink = _.isString(options.hash.autolink) && options.hash.autolink === "false" ? false : true,
separator = _.isString(options.hash.separator) ? options.hash.separator : ', ',
prefix = _.isString(options.hash.prefix) ? options.hash.prefix : '',
suffix = _.isString(options.hash.suffix) ? options.hash.suffix : '',
options = options || {};
options.hash = options.hash || {};
var autolink = options.hash && _.isString(options.hash.autolink) && options.hash.autolink === 'false' ? false : true,
separator = options.hash && _.isString(options.hash.separator) ? options.hash.separator : ', ',
prefix = options.hash && _.isString(options.hash.prefix) ? options.hash.prefix : '',
suffix = options.hash && _.isString(options.hash.suffix) ? options.hash.suffix : '',
output = '';
function createTagList(tags) {
@ -481,7 +506,7 @@ coreHelpers.ghost_foot = function (options) {
coreHelpers.meta_title = function (options) {
/*jshint unused:false*/
var title = "",
var title = '',
blog;
if (_.isString(this.relativeUrl)) {
@ -496,7 +521,7 @@ coreHelpers.meta_title = function (options) {
}
return filters.doFilter('meta_title', title).then(function (title) {
title = title || "";
title = title || '';
return title.trim();
});
};
@ -516,7 +541,7 @@ coreHelpers.meta_description = function (options) {
}
return filters.doFilter('meta_description', description).then(function (description) {
description = description || "";
description = description || '';
return description.trim();
});
};
@ -554,7 +579,7 @@ coreHelpers.foreach = function (context, options) {
j = 0,
columns = options.hash.columns,
key,
ret = "",
ret = '',
data;
if (options.data) {
@ -639,7 +664,7 @@ coreHelpers.has = function (options) {
}
if (!tagList) {
errors.logWarn("Invalid or no attribute given to has helper");
errors.logWarn('Invalid or no attribute given to has helper');
return;
}
@ -707,7 +732,7 @@ function registerAsyncHelper(hbs, name, fn) {
when.resolve(fn.call(this, options)).then(function (result) {
cb(result);
}).otherwise(function (err) {
errors.logAndThrowError(err, "registerAsyncThemeHelper: " + name);
errors.logAndThrowError(err, 'registerAsyncThemeHelper: ' + name);
});
});
}

View file

@ -179,13 +179,21 @@ describe('Core Helpers', function () {
should.exist(handlebars.helpers.author);
});
it('Returns the full name of the author from the context', function () {
var data = {'author': {'name': 'abc123'}},
it('Returns the link to the author from the context', function () {
var data = {'author': {'name': 'abc 123', slug: 'abc123', bio: '', website: '', status: '', location: ''}},
result = helpers.author.call(data);
String(result).should.equal('abc123');
String(result).should.equal('<a href="/author/abc123/">abc 123</a>');
});
it('Returns the full name of the author from the context if no autolink', function () {
var data = {'author': {'name': 'abc 123', slug: 'abc123'}},
result = helpers.author.call(data, {hash: {autolink: 'false'}});
String(result).should.equal('abc 123');
});
it('Returns a blank string where author data is missing', function () {
var data = {'author': null},
result = helpers.author.call(data);
@ -894,8 +902,7 @@ describe('Core Helpers', function () {
it('can autolink tags to tag pages', function () {
var tags = [{name: 'foo', slug: 'foo-bar'}, {name: 'bar', slug: 'bar'}],
rendered = handlebars.helpers.tags.call(
{tags: tags},
{'hash': {}}
{tags: tags}
);
should.exist(rendered);