@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,121 @@
|
|
1
|
+
import { defineCollection, z, type CollectionEntry } from 'astro:content';
|
2
|
+
import { BaseDB } from './BaseRepo';
|
3
|
+
import ExperimentDoc from '../entities/ExperimentDoc';
|
4
|
+
import { cosyLogger } from '../../cosy';
|
5
|
+
import { glob } from 'astro/loaders';
|
6
|
+
|
7
|
+
export const COLLECTION_EXPERIMENT = 'experiments' as const;
|
8
|
+
export type ExperimentEntry = CollectionEntry<typeof COLLECTION_EXPERIMENT>;
|
9
|
+
|
10
|
+
/**
|
11
|
+
* 实验数据库类,用于管理实验内容集合
|
12
|
+
*
|
13
|
+
* 目录结构:
|
14
|
+
* ```
|
15
|
+
* experiments/
|
16
|
+
* ├── safari_itp/ # 实验目录
|
17
|
+
* │ ├── images/ # 实验图片资源
|
18
|
+
* │ ├── components/ # 课程组件
|
19
|
+
* │ ├── en/ # 英文版本
|
20
|
+
* │ │ ├── index.mdx # 课程首页
|
21
|
+
* │ │ ├── 1.mdx # 第一章
|
22
|
+
* │ │ └── 2.mdx # 第二章
|
23
|
+
* │ └── zh-cn/ # 中文版本
|
24
|
+
* │ ├── index.mdx # 课程首页
|
25
|
+
* │ ├── 1.mdx # 第一章
|
26
|
+
* │ └── 2.mdx # 第二章
|
27
|
+
* └── learn_astro/ # 另一个课程
|
28
|
+
* ├── en/
|
29
|
+
* │ ├── index.mdx
|
30
|
+
* │ ├── 1.mdx
|
31
|
+
* │ └── 2.mdx
|
32
|
+
* └── zh-cn/
|
33
|
+
* ├── index.mdx
|
34
|
+
* ├── 1.mdx
|
35
|
+
* └── 2.mdx
|
36
|
+
* ```
|
37
|
+
*
|
38
|
+
* 说明:
|
39
|
+
* - 每个课程(如 build_your_own_web_toolbox)是一个独立的目录
|
40
|
+
* - 课程目录可以包含多语言版本(en, zh-cn 等)
|
41
|
+
* - 每个语言版本包含完整的课程内容
|
42
|
+
* - 课程目录可以作为 git 子模块独立管理
|
43
|
+
*/
|
44
|
+
class ExperimentRepo extends BaseDB<
|
45
|
+
typeof COLLECTION_EXPERIMENT,
|
46
|
+
ExperimentEntry,
|
47
|
+
ExperimentDoc
|
48
|
+
> {
|
49
|
+
protected collectionName = COLLECTION_EXPERIMENT;
|
50
|
+
|
51
|
+
protected createDoc(entry: ExperimentEntry): ExperimentDoc {
|
52
|
+
return new ExperimentDoc(entry);
|
53
|
+
}
|
54
|
+
|
55
|
+
/**
|
56
|
+
* 获取指定语言的所有课程
|
57
|
+
*
|
58
|
+
* @param {string} lang - 语言代码
|
59
|
+
* @returns {Promise<ExperimentDoc[]>} 返回指定语言的所有课程
|
60
|
+
*/
|
61
|
+
async allExperiments(lang: string): Promise<ExperimentDoc[]> {
|
62
|
+
const docs = await this.getDocsByDepth(2);
|
63
|
+
return docs.filter((doc) => doc.getId().endsWith(lang));
|
64
|
+
}
|
65
|
+
|
66
|
+
/**
|
67
|
+
* 获取用于 Astro 静态路由生成的路径参数
|
68
|
+
*
|
69
|
+
* @param debug - 是否开启调试模式
|
70
|
+
* @returns 返回路径参数数组
|
71
|
+
*/
|
72
|
+
async getStaticPaths(debug: boolean = false) {
|
73
|
+
const docs = await this.getEntries();
|
74
|
+
|
75
|
+
if (debug) {
|
76
|
+
cosyLogger.array('所有文档', docs);
|
77
|
+
}
|
78
|
+
|
79
|
+
const paths = docs.map((doc) => {
|
80
|
+
const id = doc.id;
|
81
|
+
const lang = id.split('/')[1];
|
82
|
+
|
83
|
+
let slug = '';
|
84
|
+
if (id.endsWith(lang)) {
|
85
|
+
slug = id.replace(`${lang}`, '');
|
86
|
+
} else {
|
87
|
+
slug = id.replace(`${lang}/`, '');
|
88
|
+
}
|
89
|
+
|
90
|
+
return {
|
91
|
+
params: {
|
92
|
+
lang: lang,
|
93
|
+
slug: slug,
|
94
|
+
},
|
95
|
+
};
|
96
|
+
});
|
97
|
+
|
98
|
+
if (debug) {
|
99
|
+
cosyLogger.array('所有文档的路径', paths);
|
100
|
+
}
|
101
|
+
|
102
|
+
return paths;
|
103
|
+
}
|
104
|
+
|
105
|
+
makeExperimentCollection = (base: string) => {
|
106
|
+
return defineCollection({
|
107
|
+
loader: glob({
|
108
|
+
pattern: '**/*.{md,mdx}',
|
109
|
+
base,
|
110
|
+
}),
|
111
|
+
schema: z.object({
|
112
|
+
title: z.string().optional(),
|
113
|
+
description: z.string().optional(),
|
114
|
+
pubDate: z.date().optional(),
|
115
|
+
}),
|
116
|
+
});
|
117
|
+
};
|
118
|
+
}
|
119
|
+
|
120
|
+
// 创建并导出单例实例
|
121
|
+
export const experimentRepo = new ExperimentRepo();
|
@@ -0,0 +1,128 @@
|
|
1
|
+
import { defineCollection, z, type CollectionEntry } from 'astro:content';
|
2
|
+
import { BaseDB } from './BaseRepo';
|
3
|
+
import LessonDoc from '../entities/LessonDoc';
|
4
|
+
import { cosyLogger } from '../../cosy';
|
5
|
+
import { glob } from 'astro/loaders';
|
6
|
+
|
7
|
+
export const COLLECTION_LESSON = 'lessons' as const;
|
8
|
+
export type LessonEntry = CollectionEntry<typeof COLLECTION_LESSON>;
|
9
|
+
|
10
|
+
/**
|
11
|
+
* 课程数据库类,用于管理课程内容集合
|
12
|
+
*
|
13
|
+
* 目录结构:
|
14
|
+
* ```
|
15
|
+
* lessons/
|
16
|
+
* ├── build_your_own_web_toolbox/ # 课程目录
|
17
|
+
* │ ├── images/ # 课程图片资源
|
18
|
+
* │ ├── components/ # 课程组件
|
19
|
+
* │ ├── en/ # 英文版本
|
20
|
+
* │ │ ├── index.mdx # 课程首页
|
21
|
+
* │ │ ├── 1.mdx # 第一章
|
22
|
+
* │ │ └── 2.mdx # 第二章
|
23
|
+
* │ └── zh-cn/ # 中文版本
|
24
|
+
* │ ├── index.mdx # 课程首页
|
25
|
+
* │ ├── 1.mdx # 第一章
|
26
|
+
* │ └── 2.mdx # 第二章
|
27
|
+
* └── learn_astro/ # 另一个课程
|
28
|
+
* ├── en/
|
29
|
+
* │ ├── index.mdx
|
30
|
+
* │ ├── 1.mdx
|
31
|
+
* │ └── 2.mdx
|
32
|
+
* └── zh-cn/
|
33
|
+
* ├── index.mdx
|
34
|
+
* ├── 1.mdx
|
35
|
+
* └── 2.mdx
|
36
|
+
* ```
|
37
|
+
*
|
38
|
+
* 说明:
|
39
|
+
* - 每个课程(如 build_your_own_web_toolbox)是一个独立的目录
|
40
|
+
* - 课程目录可以包含多语言版本(en, zh-cn 等)
|
41
|
+
* - 每个语言版本包含完整的课程内容
|
42
|
+
* - 课程目录可以作为 git 子模块独立管理
|
43
|
+
*/
|
44
|
+
class LessonRepo extends BaseDB<
|
45
|
+
typeof COLLECTION_LESSON,
|
46
|
+
LessonEntry,
|
47
|
+
LessonDoc
|
48
|
+
> {
|
49
|
+
protected collectionName = COLLECTION_LESSON;
|
50
|
+
|
51
|
+
protected createDoc(entry: LessonEntry): LessonDoc {
|
52
|
+
return new LessonDoc(entry);
|
53
|
+
}
|
54
|
+
|
55
|
+
/**
|
56
|
+
* 获取指定语言的所有课程
|
57
|
+
*
|
58
|
+
* @param {string} lang - 语言代码
|
59
|
+
* @returns {Promise<LessonDoc[]>} 返回指定语言的所有课程
|
60
|
+
*/
|
61
|
+
async allLessons(lang: string): Promise<LessonDoc[]> {
|
62
|
+
const docs = await this.getDocsByDepth(2);
|
63
|
+
return docs.filter((doc) => doc.getId().endsWith(lang));
|
64
|
+
}
|
65
|
+
|
66
|
+
/**
|
67
|
+
* 获取用于 Astro 静态路由生成的路径参数
|
68
|
+
*
|
69
|
+
* @param debug - 是否开启调试模式
|
70
|
+
* @returns 返回路径参数数组
|
71
|
+
*/
|
72
|
+
async getStaticPaths(debug: boolean = false) {
|
73
|
+
const docs = await this.getEntries();
|
74
|
+
|
75
|
+
if (debug) {
|
76
|
+
cosyLogger.array('所有文档', docs);
|
77
|
+
}
|
78
|
+
|
79
|
+
const paths = docs.map((doc) => {
|
80
|
+
const id = doc.id;
|
81
|
+
const lang = id.split('/')[1];
|
82
|
+
|
83
|
+
let slug = '';
|
84
|
+
if (id.endsWith(lang)) {
|
85
|
+
slug = id.replace(`${lang}`, '');
|
86
|
+
} else {
|
87
|
+
slug = id.replace(`${lang}/`, '');
|
88
|
+
}
|
89
|
+
|
90
|
+
return {
|
91
|
+
params: {
|
92
|
+
lang: lang,
|
93
|
+
slug: slug,
|
94
|
+
},
|
95
|
+
};
|
96
|
+
});
|
97
|
+
|
98
|
+
if (debug) {
|
99
|
+
cosyLogger.array('所有文档的路径', paths);
|
100
|
+
}
|
101
|
+
|
102
|
+
return paths;
|
103
|
+
}
|
104
|
+
|
105
|
+
makeLessonCollection = (base: string) => {
|
106
|
+
return defineCollection({
|
107
|
+
loader: glob({
|
108
|
+
pattern: '**/*.{md,mdx}',
|
109
|
+
base,
|
110
|
+
}),
|
111
|
+
schema: z.object({
|
112
|
+
title: z.string().optional(),
|
113
|
+
description: z.string().optional(),
|
114
|
+
authors: z
|
115
|
+
.array(
|
116
|
+
z.object({
|
117
|
+
name: z.string(),
|
118
|
+
picture: z.string().optional(),
|
119
|
+
})
|
120
|
+
)
|
121
|
+
.optional(),
|
122
|
+
}),
|
123
|
+
});
|
124
|
+
};
|
125
|
+
}
|
126
|
+
|
127
|
+
// 创建并导出单例实例
|
128
|
+
export const lessonRepo = new LessonRepo();
|
@@ -0,0 +1,89 @@
|
|
1
|
+
import MetaDoc from '../entities/MetaDoc';
|
2
|
+
import { cosyLogger } from '../../cosy';
|
3
|
+
import { defineCollection, z, type CollectionEntry } from 'astro:content';
|
4
|
+
import { BaseDB } from './BaseRepo';
|
5
|
+
import { glob } from 'astro/loaders';
|
6
|
+
|
7
|
+
export const COLLECTION_META = 'meta' as const;
|
8
|
+
export type MetaEntry = CollectionEntry<typeof COLLECTION_META>;
|
9
|
+
|
10
|
+
/**
|
11
|
+
* 元数据数据库类,用于管理网站的元数据内容集合(如"关于我们"等页面)
|
12
|
+
*
|
13
|
+
* 目录结构:
|
14
|
+
* ```
|
15
|
+
* meta/
|
16
|
+
* ├── zh-cn/ # 中文内容
|
17
|
+
* │ ├── about.md # 关于我们
|
18
|
+
* │ ├── advice.md # 建议
|
19
|
+
* └── en/ # 英文内容
|
20
|
+
* ├── about.md
|
21
|
+
* ├── privacy.md
|
22
|
+
* └── terms.md
|
23
|
+
* ```
|
24
|
+
*/
|
25
|
+
class MetaRepo extends BaseDB<typeof COLLECTION_META, MetaEntry, MetaDoc> {
|
26
|
+
protected collectionName = COLLECTION_META;
|
27
|
+
|
28
|
+
protected createDoc(entry: MetaEntry): MetaDoc {
|
29
|
+
return new MetaDoc(entry);
|
30
|
+
}
|
31
|
+
|
32
|
+
/**
|
33
|
+
* 获取指定文档的兄弟文档
|
34
|
+
* 例如:对于 'zh-cn/about',会返回 'zh-cn' 下的文档
|
35
|
+
*
|
36
|
+
* @param targetId - 目标文档ID
|
37
|
+
* @returns 返回兄弟文档数组(包括目标文档本身)
|
38
|
+
*/
|
39
|
+
async getSiblings(targetId: string): Promise<MetaDoc[]> {
|
40
|
+
const target = await this.find(targetId);
|
41
|
+
if (!target) {
|
42
|
+
return [];
|
43
|
+
}
|
44
|
+
const docs = await this.getDocsByDepth(2);
|
45
|
+
return docs.filter((doc) => doc.getLang() === target.getLang());
|
46
|
+
}
|
47
|
+
|
48
|
+
/**
|
49
|
+
* 获取用于 Astro 静态路由生成的路径参数,专门配合 [lang]/meta/[slug].astro 使用
|
50
|
+
*
|
51
|
+
* @param debug - 是否开启调试模式
|
52
|
+
* @returns 返回路径参数数组
|
53
|
+
*/
|
54
|
+
async getStaticPaths(debug: boolean = false) {
|
55
|
+
const docs = await this.getDescendantDocs('');
|
56
|
+
|
57
|
+
const paths = docs.map((doc) => {
|
58
|
+
return {
|
59
|
+
params: {
|
60
|
+
lang: doc.getLang(),
|
61
|
+
slug: doc.getSlug(),
|
62
|
+
},
|
63
|
+
};
|
64
|
+
});
|
65
|
+
|
66
|
+
if (debug) {
|
67
|
+
cosyLogger.array('所有元数据文档的路径', paths);
|
68
|
+
}
|
69
|
+
|
70
|
+
return paths;
|
71
|
+
}
|
72
|
+
|
73
|
+
makeMetaCollection = (base: string) => {
|
74
|
+
return defineCollection({
|
75
|
+
loader: glob({
|
76
|
+
pattern: '**/*.{md,mdx}',
|
77
|
+
base,
|
78
|
+
}),
|
79
|
+
schema: z.object({
|
80
|
+
title: z.string().optional(),
|
81
|
+
description: z.string().optional(),
|
82
|
+
}),
|
83
|
+
});
|
84
|
+
};
|
85
|
+
|
86
|
+
}
|
87
|
+
|
88
|
+
// 创建并导出单例实例
|
89
|
+
export const metaRepo = new MetaRepo();
|
package/package.json
CHANGED
@@ -1,264 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
getCollection,
|
3
|
-
getEntry,
|
4
|
-
type CollectionEntry,
|
5
|
-
type DataEntryMap,
|
6
|
-
} from 'astro:content';
|
7
|
-
import { cosyLogger, ERROR_PREFIX } from '../cosy';
|
8
|
-
import type { BaseDoc } from '../../index-astro';
|
9
|
-
|
10
|
-
/**
|
11
|
-
* BaseDB 是所有数据库类的基类,提供了通用的文档操作功能。
|
12
|
-
*
|
13
|
-
* 使用方法:
|
14
|
-
* ```typescript
|
15
|
-
* class MyDB extends BaseDB<'collection', MyEntry, MyDoc> {
|
16
|
-
* protected collectionName = 'collection' as const;
|
17
|
-
* protected createDoc(entry: MyEntry): MyDoc {
|
18
|
-
* return new MyDoc(entry);
|
19
|
-
* }
|
20
|
-
* }
|
21
|
-
*
|
22
|
-
* // 使用单例模式获取实例
|
23
|
-
* const db = MyDB.getInstance();
|
24
|
-
* const docs = await db.allTopLevelDocs();
|
25
|
-
* ```
|
26
|
-
*
|
27
|
-
* 类型参数说明:
|
28
|
-
* @template Collection - Astro content collection 的名称,必须是 DataEntryMap 的键
|
29
|
-
* @template Entry - Collection 对应的条目类型
|
30
|
-
* @template Doc - 文档类型,通常是自定义的文档类
|
31
|
-
*/
|
32
|
-
export abstract class BaseDB<
|
33
|
-
Collection extends keyof DataEntryMap,
|
34
|
-
Entry extends CollectionEntry<Collection>,
|
35
|
-
Doc extends BaseDoc<Collection, Entry>,
|
36
|
-
> {
|
37
|
-
/** 集合名称,必须在子类中指定 */
|
38
|
-
protected abstract collectionName: Collection;
|
39
|
-
|
40
|
-
/**
|
41
|
-
* 创建文档实例的方法,必须在子类中实现
|
42
|
-
* @param entry - 集合条目
|
43
|
-
* @returns 文档实例
|
44
|
-
*/
|
45
|
-
protected abstract createDoc(entry: Entry): Doc;
|
46
|
-
|
47
|
-
/**
|
48
|
-
* 获取所有文档的ID
|
49
|
-
* @returns 返回所有文档的ID数组
|
50
|
-
*/
|
51
|
-
protected async getAllIds(): Promise<string[]> {
|
52
|
-
const entries = await getCollection(this.collectionName);
|
53
|
-
return entries.map((entry) => entry.id);
|
54
|
-
}
|
55
|
-
|
56
|
-
/**
|
57
|
-
* 获取集合中的所有条目
|
58
|
-
* @returns 返回所有条目的数组
|
59
|
-
*/
|
60
|
-
protected async getEntries(): Promise<Entry[]> {
|
61
|
-
return await getCollection(this.collectionName);
|
62
|
-
}
|
63
|
-
|
64
|
-
/**
|
65
|
-
* 获取指定深度的文档
|
66
|
-
* 深度是指文档 ID 中斜杠的数量加1
|
67
|
-
* 例如:
|
68
|
-
* - "blog.md" 深度为1
|
69
|
-
* - "zh-cn/blog.md" 深度为2
|
70
|
-
* - "zh-cn/tech/blog.md" 深度为3
|
71
|
-
*
|
72
|
-
* @param depth - 文档深度
|
73
|
-
* @returns 返回指定深度的文档数组
|
74
|
-
*/
|
75
|
-
protected async getDocsByDepth(depth: number): Promise<Doc[]> {
|
76
|
-
const entries = await getCollection(
|
77
|
-
this.collectionName,
|
78
|
-
({ id }: { id: string }) => id.split('/').length === depth
|
79
|
-
);
|
80
|
-
|
81
|
-
if (entries.length === 0) {
|
82
|
-
cosyLogger.warn(
|
83
|
-
`[BaseDB] 没有找到深度为${depth}的文档(collection=${this.collectionName as string})`
|
84
|
-
);
|
85
|
-
const allEntries = await getCollection(this.collectionName);
|
86
|
-
cosyLogger.array(
|
87
|
-
'[BaseDB] 所有文档',
|
88
|
-
allEntries.map((entry) => entry.id)
|
89
|
-
);
|
90
|
-
return [];
|
91
|
-
}
|
92
|
-
|
93
|
-
return entries.map((entry) => this.createDoc(entry as Entry));
|
94
|
-
}
|
95
|
-
|
96
|
-
/**
|
97
|
-
* 根据ID查找单个文档
|
98
|
-
* @param id - 文档ID
|
99
|
-
* @param debug - 是否启用调试模式, 默认为false
|
100
|
-
* @throws 如果ID不是字符串类型,则抛出错误
|
101
|
-
* @throws 如果文档不存在,则返回null
|
102
|
-
* @throws 如果发生其他错误,则抛出错误
|
103
|
-
* @returns 返回找到的文档,如果不存在则返回null
|
104
|
-
*/
|
105
|
-
async find(id: string, debug: boolean = false): Promise<Doc | null> {
|
106
|
-
if (debug) {
|
107
|
-
cosyLogger.info(`查找文档,ID: ${id}`);
|
108
|
-
}
|
109
|
-
|
110
|
-
if (id == undefined) {
|
111
|
-
cosyLogger.error('ID is undefined');
|
112
|
-
throw new Error(ERROR_PREFIX + 'ID is undefined');
|
113
|
-
}
|
114
|
-
|
115
|
-
if (typeof id !== 'string') {
|
116
|
-
cosyLogger.error('ID must be a string, but got ' + typeof id);
|
117
|
-
cosyLogger.debug(id);
|
118
|
-
throw new Error(
|
119
|
-
ERROR_PREFIX + 'ID must be a string, but got ' + typeof id
|
120
|
-
);
|
121
|
-
}
|
122
|
-
|
123
|
-
// 获取所有文档的ID并排好顺序
|
124
|
-
if (debug) {
|
125
|
-
const allIds = (await this.getAllIds()).sort();
|
126
|
-
cosyLogger.array('所有文档的ID', allIds);
|
127
|
-
}
|
128
|
-
|
129
|
-
// 根据ID查找文档
|
130
|
-
const entry = await getEntry(this.collectionName, id);
|
131
|
-
return entry ? this.createDoc(entry as Entry) : null;
|
132
|
-
}
|
133
|
-
|
134
|
-
/**
|
135
|
-
* 获取指定文档的直接子文档(不包括更深层级的文档)
|
136
|
-
* 例如对于文档 "zh-cn/blog":
|
137
|
-
* - "zh-cn/blog/post1.md" 会被包含
|
138
|
-
* - "zh-cn/blog/2024/post2.md" 不会被包含
|
139
|
-
*
|
140
|
-
* @param parentId - 父文档ID
|
141
|
-
* @returns 返回子文档数组
|
142
|
-
*/
|
143
|
-
async getChildren(parentId: string): Promise<Doc[]> {
|
144
|
-
const parentLevel = parentId.split('/').length;
|
145
|
-
const childrenLevel = parentLevel + 1;
|
146
|
-
|
147
|
-
const entries = await getCollection(
|
148
|
-
this.collectionName,
|
149
|
-
({ id }: { id: string }) =>
|
150
|
-
id.startsWith(parentId) && id.split('/').length === childrenLevel
|
151
|
-
);
|
152
|
-
|
153
|
-
return entries.map((entry) => this.createDoc(entry as Entry));
|
154
|
-
}
|
155
|
-
|
156
|
-
/**
|
157
|
-
* 获取指定文档的所有后代文档(包括所有层级)
|
158
|
-
* 例如对于文档 "zh-cn/blog",以下都会被包含:
|
159
|
-
* - "zh-cn/blog/post1.md"
|
160
|
-
* - "zh-cn/blog/2024/post2.md"
|
161
|
-
* - "zh-cn/blog/2024/tech/post3.md"
|
162
|
-
*
|
163
|
-
* @param parentId - 父文档ID
|
164
|
-
* @returns 返回所有后代文档数组
|
165
|
-
*/
|
166
|
-
async getDescendantDocs(parentId: string): Promise<Doc[]> {
|
167
|
-
const entries = await getCollection(
|
168
|
-
this.collectionName,
|
169
|
-
({ id }: { id: string }) => id.startsWith(parentId)
|
170
|
-
);
|
171
|
-
return entries.map((entry) => this.createDoc(entry as Entry));
|
172
|
-
}
|
173
|
-
|
174
|
-
/**
|
175
|
-
* 获取指定语言和级别的文档
|
176
|
-
* 通过检查文档ID是否以指定语言代码开头来筛选
|
177
|
-
*
|
178
|
-
* @param lang - 语言代码(如 'zh-cn', 'en')
|
179
|
-
* @param level - 文档级别
|
180
|
-
* @returns 返回指定语言和级别的文档数组
|
181
|
-
*/
|
182
|
-
protected async allDocsByLangAndLevel(
|
183
|
-
lang: string,
|
184
|
-
level: number = 1,
|
185
|
-
debug: boolean = false
|
186
|
-
): Promise<Doc[]> {
|
187
|
-
const collectionName = this.collectionName as string;
|
188
|
-
const docs = await this.getDocsByDepth(level);
|
189
|
-
|
190
|
-
if (debug) {
|
191
|
-
cosyLogger.array(
|
192
|
-
`[BaseDB] 所有${level}级文档(lang=any,collection=${collectionName})`,
|
193
|
-
docs
|
194
|
-
);
|
195
|
-
}
|
196
|
-
|
197
|
-
if (docs.length === 0) {
|
198
|
-
cosyLogger.warn(
|
199
|
-
`[BaseDB] 没有找到${level}级文档(lang=any,collection=${collectionName})`
|
200
|
-
);
|
201
|
-
return [];
|
202
|
-
}
|
203
|
-
|
204
|
-
const filteredDocs = docs.filter((doc) => {
|
205
|
-
const id = (doc as any).getId();
|
206
|
-
return id && typeof id === 'string' && id.startsWith(lang);
|
207
|
-
});
|
208
|
-
|
209
|
-
if (debug) {
|
210
|
-
cosyLogger.array(
|
211
|
-
`[BaseDB] 所有${level}级文档(lang=${lang},collection=${collectionName})`,
|
212
|
-
filteredDocs
|
213
|
-
);
|
214
|
-
}
|
215
|
-
|
216
|
-
if (filteredDocs.length === 0) {
|
217
|
-
cosyLogger.warn(
|
218
|
-
`[BaseDB] 没有找到${level}级文档(lang=${lang},collection=${collectionName})`
|
219
|
-
);
|
220
|
-
cosyLogger.array(
|
221
|
-
`[BaseDB] 所有${level}级文档`,
|
222
|
-
docs.map((doc) => doc.getId())
|
223
|
-
);
|
224
|
-
return [];
|
225
|
-
}
|
226
|
-
|
227
|
-
return filteredDocs;
|
228
|
-
}
|
229
|
-
|
230
|
-
/**
|
231
|
-
* 获取用于 Astro 静态路由生成的路径参数
|
232
|
-
* 为每个文档生成包含语言和slug的路径参数
|
233
|
-
*
|
234
|
-
* @returns 返回路径参数数组,每个元素包含 lang 和 slug
|
235
|
-
* @example
|
236
|
-
* ```typescript
|
237
|
-
* const paths = await db.getStaticPaths();
|
238
|
-
* // 返回格式:
|
239
|
-
* // [
|
240
|
-
* // { params: { lang: 'zh-cn', slug: 'post1' } },
|
241
|
-
* // { params: { lang: 'en', slug: 'post1' } }
|
242
|
-
* // ]
|
243
|
-
* ```
|
244
|
-
*/
|
245
|
-
async getStaticPaths() {
|
246
|
-
const debug = false;
|
247
|
-
const docs = await this.getDescendantDocs('');
|
248
|
-
const paths = docs.map((doc) => {
|
249
|
-
const docWithMethods = doc as any;
|
250
|
-
return {
|
251
|
-
params: {
|
252
|
-
lang: docWithMethods.getLang?.() || '',
|
253
|
-
slug: docWithMethods.getSlug?.() || '',
|
254
|
-
},
|
255
|
-
};
|
256
|
-
});
|
257
|
-
|
258
|
-
if (debug) {
|
259
|
-
cosyLogger.array('所有文档的路径', paths);
|
260
|
-
}
|
261
|
-
|
262
|
-
return paths;
|
263
|
-
}
|
264
|
-
}
|