Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
chore: formatted files
Browse files Browse the repository at this point in the history
  • Loading branch information
eddiejaoude committed Nov 29, 2023
1 parent 203320c commit e3d1e09
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 44 deletions.
6 changes: 4 additions & 2 deletions pages/[username].js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ export async function getServerSideProps(context) {
if ("embed" in context.query) {
return {
redirect: {
destination: `/api/profiles/${username}/embed?theme=${context.query.theme || "default"}`,
destination: `/api/profiles/${username}/embed?theme=${
context.query.theme || "default"
}`,
permanent: true,
},
}
};
}

const { status, profile } = await getUserApi(req, res, username, {
Expand Down
65 changes: 38 additions & 27 deletions pages/api/profiles/[username]/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,51 @@ import { getUserApi } from "./index";
import Profile from "@components/embeds/external/Profile";
import { loadEmoji, getIconCode } from "@services/utils/twemoji";

const inter = await fs.readFile(path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../../../public", "Inter-Regular.ttf"));
const interBold = await fs.readFile(path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../../../public", "Inter-Bold.ttf"));
const inter = await fs.readFile(
path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"../../../../public",
"Inter-Regular.ttf",
),
);
const interBold = await fs.readFile(
path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"../../../../public",
"Inter-Bold.ttf",
),
);

export default async function handler(req, res) {
const { username } = req.query;
const { profile } = await getUserApi(req, res, username);

const response = await satori(
<Profile data={profile} />,
{
width: 350,
height: 550,
fonts: [
{
name: "Inter",
data: inter,
weight: 400,
style: "normal",
},
{
name: "Inter",
data: interBold,
weight: 700,
style: "normal",
},
],
loadAdditionalAsset: async (code, segment) => {
if (code === 'emoji') {
return (`data:image/svg+xml;base64,${btoa(await loadEmoji("twemoji", getIconCode(segment)))}`)
}
const response = await satori(<Profile data={profile} />, {
width: 350,
height: 550,
fonts: [
{
name: "Inter",
data: inter,
weight: 400,
style: "normal",
},
{
name: "Inter",
data: interBold,
weight: 700,
style: "normal",
},
],
loadAdditionalAsset: async (code, segment) => {
if (code === "emoji") {
return `data:image/svg+xml;base64,${btoa(
await loadEmoji("twemoji", getIconCode(segment)),
)}`;
}
},
);
});

res.setHeader("Content-Type", "image/svg+xml");
res.status(200).send(response);
}
}
26 changes: 11 additions & 15 deletions services/utils/twemoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
const U200D = String.fromCharCode(8205);
const UFE0Fg = /\uFE0F/g;

export function getIconCode (char) {
export function getIconCode(char) {
return toCodePoint(!char.includes(U200D) ? char.replace(UFE0Fg, "") : char);
}

function toCodePoint (unicodeSurrogates) {
function toCodePoint(unicodeSurrogates) {
const r = [];
let c = 0;
let i = 0;
Expand All @@ -36,26 +36,20 @@ function toCodePoint (unicodeSurrogates) {

export const apis = {
twemoji: (code) =>
`https://cdnjs.cloudflare.com/ajax/libs/twemoji/14.0.2/svg/${
code.toLowerCase()
}.svg`,
`https://cdnjs.cloudflare.com/ajax/libs/twemoji/14.0.2/svg/${code.toLowerCase()}.svg`,
openmoji: "https://cdn.jsdelivr.net/npm/@svgmoji/openmoji@2.0.0/svg/",
blobmoji: "https://cdn.jsdelivr.net/npm/@svgmoji/blob@2.0.0/svg/",
noto: "https://cdn.jsdelivr.net/gh/svgmoji/svgmoji/packages/svgmoji__noto/svg/",
fluent: (code) =>
`https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/${
code.toLowerCase()
}_color.svg`,
`https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/${code.toLowerCase()}_color.svg`,
fluentFlat: (code) =>
`https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/${
code.toLowerCase()
}_flat.svg`,
`https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/${code.toLowerCase()}_flat.svg`,
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const emojiCache = {};

export async function loadEmoji (type, code) {
export async function loadEmoji(type, code) {
const key = `${type}:${code}`;

if (key in emojiCache) {
Expand All @@ -69,7 +63,9 @@ export async function loadEmoji (type, code) {
const api = apis[type];

if (typeof api === "function") {
return (emojiCache[key] = fetch(api(code)).then(async r => r.text()));
return (emojiCache[key] = fetch(api(code)).then(async (r) => r.text()));
}
return (emojiCache[key] = fetch(`${api}${code.toUpperCase()}.svg`).then(async r => r.text()));
}
return (emojiCache[key] = fetch(`${api}${code.toUpperCase()}.svg`).then(
async (r) => r.text(),
));
}

0 comments on commit e3d1e09

Please sign in to comment.