mirror of
https://github.com/Aviortheking/codestats-readme.git
synced 2025-04-22 10:42:08 +00:00
Merge pull request #21 from anuraghazra/fix-desc
fix: description trimming & htmlEncode
This commit is contained in:
commit
c072662cda
55
api/pin.js
55
api/pin.js
@ -1,5 +1,5 @@
|
|||||||
const axios = require("axios");
|
const axios = require("axios");
|
||||||
const { renderError, kFormatter } = require("../utils");
|
const { renderError, kFormatter, encodeHTML } = require("../utils");
|
||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
|
|
||||||
async function fetchRepo(username, reponame) {
|
async function fetchRepo(username, reponame) {
|
||||||
@ -11,35 +11,28 @@ async function fetchRepo(username, reponame) {
|
|||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
query: `
|
query: `
|
||||||
|
fragment RepoInfo on Repository {
|
||||||
|
name
|
||||||
|
stargazers {
|
||||||
|
totalCount
|
||||||
|
}
|
||||||
|
description
|
||||||
|
primaryLanguage {
|
||||||
|
color
|
||||||
|
id
|
||||||
|
name
|
||||||
|
}
|
||||||
|
forkCount
|
||||||
|
}
|
||||||
query getRepo($login: String!, $repo: String!) {
|
query getRepo($login: String!, $repo: String!) {
|
||||||
user(login: $login) {
|
user(login: $login) {
|
||||||
repository(name: $repo) {
|
repository(name: $repo) {
|
||||||
name
|
...RepoInfo
|
||||||
stargazers {
|
|
||||||
totalCount
|
|
||||||
}
|
|
||||||
description
|
|
||||||
primaryLanguage {
|
|
||||||
color
|
|
||||||
id
|
|
||||||
name
|
|
||||||
}
|
|
||||||
forkCount
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
organization(login: $login) {
|
organization(login: $login) {
|
||||||
repository(name: $repo) {
|
repository(name: $repo) {
|
||||||
name
|
...RepoInfo
|
||||||
stargazers {
|
|
||||||
totalCount
|
|
||||||
}
|
|
||||||
description
|
|
||||||
primaryLanguage {
|
|
||||||
color
|
|
||||||
id
|
|
||||||
name
|
|
||||||
}
|
|
||||||
forkCount
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -53,7 +46,6 @@ async function fetchRepo(username, reponame) {
|
|||||||
|
|
||||||
const data = res.data.data;
|
const data = res.data.data;
|
||||||
|
|
||||||
console.log(res.data);
|
|
||||||
if (!data.user && !data.organization) {
|
if (!data.user && !data.organization) {
|
||||||
throw new Error("Not found");
|
throw new Error("Not found");
|
||||||
}
|
}
|
||||||
@ -76,8 +68,13 @@ async function fetchRepo(username, reponame) {
|
|||||||
const renderRepoCard = (repo) => {
|
const renderRepoCard = (repo) => {
|
||||||
const { name, description, primaryLanguage, stargazers, forkCount } = repo;
|
const { name, description, primaryLanguage, stargazers, forkCount } = repo;
|
||||||
const height = 120;
|
const height = 120;
|
||||||
|
|
||||||
const shiftText = primaryLanguage.name.length > 15 ? 0 : 30;
|
const shiftText = primaryLanguage.name.length > 15 ? 0 : 30;
|
||||||
|
|
||||||
|
let desc = description || "No description provided";
|
||||||
|
if (desc.length > 55) {
|
||||||
|
desc = `${description.slice(0, 55)}..`;
|
||||||
|
}
|
||||||
|
|
||||||
return `
|
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>
|
<style>
|
||||||
@ -94,10 +91,7 @@ const renderRepoCard = (repo) => {
|
|||||||
</svg>
|
</svg>
|
||||||
|
|
||||||
<text x="50" y="38" class="header">${name}</text>
|
<text x="50" y="38" class="header">${name}</text>
|
||||||
<text class="description" x="25" y="70">${
|
<text class="description" x="25" y="70">${encodeHTML(desc)}</text>
|
||||||
description ? description.slice(0, 60) : "No description provided"
|
|
||||||
}..</text>
|
|
||||||
|
|
||||||
|
|
||||||
<g transform="translate(30, 100)">
|
<g transform="translate(30, 100)">
|
||||||
<circle cx="0" cy="-5" r="6" fill="${primaryLanguage.color}" />
|
<circle cx="0" cy="-5" r="6" fill="${primaryLanguage.color}" />
|
||||||
@ -122,8 +116,7 @@ const renderRepoCard = (repo) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
module.exports = async (req, res) => {
|
module.exports = async (req, res) => {
|
||||||
const username = req.query.username;
|
const { username, repo } = req.query;
|
||||||
const repo = req.query.repo;
|
|
||||||
|
|
||||||
let repoData;
|
let repoData;
|
||||||
res.setHeader("Content-Type", "image/svg+xml");
|
res.setHeader("Content-Type", "image/svg+xml");
|
||||||
|
@ -13,12 +13,6 @@ Copy paste this into your markdown content, and that's it. Simple!
|
|||||||
|
|
||||||
change the `?username=` value to your GitHubs's username
|
change the `?username=` value to your GitHubs's username
|
||||||
|
|
||||||
```md
|
|
||||||

|
|
||||||
```
|
|
||||||
|
|
||||||
_Psst, you can also use this code so that `github-readme-stats` gets proper credit :D and other people can also try it out!_
|
|
||||||
|
|
||||||
```md
|
```md
|
||||||
[](https://github.com/anuraghazra/github-readme-stats)
|
[](https://github.com/anuraghazra/github-readme-stats)
|
||||||
```
|
```
|
||||||
|
9
utils.js
9
utils.js
@ -12,10 +12,17 @@ const renderError = (message) => {
|
|||||||
`;
|
`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// https://stackoverflow.com/a/48073476/10629172
|
||||||
|
function encodeHTML(str) {
|
||||||
|
return str.replace(/[\u00A0-\u9999<>&](?!#)/gim, function (i) {
|
||||||
|
return "&#" + i.charCodeAt(0) + ";";
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function kFormatter(num) {
|
function kFormatter(num) {
|
||||||
return Math.abs(num) > 999
|
return Math.abs(num) > 999
|
||||||
? Math.sign(num) * (Math.abs(num) / 1000).toFixed(1) + "k"
|
? Math.sign(num) * (Math.abs(num) / 1000).toFixed(1) + "k"
|
||||||
: Math.sign(num) * Math.abs(num);
|
: Math.sign(num) * Math.abs(num);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { renderError, kFormatter };
|
module.exports = { renderError, kFormatter, encodeHTML };
|
||||||
|
Loading…
x
Reference in New Issue
Block a user