Skip to content

Commit 2cc05ff

Browse files
committed
Add thumbnails generation for the blog
1 parent 751a52e commit 2cc05ff

6 files changed

Lines changed: 675 additions & 3 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
.DS_Store
1414
*.local
1515

16+
# Generated thumbnails
17+
*.thumb.jpg
18+
1619
# Logs
1720
npm-debug.log*
1821
yarn-debug.log*

.vitepress/theme/BlogPosts.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const filteredPosts = computed(() => {
1515
<div class="blog-posts">
1616
<article v-for="post in filteredPosts" :key="post.url" class="post-card">
1717
<a :href="post.url" class="post-image-link">
18-
<img v-if="post.image" :src="post.image" :alt="post.title" class="post-image" />
18+
<img v-if="post.thumb || post.image" :src="post.thumb || post.image" :alt="post.title" class="post-image" />
1919
</a>
2020
<div class="post-content">
2121
<div class="post-title">

.vitepress/theme/posts.data.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@ export interface Post {
77
date: string
88
description: string
99
image?: string
10+
thumb?: string
1011
author?: string
1112
}
1213

14+
function getThumbUrl(imageUrl: string | undefined): string | undefined {
15+
if (!imageUrl) return undefined
16+
const ext = imageUrl.lastIndexOf('.')
17+
if (ext === -1) return undefined
18+
return `${imageUrl.slice(0, ext)}.thumb.jpg`
19+
}
20+
1321
declare const data: Post[]
1422
export { data }
1523

@@ -23,6 +31,7 @@ export default createContentLoader(getBlogGlobPatterns(), {
2331
date: formatDate(page.frontmatter.date),
2432
description: page.frontmatter.description,
2533
image: page.frontmatter.image,
34+
thumb: getThumbUrl(page.frontmatter.image),
2635
author: page.frontmatter.author,
2736
}))
2837
.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime())

0 commit comments

Comments
 (0)