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

render preview image url in card, when imageUrl is provided #3438

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions api/pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { blacklist } from "../src/common/blacklist.js";
import {
clampValue,
CONSTANTS,
getBase64URIFromImage,
parseBoolean,
renderError,
} from "../src/common/utils.js";
Expand All @@ -24,6 +25,7 @@ export default async (req, res) => {
locale,
border_radius,
border_color,
show_image,
} = req.query;

res.setHeader("Content-Type", "image/svg+xml");
Expand Down Expand Up @@ -68,6 +70,10 @@ export default async (req, res) => {
}, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
);

repoData.stringifiedRepoImage = await getBase64URIFromImage(
repoData.openGraphImageUrl,
);

return res.send(
renderRepoCard(repoData, {
hide_border: parseBoolean(hide_border),
Expand All @@ -80,6 +86,7 @@ export default async (req, res) => {
border_color,
show_owner: parseBoolean(show_owner),
locale: locale ? locale.toLowerCase() : null,
show_image: parseBoolean(show_image),
}),
);
} catch (err) {
Expand Down
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ If we don't support your language, please consider contributing! You can find mo
#### Repo Card Exclusive Options

* `show_owner` - Shows the repo's owner name *(boolean)*. Default: `false`.
* `show_image` - Shows the repo's social preview image *(boolean)*. Default: `false`.

#### Gist Card Exclusive Options

Expand Down Expand Up @@ -447,6 +448,11 @@ Use [show\_owner](#repo-card-exclusive-options) query option to include the repo

![Readme Card](https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra\&repo=github-readme-stats\&show_owner=true)


Use [show\_image](#repo-card-exclusive-options) query option to include the repo's social preview image header

![Readme Card](https://github-readme-stats.vercel.app/api/pin/?username=anuraghazra\&repo=github-readme-stats\&show_image=true)

# GitHub Gist Pins

GitHub gist pins allow you to pin gists in your GitHub profile using a GitHub readme profile.
Expand Down
3 changes: 3 additions & 0 deletions src/cards/repo-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const renderRepoCard = (repo, options = {}) => {
isTemplate,
starCount,
forkCount,
stringifiedRepoImage,
} = repo;
const {
hide_border = false,
Expand All @@ -73,6 +74,7 @@ const renderRepoCard = (repo, options = {}) => {
border_radius,
border_color,
locale,
show_image = false,
} = options;

const lineHeight = 10;
Expand Down Expand Up @@ -141,6 +143,7 @@ const renderRepoCard = (repo, options = {}) => {
height,
border_radius,
colors,
stringifiedRepoImage: show_image ? stringifiedRepoImage : "",
});

card.disableAnimations();
Expand Down
1 change: 1 addition & 0 deletions src/cards/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type StatCardOptions = CommonOptions & {

export type RepoCardOptions = CommonOptions & {
show_owner: boolean;
show_image: boolean;
};

export type TopLangOptions = CommonOptions & {
Expand Down
27 changes: 27 additions & 0 deletions src/common/Card.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Card {
* @param {string?=} args.customTitle Card custom title.
* @param {string?=} args.defaultTitle Card default title.
* @param {string?=} args.titlePrefixIcon Card title prefix icon.
* @param {string?=} args.stringifiedRepoImage Card preview image.
* @param {number?=} args.imageHeight Card preview image.
* @param {object?=} args.colors Card colors arguments.
* @param {string} args.colors.titleColor Card title color.
* @param {string} args.colors.textColor Card text color.
Expand All @@ -27,6 +29,8 @@ class Card {
customTitle,
defaultTitle = "",
titlePrefixIcon,
stringifiedRepoImage = "",
imageHeight = 200,
}) {
this.width = width;
this.height = height;
Expand All @@ -36,6 +40,9 @@ class Card {

this.border_radius = border_radius;

this.imageHeight = imageHeight;
this.stringifiedRepoImage = stringifiedRepoImage;

// returns theme based colors with proper overrides and defaults
rickstaa marked this conversation as resolved.
Show resolved Hide resolved
this.colors = colors;
this.title =
Expand All @@ -51,6 +58,10 @@ class Card {
this.animations = true;
this.a11yTitle = "";
this.a11yDesc = "";
if (this.stringifiedRepoImage) {
this.height += this.imageHeight;
this.paddingY += this.imageHeight;
}
}

/**
Expand Down Expand Up @@ -199,6 +210,21 @@ class Card {
`;
};

/**
* @returns {string} Renders social preview image
*/
renderImage = () => {
rickstaa marked this conversation as resolved.
Show resolved Hide resolved
if (!this.stringifiedRepoImage) {
return "";
}
return `
<g data-testid="card-image" transform="translate(0, 0)">
<image x="0" y="0" width="${this.width}" height="${this.imageHeight}"
href="${this.stringifiedRepoImage}">
</image>
</g>`;
};

/**
* @param {string} body The inner body of the card.
* @returns {string} The rendered card.
Expand Down Expand Up @@ -264,6 +290,7 @@ class Card {
>
${body}
</g>
${this.renderImage()}
</svg>
`;
}
Expand Down
22 changes: 22 additions & 0 deletions src/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,27 @@ const dateDiff = (d1, d2) => {
return Math.round(diff / (1000 * 60));
};

const getBase64URIFromImage = async (imageURL) => {
try {
const response = await axios.get(imageURL, {
responseType: "arraybuffer",
});

if (response.status === 200) {
const base64Image = Buffer.from(response.data, "binary").toString(
"base64",
);
const mimeType = response.headers["content-type"];
return `data:${mimeType};base64,${base64Image}`;
} else {
throw new Error("Failed to fetch the image.");
}
} catch (error) {
console.error("Error:", error);
return null;
}
};

export {
ERROR_CARD_LENGTH,
renderError,
Expand All @@ -595,4 +616,5 @@ export {
chunkArray,
parseEmojis,
dateDiff,
getBase64URIFromImage,
};
1 change: 1 addition & 0 deletions src/fetchers/repo-fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const fetcher = (variables, token) => {
name
}
forkCount
openGraphImageUrl
}
query getRepo($login: String!, $repo: String!) {
user(login: $login) {
Expand Down
1 change: 1 addition & 0 deletions src/fetchers/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type RepositoryData = {
};
forkCount: number;
starCount: number;
stringifiedRepoImage: string;
};

export type StatsData = {
Expand Down
2 changes: 2 additions & 0 deletions tests/__snapshots__/renderWakatimeCard.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ exports[`Test Render WakaTime Card should render correctly with compact layout 1
</svg>

</g>

</svg>
"
`;
Expand Down Expand Up @@ -302,6 +303,7 @@ exports[`Test Render WakaTime Card should render correctly with compact layout w
</svg>

</g>

</svg>
"
`;
18 changes: 18 additions & 0 deletions tests/renderRepoCard.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const data_repo = {
},
starCount: 38000,
forkCount: 100,
stringifiedRepoImage: "data:image/png;base64,base64/image/string",
},
};

Expand Down Expand Up @@ -339,4 +340,21 @@ describe("Test renderRepoCard", () => {
"No description provided",
);
});

it("should render repo's social preview image, when show_image is true", () => {
document.body.innerHTML = renderRepoCard(data_repo.repository, {
show_image: true,
});

expect(queryByTestId(document.body, "card-image")).toBeInTheDocument();
expect(
queryByTestId(document.body, "card-image").children[0],
).toHaveAttribute("href", data_repo.repository.stringifiedRepoImage);
});

it("should not render repo's social preview image by default", () => {
document.body.innerHTML = renderRepoCard(data_repo.repository);

expect(queryByTestId(document.body, "card-image")).not.toBeInTheDocument();
});
});