@coffic/cosy-ui 0.9.4 → 0.9.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index-astro.ts +2 -19
- package/dist/index-collection.ts +1 -105
- package/dist/src-astro/collection/entities/BaseDoc.ts +28 -0
- package/dist/src-astro/collection/entities/BlogDoc.ts +221 -0
- package/dist/src-astro/collection/entities/CourseDoc.ts +254 -0
- package/dist/src-astro/collection/entities/ExperimentDoc.ts +169 -0
- package/dist/src-astro/collection/entities/LessonDoc.ts +203 -0
- package/dist/src-astro/collection/entities/MetaDoc.ts +115 -0
- package/dist/src-astro/{entities → collection/entities}/SidebarItem.ts +17 -17
- package/dist/src-astro/collection/entities/Tag.ts +42 -0
- package/dist/src-astro/collection/index.ts +11 -0
- package/dist/src-astro/collection/repos/BaseRepo.ts +276 -0
- package/dist/src-astro/collection/repos/BlogRepo.ts +226 -0
- package/dist/src-astro/collection/repos/CourseRepo.ts +137 -0
- package/dist/src-astro/collection/repos/ExperimentRepo.ts +121 -0
- package/dist/src-astro/collection/repos/LessonRepo.ts +128 -0
- package/dist/src-astro/collection/repos/MetaRepo.ts +89 -0
- package/package.json +1 -1
- package/dist/src-astro/database/BaseDB.ts +0 -264
- package/dist/src-astro/database/BlogDB.ts +0 -198
- package/dist/src-astro/database/CourseDB.ts +0 -90
- package/dist/src-astro/database/ExperimentDB.ts +0 -106
- package/dist/src-astro/database/LessonDB.ts +0 -106
- package/dist/src-astro/database/MetaDB.ts +0 -74
- package/dist/src-astro/database/index.ts +0 -3
- package/dist/src-astro/entities/BaseDoc.ts +0 -207
- package/dist/src-astro/entities/BlogDoc.ts +0 -107
- package/dist/src-astro/entities/CourseDoc.ts +0 -102
- package/dist/src-astro/entities/ExperimentDoc.ts +0 -119
- package/dist/src-astro/entities/LessonDoc.ts +0 -153
- package/dist/src-astro/entities/MetaDoc.ts +0 -93
- package/dist/src-astro/entities/Tag.ts +0 -42
- /package/dist/src-astro/{entities → collection/entities}/Feature.ts +0 -0
@@ -0,0 +1,276 @@
|
|
1
|
+
import {
|
2
|
+
getCollection,
|
3
|
+
getEntry,
|
4
|
+
type CollectionEntry,
|
5
|
+
} from 'astro:content';
|
6
|
+
import { cosyLogger, ERROR_PREFIX } from '../../cosy';
|
7
|
+
import type { BaseDoc } from '../entities/BaseDoc';
|
8
|
+
|
9
|
+
/**
|
10
|
+
* BaseDB 是所有数据库类的基类,提供了通用的文档操作功能。
|
11
|
+
*
|
12
|
+
* 使用方法:
|
13
|
+
* ```typescript
|
14
|
+
* class MyDB extends BaseDB<'collection', MyEntry, MyDoc> {
|
15
|
+
* protected collectionName = 'collection' as const;
|
16
|
+
* protected createDoc(entry: MyEntry): MyDoc {
|
17
|
+
* return new MyDoc(entry);
|
18
|
+
* }
|
19
|
+
* }
|
20
|
+
*
|
21
|
+
* // 使用单例模式获取实例
|
22
|
+
* const db = MyDB.getInstance();
|
23
|
+
* const docs = await db.allTopLevelDocs();
|
24
|
+
* ```
|
25
|
+
*
|
26
|
+
* 类型参数说明:
|
27
|
+
* @template Collection - Astro content collection 的名称
|
28
|
+
* @template Entry - Collection 对应的条目类型
|
29
|
+
* @template Doc - 文档类型,通常是自定义的文档类
|
30
|
+
*/
|
31
|
+
export abstract class BaseDB<
|
32
|
+
Collection extends string,
|
33
|
+
Entry extends CollectionEntry<Collection>,
|
34
|
+
Doc extends BaseDoc,
|
35
|
+
> {
|
36
|
+
/**
|
37
|
+
* 集合名称,必须在子类中指定
|
38
|
+
*
|
39
|
+
* 例如:
|
40
|
+
* ```typescript
|
41
|
+
* protected collectionName = 'blogs' as const;
|
42
|
+
* ```
|
43
|
+
*/
|
44
|
+
protected abstract collectionName: Collection;
|
45
|
+
|
46
|
+
/**
|
47
|
+
* 创建文档实例的方法,必须在子类中实现
|
48
|
+
* @param entry - 集合条目
|
49
|
+
* @returns 文档实例
|
50
|
+
*/
|
51
|
+
protected abstract createDoc(entry: Entry): Doc;
|
52
|
+
|
53
|
+
/**
|
54
|
+
* 获取所有文档的ID
|
55
|
+
* @returns 返回所有文档的ID数组
|
56
|
+
*/
|
57
|
+
protected async getAllIds(): Promise<string[]> {
|
58
|
+
const entries = await getCollection(this.collectionName);
|
59
|
+
return entries.map((entry: CollectionEntry<Collection>) => entry.id);
|
60
|
+
}
|
61
|
+
|
62
|
+
/**
|
63
|
+
* 获取集合中的所有条目
|
64
|
+
* @returns 返回所有条目的数组
|
65
|
+
*/
|
66
|
+
protected async getEntries(): Promise<Entry[]> {
|
67
|
+
return await getCollection(this.collectionName);
|
68
|
+
}
|
69
|
+
|
70
|
+
/**
|
71
|
+
* 获取指定深度的文档
|
72
|
+
* 深度是指文档 ID 中斜杠的数量加1
|
73
|
+
* 例如:
|
74
|
+
* - "blog.md" 深度为1
|
75
|
+
* - "zh-cn/blog.md" 深度为2
|
76
|
+
* - "zh-cn/tech/blog.md" 深度为3
|
77
|
+
*
|
78
|
+
* @param depth - 文档深度
|
79
|
+
* @returns 返回指定深度的文档数组
|
80
|
+
*/
|
81
|
+
protected async getDocsByDepth(depth: number): Promise<Doc[]> {
|
82
|
+
const entries = await getCollection(
|
83
|
+
this.collectionName,
|
84
|
+
({ id }: { id: string }) => id.split('/').length === depth
|
85
|
+
);
|
86
|
+
|
87
|
+
if (entries.length === 0) {
|
88
|
+
cosyLogger.warn(
|
89
|
+
`[BaseDB] 没有找到深度为${depth}的文档(collection=${this.collectionName as string})`
|
90
|
+
);
|
91
|
+
const allEntries = await getCollection(this.collectionName);
|
92
|
+
cosyLogger.array(
|
93
|
+
'[BaseDB] 所有文档',
|
94
|
+
allEntries.map((entry: CollectionEntry<Collection>) => entry.id)
|
95
|
+
);
|
96
|
+
return [];
|
97
|
+
}
|
98
|
+
|
99
|
+
return entries.map((entry: CollectionEntry<Collection>) =>
|
100
|
+
this.createDoc(entry as Entry)
|
101
|
+
);
|
102
|
+
}
|
103
|
+
|
104
|
+
/**
|
105
|
+
* 根据ID查找单个文档
|
106
|
+
* @param id - 文档ID
|
107
|
+
* @param debug - 是否启用调试模式, 默认为false
|
108
|
+
* @throws 如果ID不是字符串类型,则抛出错误
|
109
|
+
* @throws 如果文档不存在,则返回null
|
110
|
+
* @throws 如果发生其他错误,则抛出错误
|
111
|
+
* @returns 返回找到的文档,如果不存在则返回null
|
112
|
+
*/
|
113
|
+
async find(id: string, debug: boolean = false): Promise<Doc | null> {
|
114
|
+
if (debug) {
|
115
|
+
cosyLogger.info(`查找文档,ID: ${id}`);
|
116
|
+
}
|
117
|
+
|
118
|
+
if (id == undefined) {
|
119
|
+
cosyLogger.error('ID is undefined');
|
120
|
+
throw new Error(ERROR_PREFIX + 'ID is undefined');
|
121
|
+
}
|
122
|
+
|
123
|
+
if (typeof id !== 'string') {
|
124
|
+
cosyLogger.error('ID must be a string, but got ' + typeof id);
|
125
|
+
cosyLogger.debug(id);
|
126
|
+
throw new Error(
|
127
|
+
ERROR_PREFIX + 'ID must be a string, but got ' + typeof id
|
128
|
+
);
|
129
|
+
}
|
130
|
+
|
131
|
+
// 获取所有文档的ID并排好顺序
|
132
|
+
if (debug) {
|
133
|
+
const allIds = (await this.getAllIds()).sort();
|
134
|
+
cosyLogger.array('所有文档的ID', allIds);
|
135
|
+
}
|
136
|
+
|
137
|
+
// 根据ID查找文档
|
138
|
+
const entry = await getEntry(this.collectionName, id);
|
139
|
+
return entry ? this.createDoc(entry as Entry) : null;
|
140
|
+
}
|
141
|
+
|
142
|
+
/**
|
143
|
+
* 获取指定文档的直接子文档(不包括更深层级的文档)
|
144
|
+
* 例如对于文档 "zh-cn/blog":
|
145
|
+
* - "zh-cn/blog/post1.md" 会被包含
|
146
|
+
* - "zh-cn/blog/2024/post2.md" 不会被包含
|
147
|
+
*
|
148
|
+
* @param parentId - 父文档ID
|
149
|
+
* @returns 返回子文档数组
|
150
|
+
*/
|
151
|
+
async getChildren(parentId: string): Promise<Doc[]> {
|
152
|
+
const parentLevel = parentId.split('/').length;
|
153
|
+
const childrenLevel = parentLevel + 1;
|
154
|
+
|
155
|
+
const entries = await getCollection(
|
156
|
+
this.collectionName,
|
157
|
+
({ id }: { id: string }) =>
|
158
|
+
id.startsWith(parentId) && id.split('/').length === childrenLevel
|
159
|
+
);
|
160
|
+
|
161
|
+
return entries.map((entry: CollectionEntry<Collection>) =>
|
162
|
+
this.createDoc(entry as Entry)
|
163
|
+
);
|
164
|
+
}
|
165
|
+
|
166
|
+
/**
|
167
|
+
* 获取指定文档的所有后代文档(包括所有层级)
|
168
|
+
* 例如对于文档 "zh-cn/blog",以下都会被包含:
|
169
|
+
* - "zh-cn/blog/post1.md"
|
170
|
+
* - "zh-cn/blog/2024/post2.md"
|
171
|
+
* - "zh-cn/blog/2024/tech/post3.md"
|
172
|
+
*
|
173
|
+
* @param parentId - 父文档ID
|
174
|
+
* @returns 返回所有后代文档数组
|
175
|
+
*/
|
176
|
+
async getDescendantDocs(parentId: string): Promise<Doc[]> {
|
177
|
+
const entries = await getCollection(
|
178
|
+
this.collectionName,
|
179
|
+
({ id }: { id: string }) => id.startsWith(parentId)
|
180
|
+
);
|
181
|
+
return entries.map((entry: CollectionEntry<Collection>) =>
|
182
|
+
this.createDoc(entry as Entry)
|
183
|
+
);
|
184
|
+
}
|
185
|
+
|
186
|
+
/**
|
187
|
+
* 获取指定语言和级别的文档
|
188
|
+
* 通过检查文档ID是否以指定语言代码开头来筛选
|
189
|
+
*
|
190
|
+
* @param lang - 语言代码(如 'zh-cn', 'en')
|
191
|
+
* @param level - 文档级别
|
192
|
+
* @returns 返回指定语言和级别的文档数组
|
193
|
+
*/
|
194
|
+
protected async allDocsByLangAndLevel(
|
195
|
+
lang: string,
|
196
|
+
level: number = 1,
|
197
|
+
debug: boolean = false
|
198
|
+
): Promise<Doc[]> {
|
199
|
+
const collectionName = this.collectionName as string;
|
200
|
+
const docs = await this.getDocsByDepth(level);
|
201
|
+
|
202
|
+
if (debug) {
|
203
|
+
cosyLogger.array(
|
204
|
+
`[BaseDB] 所有${level}级文档(lang=any,collection=${collectionName})`,
|
205
|
+
docs
|
206
|
+
);
|
207
|
+
}
|
208
|
+
|
209
|
+
if (docs.length === 0) {
|
210
|
+
cosyLogger.warn(
|
211
|
+
`[BaseDB] 没有找到${level}级文档(lang=any,collection=${collectionName})`
|
212
|
+
);
|
213
|
+
return [];
|
214
|
+
}
|
215
|
+
|
216
|
+
const filteredDocs = docs.filter((doc) => {
|
217
|
+
const id = (doc as any).getId();
|
218
|
+
return id && typeof id === 'string' && id.startsWith(lang);
|
219
|
+
});
|
220
|
+
|
221
|
+
if (debug) {
|
222
|
+
cosyLogger.array(
|
223
|
+
`[BaseDB] 所有${level}级文档(lang=${lang},collection=${collectionName})`,
|
224
|
+
filteredDocs
|
225
|
+
);
|
226
|
+
}
|
227
|
+
|
228
|
+
if (filteredDocs.length === 0) {
|
229
|
+
cosyLogger.warn(
|
230
|
+
`[BaseDB] 没有找到${level}级文档(lang=${lang},collection=${collectionName})`
|
231
|
+
);
|
232
|
+
cosyLogger.array(
|
233
|
+
`[BaseDB] 所有${level}级文档`,
|
234
|
+
docs.map((doc) => doc.getId())
|
235
|
+
);
|
236
|
+
return [];
|
237
|
+
}
|
238
|
+
|
239
|
+
return filteredDocs;
|
240
|
+
}
|
241
|
+
|
242
|
+
/**
|
243
|
+
* 获取用于 Astro 静态路由生成的路径参数
|
244
|
+
* 为每个文档生成包含语言和slug的路径参数
|
245
|
+
*
|
246
|
+
* @returns 返回路径参数数组,每个元素包含 lang 和 slug
|
247
|
+
* @example
|
248
|
+
* ```typescript
|
249
|
+
* const paths = await db.getStaticPaths();
|
250
|
+
* // 返回格式:
|
251
|
+
* // [
|
252
|
+
* // { params: { lang: 'zh-cn', slug: 'post1' } },
|
253
|
+
* // { params: { lang: 'en', slug: 'post1' } }
|
254
|
+
* // ]
|
255
|
+
* ```
|
256
|
+
*/
|
257
|
+
async getStaticPaths() {
|
258
|
+
const debug = false;
|
259
|
+
const docs = await this.getDescendantDocs('');
|
260
|
+
const paths = docs.map((doc) => {
|
261
|
+
const docWithMethods = doc as any;
|
262
|
+
return {
|
263
|
+
params: {
|
264
|
+
lang: docWithMethods.getLang?.() || '',
|
265
|
+
slug: docWithMethods.getSlug?.() || '',
|
266
|
+
},
|
267
|
+
};
|
268
|
+
});
|
269
|
+
|
270
|
+
if (debug) {
|
271
|
+
cosyLogger.array('所有文档的路径', paths);
|
272
|
+
}
|
273
|
+
|
274
|
+
return paths;
|
275
|
+
}
|
276
|
+
}
|
@@ -0,0 +1,226 @@
|
|
1
|
+
import BlogDoc from '../entities/BlogDoc';
|
2
|
+
import type Tag from '../entities/Tag';
|
3
|
+
import { cosyLogger } from '../../cosy';
|
4
|
+
import { defineCollection, z, type CollectionEntry } from 'astro:content';
|
5
|
+
import { BaseDB } from './BaseRepo';
|
6
|
+
import { glob } from 'astro/loaders';
|
7
|
+
|
8
|
+
export const COLLECTION_BLOG = 'blogs' as const;
|
9
|
+
export type BlogEntry = CollectionEntry<typeof COLLECTION_BLOG>;
|
10
|
+
|
11
|
+
/**
|
12
|
+
* 博客数据库类,用于管理博客内容集合。
|
13
|
+
*
|
14
|
+
* 目录结构:
|
15
|
+
* ```
|
16
|
+
* blogs/
|
17
|
+
* ├── zh-cn/
|
18
|
+
* │ ├── typescript-intro.md # 文章内容
|
19
|
+
* │ ├── images/ # 文章相关图片
|
20
|
+
* │ └── web-performance.md
|
21
|
+
* └── en/
|
22
|
+
* └── typescript-intro.md
|
23
|
+
* └── images/
|
24
|
+
* └── web-performance.md
|
25
|
+
* ```
|
26
|
+
*/
|
27
|
+
class BlogRepo extends BaseDB<typeof COLLECTION_BLOG, BlogEntry, BlogDoc> {
|
28
|
+
protected collectionName = COLLECTION_BLOG;
|
29
|
+
|
30
|
+
protected createDoc(entry: BlogEntry): BlogDoc {
|
31
|
+
return BlogDoc.fromEntry(entry);
|
32
|
+
}
|
33
|
+
|
34
|
+
/**
|
35
|
+
* 获取所有博客
|
36
|
+
*
|
37
|
+
* @returns {Promise<BlogDoc[]>} 返回所有博客
|
38
|
+
*/
|
39
|
+
async allBlogs(): Promise<BlogDoc[]> {
|
40
|
+
const debug = false;
|
41
|
+
const entries = await this.getDocsByDepth(2);
|
42
|
+
|
43
|
+
if (debug) {
|
44
|
+
cosyLogger.array('所有博客文档', entries);
|
45
|
+
}
|
46
|
+
|
47
|
+
return entries;
|
48
|
+
}
|
49
|
+
|
50
|
+
/**
|
51
|
+
* 获取指定语言的所有博客
|
52
|
+
*
|
53
|
+
* @param lang - 语言代码
|
54
|
+
* @returns {Promise<BlogDoc[]>} 返回指定语言的所有博客
|
55
|
+
*/
|
56
|
+
async allBlogsByLang(lang: string): Promise<BlogDoc[]> {
|
57
|
+
const docs = await this.allBlogs();
|
58
|
+
const filteredEntries = docs.filter((doc) => doc.getId().startsWith(lang));
|
59
|
+
|
60
|
+
return filteredEntries;
|
61
|
+
}
|
62
|
+
|
63
|
+
/**
|
64
|
+
* 获取用于 Astro 静态路由生成的路径参数,专门配合 [lang]/blogs/[slug].astro 使用
|
65
|
+
*
|
66
|
+
* @param debug - 是否开启调试模式
|
67
|
+
* @returns 返回路径参数数组
|
68
|
+
*/
|
69
|
+
async getStaticPaths(debug: boolean = false) {
|
70
|
+
const docs = await this.allBlogs();
|
71
|
+
|
72
|
+
const paths = docs.map((doc) => {
|
73
|
+
return {
|
74
|
+
params: {
|
75
|
+
lang: doc.getLang(),
|
76
|
+
slug: doc.getSlug(),
|
77
|
+
},
|
78
|
+
};
|
79
|
+
});
|
80
|
+
|
81
|
+
if (debug) {
|
82
|
+
cosyLogger.array('所有博客文档的路径', paths);
|
83
|
+
}
|
84
|
+
|
85
|
+
return paths;
|
86
|
+
}
|
87
|
+
|
88
|
+
/**
|
89
|
+
* 获取所有博客标签
|
90
|
+
* 标签会根据语言和名称去重,使用复合键 "lang:name" 确保唯一性
|
91
|
+
*
|
92
|
+
* @returns 返回所有标签数组
|
93
|
+
* 返回格式:
|
94
|
+
* [
|
95
|
+
* { name: 'typescript', lang: 'zh-cn', count: 5 },
|
96
|
+
* { name: 'javascript', lang: 'en', count: 3 }
|
97
|
+
* ]
|
98
|
+
*/
|
99
|
+
async getTags(): Promise<Tag[]> {
|
100
|
+
const tagsMap = new Map<string, Tag>();
|
101
|
+
const posts = await this.allBlogs();
|
102
|
+
|
103
|
+
posts.forEach((post) => {
|
104
|
+
post.getTags().forEach((tag) => {
|
105
|
+
const key = `${tag.lang}:${tag.name}`;
|
106
|
+
if (!tagsMap.has(key)) {
|
107
|
+
tagsMap.set(key, tag);
|
108
|
+
}
|
109
|
+
});
|
110
|
+
});
|
111
|
+
|
112
|
+
return Array.from(tagsMap.values());
|
113
|
+
}
|
114
|
+
|
115
|
+
/**
|
116
|
+
* 获取指定语言的博客标签
|
117
|
+
*
|
118
|
+
* @param lang - 语言代码(如 'zh-cn', 'en')
|
119
|
+
* @returns 返回指定语言的标签数组
|
120
|
+
*/
|
121
|
+
async getTagsByLang(lang: string): Promise<Tag[]> {
|
122
|
+
const debug = false;
|
123
|
+
const tagsMap = new Map<string, Tag>();
|
124
|
+
const posts = await this.allBlogsByLang(lang);
|
125
|
+
|
126
|
+
if (debug) {
|
127
|
+
cosyLogger.array('posts', posts);
|
128
|
+
}
|
129
|
+
|
130
|
+
if (posts.length === 0) {
|
131
|
+
return [];
|
132
|
+
}
|
133
|
+
|
134
|
+
posts.forEach((post) => {
|
135
|
+
post.getTags().forEach((tag) => {
|
136
|
+
const key = `${tag.lang}:${tag.name}`;
|
137
|
+
if (!tagsMap.has(key)) {
|
138
|
+
tagsMap.set(key, tag);
|
139
|
+
}
|
140
|
+
});
|
141
|
+
});
|
142
|
+
|
143
|
+
if (debug) {
|
144
|
+
cosyLogger.array('tags', Array.from(tagsMap.values()));
|
145
|
+
}
|
146
|
+
|
147
|
+
return Array.from(tagsMap.values());
|
148
|
+
}
|
149
|
+
|
150
|
+
/**
|
151
|
+
* 获取指定标签和语言的博客文章
|
152
|
+
*
|
153
|
+
* @param tag - 标签名称
|
154
|
+
* @param lang - 语言代码
|
155
|
+
* @returns 返回匹配的博客文档数组
|
156
|
+
* @example
|
157
|
+
* ```typescript
|
158
|
+
* const posts = await blogDB.getBlogsByTag('typescript', 'zh-cn');
|
159
|
+
* // 返回所有包含 'typescript' 标签的中文博客
|
160
|
+
* ```
|
161
|
+
*/
|
162
|
+
async getBlogsByTag(tag: string, lang: string): Promise<BlogDoc[]> {
|
163
|
+
const posts = await this.allBlogsByLang(lang);
|
164
|
+
return posts.filter((post) => post.getTags().some((t) => t.name === tag));
|
165
|
+
}
|
166
|
+
|
167
|
+
/**
|
168
|
+
* 获取标签的静态路径参数,用于生成标签页面的路由,专门配合 [lang]/blogs/tag/[name].astro 使用
|
169
|
+
*
|
170
|
+
* @returns 返回所有标签的路径参数数组
|
171
|
+
* 返回格式:
|
172
|
+
* [
|
173
|
+
* { params: { lang: 'zh-cn', name: 'typescript' } },
|
174
|
+
* { params: { lang: 'en', name: 'javascript' } }
|
175
|
+
* ]
|
176
|
+
*/
|
177
|
+
async getTagsStaticPaths() {
|
178
|
+
const debug = false;
|
179
|
+
const tags = await this.getTags();
|
180
|
+
|
181
|
+
const paths = tags.map((tag) => {
|
182
|
+
return {
|
183
|
+
params: {
|
184
|
+
lang: tag.lang,
|
185
|
+
name: tag.name,
|
186
|
+
},
|
187
|
+
};
|
188
|
+
});
|
189
|
+
|
190
|
+
if (debug) {
|
191
|
+
cosyLogger.array('所有的标签路径', paths);
|
192
|
+
}
|
193
|
+
|
194
|
+
return paths;
|
195
|
+
}
|
196
|
+
|
197
|
+
makeBlogCollection = (base: string) => {
|
198
|
+
return defineCollection({
|
199
|
+
loader: glob({
|
200
|
+
pattern: '**/*.{md,mdx}',
|
201
|
+
base,
|
202
|
+
}),
|
203
|
+
schema: z.object({
|
204
|
+
title: z.string(),
|
205
|
+
description: z.string().optional(),
|
206
|
+
tags: z.array(z.string()).optional(),
|
207
|
+
date: z.date().optional(),
|
208
|
+
draft: z.boolean().optional(),
|
209
|
+
hidden: z.boolean().optional(),
|
210
|
+
famous: z.boolean().optional(),
|
211
|
+
authors: z
|
212
|
+
.array(
|
213
|
+
z.object({
|
214
|
+
name: z.string(),
|
215
|
+
picture: z.string().optional(),
|
216
|
+
url: z.string().optional(),
|
217
|
+
})
|
218
|
+
)
|
219
|
+
.optional(),
|
220
|
+
}),
|
221
|
+
});
|
222
|
+
};
|
223
|
+
}
|
224
|
+
|
225
|
+
// 创建并导出单例实例
|
226
|
+
export const blogRepo = new BlogRepo();
|
@@ -0,0 +1,137 @@
|
|
1
|
+
import CourseDoc from '../entities/CourseDoc';
|
2
|
+
import { defineCollection, getCollection, z, type CollectionEntry } from 'astro:content';
|
3
|
+
import { BaseDB } from './BaseRepo';
|
4
|
+
import { glob } from 'astro/loaders';
|
5
|
+
|
6
|
+
export const COLLECTION_COURSE = 'courses' as const;
|
7
|
+
export type CourseEntry = CollectionEntry<typeof COLLECTION_COURSE>;
|
8
|
+
|
9
|
+
/**
|
10
|
+
* 课程数据库类,用于管理课程内容集合。
|
11
|
+
*
|
12
|
+
* 目录结构:
|
13
|
+
* ```
|
14
|
+
* courses/
|
15
|
+
* ├── zh-cn/ # 中文版本
|
16
|
+
* │ ├── web-development/ # 课程1
|
17
|
+
* │ │ ├── index.md # 课程文档
|
18
|
+
* │ │ ├── chapter1
|
19
|
+
* │ │ │ ├── index.md
|
20
|
+
* │ │ │ ├── content.md
|
21
|
+
* │ │ │ └── ...
|
22
|
+
* │ │ └── chapter2
|
23
|
+
* │ │ ├── index.md
|
24
|
+
* │ │ ├── content.md
|
25
|
+
* │ │ └── ...
|
26
|
+
* │ └── mobile-dev/ # 课程2
|
27
|
+
* │ ├── index.md
|
28
|
+
* │ └── ...
|
29
|
+
* └── en/ # 英文版本
|
30
|
+
* └── ...
|
31
|
+
* ```
|
32
|
+
*
|
33
|
+
* 说明:
|
34
|
+
* - 如果希望单个课程可以作为 git 项目管理,考虑使用 LessonRepo 代替 CourseRepo
|
35
|
+
*/
|
36
|
+
class CourseRepo extends BaseDB<
|
37
|
+
typeof COLLECTION_COURSE,
|
38
|
+
CourseEntry,
|
39
|
+
CourseDoc
|
40
|
+
> {
|
41
|
+
protected collectionName = COLLECTION_COURSE;
|
42
|
+
|
43
|
+
protected createDoc(entry: CourseEntry): CourseDoc {
|
44
|
+
return new CourseDoc(entry);
|
45
|
+
}
|
46
|
+
|
47
|
+
/**
|
48
|
+
* 获取指定语言的所有顶级课程,适用于在索引页面展示课程列表的情况
|
49
|
+
*
|
50
|
+
* @param lang - 语言代码
|
51
|
+
* @returns 返回指定语言的顶级课程数组
|
52
|
+
*/
|
53
|
+
async allCoursesByLang(lang: string): Promise<CourseDoc[]> {
|
54
|
+
return await getCollection(COLLECTION_COURSE, ({ id }: { id: string }) => {
|
55
|
+
return id.startsWith(lang) && id.split('/').length === 2;
|
56
|
+
}).map((entry: CourseEntry) => new CourseDoc(entry));
|
57
|
+
}
|
58
|
+
|
59
|
+
/**
|
60
|
+
* 获取用于 Astro 静态路由生成的路径参数,专门配合 [lang]/courses/[...slug].astro 使用
|
61
|
+
*
|
62
|
+
* @returns 返回路径参数数组
|
63
|
+
*/
|
64
|
+
async getStaticPaths(): Promise<
|
65
|
+
{ params: { lang: string; slug: string } }[]
|
66
|
+
> {
|
67
|
+
const entries = await getCollection(COLLECTION_COURSE);
|
68
|
+
return entries.map((entry: CourseEntry) => {
|
69
|
+
const doc = new CourseDoc(entry);
|
70
|
+
return {
|
71
|
+
params: {
|
72
|
+
lang: doc.getLang(),
|
73
|
+
slug: doc.getSlug(),
|
74
|
+
},
|
75
|
+
};
|
76
|
+
});
|
77
|
+
}
|
78
|
+
|
79
|
+
/**
|
80
|
+
* 获取精选课程
|
81
|
+
*
|
82
|
+
* @param lang - 语言代码(如 'zh-cn', 'en')
|
83
|
+
* @param count - 返回的课程数量(默认4个)
|
84
|
+
* @returns 返回精选课程文档数组
|
85
|
+
*/
|
86
|
+
async getFamousCourses(lang: string, count: number = 4): Promise<CourseDoc[]> {
|
87
|
+
return (await this.allCoursesByLang(lang))
|
88
|
+
.filter((course) => course.isFamous())
|
89
|
+
.slice(0, count);
|
90
|
+
}
|
91
|
+
|
92
|
+
/**
|
93
|
+
* 获取指定语言的课程,并根据标签过滤
|
94
|
+
*
|
95
|
+
* @param lang - 语言代码(如 'zh-cn', 'en')
|
96
|
+
* @param tag - 标签
|
97
|
+
* @returns 返回符合条件的课程文档数组
|
98
|
+
*/
|
99
|
+
async getCoursesWithTag(lang: string, tag: string): Promise<CourseDoc[]> {
|
100
|
+
return (await this.allCoursesByLang(lang))
|
101
|
+
.filter((course) => course.hasTag(tag));
|
102
|
+
}
|
103
|
+
|
104
|
+
makeCourseCollection = (base: string) => {
|
105
|
+
return defineCollection({
|
106
|
+
loader: glob({
|
107
|
+
pattern: '**/*.{md,mdx}',
|
108
|
+
base,
|
109
|
+
}),
|
110
|
+
schema: z.object({
|
111
|
+
title: z.string().optional(),
|
112
|
+
description: z.string().optional(),
|
113
|
+
folder: z.boolean().optional(),
|
114
|
+
authors: z
|
115
|
+
.array(
|
116
|
+
z.object({
|
117
|
+
name: z.string(),
|
118
|
+
picture: z.string().optional(),
|
119
|
+
url: z.string().optional(),
|
120
|
+
})
|
121
|
+
)
|
122
|
+
.optional(),
|
123
|
+
date: z.date().optional(),
|
124
|
+
order: z.number().optional(),
|
125
|
+
badge: z.string().optional(),
|
126
|
+
draft: z.boolean().optional(),
|
127
|
+
hidden: z.boolean().optional(),
|
128
|
+
famous: z.boolean().optional(),
|
129
|
+
tags: z.array(z.string()).optional(),
|
130
|
+
category: z.string().optional(),
|
131
|
+
}),
|
132
|
+
});
|
133
|
+
};
|
134
|
+
}
|
135
|
+
|
136
|
+
// 创建并导出单例实例
|
137
|
+
export const courseRepo = new CourseRepo();
|