部落格
新增部落格,並支援作者、標籤與文章導覽。
安裝
npx @fumadocs/cli@latest add blog使用 blogPageSchema 定義集合,並註冊您的作者:
import { defineConfig } from 'astro/config';
import fumadocs from '@fumadocs/astro';
export default defineConfig({
integrations: [
fumadocs({
components: './src/components/mdx.tsx',
collections: {
docs: { dir: 'content/docs' },
blog: { dir: 'content/blog' },
},
}),
],
});撰寫文章
content/blog 底下的每個檔案都是一篇文章:
---
title: Hello World
description: My first post.
date: 2026-10-12 # 文章依日期排序,最新的在前
tags: [Announcement] # 每個標籤在 /blog/tags 底下都有一個頁面
authors: [fuma] # blogAuthors 中的鍵,其他值會直接顯示為名稱
image: /blog/hello-world.png # 文章卡片上的封面圖片
---文章沒有草稿欄位,因此請將尚未完成的文章放在 content/blog 以外的位置。
路由
請在您的首頁版面配置下新增部落格路由。getPosts() 會將部落格頁面轉換為文章,並附上作者與標籤資訊。若想為文章提供訂閱摘要,請參閱 RSS 摘要。
文章列表
---
import Layout from '@/layouts/Layout.astro';
import { BlogPosts } from '@/components/blog-posts';
import { blogAuthors, blogSource } from '@/lib/source';
import { getPosts } from '@/lib/blog';
---
<Layout title="Blog">
<BlogPosts
client:load
pathname={Astro.url.pathname}
params={Astro.params}
posts={getPosts(blogSource.getPages(), blogAuthors)}
/>
</Layout>文章頁面
findPost() 會回傳指定文章,以及它的上一篇 (較新) 與下一篇 (較舊) 文章。BlogPage 的 toc prop 會加入附有分享按鈕的浮動目錄。
---
import type { GetStaticPaths, InferGetStaticPropsType } from 'astro';
import Layout from '@/layouts/Layout.astro';
import { BlogPostPage } from '@/components/blog-post';
import { blogAuthors, blogSource } from '@/lib/source';
import { findPost, getPosts } from '@/lib/blog';
export const getStaticPaths = (() => {
const posts = getPosts(blogSource.getPages(), blogAuthors);
return blogSource.getPages().map((page) => ({
params: { slug: decodeURI(page.slugs[0]) },
props: { page, ...findPost(posts, page.url)! },
}));
}) satisfies GetStaticPaths;
type Props = InferGetStaticPropsType<typeof getStaticPaths>;
const { page, post, newer, older } = Astro.props;
const { body: Content, toc } = await page.data.load();
---
<Layout title={post.title} description={post.description}>
<BlogPostPage
client:load
pathname={Astro.url.pathname}
params={Astro.params}
post={post}
newer={newer}
older={older}
toc={toc}
>
<Content />
</BlogPostPage>
</Layout>標籤頁面
getTags() 會列出所有標籤:
---
import Layout from '@/layouts/Layout.astro';
import { BlogTagsPage } from '@/components/blog-tags';
import { blogSource } from '@/lib/source';
import { getPosts, getTags } from '@/lib/blog';
---
<Layout title="All Tags">
<BlogTagsPage
client:load
pathname={Astro.url.pathname}
params={Astro.params}
tags={getTags(getPosts(blogSource.getPages()))}
/>
</Layout>getTagPosts() 會列出某個標籤下的所有文章:
---
import type { GetStaticPaths, InferGetStaticPropsType } from 'astro';
import Layout from '@/layouts/Layout.astro';
import { BlogTagPage } from '@/components/blog-tag';
import { blogAuthors, blogSource } from '@/lib/source';
import { getPosts, getTagPosts, getTags } from '@/lib/blog';
export const getStaticPaths = (() => {
const posts = getPosts(blogSource.getPages(), blogAuthors);
return getTags(posts).map((tag) => ({
params: { tag: tag.slug },
props: getTagPosts(posts, tag.slug)!,
}));
}) satisfies GetStaticPaths;
type Props = InferGetStaticPropsType<typeof getStaticPaths>;
const { tag, posts } = Astro.props;
---
<Layout title={tag.name}>
<BlogTagPage
client:load
pathname={Astro.url.pathname}
params={Astro.params}
tag={tag}
posts={posts}
/>
</Layout>國際化
請將您的 i18n 設定傳入 getBlogUrl,此函式會用來建立標籤的 URL:
import { i18n } from '@/lib/i18n';
// 請與 `blogSource` 的 `baseUrl` 保持一致
export const getBlogUrl = createGetUrl('/blog', i18n);在路由中,將特定語系的頁面傳入 getPosts():
const posts = getPosts(blogSource.getPages(lang), blogAuthors);How is this guide?
最後更新於
