Skip to content
This repository was archived by the owner on Jul 27, 2026. It is now read-only.

Commit a4332e2

Browse files
committed
fix: 修复 Astro v6 升级后文章 URL 显示为 undefined 的问题,将所有 entry.slug 引用替换为 entry.id
1 parent bc89c49 commit a4332e2

8 files changed

Lines changed: 12 additions & 12 deletions

File tree

src/components/ArchivePanel.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function formatDate(date: Date) {
7474
<div class="w-[70%] md:w-[80%] transition text-left text-50">{group.posts.length} 篇文章</div>
7575
</div>
7676
{group.posts.map(post => (
77-
<a href={getPostUrlBySlug(post.slug)}
77+
<a href={getPostUrlBySlug(post.id)}
7878
aria-label={post.data.title}
7979
class="group btn-plain !block h-10 w-full hover:text-[initial]"
8080
>

src/components/PostCard.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const { remarkPluginFrontmatter } = await render(entry);
7373
updated={updated}
7474
hideUpdateDate={true}
7575
hidePublishedDate={false}
76-
slug={entry.slug}
76+
slug={entry.id}
7777
showTime={false}
7878
class="mb-2 md:mb-4"
7979
></PostMetadata>

src/components/PostPage.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const interval = 50;
1818
title={entry.data.title}
1919
published={entry.data.published}
2020
updated={entry.data.updated}
21-
url={getPostUrlBySlug(entry.slug)}
21+
url={getPostUrlBySlug(entry.id)}
2222
image={entry.data.image}
2323
description={entry.data.description}
2424
draft={entry.data.draft}

src/pages/posts/[...slug].astro

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
export async function getStaticPaths() {
2525
const blogEntries = await getSortedPosts();
2626
return blogEntries.map((entry) => ({
27-
params: { slug: entry.slug },
27+
params: { slug: entry.id },
2828
props: { entry },
2929
}));
3030
}
@@ -146,7 +146,7 @@ const jsonLd = [
146146
published={entry.data.published}
147147
updated={entry.data.updated}
148148
tags={entry.data.tags}
149-
slug={entry.slug}
149+
slug={entry.id}
150150
></PostMetadata>
151151
{!entry.data.image && <div class="border-[var(--line-divider)] border-dashed border-b-[1px] mb-5"></div>}
152152
</div>
@@ -201,7 +201,7 @@ const jsonLd = [
201201
<div class="flex-1">
202202
<p class="text-white/80 text-sm mb-1">发现错误或想要改进这篇文章?</p>
203203
<a
204-
href={`${gitHubEditConfig.baseUrl}/${entry.slug}.md`}
204+
href={`${gitHubEditConfig.baseUrl}/${entry.id}.md`}
205205
target="_blank"
206206
rel="noopener noreferrer"
207207
class="inline-flex items-center gap-1 text-[var(--primary)] hover:text-[oklch(0.65_0.16_var(--hue))] text-sm font-medium transition-colors"
@@ -246,7 +246,7 @@ const jsonLd = [
246246
)}
247247
</div>
248248

249-
{licenseConfig.enable && <License title={entry.data.title} slug={entry.slug} pubDate={entry.data.published} class="mb-6 rounded-xl license-container"></License>}
249+
{licenseConfig.enable && <License title={entry.data.title} slug={entry.id} pubDate={entry.data.published} class="mb-6 rounded-xl license-container"></License>}
250250

251251
<!-- Giscus 评论区 -->
252252
<div id="giscus-container" class="giscus"></div>

src/pages/rss.xml.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export async function GET(context: APIContext): Promise<Response> {
6868
title: post.data.title,
6969
description: post.data.description,
7070
pubDate: post.data.published,
71-
link: new URL(`posts/${post.slug}/`, context.site).href,
71+
link: new URL(`posts/${post.id}/`, context.site).href,
7272
// sanitize the new html string with corrected image paths
7373
content: sanitizeHtml(html.toString(), {
7474
allowedTags: sanitizeHtml.defaults.allowedTags.concat(["img"]),

src/pages/search.json.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export async function GET(_context: APIContext): Promise<Response> {
1919
title: post.data.title || "",
2020
description: post.data.description || "",
2121
content: toPlainText(post.body || ""),
22-
link: post.slug,
22+
link: post.id,
2323
published: post.data.published || "",
2424
}));
2525

src/pages/sitemap.xml.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const GET: APIRoute = async () => {
2424
];
2525

2626
const postPages: SitemapPage[] = posts.map((post) => ({
27-
url: `/posts/${post.slug}/`,
27+
url: `/posts/${post.id}/`,
2828
priority: 0.7,
2929
changefreq: "weekly",
3030
lastmod: post.data.updated || post.data.published,

src/utils/content-utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ export async function getSortedPosts(): Promise<CollectionEntry<"posts">[]> {
1717
});
1818

1919
for (let i = 1; i < sorted.length; i++) {
20-
sorted[i].data.nextSlug = sorted[i - 1].slug;
20+
sorted[i].data.nextSlug = sorted[i - 1].id;
2121
sorted[i].data.nextTitle = sorted[i - 1].data.title;
2222
}
2323
for (let i = 0; i < sorted.length - 1; i++) {
24-
sorted[i].data.prevSlug = sorted[i + 1].slug;
24+
sorted[i].data.prevSlug = sorted[i + 1].id;
2525
sorted[i].data.prevTitle = sorted[i + 1].data.title;
2626
}
2727

0 commit comments

Comments
 (0)