@coffic/cosy-content
v0.2.28
Published
Cosy Content
Downloads
351
Maintainers
Readme
Cosy Content
一个用于管理多种类型内容的 TypeScript 库,支持博客、课程、实验、课程、手册等多种内容类型。
功能特性
- 多种内容类型支持:博客、课程、实验、故事、手册等
- 多语言支持:内置中文和英文支持
- 类型安全:完整的 TypeScript 类型定义
- Schema 扩展:支持自定义字段,保持类型安全
- 灵活的内容管理:支持层级结构、标签、分类等
- SEO 友好:内置 SEO 优化功能
- 易于扩展:模块化设计,易于添加新的内容类型
内容类型
BlogDoc - 博客文档
适合发布独立的博客文章,支持标签、分类、作者等信息。
CourseDoc - 课程文档
适合组织成体系的课程内容,支持多级章节结构。
LessonDoc - 课程文档
适合单个课程的详细教程,支持步骤化的学习内容。
ExperimentDoc - 实验文档
适合记录技术实验和探索,支持实验过程和结果展示。
ManualDoc - 手册文档
适合展示成体系的文档,如产品用户手册、技术框架文档等。每个仓库只有一个完整的手册,按章节组织。
StoryDoc - 故事文档
适合绘本、故事等内容,支持多级章节结构和自定义字段扩展。
目录结构
content/
├── blogs/ # 博客文章
├── courses/ # 课程内容
├── lessons/ # 课程教程
├── experiments/ # 实验记录
├── stories/ # 故事内容
├── manuals/ # 手册文档
└── meta/ # 元数据快速开始
安装
npm install @coffic/cosy-content基本使用
import { blogRepo, courseRepo, storyRepo, manualRepo } from '@coffic/cosy-content';
// 获取博客文章
const blogs = await blogRepo.allBlogsByLang('zh-cn');
// 获取课程内容
const courses = await courseRepo.allCoursesByLang('zh-cn');
// 获取故事内容
const stories = await storyRepo.allStoriesByLang('zh-cn');
// 获取手册内容
const manuals = await manualRepo.allManualsByLang('zh-cn');API 参考
BlogRepo
allBlogsByLang(lang: string)- 获取指定语言的所有博客getFamousBlogs(lang: string, count?: number)- 获取精选博客getBlogsWithTag(lang: string, tag: string)- 根据标签获取博客
CourseRepo
allCoursesByLang(lang: string)- 获取指定语言的所有课程getFamousCourses(lang: string, count?: number)- 获取精选课程getCoursesWithTag(lang: string, tag: string)- 根据标签获取课程
StoryRepo
allStoriesByLang(lang: string)- 获取指定语言的所有故事getFamousStories(lang: string, count?: number)- 获取精选故事getStoriesWithTag(lang: string, tag: string)- 根据标签获取故事
ManualRepo
allManualsByLang(lang: string)- 获取指定语言的所有手册章节getImportantManuals(lang: string, count?: number)- 获取重要章节getManualsWithTag(lang: string, tag: string)- 根据标签获取手册内容getManualsByCategory(lang: string, category: string)- 根据分类获取手册内容getManualStructure(lang: string)- 获取手册的完整目录结构
配置
在 content.config.ts 中配置内容集合:
import {
makeBlogCollection,
makeCourseCollection,
makeStoryCollection,
makeManualCollection,
} from '@coffic/cosy-content/schema';
export const collections = {
blogs: makeBlogCollection('./content/blogs'),
courses: makeCourseCollection('./content/courses'),
stories: makeStoryCollection('./content/stories'),
manuals: makeManualCollection('./content/manuals'),
};Schema 扩展
支持为任何集合添加自定义字段:
import { z } from 'astro:content';
import { makeStoryCollection } from '@coffic/cosy-content/schema';
export const collections = {
stories: makeStoryCollection('./content/stories', {
// 自定义字段
background: z.string().optional(),
pageAspectRatio: z.number().optional(),
textBoxes: z.array(z.object({
slot: z.string(),
top: z.number(),
left: z.number(),
width: z.number(),
bg: z.string(),
content: z.string(),
})).optional(),
}),
};开发
构建
npm run build测试
npm run test许可证
MIT License
