Compare commits
13
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e3278c708 | ||
|
|
223bd659ab | ||
|
|
c072662cda | ||
|
|
12fd842fce | ||
|
|
137a9f2038 | ||
|
|
9083ae3e49 | ||
|
|
43c49de37f | ||
|
|
4292212b30 | ||
|
|
6f6a7f2763 | ||
|
|
a27d977c7d | ||
|
|
86594ee1b8 | ||
|
|
8d982d59c6 | ||
|
|
2f91bd5222 |
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2020 Anurag Hazra
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+19
-12
@@ -1,7 +1,10 @@
|
||||
const axios = require("axios");
|
||||
const { renderError, kFormatter } = require("../utils");
|
||||
require("dotenv").config();
|
||||
|
||||
async function fetchStats(username) {
|
||||
if (!username) throw Error("Invalid username");
|
||||
|
||||
const res = await axios({
|
||||
url: "https://api.github.com/graphql",
|
||||
method: "post",
|
||||
@@ -49,7 +52,11 @@ async function fetchStats(username) {
|
||||
totalStars: 0,
|
||||
contributedTo: 0,
|
||||
};
|
||||
if (res.data.error) return stats;
|
||||
|
||||
if (res.data.errors) {
|
||||
console.log(res.data.errors);
|
||||
throw Error("Could not fetch user");
|
||||
}
|
||||
|
||||
const user = res.data.data.user;
|
||||
|
||||
@@ -67,12 +74,11 @@ async function fetchStats(username) {
|
||||
}
|
||||
|
||||
const createTextNode = (icon, label, value, lheight) => {
|
||||
const classname = icon === "★" && "star-icon";
|
||||
return `
|
||||
<tspan x="25" dy="${lheight}" class="stat bold">
|
||||
<tspan class="icon ${
|
||||
icon === "★" && "star-icon"
|
||||
}" fill="#4C71F2">${icon}</tspan> ${label}:</tspan>
|
||||
<tspan x="155" dy="0" class="stat">${value}</tspan>
|
||||
<tspan class="icon ${classname}" fill="#4C71F2">${icon}</tspan> ${label}:</tspan>
|
||||
<tspan x="155" dy="0" class="stat">${kFormatter(value)}</tspan>
|
||||
`;
|
||||
};
|
||||
|
||||
@@ -129,15 +135,16 @@ const renderSVG = (stats, options) => {
|
||||
};
|
||||
|
||||
module.exports = async (req, res) => {
|
||||
const username = req.query.username;
|
||||
const hide = req.query.hide;
|
||||
const hide_border = req.query.hide_border;
|
||||
const show_icons = req.query.show_icons;
|
||||
const line_height = req.query.line_height;
|
||||
|
||||
const stats = await fetchStats(username);
|
||||
const { username, hide, hide_border, show_icons, line_height } = req.query;
|
||||
let stats;
|
||||
|
||||
res.setHeader("Content-Type", "image/svg+xml");
|
||||
try {
|
||||
stats = await fetchStats(username);
|
||||
} catch (err) {
|
||||
return res.send(renderError(err.message));
|
||||
}
|
||||
|
||||
res.send(
|
||||
renderSVG(stats, {
|
||||
hide: JSON.parse(hide || "[]"),
|
||||
|
||||
+51
-44
@@ -1,12 +1,7 @@
|
||||
const axios = require("axios");
|
||||
const { renderError, kFormatter, encodeHTML } = require("../utils");
|
||||
require("dotenv").config();
|
||||
|
||||
function kFormatter(num) {
|
||||
return Math.abs(num) > 999
|
||||
? Math.sign(num) * (Math.abs(num) / 1000).toFixed(1) + "k"
|
||||
: Math.sign(num) * Math.abs(num);
|
||||
}
|
||||
|
||||
async function fetchRepo(username, reponame) {
|
||||
const res = await axios({
|
||||
url: "https://api.github.com/graphql",
|
||||
@@ -16,35 +11,28 @@ async function fetchRepo(username, reponame) {
|
||||
},
|
||||
data: {
|
||||
query: `
|
||||
fragment RepoInfo on Repository {
|
||||
name
|
||||
stargazers {
|
||||
totalCount
|
||||
}
|
||||
description
|
||||
primaryLanguage {
|
||||
color
|
||||
id
|
||||
name
|
||||
}
|
||||
forkCount
|
||||
}
|
||||
query getRepo($login: String!, $repo: String!) {
|
||||
user(login: $login) {
|
||||
repository(name: $repo) {
|
||||
name
|
||||
stargazers {
|
||||
totalCount
|
||||
}
|
||||
description
|
||||
primaryLanguage {
|
||||
color
|
||||
id
|
||||
name
|
||||
}
|
||||
forkCount
|
||||
...RepoInfo
|
||||
}
|
||||
}
|
||||
organization(login: $login) {
|
||||
repository(name: $repo) {
|
||||
name
|
||||
stargazers {
|
||||
totalCount
|
||||
}
|
||||
description
|
||||
primaryLanguage {
|
||||
color
|
||||
id
|
||||
name
|
||||
}
|
||||
forkCount
|
||||
...RepoInfo
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -56,23 +44,39 @@ async function fetchRepo(username, reponame) {
|
||||
},
|
||||
});
|
||||
|
||||
if (res.data.error && res.data.error.type !== "NOT_FOUND") return {};
|
||||
if (!res.data.data.organization && res.data.data.user)
|
||||
return res.data.data.user.repository;
|
||||
const data = res.data.data;
|
||||
|
||||
const isOrg = res.data.data.organization && !res.data.data.user;
|
||||
if (isOrg) return res.data.data.organization.repository;
|
||||
if (!data.user && !data.organization) {
|
||||
throw new Error("Not found");
|
||||
}
|
||||
|
||||
return res.data.data.user.repository;
|
||||
if (data.organization === null && data.user) {
|
||||
if (!data.user.repository) {
|
||||
throw new Error("User Repository Not found");
|
||||
}
|
||||
return data.user.repository;
|
||||
}
|
||||
|
||||
if (data.user === null && data.organization) {
|
||||
if (!data.organization.repository) {
|
||||
throw new Error("Organization Repository Not found");
|
||||
}
|
||||
return data.organization.repository;
|
||||
}
|
||||
}
|
||||
|
||||
const renderRepoCard = (repo) => {
|
||||
const { name, description, primaryLanguage, stargazers, forkCount } = repo;
|
||||
const height = 120;
|
||||
|
||||
const shiftText = primaryLanguage.name.length > 15 ? 0 : 30;
|
||||
|
||||
let desc = description || "No description provided";
|
||||
if (desc.length > 55) {
|
||||
desc = `${description.slice(0, 55)}..`;
|
||||
}
|
||||
|
||||
return `
|
||||
<svg width="400" height="${height}" viewBox="0 0 400 ${height}" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||
<svg width="400" height="${height}" viewBox="0 0 400 ${height}" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||
<style>
|
||||
.header { font: 600 18px 'Segoe UI', Ubuntu, Sans-Serif; fill: #2F80ED }
|
||||
.stat { font: 600 14px 'Segoe UI', Ubuntu, Sans-Serif; fill: #333 }
|
||||
@@ -87,11 +91,8 @@ const renderRepoCard = (repo) => {
|
||||
</svg>
|
||||
|
||||
<text x="50" y="38" class="header">${name}</text>
|
||||
<text class="description" x="25" y="70">${
|
||||
description ? description.slice(0, 60) : "No description provided"
|
||||
}..</text>
|
||||
<text class="description" x="25" y="70">${encodeHTML(desc)}</text>
|
||||
|
||||
|
||||
<g transform="translate(30, 100)">
|
||||
<circle cx="0" cy="-5" r="6" fill="${primaryLanguage.color}" />
|
||||
<text class="gray" x="15">${primaryLanguage.name}</text>
|
||||
@@ -115,11 +116,17 @@ const renderRepoCard = (repo) => {
|
||||
};
|
||||
|
||||
module.exports = async (req, res) => {
|
||||
const username = req.query.username;
|
||||
const repo = req.query.repo;
|
||||
|
||||
const repoData = await fetchRepo(username, repo);
|
||||
const { username, repo } = req.query;
|
||||
|
||||
let repoData;
|
||||
res.setHeader("Content-Type", "image/svg+xml");
|
||||
|
||||
try {
|
||||
repoData = await fetchRepo(username, repo);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
return res.send(renderError(err.message));
|
||||
}
|
||||
|
||||
res.send(renderRepoCard(repoData));
|
||||
};
|
||||
|
||||
@@ -1,25 +1,26 @@
|
||||
## Github Readme stats
|
||||
|
||||
Get dynamically generated github stats on your readmes!
|
||||
Get dynamically generated GitHub stats on your readmes!
|
||||
|
||||
# Features
|
||||
|
||||
- [Github Stats Card](#github-stats-card)
|
||||
- [Github Extra Pins](#github-extra-pins)
|
||||
- [Deploy Yourself](#deploy-on-your-own-vercel-instance)
|
||||
|
||||
# Github Stats Card
|
||||
|
||||
Copy paste this into your markdown content and thats it. simple!
|
||||
Copy paste this into your markdown content, and that's it. Simple!
|
||||
|
||||
change the `?username=` value to your GitHubs's username
|
||||
|
||||
```md
|
||||

|
||||
[](https://github.com/anuraghazra/github-readme-stats)
|
||||
```
|
||||
|
||||
### Hiding certain stats
|
||||
### Hiding individual stats
|
||||
|
||||
To hide any specific stats you can pass a query parameter `?hide=` with an array of items you wanna hide.
|
||||
To hide any specific stats, you can pass a query parameter `?hide=` with an array of items, you wanna hide.
|
||||
|
||||
> Options: `&hide=["stars","prs","issues","contribs"]`
|
||||
|
||||
@@ -29,7 +30,7 @@ To hide any specific stats you can pass a query parameter `?hide=` with an array
|
||||
|
||||
### Showing icons
|
||||
|
||||
To enable icons you can pass `show_icons=true` in the query param like so
|
||||
To enable icons, you can pass `show_icons=true` in the query param like so
|
||||
|
||||
```md
|
||||

|
||||
@@ -56,13 +57,13 @@ Other options:
|
||||
|
||||
# Github Extra Pins
|
||||
|
||||
Github extra pins allows you to pin more than 6 repositories in your profile by using github readme profile.
|
||||
Github extra pins allow you to pin more than 6 repositories in your profile using a GitHub readme profile.
|
||||
|
||||
Yey! you are no longer limited to 6 pinned repos
|
||||
Yey! you are no longer limited to 6 pinned repositories.
|
||||
|
||||
### Usage
|
||||
|
||||
Copy paste this code into your readme and change the links.
|
||||
Copy-paste this code into your readme and change the links.
|
||||
|
||||
Endpoint: `api/pin?username=anuraghazra&repo=github-readme-stats`
|
||||
|
||||
@@ -76,7 +77,7 @@ Endpoint: `api/pin?username=anuraghazra&repo=github-readme-stats`
|
||||
|
||||
### Quick Tip (Align The Repo Cards)
|
||||
|
||||
Normally you won't be able to layout the images side by side to do that you can use this approach
|
||||
You usually won't be able to layout the images side by side to do that you can use this approach
|
||||
|
||||
```md
|
||||
<a href="https://github.com/anuraghazra/github-readme-stats">
|
||||
@@ -87,6 +88,15 @@ Normally you won't be able to layout the images side by side to do that you can
|
||||
</a>
|
||||
```
|
||||
|
||||
## Deploy on your own vercel instance
|
||||
|
||||
Since Github API only allows 5k requests per hour it is possible that my `https://github-readme-stats.vercel.app/api` could hit the rate limiter thats why if you want to host it on your own vercel server then you don't have to worry about anything. click on the deploy button to get started
|
||||
|
||||
[](https://vercel.com/import/project?template=https://github.com/anuraghazra/github-readme-stats)
|
||||
|
||||
Make sure to add your own `GITHUB_TOKEN` in `Environment Variables`
|
||||
[Github Docs: Creating a github personal token](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token)
|
||||
|
||||
Contributions are welcomed! <3
|
||||
|
||||
Made with :heart: and javascript.
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
const renderError = (message) => {
|
||||
return `
|
||||
<svg width="495" height="100" viewBox="0 0 495 100" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||
<style>
|
||||
.text { font: 600 16px 'Segoe UI', Ubuntu, Sans-Serif; fill: #2F80ED }
|
||||
.small { font: 600 12px 'Segoe UI', Ubuntu, Sans-Serif; fill: #252525 }
|
||||
</style>
|
||||
<rect x="0.5" y="0.5" width="494" height="99%" rx="4.5" fill="#FFFEFE" stroke="#E4E2E2"/>
|
||||
<text x="25" y="45" class="text">Something went wrong! file an issue at https://git.io/JJmN9</text>
|
||||
<text x="25" y="65" class="text small">${message}</text>
|
||||
</svg>
|
||||
`;
|
||||
};
|
||||
|
||||
// https://stackoverflow.com/a/48073476/10629172
|
||||
function encodeHTML(str) {
|
||||
return str.replace(/[\u00A0-\u9999<>&](?!#)/gim, function (i) {
|
||||
return "&#" + i.charCodeAt(0) + ";";
|
||||
});
|
||||
}
|
||||
|
||||
function kFormatter(num) {
|
||||
return Math.abs(num) > 999
|
||||
? Math.sign(num) * (Math.abs(num) / 1000).toFixed(1) + "k"
|
||||
: Math.sign(num) * Math.abs(num);
|
||||
}
|
||||
|
||||
module.exports = { renderError, kFormatter, encodeHTML };
|
||||
Reference in New Issue
Block a user