NEXTDEVKIT 博客系统
1/11/2025
NEXTDEVKIT 包含一个基于 Fumadocs MDX 构建的强大博客系统,允许你使用 Markdown 和 MDX 组件创建丰富的交互式博客内容。本指南将带你了解在 NEXTDEVKIT 项目中创建和管理博客内容所需的一切知识。
🏗️ 博客系统架构
NEXTDEVKIT 的博客系统采用良好组织的结构,将内容、组件和配置分离:
src/
├── content/
│ └── blog/
│ ├── writing-a-new-blog-post-here.md
│ ├── writing-a-new-blog-post-here.zh.md
│ ├── md-test.md
│ └── md-test.zh.md
├── app/
│ └── [locale]/
│ └── (marketing)/
│ └── blog/
│ ├── page.tsx # 博客列表页
│ ├── layout.tsx # 博客布局
│ └── [...slug]/
│ └── page.tsx # 单个博客文章
├── components/
│ └── blog/
│ ├── mdx-components.tsx # 自定义 MDX 组件
│ ├── toc.tsx # 目录
│ └── go-to-top.tsx # 返回顶部按钮
├── config/
│ └── marketing/
│ └── blog.ts # 博客配置
└── source.config.ts # 博客集合架构
这种架构提供了清晰的关注点分离,使内容管理、组件自定义和博客系统维护变得简单。
📝 编写博客文章
创建新的博客文章很简单。只需在 src/content/blog/
目录中创建一个新的 Markdown 文件,文件名为所需的 URL 段。
文件命名约定
文件名决定了博客文章的 URL 段:
- URL
https://your-app.com/blog/my-first-post
→ 文件名:my-first-post.md
- URL
https://your-app.com/blog/getting-started
→ 文件名:getting-started.md
前言结构
每篇博客文章必须包含以下结构的前言:
---
title: "我的第一篇博客文章"
description: "这是我第一篇博客文章的描述"
createdAt: 2024-01-15T10:00:00Z
updatedAt: 2024-01-15T10:00:00Z
author: "张三"
image: "/blog/my-first-post.jpg"
tags: ["教程", "入门"]
keywords: "nextdevkit, saas, 教程, 入门"
---
博客文章架构
每篇博客文章必须包含这些前言字段:
字段 | 类型 | 必需 | 描述 |
---|---|---|---|
title | string | ✅ | 博客文章标题 |
description | string | ✅ | SEO 简要描述 |
keywords | string | ❌ | SEO 关键词(逗号分隔) |
createdAt | date | ✅ | 发布日期 |
updatedAt | date | ✅ | 最后更新日期 |
tags | string[] | ❌ | 分类标签 |
author | string | ✅ | 作者姓名 |
image | string | ❌ | 封面图片 URL |
title
、description
和 keywords
字段用于 SEO 优化,帮助你的博客文章在搜索引擎中获得更好的排名。
⚙️ 博客配置
源配置
博客系统在 source.config.ts
中使用 Fumadocs 的集合系统进行配置:
import { defineCollections } from "fumadocs-mdx/config";
import { z } from "zod";
export const blogs = defineCollections({
type: "doc",
dir: "src/content/blog",
schema: (ctx) => {
return z.object({
title: z.string(),
description: z.string(),
createdAt: z.date(),
updatedAt: z.date(),
author: z.string(),
tags: z.array(z.string()).optional(),
keywords: z.string().optional(),
image: z.string().optional(),
});
},
});
博客配置
博客设置在 src/config/marketing/blog.ts
中定义:
export interface BlogConfig {
title: string;
description: string;
placeholderImage: string;
}
export async function getBlogConfig(): Promise<BlogConfig> {
const t = await getTranslations("blog");
return {
title: t("title"),
description: t("description"),
placeholderImage: "/marketing/feature-techstacks.png",
};
}
此配置允许你在保持国际化支持的同时自定义博客的外观和行为。
🌐 多语言支持
NEXTDEVKIT 的博客系统使用简单的文件命名约定支持多种语言,使为不同受众创建内容变得简单。
文件命名约定
- 默认语言(英语):
filename.md
- 其他语言:
filename.{locale}.md
示例:多语言博客文章
英语版本:getting-started.md
---
title: "Getting Started with NEXTDEVKIT"
description: "Learn how to build modern SaaS applications"
createdAt: 2024-01-15T10:00:00Z
updatedAt: 2024-01-15T10:00:00Z
tags: ["tutorial", "getting-started"]
author: "NEXTDEVKIT Team"
---
# Getting Started with NEXTDEVKIT
Welcome to NEXTDEVKIT! This guide will help you build modern SaaS applications.
## What is NEXTDEVKIT?
NEXTDEVKIT is a complete toolkit for building SaaS applications...
中文版本:getting-started.zh.md
---
title: "NEXTDEVKIT 快速入门"
description: "学习如何使用 NEXTDEVKIT 构建现代 SaaS 应用"
createdAt: 2024-01-15T10:00:00Z
updatedAt: 2024-01-15T10:00:00Z
tags: ["教程", "快速入门"]
author: "NEXTDEVKIT 团队"
---
# NEXTDEVKIT 快速入门
欢迎使用 NEXTDEVKIT!本指南将帮助您构建现代 SaaS 应用程序。
## 什么是 NEXTDEVKIT?
NEXTDEVKIT 是一个完整的 SaaS 应用程序构建工具包...
🎨 丰富内容组件
自定义 MDX 组件
NEXTDEVKIT 在 src/components/blog/mdx-components.tsx
中提供自定义 MDX 组件,用于增强内容展示:
const components = {
// 带有自定义样式的增强标题
h1: ({ className, ...props }) => (
<h1 className={cn("font-heading my-8 text-2xl md:text-[1.8rem]", className)} {...props} />
),
h2: ({ className, ...props }) => (
<h2 className={cn("font-heading mt-12 mb-4 text-xl md:text-2xl", className)} {...props} />
),
// 带有语法高亮的自定义代码块
pre: ({ className, ...props }) => (
<pre className={cn("mb-4 mt-6 overflow-x-auto rounded-lg border bg-zinc-950 py-4", className)} {...props} />
),
// 带有外部链接指示器的增强链接
a: ({ className, ...props }) => (
<a className={cn("font-medium underline underline-offset-4", className)} {...props} />
),
// ... 其他组件
};
这些组件确保所有博客文章的样式一致性和功能增强。
📑 目录
自动目录生成
NEXTDEVKIT 使用 toc.tsx
组件自动从博客文章标题生成目录:
export function DashboardTableOfContents({ items }: TocProps) {
const itemIds = React.useMemo(
() => items
.map((item) => item.url)
.filter(Boolean)
.map((id) => id?.split("#")[1]),
[items]
);
const activeHeading = useActiveItem(itemIds);
return (
<div className="space-y-2">
<Tree items={items} activeItem={activeHeading} />
</div>
);
}
目录功能
目录包含几个高级功能:
- 自动生成 基于标题结构
- 活动标题 基于滚动位置的高亮显示
- 平滑滚动 点击时滚动到相应部分
- 响应式设计 适应移动端和桌面端
- 交集观察器 准确的活动状态
🔧 自定义
添加自定义组件
你可以创建自定义 MDX 组件来增强博客文章:
export function VideoEmbed({ url, title }) {
return (
<div className="my-8">
<iframe
src={url}
title={title}
className="w-full aspect-video rounded-lg"
allowFullScreen
/>
</div>
);
}
export function Newsletter() {
return (
<div className="my-8 p-6 bg-blue-50 rounded-lg">
<h3 className="text-lg font-semibold mb-2">订阅我们的新闻通讯</h3>
<p className="text-gray-600 mb-4">获取最新的 NEXTDEVKIT 更新和技巧。</p>
<div className="flex gap-2">
<input
type="email"
placeholder="输入您的邮箱"
className="flex-1 px-3 py-2 border rounded-md"
/>
<button className="px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700">
订阅
</button>
</div>
</div>
);
}
然后将这些组件添加到你的 MDX 组件中:
import { VideoEmbed, Newsletter } from './custom-components';
const components = {
...defaultMdxComponents,
VideoEmbed,
Newsletter,
// ... 其他组件
};
在博客文章中使用自定义组件
注册后,你可以直接在 MDX 文件中使用这些组件:
# 我的博客文章
这里是一些常规的 markdown 内容。
<VideoEmbed url="https://www.youtube.com/embed/dQw4w9WgXcQ" title="Rick Roll" />
更多内容...
<Newsletter />
🔧 故障排除
常见问题和解决方案
文章不显示:
- 检查前言语法是否为正确的 YAML 格式
- 验证文件扩展名是
.md
或.mdx
- 确保日期格式正确(ISO 8601)
- 检查语言配置是否匹配文件命名
MDX 组件错误:
- 验证组件导入是否正确
- 检查 markdown 内容中的 JSX 语法
- 确保组件正确导出
- 验证组件属性是否匹配预期类型
样式问题:
- 检查 CSS 类名是否正确应用
- 验证 Tailwind 配置包含所有必要的类
- 查看 MDX 组件样式是否有冲突
- 在不同屏幕尺寸上测试响应式设计
构建错误:
- 验证前言架构是否匹配配置
- 检查前言中是否缺少必需字段
- 验证图片路径是否存在于 public 目录中
- 确保所有导入都正确解析
🚀 高级功能
代码语法高亮
NEXTDEVKIT 包含代码块的语法高亮:
// 这将自动高亮显示
function greet(name: string) {
return `你好,${name}!`;
}
图片优化
使用 Next.js Image 组件优化图片:

