跳至內容
Fumadocs

部落格

為您的應用程式加入部落格。

安裝

npx @fumadocs/cli@latest add blog

此指令會將元件安裝至 components/blog,並將文章相關的工具函式安裝至 lib/blog.ts。

請使用 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]
authors: [fuma]
image: /blog/hello-world.png
---
欄位說明
date發布日期,文章會依此排序
tags每個標籤在 /blog/tags 下都有專屬頁面
authors作者的鍵,無法識別的鍵會直接視為作者名稱
image封面圖片 URL,會顯示在文章卡片上

頁面

getPosts() 會解析頁面的作者與標籤,並依日期由新到舊排序。請在首頁版面配置底下的路由中算繪這些頁面。

文章

src/pages/(home)/blog/index.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>
  );
}

export const getConfig = async () => ({ render: 'static' }) as const;

文章

findPost() 會找出指定的文章,以及其前後較新與較舊的文章。若傳入 toc,浮動面板會顯示目錄和分享按鈕。

src/pages/(home)/blog/[slug].tsx
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() 列出所有標籤:

src/pages/(home)/blog/tags/index.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>
  );
}

export const getConfig = async () => ({ render: 'static' }) as const;

接著使用 getTagPosts() 取得特定標籤下的文章;若標籤不存在,則會回傳 undefined:

src/pages/(home)/blog/tags/[tag].tsx
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;
}

國際化

將某個語系的頁面傳入 getPosts(),例如 blogSource.getPages(lang)。

標籤頁面的 URL 由 lib/blog.ts 中的 getBlogUrl 產生。請將您的 i18n 設定傳給它,使其與載入器的 URL 保持一致:

lib/blog.ts
import { i18n } from '@/lib/i18n';

export const getBlogUrl = createGetUrl('/blog', i18n);

若要為您的文章發布 RSS 摘要,請參閱 RSS 摘要。

How is this guide?

最後更新於