From e3d1e094964a9ef64ebe5fb4672d49e16dfe8bc5 Mon Sep 17 00:00:00 2001 From: Eddie Jaoude Date: Wed, 29 Nov 2023 07:41:40 +0000 Subject: [PATCH] chore: formatted files --- pages/[username].js | 6 ++- pages/api/profiles/[username]/embed.js | 65 +++++++++++++++----------- services/utils/twemoji.js | 26 +++++------ 3 files changed, 53 insertions(+), 44 deletions(-) diff --git a/pages/[username].js b/pages/[username].js index 4a088e8a80a..2470aa75190 100644 --- a/pages/[username].js +++ b/pages/[username].js @@ -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, { diff --git a/pages/api/profiles/[username]/embed.js b/pages/api/profiles/[username]/embed.js index 31985add68a..9da0b1fedd5 100644 --- a/pages/api/profiles/[username]/embed.js +++ b/pages/api/profiles/[username]/embed.js @@ -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( - , - { - 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(, { + 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); -} \ No newline at end of file +} diff --git a/services/utils/twemoji.js b/services/utils/twemoji.js index 85ed8fbc2cf..7bcc9ee61e3 100644 --- a/services/utils/twemoji.js +++ b/services/utils/twemoji.js @@ -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; @@ -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) { @@ -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())); -} \ No newline at end of file + return (emojiCache[key] = fetch(`${api}${code.toUpperCase()}.svg`).then( + async (r) => r.text(), + )); +}