Skip to content

Commit

Permalink
Hotpatch Avatar Resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans committed May 7, 2024
1 parent 1cf09f0 commit a07c115
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
42 changes: 22 additions & 20 deletions blog/buildAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const getCoverImages = async () => {

// Read meta.json and check for cover
const meta = await import(`../content/${post}/meta.json`).catch(
() => {}
() => { }
);

if (meta?.cover) {
Expand Down Expand Up @@ -180,19 +180,17 @@ const handleCoverImages = async () => {

// eslint-disable-next-line sonarjs/no-nested-template-literals
const key = `${prefix || ''}cover${suffix ? `-${suffix}` : ''}`;
const output = `${ASSETS_FOLDER}/${cover.post}/${key}.${
format || 'webp'
}`;
const output = `${ASSETS_FOLDER}/${cover.post}/${key}.${format || 'webp'
}`;

console.log(`Converting image to ${output}`);

await makeDirecectoryIfNotExists(`${ASSETS_FOLDER}/${cover.post}`);

await sharp(cover.data).resize(width, height).toFile(output);

result += ` '${key}': import('./${cover.post}/${key}.${
format || 'webp'
}') as Promise<{default: StaticImageData}>,\n`;
result += ` '${key}': import('./${cover.post}/${key}.${format || 'webp'
}') as Promise<{default: StaticImageData}>,\n`;
}

result += ' },\n';
Expand Down Expand Up @@ -228,7 +226,7 @@ const getAvatarImages = async () => {
// Read meta.json and check for cover
const meta: BlogPostMetadata = await import(
`../content/${post}/meta.json`
).catch(() => {});
).catch(() => { });

if (!meta) continue;

Expand Down Expand Up @@ -307,24 +305,28 @@ const handleAvatarImages = async () => {

// eslint-disable-next-line sonarjs/no-nested-template-literals
const key = `${prefix || ''}avatar${suffix ? `-${suffix}` : ''}`;
const output = `${ASSETS_FOLDER}/${author}/${key}.${
format || 'webp'
}`;
const output = `${ASSETS_FOLDER}/${author}/${key}.${format || 'webp'
}`;

console.log(`Converting image to ${output}`);

await makeDirecectoryIfNotExists(`${ASSETS_FOLDER}/${author}`);

await sharp(avatar, {
pages: -1,
animated: true,
})
.resize(width, height)
.toFile(output);
try {
await sharp(avatar, {
pages: -1,
animated: true,
})
.resize(width, height)
.toFile(output);
} catch (error) {
console.log('Failed to convert image', error);
continue;
}


result += ` '${key}': import('./${author}/${key}.${
format || 'webp'
}') as Promise<{default: StaticImageData}>,\n`;
result += ` '${key}': import('./${author}/${key}.${format || 'webp'
}') as Promise<{default: StaticImageData}>,\n`;
}

result += ' },\n';
Expand Down
4 changes: 2 additions & 2 deletions blog/src/components/Author.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export const Author: FC<{
}> = async ({ name, socials = true, size = 'normal', link = false }) => {
const author_metadata = await getAuthorMetadata(name);

let avatar = avatars[name as keyof typeof avatars]
? await avatars[name as keyof typeof avatars].avatar.then(
let avatar = avatars[name as keyof typeof avatars]?.['avatar']
? await avatars[name as keyof typeof avatars]['avatar'].then(
(imported) => imported.default.src
)
: undefined;
Expand Down

0 comments on commit a07c115

Please sign in to comment.