Fumadocs rebuilt on Hamur, the original is fumadocs.dev
跳至內容
Fumadocs

部落格

新增部落格,並支援作者、標籤與文章導覽。

安裝

npx @fumadocs/cli@latest add blog

使用 blogPageSchema 定義集合,並註冊您的作者:

lib/source.ts
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 底下的每個檔案都是一篇文章:

content/blog/hello-world.mdx
---
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 以外的位置。

遠端圖片

封面圖片與頭像會透過 next/image 算繪,因此請在 images.remotePatterns 中允許這些圖片的來源主機。

路由

請在您的首頁版面配置下新增部落格路由。getPosts() 會將部落格頁面轉換為文章,並附上作者與標籤資訊。若想為文章提供訂閱摘要,請參閱 RSS 摘要。

文章列表

app/(home)/blog/page.tsx
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>
  );
}

文章頁面

findPost() 會回傳指定文章,以及它的上一篇 (較新) 與下一篇 (較舊) 文章。BlogPage 的 toc prop 會加入附有分享按鈕的浮動目錄。

app/(home)/blog/[slug]/page.tsx
import { notFound } from 'next/navigation';
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 async function Page(props: PageProps<'/blog/[slug]'>) {
  const { slug } = await props.params;
  const page = blogSource.getPage([slug]);
  const found = page && findPost(getPosts(blogSource.getPages(), blogAuthors), page.url);
  if (!page || !found) 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>
  );
}

標籤頁面

getTags() 會列出所有標籤:

app/(home)/blog/tags/page.tsx
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>
  );
}

getTagPosts() 會列出某個標籤下的所有文章:

app/(home)/blog/tags/[tag]/page.tsx
import { notFound } from 'next/navigation';
import { blogAuthors, blogSource } from '@/lib/source';
import { getPosts, getTagPosts, getTags } from '@/lib/blog';
import { BlogCards, BlogHeader, BlogTitle } from '@/components/blog';

export default async function Page(props: PageProps<'/blog/tags/[tag]'>) {
  const { tag } = await props.params;
  const result = getTagPosts(getPosts(blogSource.getPages(), blogAuthors), tag);
  if (!result) 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 function generateStaticParams() {
  return getTags(getPosts(blogSource.getPages())).map((tag) => ({ tag: tag.slug }));
}

使用 output: 'export' 時,若 generateStaticParams() 回傳空陣列,Next.js 會建置失敗,因此請等到有文章加上標籤後,再新增這個路由。

國際化

請將您的 i18n 設定傳入 getBlogUrl,此函式會用來建立標籤的 URL:

lib/blog.ts
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?

最後更新於