hexo/node_modules/hexo-util/lib/gravatar.js

31 lines
631 B
JavaScript
Raw Normal View History

2023-09-25 15:58:56 +08:00
'use strict';
const { createHash } = require('crypto');
const { stringify } = require('querystring');
const Cache = require('./cache');
const cache = new Cache();
function md5(str) {
return createHash('md5').update(str).digest('hex');
}
function gravatarHelper(email, options) {
if (typeof options === 'number') {
options = {s: options};
}
const hash = cache.has(email) ? cache.get(email) : md5(email.toLowerCase());
let str = `https://www.gravatar.com/avatar/${hash}`;
const qs = stringify(options);
if (qs) str += `?${qs}`;
cache.set('email', hash);
return str;
}
module.exports = gravatarHelper;