Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "exclude_repo" option to Top Langs #493

Merged
merged 3 commits into from
Sep 27, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion api/top-langs.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = async (req, res) => {
cache_seconds,
layout,
langs_count,
exclude_repo,
} = req.query;
let topLangs;

Expand All @@ -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),
Expand Down
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
21 changes: 19 additions & 2 deletions src/fetchers/top-languages-fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -45,7 +46,23 @@ async function fetchTopLanguages(username, langsCount = 5) {
throw Error(res.data.errors[0].message || "Could not fetch user");
}

let repoNodes = res.data.data.user.repositories.nodes;
let repoNodes = res.data.data.user.repositories.nodes,
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) => {
Expand Down