Skip to content

Commit

Permalink
add querystring to options; add JetBrains project dir to .gitignore;
Browse files Browse the repository at this point in the history
  • Loading branch information
azurelogic committed Apr 2, 2015
1 parent 291ec40 commit 763abca
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ npm-debug.log
node_modules

build/

.idea/
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,51 @@ Below is a list of options that are supported:
Sets the URL prefix for Gravatar images. Useful if you are serving a page via SSL and want to also
load the external images via SSL.

### querystring

`String` or `Object`

Adds a query string to all Gravatar URLs to all for modifications to the requested image, including
image size, default image, and max rating (g, pg, r, x). The query string parameters are detailed
on [Gravatar's website](https://en.gravatar.com/site/implement/images/). Here is the string version:

```json
{
"plugins": {
"metalsmith-gravatar": {
"options": {
"querystring": "s=200&r=pg"
},
"avatars": {
"stevenschobert": "spschobert@gmail.com",
"authors": { ... }
}
}
}
}
```

Here is the object version

```json
{
"plugins": {
"metalsmith-gravatar": {
"options": {
"querystring": {
"s": 200,
"r": "pg"
}
},
"avatars": {
"stevenschobert": "spschobert@gmail.com",
"authors": { ... }
}
}
}
}
```

## Credits

Thanks to [Segment.io](http://github.com/segmentio) for creating and open-sourcing
Expand Down
18 changes: 12 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
'use strict';

var md5 = require('md5'),
qs = require('querystring'),

GRAVATAR_URL = 'www.gravatar.com/avatar',

assembleUrl = function assembleUrl(protocol, hash) {
return protocol + '://' + GRAVATAR_URL + '/' + hash;
assembleUrl = function assembleUrl(protocol, hash, querystring) {
return protocol + '://' + GRAVATAR_URL + '/' + hash + (querystring ? "?" + querystring : '');
},

gravatarForEmail = function gravatarForEmail(protocol, email) {
return assembleUrl(protocol, md5.digest_s(email));
gravatarForEmail = function gravatarForEmail(protocol, email, querystring) {
return assembleUrl(protocol, md5.digest_s(email), querystring);
},

mutateStringsInObject = function mutateStringsInObject(obj, mutator) {
Expand All @@ -33,7 +34,8 @@

plugin = function plugin(args) {
var options = {
protocol: 'http'
protocol: 'http',
querystring: ''
},
avatars = args || {};

Expand All @@ -47,8 +49,12 @@
return function(files, metalsmith, done) {
var metadata = metalsmith.metadata();

if (options.querystring && typeof options.querystring === 'object') {
options.querystring = qs.stringify(options.querystring);
}

metadata.gravatar = mutateStringsInObject(avatars, function(email) {
return gravatarForEmail(options.protocol, email);
return gravatarForEmail(options.protocol, email, options.querystring);
});

done();
Expand Down
47 changes: 44 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
Metalsmith = require('metalsmith'),
gravatar = require('..'),

assembleUrl = function assembleUrl(protocol, url, hash) {
return protocol + '://' + url + '/' + hash;
assembleUrl = function assembleUrl(protocol, url, hash, querystring) {
return protocol + '://' + url + '/' + hash + (querystring ? '?' + querystring : '');
};

describe('the plugin', function() {
Expand All @@ -15,7 +15,9 @@
testHash = 'bc13eedc2642303b1a2251a4da7f157e',
defaultProtocol = 'http',
secureProtocol = 'https',
expected = assembleUrl(defaultProtocol, baseUrl, testHash);
testQuerystring = 's=200&r=pg',
expected = assembleUrl(defaultProtocol, baseUrl, testHash),
expectedWithQuerystring = assembleUrl(defaultProtocol, baseUrl, testHash, testQuerystring);

it('should convert email addresses to urls', function(testDone) {
new Metalsmith(__dirname)
Expand Down Expand Up @@ -63,6 +65,45 @@
})
.build(testDone);
});
it('should append the query string when given a string', function(testDone) {
new Metalsmith(__dirname)
.use(gravatar({
options: {
querystring: 's=200&r=pg'
},
avatars: {
test: testEmail
}
}))
.use(function(files, metalsmith, done) {
assert.equal(metalsmith.metadata().gravatar.options, undefined);
assert.equal(metalsmith.metadata().gravatar.emails, undefined);
assert.equal(metalsmith.metadata().gravatar.test, expectedWithQuerystring);
done();
})
.build(testDone);
});
it('should construct the query string when given an object', function(testDone) {
new Metalsmith(__dirname)
.use(gravatar({
options: {
querystring: {
s: 200,
r: 'pg'
}
},
avatars: {
test: testEmail
}
}))
.use(function(files, metalsmith, done) {
assert.equal(metalsmith.metadata().gravatar.options, undefined);
assert.equal(metalsmith.metadata().gravatar.emails, undefined);
assert.equal(metalsmith.metadata().gravatar.test, expectedWithQuerystring);
done();
})
.build(testDone);
});
});

describe('the protocol option', function() {
Expand Down

0 comments on commit 763abca

Please sign in to comment.