Commit 12fd842f authored by anuraghazra's avatar anuraghazra
Browse files

fix: description trimming & htmlEncode

parent 137a9f20
Loading
Loading
Loading
Loading
+24 −31
Original line number Diff line number Diff line
const axios = require("axios");
const { renderError, kFormatter } = require("../utils");
const { renderError, kFormatter, encodeHTML } = require("../utils");
require("dotenv").config();

async function fetchRepo(username, reponame) {
@@ -11,9 +11,7 @@ async function fetchRepo(username, reponame) {
    },
    data: {
      query: `
        query getRepo($login: String!, $repo: String!) {
          user(login: $login) {
            repository(name: $repo) {
        fragment RepoInfo on Repository {
          name
          stargazers {
            totalCount
@@ -26,20 +24,15 @@ async function fetchRepo(username, reponame) {
          }
          forkCount
        }
          }
          organization(login: $login) {
        query getRepo($login: String!, $repo: String!) {
          user(login: $login) {
            repository(name: $repo) {
              name
              stargazers {
                totalCount
              ...RepoInfo
            }
              description
              primaryLanguage {
                color
                id
                name
          }
              forkCount
          organization(login: $login) {
            repository(name: $repo) {
              ...RepoInfo
            }
          }
        }
@@ -53,7 +46,6 @@ async function fetchRepo(username, reponame) {

  const data = res.data.data;

  console.log(res.data);
  if (!data.user && !data.organization) {
    throw new Error("Not found");
  }
@@ -76,8 +68,13 @@ async function fetchRepo(username, reponame) {
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">
      <style>
@@ -94,10 +91,7 @@ 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}" />
@@ -122,8 +116,7 @@ const renderRepoCard = (repo) => {
};

module.exports = async (req, res) => {
  const username = req.query.username;
  const repo = req.query.repo;
  const { username, repo } = req.query;

  let repoData;
  res.setHeader("Content-Type", "image/svg+xml");
+0 −6
Original line number Diff line number Diff line
@@ -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

```md
![Anurag's github stats](https://github-readme-stats.vercel.app/api?username=anuraghazra)
```

_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
[![Anurag's github stats](https://github-readme-stats.vercel.app/api?username=anuraghazra)](https://github.com/anuraghazra/github-readme-stats)
```
+8 −1
Original line number Diff line number Diff line
@@ -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) {
  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 };
module.exports = { renderError, kFormatter, encodeHTML };