Skip to content

Commit c2adcfd

Browse files
feat: hide specific languages in "Top languages" card (anuraghazra#150)
* add new query param to hide specific languages in top languages card * [top-langs] add function to clean out the provided lang name * [top-langs] rename 'hide_lang' => 'hide', refactor logic for parsing the list of provided languages to hide * [top-langs] take list of languages to hide, as a json array * chore: minor changes * docs: added docs for hide lang Co-authored-by: anuraghazra <[email protected]>
1 parent 17c33cd commit c2adcfd

File tree

6 files changed

+64
-34
lines changed

6 files changed

+64
-34
lines changed

api/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ require("dotenv").config();
22
const {
33
renderError,
44
parseBoolean,
5+
parseJSON,
56
clampValue,
67
CONSTANTS,
78
} = require("../src/utils");
@@ -44,7 +45,7 @@ module.exports = async (req, res) => {
4445

4546
res.send(
4647
renderStatsCard(stats, {
47-
hide: JSON.parse(hide || "[]"),
48+
hide: parseJSON(hide),
4849
show_icons: parseBoolean(show_icons),
4950
hide_title: parseBoolean(hide_title),
5051
hide_border: parseBoolean(hide_border),

api/top-langs.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const {
33
renderError,
44
clampValue,
55
parseBoolean,
6+
parseJSON,
67
CONSTANTS,
78
} = require("../src/utils");
89
const fetchTopLanguages = require("../src/fetchTopLanguages");
@@ -11,7 +12,7 @@ const renderTopLanguages = require("../src/renderTopLanguages");
1112
module.exports = async (req, res) => {
1213
const {
1314
username,
14-
hide_langs_below,
15+
hide,
1516
hide_title,
1617
card_width,
1718
title_color,
@@ -43,7 +44,7 @@ module.exports = async (req, res) => {
4344
theme,
4445
hide_title: parseBoolean(hide_title),
4546
card_width: parseInt(card_width, 10),
46-
hide_langs_below: parseFloat(hide_langs_below, 10),
47+
hide: parseJSON(hide),
4748
title_color,
4849
text_color,
4950
bg_color,

readme.md

+18-18
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,21 @@ You can customize the appearance of your `Stats Card` or `Repo Card` however you
9797

9898
Customization Options:
9999

100-
| Option | type | description | Stats Card (default) | Repo Card (default) | Top Lang Card (default) |
101-
| ---------------- | --------- | ---------------------------------------------- | -------------------- | ------------------- | ----------------------- |
102-
| title_color | hex color | title color | 2f80ed | 2f80ed | 2f80ed |
103-
| text_color | hex color | body color | 333 | 333 | 333 |
104-
| icon_color | hex color | icon color | 4c71f2 | 586069 | 586069 |
105-
| bg_color | hex color | card bg color | FFFEFE | FFFEFE | FFFEFE |
106-
| line_height | number | control the line-height between text | 30 | N/A | N/A |
107-
| hide_rank | boolean | hides the ranking | false | N/A | N/A |
108-
| hide_title | boolean | hides the stats title | false | N/A | false |
109-
| hide_border | boolean | hides the stats card border | false | N/A | N/A |
110-
| show_owner | boolean | shows owner name in repo card | N/A | false | N/A |
111-
| show_icons | boolean | shows icons | false | N/A | N/A |
112-
| theme | string | sets inbuilt theme | 'default' | 'default_repocard' | 'default |
113-
| cache_seconds | number | manually set custom cache control | 1800 | 1800 | '1800' |
114-
| hide_langs_below | number | hide langs below certain threshold (lang card) | N/A | N/A | undefined |
100+
| Option | type | description | Stats Card (default) | Repo Card (default) | Top Lang Card (default) |
101+
| ------------- | ---------- | -------------------------------------- | -------------------- | ------------------- | ----------------------- |
102+
| title_color | hex color | title color | 2f80ed | 2f80ed | 2f80ed |
103+
| text_color | hex color | body color | 333 | 333 | 333 |
104+
| icon_color | hex color | icon color | 4c71f2 | 586069 | 586069 |
105+
| bg_color | hex color | card bg color | FFFEFE | FFFEFE | FFFEFE |
106+
| line_height | number | control the line-height between text | 30 | N/A | N/A |
107+
| hide | JSON array | hides the items specified on the array | [] | N/A | [] |
108+
| hide_rank | boolean | hides the ranking | false | N/A | N/A |
109+
| hide_title | boolean | hides the stats title | false | N/A | false |
110+
| hide_border | boolean | hides the stats card border | false | N/A | N/A |
111+
| show_owner | boolean | shows owner name in repo card | N/A | false | N/A |
112+
| show_icons | boolean | shows icons | false | N/A | N/A |
113+
| theme | string | sets inbuilt theme | 'default' | 'default_repocard' | 'default |
114+
| cache_seconds | number | manually set custom cache control | 1800 | 1800 | '1800' |
115115

116116
> Note on cache: Repo cards have default cache of 30mins (1800 seconds) if the fork count & star count is less than 1k otherwise it's 2hours (7200). Also note that cache is clamped to minimum of 30min and maximum of 24hours
117117
@@ -155,12 +155,12 @@ Endpoint: `api/top-langs?username=anuraghazra`
155155
[![Top Langs](https://github-readme-stats.vercel.app/api/top-langs/?username=anuraghazra)](https://github.com/anuraghazra/github-readme-stats)
156156
```
157157

158-
### Hide languages below certain threshold
158+
### Hide individual languages
159159

160-
You can use `?hide_langs_below=NUMBER` parameter to hide languages below a specified threshold percentage.
160+
You can use `?hide=["language1","language2"]` parameter to hide languages below a specified threshold percentage.
161161

162162
```md
163-
[![Top Langs](https://github-readme-stats.vercel.app/api/top-langs/?username=anuraghazra&hide_langs_below=1)](https://github.com/anuraghazra/github-readme-stats)
163+
[![Top Langs](https://github-readme-stats.vercel.app/api/top-langs/?username=anuraghazra&hide=["javascript","html"])](https://github.com/anuraghazra/github-readme-stats)
164164
```
165165

166166
### Demo

src/renderTopLanguages.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,41 @@ const createProgressNode = ({ width, color, name, progress }) => {
2323
`;
2424
};
2525

26+
const lowercaseTrim = (name) => name.toLowerCase().trim();
27+
2628
const renderTopLanguages = (topLangs, options = {}) => {
2729
const {
2830
hide_title,
2931
card_width,
3032
title_color,
3133
text_color,
3234
bg_color,
33-
hide_langs_below,
35+
hide,
3436
theme,
3537
} = options;
3638

3739
let langs = Object.values(topLangs);
40+
let langsToHide = {};
3841

39-
const totalSize = langs.reduce((acc, curr) => {
40-
return acc + curr.size;
41-
}, 0);
42+
// populate langsToHide map for quick lookup
43+
// while filtering out
44+
if (hide) {
45+
hide.forEach((langName) => {
46+
langsToHide[lowercaseTrim(langName)] = true;
47+
});
48+
}
4249

43-
// hide langs
50+
// filter out langauges to be hidden
4451
langs = langs
4552
.sort((a, b) => b.size - a.size)
4653
.filter((lang) => {
47-
if (!hide_langs_below) return true;
48-
return (lang.size / totalSize) * 100 > hide_langs_below;
54+
return !langsToHide[lowercaseTrim(lang.name)];
4955
});
5056

57+
const totalSize = langs.reduce((acc, curr) => {
58+
return acc + curr.size;
59+
}, 0);
60+
5161
// returns theme based colors with proper overrides and defaults
5262
const { titleColor, textColor, bgColor } = getCardColors({
5363
title_color,

src/utils.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ function parseBoolean(value) {
4444
}
4545
}
4646

47+
function parseJSON(str) {
48+
try {
49+
return JSON.parse(str);
50+
} catch (err) {
51+
return [];
52+
}
53+
}
54+
4755
function clampValue(number, min, max) {
4856
return Math.max(min, Math.min(number, max));
4957
}
@@ -118,7 +126,8 @@ function getCardColors({
118126

119127
const fn = () => {};
120128
// return console instance based on the environment
121-
const logger = process.env.NODE_ENV !== "test" ? console : { log: fn, error: fn };
129+
const logger =
130+
process.env.NODE_ENV !== "test" ? console : { log: fn, error: fn };
122131

123132
const CONSTANTS = {
124133
THIRTY_MINUTES: 1800,
@@ -132,6 +141,7 @@ module.exports = {
132141
encodeHTML,
133142
isValidHexColor,
134143
request,
144+
parseJSON,
135145
parseBoolean,
136146
fallbackColor,
137147
FlexLayout,

tests/renderTopLanguages.test.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,26 @@ describe("Test renderTopLanguages", () => {
5858
);
5959
});
6060

61-
it("should hide_langs_below", () => {
61+
it("should hide languages when hide is passed", () => {
6262
document.body.innerHTML = renderTopLanguages(langs, {
63-
hide_langs_below: 34,
63+
hide: ["HTML"],
6464
});
65-
6665
expect(queryAllByTestId(document.body, "lang-name")[0]).toBeInTheDocument(
67-
"HTML"
66+
"javascript"
6867
);
6968
expect(queryAllByTestId(document.body, "lang-name")[1]).toBeInTheDocument(
70-
"javascript"
69+
"css"
7170
);
7271
expect(queryAllByTestId(document.body, "lang-name")[2]).not.toBeDefined();
72+
73+
// multiple languages passed
74+
document.body.innerHTML = renderTopLanguages(langs, {
75+
hide: ["HTML","css"],
76+
});
77+
expect(queryAllByTestId(document.body, "lang-name")[0]).toBeInTheDocument(
78+
"javascript"
79+
);
80+
expect(queryAllByTestId(document.body, "lang-name")[1]).not.toBeDefined();
7381
});
7482

7583
it("should resize the height correctly depending on langs", () => {

0 commit comments

Comments
 (0)