交互元素
使用 React 组件添加交互元素:
<button onClick={() => alert('你好!')}>点击我</button>
🔗 相关资源
🎯 下一步
现在你了解了博客系统,探索这些相关功能:
- 📚 文档系统 - 了解文档结构
- 🎨 UI 组件 - 发现可用组件
- 🌐 国际化 - 实现多语言支持
- 🔍 SEO 优化 - 提高搜索引擎可见性
NEXTDEVKIT 博客系统为创建引人入胜的交互式内容提供了强大的基础。通过 Markdown 的简单性和 React 组件的灵活性相结合,你可以构建一个真正出色的博客。
📝 最佳实践
内容组织
- 使用与 URL 结构匹配的描述性文件名
- 如果需要,按日期或类别在子目录中组织文章
- 在所有文章中保持前言的一致性
- 使用有意义的标签以便更好地发现内容
SEO 优化
- 编写引人注目的标题和描述
- 在内容中自然地使用相关关键词
- 为图片包含 alt 文本
- 优化图片以提高网络性能
- 使用正确的标题层次结构(H1、H2、H3 等)
性能考虑
- 上传前优化图片
- 对大型组件使用代码分割
- 通过仅导入必要组件来最小化包大小
- 在不同设备上测试加载性能
有了这些工具和最佳实践,你就可以为你的 NEXTDEVKIT 应用程序创建引人入胜的博客体验了!