Handle image resizing for users without an avatar. Fixes #14

This commit is contained in:
Jeroen Engels 2016-05-05 17:30:15 +02:00
parent afb8f4e287
commit 495157cc6a
2 changed files with 9 additions and 39 deletions

View file

@ -3,7 +3,7 @@
var _ = require('lodash/fp');
var formatContributionType = require('./format-contribution-type');
var avatarTemplate = _.template('![<%= contributor.name %>](<%= contributor.avatar_url %>)');
var avatarTemplate = _.template('<img src="<%= contributor.avatar_url %>" width="<%= options.imageSize %>px;"/>');
var avatarBlockTemplate = _.template('[<%= avatar %><br /><sub><%= contributor.name %></sub>](<%= contributor.profile %>)');
var contributorTemplate = _.template('<%= avatarBlock %><br /><%= contributions %>');
@ -15,15 +15,6 @@ function defaultTemplate(templateData) {
return contributorTemplate(_.assign({avatarBlock: avatarBlock}, templateData));
}
function updateAvatarUrl(options, contributor) {
var avatarUrl = contributor.avatar_url;
var paramJoiner = _.includes('?', avatarUrl) ? '&' : '?';
var imageSize = options.imageSize || defaultImageSize;
return _.assign(contributor, {
avatar_url: avatarUrl + paramJoiner + 's=' + imageSize
});
}
module.exports = function formatContributor(options, contributor) {
var formatter = _.partial(formatContributionType, [options, contributor]);
var contributions = contributor.contributions
@ -31,8 +22,8 @@ module.exports = function formatContributor(options, contributor) {
.join(' ');
var templateData = {
contributions: contributions,
contributor: updateAvatarUrl(options, contributor),
options: options
contributor: contributor,
options: _.assign({imageSize: defaultImageSize}, options)
};
var customTemplate = options.contributorTemplate && _.template(options.contributorTemplate);
return (customTemplate || defaultTemplate)(templateData);

View file

@ -16,7 +16,7 @@ test('should format a simple contributor', t => {
const contributor = _.assign(contributors.kentcdodds, {contributions: ['review']});
const {options} = fixtures();
const expected = '[![Kent C. Dodds](https://avatars1.githubusercontent.com/u/1500684?s=150)<br /><sub>Kent C. Dodds</sub>](http://kentcdodds.com)<br />👀';
const expected = '[<img src="https://avatars1.githubusercontent.com/u/1500684" width="150px;"/><br /><sub>Kent C. Dodds</sub>](http://kentcdodds.com)<br />👀';
t.is(formatContributor(options, contributor), expected);
});
@ -25,7 +25,7 @@ test('should format contributor with complex contribution types', t => {
const contributor = contributors.kentcdodds;
const {options} = fixtures();
const expected = '[![Kent C. Dodds](https://avatars1.githubusercontent.com/u/1500684?s=150)<br /><sub>Kent C. Dodds</sub>](http://kentcdodds.com)<br />[📖](https://github.com/jfmengels/all-contributors-cli/commits?author=kentcdodds) 👀 💁';
const expected = '[<img src="https://avatars1.githubusercontent.com/u/1500684" width="150px;"/><br /><sub>Kent C. Dodds</sub>](http://kentcdodds.com)<br />[📖](https://github.com/jfmengels/all-contributors-cli/commits?author=kentcdodds) 👀 💁';
t.is(formatContributor(options, contributor), expected);
});
@ -40,33 +40,12 @@ test('should format contributor using custom template', t => {
t.is(formatContributor(options, contributor), expected);
});
test('should add image size to url', t => {
const {options} = fixtures();
const contributor = contributors.kentcdodds;
options.contributorTemplate = '<%= contributor.name %> at <%= contributor.avatar_url %>';
var contributionWithoutQuestionMarkUrl = _.assign(contributor, {
avatar_url: 'www.some-url-without-question-mark.com'
});
var contributionWithQuestionMarkUrl = _.assign(contributor, {
avatar_url: 'www.some-url-with-question-mark.com?v=3'
});
t.is(formatContributor(options, contributionWithoutQuestionMarkUrl),
'Kent C. Dodds at www.some-url-without-question-mark.com?s=150'
);
t.is(formatContributor(options, contributionWithQuestionMarkUrl),
'Kent C. Dodds at www.some-url-with-question-mark.com?v=3&s=150'
);
});
test('should default image size to 100', t => {
const contributor = _.assign(contributors.kentcdodds, {contributions: ['review']});
const {options} = fixtures();
const contributor = contributors.kentcdodds;
options.contributorTemplate = '<%= contributor.name %> at <%= contributor.avatar_url %>';
delete options.imageSize;
t.is(formatContributor(options, contributor),
'Kent C. Dodds at https://avatars1.githubusercontent.com/u/1500684?s=100'
);
const expected = '[<img src="https://avatars1.githubusercontent.com/u/1500684" width="100px;"/><br /><sub>Kent C. Dodds</sub>](http://kentcdodds.com)<br />👀';
t.is(formatContributor(options, contributor), expected);
});