From d4e2a1b39582951d7c301eeec8a92a1edf33d75d Mon Sep 17 00:00:00 2001 From: Bas950 Date: Sun, 27 Sep 2020 09:09:44 +0200 Subject: [PATCH] feat: added "exclude_repo" option to Top Langs (#493) * :rocket: Added "exclude_repo" option to Top Langs * chore: code style update Co-authored-by: Anurag --- api/top-langs.js | 7 ++++++- readme.md | 9 +++++++++ src/fetchers/top-languages-fetcher.js | 19 ++++++++++++++++++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/api/top-langs.js b/api/top-langs.js index 6dd7bbd..a247f7f 100644 --- a/api/top-langs.js +++ b/api/top-langs.js @@ -24,6 +24,7 @@ module.exports = async (req, res) => { cache_seconds, layout, langs_count, + exclude_repo, } = req.query; let topLangs; @@ -34,7 +35,11 @@ module.exports = async (req, res) => { } try { - topLangs = await fetchTopLanguages(username, langs_count); + topLangs = await fetchTopLanguages( + username, + langs_count, + parseArray(exclude_repo), + ); const cacheSeconds = clampValue( parseInt(cache_seconds || CONSTANTS.TWO_HOURS, 10), diff --git a/readme.md b/readme.md index 0ce2ad1..9ef53d3 100644 --- a/readme.md +++ b/readme.md @@ -168,6 +168,7 @@ You can provide multiple comma-separated values in bg_color option to render a g - `layout` - Switch between two available layouts `default` & `compact` - `card_width` - Set the card's width manually _(number)_ - `langs_count` - Show more languages on the card, between 1-10, defaults to 5 _(number)_ +- `exclude_repo` - Exclude specified repositories _(Comma-separated values)_ > :warning: **Important:** > Language names should be uri-escaped, as specified in [Percent Encoding](https://en.wikipedia.org/wiki/Percent-encoding) @@ -221,6 +222,14 @@ Endpoint: `api/top-langs?username=anuraghazra` [![Top Langs](https://github-readme-stats.vercel.app/api/top-langs/?username=anuraghazra)](https://github.com/anuraghazra/github-readme-stats) ``` +### Exclude individual repositories + +You can use `?exclude_repo=repo1,repo2` parameter to exclude individual repositories. + +```md +[![Top Langs](https://github-readme-stats.vercel.app/api/top-langs/?username=anuraghazra&exclude_repo=github-readme-stats,anuraghazra.github.io)](https://github.com/anuraghazra/github-readme-stats) +``` + ### Hide individual languages You can use `?hide=language1,language2` parameter to hide individual languages. diff --git a/src/fetchers/top-languages-fetcher.js b/src/fetchers/top-languages-fetcher.js index 2c90663..9002f21 100644 --- a/src/fetchers/top-languages-fetcher.js +++ b/src/fetchers/top-languages-fetcher.js @@ -11,6 +11,7 @@ const fetcher = (variables, token) => { # fetch only owner repos & not forks repositories(ownerAffiliations: OWNER, isFork: false, first: 100) { nodes { + name languages(first: 10, orderBy: {field: SIZE, direction: DESC}) { edges { size @@ -33,7 +34,7 @@ const fetcher = (variables, token) => { ); }; -async function fetchTopLanguages(username, langsCount = 5) { +async function fetchTopLanguages(username, langsCount = 5, exclude_repo = []) { if (!username) throw Error("Invalid username"); langsCount = clampValue(parseInt(langsCount), 1, 10); @@ -46,6 +47,22 @@ async function fetchTopLanguages(username, langsCount = 5) { } let repoNodes = res.data.data.user.repositories.nodes; + let repoToHide = {}; + + // populate repoToHide map for quick lookup + // while filtering out + if (exclude_repo) { + exclude_repo.forEach((repoName) => { + repoToHide[repoName] = true; + }); + } + + // filter out repositories to be hidden + repoNodes = repoNodes + .sort((a, b) => b.size - a.size) + .filter((name) => { + return !repoToHide[name.name]; + }); repoNodes = repoNodes .filter((node) => {