Skip to content

Commit af5cd52

Browse files
xkcodingclaude
andcommitted
fix: 修复无头图文章错误显示动态 OG 图片的问题
- 分离 coverImageUrl(页面显示)和 ogImageUrl(meta 标签) - 页面封面图只在用户明确设置 ogImage 时才显示 - 动态 OG 图片仅用于 meta 标签,不影响页面显示 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 6886398 commit af5cd52

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

src/layouts/PostDetails.astro

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,25 @@ const showToc =
4646
!hideToc &&
4747
headings.filter(h => h.depth >= 2 && h.depth <= 4).length >= 2;
4848
49-
let ogImageUrl: string | undefined;
49+
// 用于页面显示的封面图(只有用户明确设置了 ogImage 时才显示)
50+
let coverImageUrl: string | undefined;
5051
51-
// Determine OG image source
52+
// Determine cover image source from user-defined ogImage
5253
if (typeof initOgImage === "string") {
53-
ogImageUrl = initOgImage; // Remote OG image (absolute URL)
54+
coverImageUrl = initOgImage; // Remote image (absolute URL)
5455
} else if (initOgImage?.src) {
55-
ogImageUrl = initOgImage.src; // Local asset
56+
coverImageUrl = initOgImage.src; // Local asset
5657
}
5758
58-
// Use dynamic OG image if enabled and no remote|local ogImage
59+
// 用于 meta 标签的 OG 图片(可以是用户设置的或动态生成的)
60+
let ogImageUrl = coverImageUrl;
61+
62+
// Use dynamic OG image for meta tags if enabled and no user-defined ogImage
5963
if (!ogImageUrl && SITE.dynamicOgImage) {
6064
ogImageUrl = `${getPath(post.id, post.filePath)}/index.png`;
6165
}
6266
63-
// Resolve OG image URL (or fallback to SITE.ogImage / default `og.png`)
67+
// Resolve OG image URL for meta tags
6468
const ogImage = ogImageUrl
6569
? new URL(ogImageUrl, Astro.url.origin).href
6670
: undefined;
@@ -101,10 +105,10 @@ const nextPost =
101105
data-pagefind-body
102106
>
103107
{
104-
ogImageUrl && (
108+
coverImageUrl && (
105109
<div class="group/cover mb-6 overflow-hidden rounded-xl border border-foreground/10 shadow-lg shadow-foreground/5 transition-shadow duration-300 hover:shadow-xl hover:shadow-foreground/10">
106110
<img
107-
src={ogImageUrl}
111+
src={coverImageUrl}
108112
alt={title}
109113
class="h-auto max-h-[250px] w-full object-cover transition-transform duration-300 group-hover/cover:scale-[1.02] sm:max-h-[400px]"
110114
loading="lazy"

0 commit comments

Comments
 (0)