部落格
為您的應用程式加入部落格。
安裝
npx @fumadocs/cli@latest add blog此指令會將元件安裝至 components/blog,並將文章相關的工具函式安裝至 lib/blog.ts。
請使用 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]
authors: [fuma]
image: /blog/hello-world.png
---| 欄位 | 說明 |
|---|---|
date | 發布日期,文章會依此排序 |
tags | 每個標籤在 /blog/tags 下都有專屬頁面 |
authors | 作者的鍵,無法識別的鍵會直接視為作者名稱 |
image | 封面圖片 URL,會顯示在文章卡片上 |
頁面
getPosts() 會解析頁面的作者與標籤,並依日期由新到舊排序。請在首頁版面配置底下的路由中算繪這些頁面。
文章
import type { Route } from './+types/blog';
import { HomeLayout } from 'fumadocs-ui/layouts/home';
import { baseOptions } from '@/lib/layout.shared';
import { blogAuthors, blogSource } from '@/lib/source';
import { getPosts } from '@/lib/blog';
import { BlogCards } from '@/components/blog';
export function loader() {
return getPosts(blogSource.getPages(), blogAuthors);
}
export default function Page({ loaderData }: Route.ComponentProps) {
return (
<HomeLayout {...baseOptions()}>
<div className="mx-auto w-full max-w-(--fd-layout-width) px-4 py-12">
<BlogCards posts={loaderData} />
</div>
</HomeLayout>
);
}文章
findPost() 會找出指定的文章,以及其前後較新與較舊的文章。若傳入 toc,浮動面板會顯示目錄和分享按鈕。
import type { Route } from './+types/blog.post';
import { HomeLayout } from 'fumadocs-ui/layouts/home';
import { baseOptions } from '@/lib/layout.shared';
import { blog, blogAuthors, blogSource } from '@/lib/source';
import { findPost, getPosts } from '@/lib/blog';
import { useMDXComponents } from '@/components/mdx';
import {
BlogAuthors,
BlogBackLink,
BlogBody,
BlogDate,
BlogDescription,
BlogHeader,
BlogPage,
BlogPostNav,
BlogTags,
BlogTitle,
} from '@/components/blog';
export function loader({ params }: Route.LoaderArgs) {
const page = blogSource.getPage([params.slug]);
const found = page && findPost(getPosts(blogSource.getPages(), blogAuthors), page.url);
if (!page || !found) throw new Response('Not Found', { status: 404 });
return { path: page.path, ...found };
}
export default function Page({ loaderData }: Route.ComponentProps) {
const { path, post, newer, older } = loaderData;
const page = blog.get(path)!;
const MDX = page.body;
return (
<HomeLayout {...baseOptions()}>
<BlogPage toc={page.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={useMDXComponents()} />
</BlogBody>
<BlogPostNav newer={newer} older={older} />
</BlogPage>
</HomeLayout>
);
}標籤
使用 getTags() 列出所有標籤:
import type { Route } from './+types/blog.tags';
import { HomeLayout } from 'fumadocs-ui/layouts/home';
import { baseOptions } from '@/lib/layout.shared';
import { blogSource } from '@/lib/source';
import { getPosts, getTags } from '@/lib/blog';
import { BlogHeader, BlogTagCards, BlogTitle } from '@/components/blog';
export function loader() {
return getTags(getPosts(blogSource.getPages()));
}
export default function Page({ loaderData }: Route.ComponentProps) {
return (
<HomeLayout {...baseOptions()}>
<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={loaderData} />
</div>
</HomeLayout>
);
}接著使用 getTagPosts() 取得特定標籤下的文章;若標籤不存在,則會回傳 undefined:
import type { Route } from './+types/blog.tag';
import { HomeLayout } from 'fumadocs-ui/layouts/home';
import { baseOptions } from '@/lib/layout.shared';
import { blogAuthors, blogSource } from '@/lib/source';
import { getPosts, getTagPosts } from '@/lib/blog';
import { BlogCards, BlogHeader, BlogTitle } from '@/components/blog';
export function loader({ params }: Route.LoaderArgs) {
const result = getTagPosts(getPosts(blogSource.getPages(), blogAuthors), params.tag);
if (!result) throw new Response('Not Found', { status: 404 });
return result;
}
export default function Page({ loaderData }: Route.ComponentProps) {
return (
<HomeLayout {...baseOptions()}>
<div className="mx-auto flex w-full max-w-(--fd-layout-width) flex-col gap-4 px-4 py-12">
<BlogHeader>
<BlogTitle>{loaderData.tag.name}</BlogTitle>
</BlogHeader>
<BlogCards posts={loaderData.posts} />
</div>
</HomeLayout>
);
}國際化
將某個語系的頁面傳入 getPosts(),例如 blogSource.getPages(lang)。
標籤頁面的 URL 由 lib/blog.ts 中的 getBlogUrl 產生。請將您的 i18n 設定傳給它,使其與載入器的 URL 保持一致:
import { i18n } from '@/lib/i18n';
export const getBlogUrl = createGetUrl('/blog', i18n);若要為您的文章發布 RSS 摘要,請參閱 RSS 摘要。
How is this guide?
最後更新於
