-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtopmate.js
45 lines (39 loc) · 1.43 KB
/
topmate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const base64Img = require('base64-img'),
axios = require('axios'),
logo = base64Img.base64Sync('./public/images/topmate.png'),
// default config for badge
{
DEFAULT_USERNAME,
DEFAULT_PROFILE_URL,
TOPMATE_BASE_URL,
BADGE_OPTIONS,
} = require('../config'),
{ COLOR_A, COLOR_B, DEFAULT_STYLE, BASE_URL } = BADGE_OPTIONS;
(getProfileUrl = (username) => {
if (!username) return DEFAULT_PROFILE_URL;
const profileURL = `${TOPMATE_BASE_URL}/${username}`;
return profileURL;
}),
(downloadBadge = async (badgeURL) => {
const response = await axios.get(badgeURL),
svgXml = response.data;
return svgXml;
});
module.exports = {
/**
*
* Generate a badge for your Topmate profile
* Sample profile: https://topmate.io/vinitshahdeo
*
* @param {string} username - topmate username like `vinitshahdeo`
* @param {style} style - possible values are `flat`, `flat-square`, `plastic`, `for-the-badge`, `social`
* @returns Topmate README Badge
*/
generateBadge: (username = DEFAULT_USERNAME, style = DEFAULT_STYLE) => {
const profileUrl = getProfileUrl(username),
sanitizedUsername = username.replace(/[^a-zA-Z0-9 ]/g, ''), // remove special chars otherwise shields will throw an error
qs = `link=${profileUrl}&logo=${logo}&colorA=${COLOR_A}&colorB=${COLOR_B}&style=${style}`,
badgeURL = `${BASE_URL}-${sanitizedUsername}-critical?${qs}`;
return downloadBadge(badgeURL);
},
};