# Fumadocs (Framework Mode, Next.js): 部落格
URL: /zh-TW/docs/next/integrations/blog
Source: https://raw.githubusercontent.com/fuma-nama/fumadocs/refs/heads/main/apps/docs/content/docs/(framework)/[framework]/integrations/blog.zh-TW.mdx

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

## 安裝 [#installation]

```npm
npx @fumadocs/cli@latest add blog
```

使用 `blogPageSchema` 定義集合，並註冊您的作者：

```ts title="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',
  },
};
```

## 撰寫文章 [#writing-posts]

`content/blog` 底下的每個檔案都是一篇文章：

```mdx title="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` 以外的位置。

<Callout title="遠端圖片">
封面圖片與頭像會透過 `next/image` 算繪，因此請在
`images.remotePatterns` 中允許這些圖片的來源主機。
</Callout>

## 路由 [#routes]

請在您的[首頁版面配置](/docs/ui/layouts/home-layout)下新增部落格路由。`getPosts()` 會將部落格頁面轉換為文章，並附上作者與標籤資訊。若想為文章提供訂閱摘要，請參閱 [RSS 摘要](/docs/next/guides/rss)。

### 文章列表 [#post-list]

```tsx title="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>
  );
}
```

### 文章頁面 [#post-page]

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

```tsx title="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>
  );
}
```

### 標籤頁面 [#tag-pages]

`getTags()` 會列出所有標籤：

```tsx title="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()` 會列出某個標籤下的所有文章：

```tsx title="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 會建置失敗，因此請等到有文章加上標籤後，再新增這個路由。

## 國際化 [#internationalization]

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

```ts title="lib/blog.ts"
import { i18n } from '@/lib/i18n';

// 請與 `blogSource` 的 `baseUrl` 保持一致
export const getBlogUrl = createGetUrl('/blog', i18n);
```

在路由中，將特定語系的頁面傳入 `getPosts()`：

```ts
const posts = getPosts(blogSource.getPages(lang), blogAuthors);
```