Unverified Commit d4e2a1b3 authored by Bas950's avatar Bas950 Committed by GitHub
Browse files

feat: added "exclude_repo" option to Top Langs (#493)

* 🚀

 Added "exclude_repo" option to Top Langs

* chore: code style update

Co-authored-by: default avatarAnurag <hazru.anurag@gmail.com>
parent 3443b379
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -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),
+9 −0
Original line number Diff line number Diff line
@@ -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.
+18 −1
Original line number Diff line number Diff line
@@ -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) => {