Skip to content

Commit fc02856

Browse files
committed
feat(blog): 改进博客组件图片处理和占位符显示
重构博客卡片组件中的图片处理逻辑,统一使用对象结构检查 优化占位符图片的文字显示逻辑,支持中英文字符处理 更新多篇博客文章的元数据,移除冗余图片字段
1 parent 4021ae5 commit fc02856

9 files changed

Lines changed: 43 additions & 20 deletions

src/components/blog/BlogListCard.astro

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const { title, description, createdAt, categories, tags, image, imageAlt } =
2424
const descriptionLength = 100; // 列表页描述稍短
2525
2626
// 检查是否有自定义图片
27-
const hasCustomImage = image && image !== defaultImage;
27+
const hasCustomImage = !!(image && typeof image === "object" && "src" in image);
2828
// console.log("图片:", image);
2929
const displayImageAlt = imageAlt || `${title} - 默认图片`;
3030
@@ -50,14 +50,14 @@ const entryDescription = description?.substring(0, descriptionLength) || "";
5050
>
5151
<a href={`/blog/${entry.id}`}>
5252
{hasCustomImage ? (
53-
<Image
54-
class="w-full h-full rounded-lg object-cover transition-all duration-500 group-hover:scale-110 group-hover:brightness-110"
55-
src={image}
56-
alt={displayImageAlt}
57-
inferSize={true}
58-
loading="lazy"
59-
format="webp"
60-
quality={40}
53+
<Image
54+
class="w-full h-full rounded-lg object-cover transition-all duration-500 group-hover:scale-110 group-hover:brightness-110"
55+
src={image}
56+
alt={displayImageAlt}
57+
inferSize={true}
58+
loading="lazy"
59+
format="webp"
60+
quality={40}
6161
decoding="async"
6262
fetchpriority="high"
6363
/>

src/components/blog/Card.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const { title, description, createdAt, categories, tags, image, imageAlt } =
1919
const descriptionLength = 200;
2020
2121
// 检查是否有自定义图片
22-
const hasCustomImage = image && image !== defaultImage;
22+
const hasCustomImage = !!(image && typeof image === "object" && "src" in image);
2323
const displayImageAlt = imageAlt || `${title} - 默认图片`;
2424
2525
const entryDate = createdAt ? formatDate(createdAt) : null;

src/components/blog/PlaceholderImage.astro

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ interface Props {
66
77
const { title, className = "" } = Astro.props;
88
9-
// 从标题中提取前两个字符作为显示文字
10-
const displayText = title.slice(0, 2).toUpperCase();
9+
const normalizedTitle = (title || "").trim();
10+
const firstTwo = normalizedTitle.slice(0, 2);
11+
const isHan = (ch: string) => /[\u3400-\u9FFF]/.test(ch);
12+
const firstTwoAreHan = firstTwo.length === 2 && isHan(firstTwo[0]) && isHan(firstTwo[1]);
13+
const englishWord = normalizedTitle.match(/[A-Za-z]+(?:'[A-Za-z]+)?/i)?.[0] || "";
14+
const displayText = firstTwoAreHan
15+
? firstTwo
16+
: (englishWord ? englishWord.toUpperCase() : firstTwo.toUpperCase());
1117
1218
// 预设的渐变背景色组合
1319
const gradients = [
@@ -56,7 +62,7 @@ const selectedGradient = gradients[getGradientIndex(title)];
5662
</div>
5763
<!-- 副标题效果 -->
5864
<div class="mt-2 text-white/80 text-sm md:text-base font-medium tracking-wide">
59-
{title.length > 10 ? title.slice(0, 10) + '...' : title}
65+
{title.length > 10 ? title.slice(0, 20) + '...' : title}
6066
</div>
6167
</div>
6268

src/components/blog/RecommendedReadCard.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const {
2525
const { title, createdAt, image, imageAlt } = entry.data;
2626
2727
// 检查是否有自定义图片
28-
const hasCustomImage = image && image !== defaultImage;
28+
const hasCustomImage = !!(image && typeof image === "object" && "src" in image);
2929
const displayImageAlt = imageAlt || `${title} - 推荐阅读`;
3030
3131
const entryDate = createdAt ? formatDate(createdAt) : null;

src/components/blog/RelatedCardCompact.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const {
3434
const descriptionLength = 80; // 更精简的描述长度
3535
3636
// 使用默认图片如果没有设置图片
37-
const displayImage = image || defaultImage;
37+
const displayImage = image && typeof image === "object" && "src" in image ? image : defaultImage;
3838
const displayImageAlt = imageAlt || `${title} - 默认图片`;
3939
4040
const entryDate = createdAt ? formatDate(createdAt) : null;

src/components/common/EntryHeader.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ tags?.sort((a: string, b: string) => a.localeCompare(b));
8989
textClass={`${image && showImage ? "" : "border border-white/10 dark:border-white/5"}`}
9090
>
9191
{/* 背景图片层 */}
92-
{image && showImage && (
92+
{image && typeof image === "object" && "src" in image && showImage && (
9393
<div class="absolute inset-0">
9494
<Image
9595
class="w-full h-full object-cover rounded-[35px]"

src/content/blog/Liquid_Glass组件.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
---
2-
title: LiquidGlass 组件使用说明
2+
title: Liquid Glass 组件使用说明
33
description: 介绍 LiquidGlass 组件的功能、使用方法与相关代码。
4-
createdAt: 2024-01-01
4+
createdAt: 2025-11-17
55
published: true
66
categories:
77
- 使用说明
88
author: Maple
9-
image: "@assets/uploads/demo.jpg"
109
tags:
1110
- 文章
1211
- 示例

src/content/blog/文章模板.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: 文章模板
3+
description: 一句话描述
4+
createdAt: 2025-11-19
5+
updatedAt: 2025-11-19
6+
author: 作者昵称
7+
image: "@assets/uploads/demo.jpg"
8+
categories:
9+
- 示例分类
10+
tags:
11+
- 示例标签
12+
draft: false
13+
hideToc: false
14+
---
15+
16+
> 下面是文章的正文部分
17+
# 主标题
18+
## 小节标题
19+
正文内容

src/content/blog/组件标签目录说明.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ published: true
66
categories:
77
- 使用说明
88
author: Maple
9-
image: "@assets/uploads/demo.jpg"
109
tags:
1110
- 文章
1211
- 示例

0 commit comments

Comments
 (0)