From 1e044405a4d08bdaa2d687dca6635ab74daa19bd Mon Sep 17 00:00:00 2001 From: Arik Chakma Date: Fri, 15 Mar 2024 09:36:39 +0600 Subject: [PATCH] fix: external author image --- scripts/generate-og-images.mjs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/scripts/generate-og-images.mjs b/scripts/generate-og-images.mjs index d6d64baf4..be7ae451c 100644 --- a/scripts/generate-og-images.mjs +++ b/scripts/generate-og-images.mjs @@ -219,17 +219,22 @@ async function generateGuideOpenGraph() { for (const guide of allGuides) { const author = allAuthors.find((author) => author.id === guide.authorId); - const authorAvatar = await fs.readFile( - path.join(ALL_AUTHOR_IMAGE_DIR, author.imageUrl), - ); - const avatarExt = author.imageUrl.split('.').pop(); + const image = + author?.imageUrl || 'https://roadmap.sh/images/default-avatar.png'; + const isExternalImage = image?.startsWith('http'); + let authorImageExtention = ''; + let authorAvatar; + if (!isExternalImage) { + authorAvatar = await fs.readFile(path.join(ALL_AUTHOR_IMAGE_DIR, image)); + authorImageExtention = image?.split('.')[1]; + } const template = getGuideTemplate({ ...guide, authorName: author.name, - authorAvatar: `data:image/${avatarExt};base64,${authorAvatar.toString( - 'base64', - )}`, + authorAvatar: isExternalImage + ? image + : `data:image/${authorImageExtention};base64,${authorAvatar.toString('base64')}`, }); await generateOpenGraph(template, 'guides', guide.id + '.png'); }