Add getCoverImage utility function and update layouts

- Add getCoverImage function in utils.ts to handle different cover image formats
- Update MarkdownPost.astro to use getCoverImage for consistent image handling
- Update MoreTile.astro to use getCoverImage and remove direct CA_LOGO_URL import
- Update preview.png

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Clas Wen 2025-09-28 23:35:38 +08:00
parent a0aaf4984a
commit 6bcb1cf581
4 changed files with 20 additions and 5 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 189 KiB

After

Width:  |  Height:  |  Size: 132 KiB

View file

@ -3,7 +3,7 @@ import BaseHead from "../components/header/BaseHead.astro"
import Header from "../components/header/Header.astro" import Header from "../components/header/Header.astro"
import Footer from "../components/Footer.astro" import Footer from "../components/Footer.astro"
import { formatDate } from "../utils" import { formatDate, getCoverImage } from "../utils"
import { SITE_TITLE } from "../consts" import { SITE_TITLE } from "../consts"
const { frontmatter } = Astro.props const { frontmatter } = Astro.props
const type = frontmatter.tags?.[0] ?? "默认" const type = frontmatter.tags?.[0] ?? "默认"
@ -20,7 +20,7 @@ const dateFormatted = formatDate(pubDate)
<BaseHead <BaseHead
title={`${title} - ${SITE_TITLE}`} title={`${title} - ${SITE_TITLE}`}
description={description} description={description}
image={frontmatter.cover?.square} image={getCoverImage(frontmatter.cover)}
/> />
</head> </head>
<body <body

View file

@ -1,11 +1,10 @@
--- ---
import { CA_LOGO_URL } from "../consts" import { formatDate, getCoverImage } from "../utils"
import { formatDate } from "../utils"
const { title, href, cover, tags, date } = Astro.props const { title, href, cover, tags, date } = Astro.props
const dateFormatted = formatDate(date) const dateFormatted = formatDate(date)
const type = tags?.[0] ?? "默认" const type = tags?.[0] ?? "默认"
const image = cover?.url ?? cover ?? CA_LOGO_URL const image = getCoverImage(cover)
const label = `${title} - ${type} - 发表时间 ${dateFormatted}`; const label = `${title} - ${type} - 发表时间 ${dateFormatted}`;
--- ---

View file

@ -1,3 +1,5 @@
import { CA_LOGO_URL } from "./consts"
export function formatDate(dateString: string) { export function formatDate(dateString: string) {
const date = new Date(dateString) const date = new Date(dateString)
const year = date.getFullYear() const year = date.getFullYear()
@ -16,3 +18,17 @@ export function formatDateV2(date: string) {
// 返回格式化后的日期 // 返回格式化后的日期
return formattedDate return formattedDate
} }
export const getCoverImage = (cover: unknown) => {
switch (typeof cover) {
case "string":
return cover
case "object":
if (cover && "url" in cover) {
return (cover as { url: string }).url
}
return CA_LOGO_URL
default:
return CA_LOGO_URL
}
}