Skip to content

Commit cdcf9be

Browse files
committed
feat(post-thumbnail): add page /images/{slug}.png
1 parent 4732ce8 commit cdcf9be

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

public/logo-leetcode-dark.png

11 KB
Loading

src/pages/images/[slug].png.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import IBMPlexMonoRegular from '@fontsource/ibm-plex-mono/files/ibm-plex-mono-latin-400-normal.woff'
2+
import IBMPlexMonoBold from '@fontsource/ibm-plex-mono/files/ibm-plex-mono-latin-700-normal.woff'
3+
import { Resvg } from '@resvg/resvg-js'
4+
import type { APIContext } from 'astro'
5+
import { getCollection, getEntryBySlug } from 'astro:content'
6+
import satori from 'satori'
7+
import { html } from 'satori-html'
8+
9+
import siteConfig from '@/configs/site'
10+
11+
const dimensions = {
12+
width: 1200,
13+
height: 630,
14+
}
15+
16+
const { author } = siteConfig
17+
18+
export async function get({ site, params }: APIContext) {
19+
const url = new URL(site)
20+
const domain = url.hostname.replace(/^[^.]+\./g, '')
21+
22+
const post = await getEntryBySlug('leetcode-solutions', params.slug)
23+
const { title, pubDate } = post?.data || { title: '', pubDate: new Date() }
24+
25+
const titleArr = title.split('. ')
26+
const leetcodeProblemNumber = titleArr[0]
27+
const postTitle = titleArr[1]
28+
29+
const date = new Date(pubDate).toLocaleDateString('en-US', {
30+
dateStyle: 'full',
31+
})
32+
33+
const markup = html`<div tw="bg-white flex flex-col w-full h-full">
34+
<div tw="flex items-center w-full h-4/5 p-10">
35+
<div tw="flex flex-col">
36+
<div tw="text-black text-2xl mb-4">${date}</div>
37+
<div tw="flex text-5xl w-full font-bold leading-snug tracking-tight text-black">
38+
<img
39+
src="${site}logo-leetcode-dark.png"
40+
tw="h-15"
41+
/>
42+
<span tw="ml-4">#${leetcodeProblemNumber}</span>
43+
</div>
44+
<div tw="flex text-5xl w-full font-bold leading-snug tracking-tight text-black">
45+
${postTitle}
46+
</div>
47+
</div>
48+
</div>
49+
<div tw="w-full h-1/5 border-t border-black flex p-10 items-center justify-between text-2xl">
50+
<div tw="flex items-center">
51+
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="bi bi-globe" viewBox="0 0 16 16">
52+
<path d="M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8zm7.5-6.923c-.67.204-1.335.82-1.887 1.855A7.97 7.97 0 0 0 5.145 4H7.5V1.077zM4.09 4a9.267 9.267 0 0 1 .64-1.539 6.7 6.7 0 0 1 .597-.933A7.025 7.025 0 0 0 2.255 4H4.09zm-.582 3.5c.03-.877.138-1.718.312-2.5H1.674a6.958 6.958 0 0 0-.656 2.5h2.49zM4.847 5a12.5 12.5 0 0 0-.338 2.5H7.5V5H4.847zM8.5 5v2.5h2.99a12.495 12.495 0 0 0-.337-2.5H8.5zM4.51 8.5a12.5 12.5 0 0 0 .337 2.5H7.5V8.5H4.51zm3.99 0V11h2.653c.187-.765.306-1.608.338-2.5H8.5zM5.145 12c.138.386.295.744.468 1.068.552 1.035 1.218 1.65 1.887 1.855V12H5.145zm.182 2.472a6.696 6.696 0 0 1-.597-.933A9.268 9.268 0 0 1 4.09 12H2.255a7.024 7.024 0 0 0 3.072 2.472zM3.82 11a13.652 13.652 0 0 1-.312-2.5h-2.49c.062.89.291 1.733.656 2.5H3.82zm6.853 3.472A7.024 7.024 0 0 0 13.745 12H11.91a9.27 9.27 0 0 1-.64 1.539 6.688 6.688 0 0 1-.597.933zM8.5 12v2.923c.67-.204 1.335-.82 1.887-1.855.173-.324.33-.682.468-1.068H8.5zm3.68-1h2.146c.365-.767.594-1.61.656-2.5h-2.49a13.65 13.65 0 0 1-.312 2.5zm2.802-3.5a6.959 6.959 0 0 0-.656-2.5H12.18c.174.782.282 1.623.312 2.5h2.49zM11.27 2.461c.247.464.462.98.64 1.539h1.835a7.024 7.024 0 0 0-3.072-2.472c.218.284.418.598.597.933zM10.855 4a7.966 7.966 0 0 0-.468-1.068C9.835 1.897 9.17 1.282 8.5 1.077V4h2.355z"/>
53+
</svg>
54+
<span tw="ml-3 text-blue-500">${domain}</span>
55+
</div>
56+
<div tw="flex items-center">
57+
<img
58+
src="${site}${author.avatar}"
59+
tw="w-15 h-15"
60+
/>
61+
<div tw="flex flex-col ml-4">
62+
<span tw="text-black">${author.name}</span>
63+
<span tw="text-blue-500">@${author.nickname}</span>
64+
</div>
65+
</div>
66+
</div>
67+
</div>`
68+
69+
const svg = await satori(markup, {
70+
fonts: [
71+
{
72+
name: 'Inter',
73+
data: Buffer.from(IBMPlexMonoRegular),
74+
weight: 400,
75+
},
76+
{
77+
name: 'Inter',
78+
data: Buffer.from(IBMPlexMonoBold),
79+
weight: 700,
80+
},
81+
],
82+
height: dimensions.height,
83+
width: dimensions.width,
84+
})
85+
86+
const image = new Resvg(svg, {
87+
fitTo: {
88+
mode: 'width',
89+
value: dimensions.width,
90+
},
91+
}).render()
92+
93+
return {
94+
body: image.asPng(),
95+
encoding: 'binary',
96+
}
97+
}
98+
99+
export async function getStaticPaths() {
100+
const posts = await getCollection('leetcode-solutions')
101+
const paths = posts.map((post) => {
102+
return {
103+
params: {
104+
slug: post.slug,
105+
},
106+
}
107+
})
108+
return paths
109+
}

0 commit comments

Comments
 (0)