部落格
新增部落格,並支援作者、標籤與文章導覽。
安裝
npx @fumadocs/cli@latest add blog使用 blogPageSchema 定義集合,並註冊您的作者:
import { defineCollections } from 'fumadocs-mdx/macro';
import { loader } from 'fumadocs-core/source';
import { type BlogAuthor, blogPageSchema } from './blog';
export const blog = defineCollections({
type: 'doc',
dir: 'content/blog',
schema: blogPageSchema,
});
export const blogSource = loader({
baseUrl: '/blog',
source: blog.toFumadocsSource(),
});
export const blogAuthors: Record<string, BlogAuthor> = {
fuma: {
name: 'Fuma Nama',
title: 'Maintainer',
url: 'https://fuma-nama.dev',
image: 'https://avatars.githubusercontent.com/u/76240755',
},
};撰寫文章
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 { blogAuthors, blogSource } from '@/lib/source';
import { getPosts } from '@/lib/blog';
import { BlogCards } from '@/components/blog';
export default function Page() {
return (
<div className="mx-auto w-full max-w-(--fd-layout-width) px-4 py-12">
<BlogCards posts={getPosts(blogSource.getPages(), blogAuthors)} />
</div>
);
}
export const getConfig = async () => ({ render: 'static' }) as const;文章頁面
findPost() 會回傳指定文章,以及它的上一篇 (較新) 與下一篇 (較舊) 文章。BlogPage 的 toc prop 會加入附有分享按鈕的浮動目錄。
import type { PageProps } from 'waku/router';
import { unstable_notFound } from 'waku/router/server';
import { blogAuthors, blogSource } from '@/lib/source';
import { findPost, getPosts } from '@/lib/blog';
import { getMDXComponents } from '@/components/mdx';
import {
BlogAuthors,
BlogBackLink,
BlogBody,
BlogDate,
BlogDescription,
BlogHeader,
BlogPage,
BlogPostNav,
BlogTags,
BlogTitle,
} from '@/components/blog';
export default function Page({ slug }: PageProps<'/blog/[slug]'>) {
const page = blogSource.getPage([slug]);
const found = page && findPost(getPosts(blogSource.getPages(), blogAuthors), page.url);
if (!page || !found) unstable_notFound();
const { post, newer, older } = found;
const MDX = page.data.body;
return (
<BlogPage toc={page.data.toc}>
<BlogHeader>
<BlogBackLink href="/blog" />
<BlogTitle>{post.title}</BlogTitle>
<BlogDescription>{post.description}</BlogDescription>
<BlogAuthors authors={post.authors} />
<BlogTags tags={post.tags} />
{post.date && <BlogDate date={post.date} />}
</BlogHeader>
<BlogBody>
<MDX components={getMDXComponents()} />
</BlogBody>
<BlogPostNav newer={newer} older={older} />
</BlogPage>
);
}
export async function getConfig() {
return {
render: 'static',
staticPaths: blogSource.getPages().map((page) => page.slugs),
} as const;
}標籤頁面
getTags() 會列出所有標籤:
import { blogSource } from '@/lib/source';
import { getPosts, getTags } from '@/lib/blog';
import { BlogHeader, BlogTagCards, BlogTitle } from '@/components/blog';
export default function Page() {
const tags = getTags(getPosts(blogSource.getPages()));
return (
<div className="mx-auto flex w-full max-w-(--fd-layout-width) flex-col gap-4 px-4 py-12">
<BlogHeader>
<BlogTitle>All Tags</BlogTitle>
</BlogHeader>
<BlogTagCards tags={tags} />
</div>
);
}
export const getConfig = async () => ({ render: 'static' }) as const;getTagPosts() 會列出某個標籤下的所有文章:
import type { PageProps } from 'waku/router';
import { unstable_notFound } from 'waku/router/server';
import { blogAuthors, blogSource } from '@/lib/source';
import { getPosts, getTagPosts, getTags } from '@/lib/blog';
import { BlogCards, BlogHeader, BlogTitle } from '@/components/blog';
export default function Page({ tag }: PageProps<'/blog/tags/[tag]'>) {
const result = getTagPosts(getPosts(blogSource.getPages(), blogAuthors), tag);
if (!result) unstable_notFound();
return (
<div className="mx-auto flex w-full max-w-(--fd-layout-width) flex-col gap-4 px-4 py-12">
<BlogHeader>
<BlogTitle>{result.tag.name}</BlogTitle>
</BlogHeader>
<BlogCards posts={result.posts} />
</div>
);
}
export async function getConfig() {
return {
render: 'static',
staticPaths: getTags(getPosts(blogSource.getPages())).map((tag) => tag.slug),
} as const;
}國際化
請將您的 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?
最後更新於
