ブログ
著者、タグ、記事間のナビゲーションを備えたブログを追加します。
インストール
npx @fumadocs/cli@latest add blogblogPageSchema を使ってコレクションを定義し、著者を登録します。
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 配下の各ファイルが、それぞれ1つの記事になります:
---
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 の外に置いてください。
ルート
Home Layout の配下にブログのルートを追加します。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;
}国際化
タグの URL を生成する getBlogUrl に、i18n の設定を渡してください。
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?
最終更新日
