@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
@@ -1,207 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
render,
|
3
|
-
type RenderResult,
|
4
|
-
type CollectionEntry,
|
5
|
-
type DataEntryMap,
|
6
|
-
} from 'astro:content';
|
7
|
-
import { SidebarItemEntity, type SidebarProvider } from './SidebarItem';
|
8
|
-
import { cosyLogger, ERROR_PREFIX } from '../cosy';
|
9
|
-
|
10
|
-
/**
|
11
|
-
* 文档基类,提供所有文档类型共享的基本功能
|
12
|
-
*/
|
13
|
-
export abstract class BaseDoc<
|
14
|
-
Collection extends keyof DataEntryMap,
|
15
|
-
T extends CollectionEntry<Collection>,
|
16
|
-
> implements SidebarProvider
|
17
|
-
{
|
18
|
-
protected entry: T;
|
19
|
-
|
20
|
-
constructor(entry: T) {
|
21
|
-
this.entry = entry;
|
22
|
-
}
|
23
|
-
|
24
|
-
/**
|
25
|
-
* 获取顶级文档
|
26
|
-
* 子类应该实现此方法以提供正确的顶级文档
|
27
|
-
*/
|
28
|
-
abstract getTopDoc(): Promise<BaseDoc<Collection, T> | null>;
|
29
|
-
|
30
|
-
/**
|
31
|
-
* 获取子文档
|
32
|
-
* 子类应该实现此方法以提供正确的子文档列表
|
33
|
-
*/
|
34
|
-
abstract getChildren(): Promise<BaseDoc<Collection, T>[]>;
|
35
|
-
|
36
|
-
/**
|
37
|
-
* 获取文档的层级深度
|
38
|
-
* 例如:对于 ID 为 "zh-cn/blog/typescript" 的文档,深度为3
|
39
|
-
*/
|
40
|
-
getLevel(): number {
|
41
|
-
return this.entry.id.split('/').length;
|
42
|
-
}
|
43
|
-
|
44
|
-
/**
|
45
|
-
* 获取所有祖先文档的ID
|
46
|
-
* 例如:对于 ID 为 "zh-cn/blog/typescript" 的文档,祖先ID为 ["zh-cn/blog", "zh-cn"]
|
47
|
-
* @returns 祖先文档的ID列表
|
48
|
-
*/
|
49
|
-
getAncestorIds(): string[] {
|
50
|
-
const parts = this.entry.id.split('/');
|
51
|
-
return parts
|
52
|
-
.slice(0, -1)
|
53
|
-
.map((_part, index) => parts.slice(0, index + 1).join('/'));
|
54
|
-
}
|
55
|
-
|
56
|
-
/**
|
57
|
-
* 获取指定层级的祖先文档ID
|
58
|
-
* 例如:对于 ID 为 "zh-cn/blog/typescript" 的文档,level=2 的祖先ID为 "zh-cn/blog"
|
59
|
-
* @param level 层级深度,从1开始
|
60
|
-
* @returns 祖先文档的ID
|
61
|
-
*/
|
62
|
-
getAncestorId(level: number): string {
|
63
|
-
const ancestorIds = this.getAncestorIds();
|
64
|
-
|
65
|
-
if (level < 1) {
|
66
|
-
cosyLogger.error('Level must be greater than 0');
|
67
|
-
throw new Error(ERROR_PREFIX + 'Level must be greater than 0');
|
68
|
-
}
|
69
|
-
|
70
|
-
if (level >= this.getLevel()) {
|
71
|
-
cosyLogger.debug('当前文档的ID:' + this.entry.id);
|
72
|
-
cosyLogger.debug('当前文档的Level:' + this.getLevel());
|
73
|
-
cosyLogger.debug('正在获取祖先ID,level:' + level);
|
74
|
-
|
75
|
-
throw new Error(
|
76
|
-
ERROR_PREFIX + 'Level must be less than the document level'
|
77
|
-
);
|
78
|
-
}
|
79
|
-
|
80
|
-
if (ancestorIds.length < level) {
|
81
|
-
cosyLogger.debug('当前文档的ID\n' + this.entry.id);
|
82
|
-
cosyLogger.array('当前文档的祖先IDs', ancestorIds);
|
83
|
-
cosyLogger.debug('正在获取祖先ID,level:\n' + level);
|
84
|
-
|
85
|
-
throw new Error(ERROR_PREFIX + 'Level must be greater than 0');
|
86
|
-
}
|
87
|
-
|
88
|
-
return ancestorIds[level - 1];
|
89
|
-
}
|
90
|
-
|
91
|
-
/**
|
92
|
-
* 获取顶级侧边栏项目
|
93
|
-
* 如果有顶级文档,返回顶级文档的侧边栏项目
|
94
|
-
* 否则返回当前文档的侧边栏项目
|
95
|
-
*/
|
96
|
-
async getTopSidebarItem(): Promise<SidebarItemEntity> {
|
97
|
-
const topDoc = await this.getTopDoc();
|
98
|
-
if (topDoc) {
|
99
|
-
return await topDoc.toSidebarItem();
|
100
|
-
}
|
101
|
-
|
102
|
-
return new SidebarItemEntity({
|
103
|
-
text: this.getTitle(),
|
104
|
-
items: [],
|
105
|
-
link: this.getLink(),
|
106
|
-
});
|
107
|
-
}
|
108
|
-
|
109
|
-
/**
|
110
|
-
* 获取父文档ID
|
111
|
-
* 例如:对于 ID 为 "zh-cn/blog/typescript" 的文档,父ID为 "zh-cn/blog"
|
112
|
-
*
|
113
|
-
* @returns 父文档ID,如果没有父文档则返回null
|
114
|
-
*/
|
115
|
-
getParentId(): string | null {
|
116
|
-
return this.getAncestorId(this.getLevel() - 1);
|
117
|
-
}
|
118
|
-
|
119
|
-
/**
|
120
|
-
* 获取顶级文档的ID
|
121
|
-
* 例如:对于 ID 为 "zh-cn/blog/typescript" 的文档,顶级ID为 "zh-cn/blog"
|
122
|
-
*
|
123
|
-
* 默认实现假设顶级ID是前两部分
|
124
|
-
* 子类可以根据需要覆盖此方法
|
125
|
-
*/
|
126
|
-
getTopDocId(): string {
|
127
|
-
const level = this.getLevel();
|
128
|
-
|
129
|
-
if (level <= 2) {
|
130
|
-
return this.entry.id;
|
131
|
-
}
|
132
|
-
|
133
|
-
return this.getAncestorId(2);
|
134
|
-
}
|
135
|
-
|
136
|
-
/**
|
137
|
-
* 获取文档ID
|
138
|
-
*/
|
139
|
-
getId(): string {
|
140
|
-
return this.entry.id;
|
141
|
-
}
|
142
|
-
|
143
|
-
/**
|
144
|
-
* 获取文档标题
|
145
|
-
*/
|
146
|
-
getTitle(): string {
|
147
|
-
return this.entry.data.title as string;
|
148
|
-
}
|
149
|
-
|
150
|
-
/**
|
151
|
-
* 获取文档语言
|
152
|
-
*/
|
153
|
-
getLang(): string {
|
154
|
-
return this.entry.id.split('/')[0];
|
155
|
-
}
|
156
|
-
|
157
|
-
/**
|
158
|
-
* 获取文档slug
|
159
|
-
*/
|
160
|
-
getSlug(): string {
|
161
|
-
return this.getId().split('/').slice(1).join('/');
|
162
|
-
}
|
163
|
-
|
164
|
-
/**
|
165
|
-
* 获取文档描述
|
166
|
-
*/
|
167
|
-
getDescription(): string {
|
168
|
-
return this.entry.data.description as string;
|
169
|
-
}
|
170
|
-
|
171
|
-
/**
|
172
|
-
* 获取文档链接
|
173
|
-
* 每个子类必须实现此方法以提供正确的链接
|
174
|
-
*/
|
175
|
-
abstract getLink(): string;
|
176
|
-
|
177
|
-
/**
|
178
|
-
* 渲染文档内容
|
179
|
-
*/
|
180
|
-
async render(): Promise<RenderResult> {
|
181
|
-
return await render(this.entry);
|
182
|
-
}
|
183
|
-
|
184
|
-
/**
|
185
|
-
* 转换为侧边栏项目
|
186
|
-
* 如果文档有子文档,会包含子文档的侧边栏项目
|
187
|
-
*/
|
188
|
-
async toSidebarItem(): Promise<SidebarItemEntity> {
|
189
|
-
const debug = false;
|
190
|
-
|
191
|
-
const children = await this.getChildren();
|
192
|
-
const childItems = await Promise.all(
|
193
|
-
children.map((child) => child.toSidebarItem())
|
194
|
-
);
|
195
|
-
|
196
|
-
if (debug) {
|
197
|
-
cosyLogger.info(`${this.entry.id} 的侧边栏项目`);
|
198
|
-
console.log(childItems);
|
199
|
-
}
|
200
|
-
|
201
|
-
return new SidebarItemEntity({
|
202
|
-
text: this.getTitle(),
|
203
|
-
items: childItems,
|
204
|
-
link: this.getLink(),
|
205
|
-
});
|
206
|
-
}
|
207
|
-
}
|
@@ -1,107 +0,0 @@
|
|
1
|
-
import type { BlogEntry } from '../database/BlogDB';
|
2
|
-
import { LinkUtil } from '../../src/utils/link';
|
3
|
-
import Tag from './Tag';
|
4
|
-
import { BaseDoc } from './BaseDoc';
|
5
|
-
import { COLLECTION_BLOG } from '../database/BlogDB';
|
6
|
-
import { blogDB } from '../database/BlogDB';
|
7
|
-
|
8
|
-
/**
|
9
|
-
* 博客文档类,配合 BlogDB 使用
|
10
|
-
*
|
11
|
-
* 目录结构:
|
12
|
-
* ```
|
13
|
-
* blogs/
|
14
|
-
* ├── zh-cn/ # 中文博客目录
|
15
|
-
* │ ├── typescript-intro.md # 文章内容
|
16
|
-
* │ ├── images/ # 文章相关图片
|
17
|
-
* │ └── web-performance.md
|
18
|
-
* └── en/ # 英文博客目录
|
19
|
-
* ├── typescript-intro.md
|
20
|
-
* ├── images/
|
21
|
-
* └── web-performance.md
|
22
|
-
* ```
|
23
|
-
*
|
24
|
-
* 说明:
|
25
|
-
* - 每个语言(如 zh-cn, en)是一个独立的目录,存放该语言下的所有博客文章
|
26
|
-
* - 每篇博客为一个 markdown 文件,文件名即为 slug
|
27
|
-
* - 每个语言目录下可包含 images 文件夹,存放该语言博客相关图片
|
28
|
-
* - 支持多语言博客内容,便于国际化
|
29
|
-
* - 博客目录可作为 git 子模块独立管理
|
30
|
-
*
|
31
|
-
* 相关:
|
32
|
-
* - BlogDB:博客数据库管理类,负责博客文档的增删查改
|
33
|
-
* - BlogDoc:博客文档实体类,封装单篇博客的相关操作
|
34
|
-
*
|
35
|
-
* 用法示例:
|
36
|
-
* ```typescript
|
37
|
-
* import BlogDoc from '../entities/BlogDoc';
|
38
|
-
* import { blogDB } from '../database/BlogDB';
|
39
|
-
*
|
40
|
-
* // 获取所有中文博客
|
41
|
-
* const blogs = await blogDB.allBlogsByLang('zh-cn');
|
42
|
-
*
|
43
|
-
* // 获取博客链接
|
44
|
-
* const link = blogs[0].getLink();
|
45
|
-
* ```
|
46
|
-
*/
|
47
|
-
|
48
|
-
export default class BlogDoc extends BaseDoc<
|
49
|
-
typeof COLLECTION_BLOG,
|
50
|
-
BlogEntry
|
51
|
-
> {
|
52
|
-
private constructor(entry: BlogEntry) {
|
53
|
-
super(entry);
|
54
|
-
}
|
55
|
-
|
56
|
-
static fromEntry(entry: BlogEntry) {
|
57
|
-
return new BlogDoc(entry);
|
58
|
-
}
|
59
|
-
|
60
|
-
async getTopDoc(): Promise<BlogDoc | null> {
|
61
|
-
const id = this.getTopDocId();
|
62
|
-
const doc = await blogDB.find(id);
|
63
|
-
return doc;
|
64
|
-
}
|
65
|
-
|
66
|
-
async getChildren(): Promise<BlogDoc[]> {
|
67
|
-
return await blogDB.getChildren(this.entry.id);
|
68
|
-
}
|
69
|
-
|
70
|
-
getLink(): string {
|
71
|
-
return LinkUtil.getBlogLink(this.entry.id, this.getLang());
|
72
|
-
}
|
73
|
-
|
74
|
-
getTags(): Tag[] {
|
75
|
-
const tags = this.entry.data.tags as string[];
|
76
|
-
|
77
|
-
if (!tags || tags.length === 0) {
|
78
|
-
return [];
|
79
|
-
}
|
80
|
-
|
81
|
-
return tags.map((tag) => new Tag(tag, 0, this.getLang()));
|
82
|
-
}
|
83
|
-
|
84
|
-
getDate(): Date {
|
85
|
-
return new Date(this.entry.data.date as Date);
|
86
|
-
}
|
87
|
-
|
88
|
-
getDateForDisplay() {
|
89
|
-
try {
|
90
|
-
const dateObj = new Date(this.entry.data.date as Date);
|
91
|
-
|
92
|
-
// Check if date is valid
|
93
|
-
if (isNaN(dateObj.getTime())) {
|
94
|
-
console.warn(`Invalid date format: ${this.entry.data.date}`);
|
95
|
-
return 'Date unavailable: ' + this.getTitle() + ' ' + this.getLink();
|
96
|
-
}
|
97
|
-
return dateObj.toLocaleDateString('zh-CN', {
|
98
|
-
year: 'numeric',
|
99
|
-
month: 'long',
|
100
|
-
day: 'numeric',
|
101
|
-
});
|
102
|
-
} catch (error) {
|
103
|
-
console.error(`Error formatting date: ${this.entry.data.date}`, error);
|
104
|
-
return 'Date unavailable';
|
105
|
-
}
|
106
|
-
}
|
107
|
-
}
|
@@ -1,102 +0,0 @@
|
|
1
|
-
import { cosyLogger } from '../cosy';
|
2
|
-
import { SidebarItemEntity } from './SidebarItem';
|
3
|
-
import type { CourseEntry } from '../database/CourseDB';
|
4
|
-
import { courseDB } from '../database/CourseDB';
|
5
|
-
import { LinkUtil } from '../../src/utils/link';
|
6
|
-
import { COLLECTION_COURSE } from '../database/CourseDB';
|
7
|
-
import { BaseDoc } from './BaseDoc';
|
8
|
-
|
9
|
-
export default class CourseDoc extends BaseDoc<
|
10
|
-
typeof COLLECTION_COURSE,
|
11
|
-
CourseEntry
|
12
|
-
> {
|
13
|
-
constructor(entry: CourseEntry) {
|
14
|
-
super(entry);
|
15
|
-
}
|
16
|
-
|
17
|
-
static fromEntry(entry: CourseEntry) {
|
18
|
-
return new CourseDoc(entry);
|
19
|
-
}
|
20
|
-
|
21
|
-
getLink(): string {
|
22
|
-
return LinkUtil.getCourseLink(this.entry.id);
|
23
|
-
}
|
24
|
-
|
25
|
-
getOrder(): number {
|
26
|
-
return this.entry.data.order ?? 0;
|
27
|
-
}
|
28
|
-
|
29
|
-
isFolder(): boolean {
|
30
|
-
return this.entry.data.folder ?? false;
|
31
|
-
}
|
32
|
-
|
33
|
-
async getTopDoc(): Promise<CourseDoc | null> {
|
34
|
-
const id = this.getTopDocId();
|
35
|
-
const doc = await courseDB.find(id);
|
36
|
-
return doc;
|
37
|
-
}
|
38
|
-
|
39
|
-
async getAncestor(level: number): Promise<CourseDoc | null> {
|
40
|
-
const debug = false;
|
41
|
-
if (debug) {
|
42
|
-
cosyLogger.info(`获取 ${this.entry.id} 的祖先文档,level: ${level}`);
|
43
|
-
}
|
44
|
-
|
45
|
-
if (level >= this.getLevel()) {
|
46
|
-
if (debug) {
|
47
|
-
cosyLogger.info(`祖先文档为自身`);
|
48
|
-
}
|
49
|
-
return this;
|
50
|
-
}
|
51
|
-
|
52
|
-
const id = this.getAncestorId(level);
|
53
|
-
const doc = await courseDB.find(id);
|
54
|
-
return doc;
|
55
|
-
}
|
56
|
-
|
57
|
-
/**
|
58
|
-
* 获取子文档
|
59
|
-
* @returns 子文档列表
|
60
|
-
*/
|
61
|
-
async getChildren(): Promise<CourseDoc[]> {
|
62
|
-
const debug = false;
|
63
|
-
const children = (await courseDB.getChildren(this.entry.id)).sort(
|
64
|
-
(a, b) => a.getOrder() - b.getOrder()
|
65
|
-
);
|
66
|
-
if (debug && children.length > 0) {
|
67
|
-
cosyLogger.array(
|
68
|
-
`${this.entry.id} 的子文档(${children.length})`,
|
69
|
-
children.map((child) => `#${child.getOrder()} ${child.entry.id}`)
|
70
|
-
);
|
71
|
-
}
|
72
|
-
return children;
|
73
|
-
}
|
74
|
-
|
75
|
-
override async toSidebarItem(): Promise<SidebarItemEntity> {
|
76
|
-
const debug = false;
|
77
|
-
const children = await this.getChildren();
|
78
|
-
let childItems = await Promise.all(
|
79
|
-
children.map((child) => child.toSidebarItem())
|
80
|
-
);
|
81
|
-
|
82
|
-
if (this.isBook()) {
|
83
|
-
childItems = [...childItems];
|
84
|
-
}
|
85
|
-
|
86
|
-
if (debug) {
|
87
|
-
cosyLogger.info(`${this.entry.id} 的侧边栏项目`);
|
88
|
-
console.log(childItems);
|
89
|
-
}
|
90
|
-
|
91
|
-
return new SidebarItemEntity({
|
92
|
-
text: this.getTitle(),
|
93
|
-
items: childItems,
|
94
|
-
link: this.getLink(),
|
95
|
-
badge: this.entry.data.badge,
|
96
|
-
});
|
97
|
-
}
|
98
|
-
|
99
|
-
isBook(): boolean {
|
100
|
-
return this.entry.id.split('/').length === 2;
|
101
|
-
}
|
102
|
-
}
|
@@ -1,119 +0,0 @@
|
|
1
|
-
import type { ExperimentEntry } from '../database/ExperimentDB';
|
2
|
-
import { experimentDB } from '../database/ExperimentDB';
|
3
|
-
import { cosyLogger } from '../cosy';
|
4
|
-
import { SidebarItemEntity } from './SidebarItem';
|
5
|
-
import { LinkUtil } from '../../src/utils/link';
|
6
|
-
import { COLLECTION_EXPERIMENT } from '../database/ExperimentDB';
|
7
|
-
import { BaseDoc } from './BaseDoc';
|
8
|
-
import type { IHeadingType } from '../types/heading';
|
9
|
-
|
10
|
-
export default class ExperimentDoc extends BaseDoc<
|
11
|
-
typeof COLLECTION_EXPERIMENT,
|
12
|
-
ExperimentEntry
|
13
|
-
> {
|
14
|
-
constructor(entry: ExperimentEntry) {
|
15
|
-
super(entry);
|
16
|
-
}
|
17
|
-
|
18
|
-
isBook(): boolean {
|
19
|
-
return this.entry.id.split('/').length === 2;
|
20
|
-
}
|
21
|
-
|
22
|
-
async getBookId(): Promise<string> {
|
23
|
-
return this.getTopDocId();
|
24
|
-
}
|
25
|
-
|
26
|
-
async getBook(): Promise<ExperimentDoc | null> {
|
27
|
-
const bookId = await this.getBookId();
|
28
|
-
return await experimentDB.find(bookId);
|
29
|
-
}
|
30
|
-
|
31
|
-
getLink(): string {
|
32
|
-
const debug = false;
|
33
|
-
const lang = this.getLang();
|
34
|
-
const link = LinkUtil.getExperimentLink(lang, this.getId());
|
35
|
-
|
36
|
-
if (debug) {
|
37
|
-
cosyLogger.info(`获取 ${this.entry.id} 的链接: ${link}`);
|
38
|
-
}
|
39
|
-
|
40
|
-
return link;
|
41
|
-
}
|
42
|
-
|
43
|
-
/**
|
44
|
-
* 获取文档的语言
|
45
|
-
*
|
46
|
-
* 文档的 id 格式为 `book-id/zh-cn/chapter-id/lesson-id`
|
47
|
-
*
|
48
|
-
* @returns 语言
|
49
|
-
*/
|
50
|
-
override getLang(): string {
|
51
|
-
const debug = false;
|
52
|
-
|
53
|
-
const parts = this.entry.id.split('/');
|
54
|
-
const lang = parts[1];
|
55
|
-
|
56
|
-
if (debug) {
|
57
|
-
cosyLogger.info(`获取 ${this.entry.id} 的语言: ${lang}`);
|
58
|
-
}
|
59
|
-
|
60
|
-
return lang;
|
61
|
-
}
|
62
|
-
|
63
|
-
getHTML(): string {
|
64
|
-
const debug = false;
|
65
|
-
|
66
|
-
if (debug) {
|
67
|
-
cosyLogger.info(`获取 ${this.entry.id} 的 HTML`);
|
68
|
-
}
|
69
|
-
|
70
|
-
return this.entry.rendered?.html || '';
|
71
|
-
}
|
72
|
-
|
73
|
-
getHeadings(): IHeadingType[] {
|
74
|
-
const debug = false;
|
75
|
-
|
76
|
-
if (debug) {
|
77
|
-
cosyLogger.info(`获取 ${this.entry.id} 的 headings`);
|
78
|
-
}
|
79
|
-
|
80
|
-
return (this.entry.rendered?.metadata?.headings as IHeadingType[]) || [];
|
81
|
-
}
|
82
|
-
|
83
|
-
async getTopDoc(): Promise<ExperimentDoc | null> {
|
84
|
-
const bookId = await this.getBookId();
|
85
|
-
return await experimentDB.find(bookId);
|
86
|
-
}
|
87
|
-
|
88
|
-
async getChildren(): Promise<ExperimentDoc[]> {
|
89
|
-
return await experimentDB.getChildren(this.entry.id);
|
90
|
-
}
|
91
|
-
|
92
|
-
async getDescendants(): Promise<ExperimentDoc[]> {
|
93
|
-
return await experimentDB.getDescendantDocs(this.entry.id);
|
94
|
-
}
|
95
|
-
|
96
|
-
override async toSidebarItem(): Promise<SidebarItemEntity> {
|
97
|
-
const debug = false;
|
98
|
-
|
99
|
-
const children = await this.getChildren();
|
100
|
-
let childItems = await Promise.all(
|
101
|
-
children.map((child) => child.toSidebarItem())
|
102
|
-
);
|
103
|
-
|
104
|
-
if (this.isBook()) {
|
105
|
-
childItems = [...childItems];
|
106
|
-
}
|
107
|
-
|
108
|
-
if (debug) {
|
109
|
-
cosyLogger.info(`${this.entry.id} 的侧边栏项目`);
|
110
|
-
console.log(childItems);
|
111
|
-
}
|
112
|
-
|
113
|
-
return new SidebarItemEntity({
|
114
|
-
text: this.getTitle(),
|
115
|
-
items: childItems,
|
116
|
-
link: this.getLink(),
|
117
|
-
});
|
118
|
-
}
|
119
|
-
}
|
@@ -1,153 +0,0 @@
|
|
1
|
-
import type { LessonEntry } from '../database/LessonDB';
|
2
|
-
import { lessonDB } from '../database/LessonDB';
|
3
|
-
import { cosyLogger } from '../cosy';
|
4
|
-
import { SidebarItemEntity } from './SidebarItem';
|
5
|
-
import { LinkUtil } from '../../src/utils/link';
|
6
|
-
import { COLLECTION_LESSON } from '../database/LessonDB';
|
7
|
-
import { BaseDoc } from './BaseDoc';
|
8
|
-
import type { IHeadingType } from '../types/heading';
|
9
|
-
|
10
|
-
/**
|
11
|
-
* 课程文档类,配合 LessonDB 使用
|
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
|
-
export default class LessonDoc extends BaseDoc<
|
45
|
-
typeof COLLECTION_LESSON,
|
46
|
-
LessonEntry
|
47
|
-
> {
|
48
|
-
constructor(entry: LessonEntry) {
|
49
|
-
super(entry);
|
50
|
-
}
|
51
|
-
|
52
|
-
isBook(): boolean {
|
53
|
-
return this.entry.id.split('/').length === 2;
|
54
|
-
}
|
55
|
-
|
56
|
-
async getBookId(): Promise<string> {
|
57
|
-
return this.getTopDocId();
|
58
|
-
}
|
59
|
-
|
60
|
-
async getBook(): Promise<LessonDoc | null> {
|
61
|
-
const bookId = await this.getBookId();
|
62
|
-
return await lessonDB.find(bookId);
|
63
|
-
}
|
64
|
-
|
65
|
-
getLink(): string {
|
66
|
-
const debug = false;
|
67
|
-
const lang = this.getLang();
|
68
|
-
const link = LinkUtil.getLessonLink(lang, this.getId());
|
69
|
-
|
70
|
-
if (debug) {
|
71
|
-
cosyLogger.info(`获取 ${this.entry.id} 的链接: ${link}`);
|
72
|
-
}
|
73
|
-
|
74
|
-
return link;
|
75
|
-
}
|
76
|
-
|
77
|
-
/**
|
78
|
-
* 获取文档的语言
|
79
|
-
*
|
80
|
-
* 文档的 id 格式为 `book-id/zh-cn/chapter-id/lesson-id`
|
81
|
-
*
|
82
|
-
* @returns 语言
|
83
|
-
*/
|
84
|
-
override getLang(): string {
|
85
|
-
const debug = false;
|
86
|
-
|
87
|
-
const parts = this.entry.id.split('/');
|
88
|
-
const lang = parts[1];
|
89
|
-
|
90
|
-
if (debug) {
|
91
|
-
cosyLogger.info(`获取 ${this.entry.id} 的语言: ${lang}`);
|
92
|
-
}
|
93
|
-
|
94
|
-
return lang;
|
95
|
-
}
|
96
|
-
|
97
|
-
getHTML(): string {
|
98
|
-
const debug = false;
|
99
|
-
|
100
|
-
if (debug) {
|
101
|
-
cosyLogger.info(`获取 ${this.entry.id} 的 HTML`);
|
102
|
-
}
|
103
|
-
|
104
|
-
return this.entry.rendered?.html || '';
|
105
|
-
}
|
106
|
-
|
107
|
-
getHeadings(): IHeadingType[] {
|
108
|
-
const debug = false;
|
109
|
-
|
110
|
-
if (debug) {
|
111
|
-
cosyLogger.info(`获取 ${this.entry.id} 的 headings`);
|
112
|
-
}
|
113
|
-
|
114
|
-
return (this.entry.rendered?.metadata?.headings as IHeadingType[]) || [];
|
115
|
-
}
|
116
|
-
|
117
|
-
async getTopDoc(): Promise<LessonDoc | null> {
|
118
|
-
const bookId = await this.getBookId();
|
119
|
-
return await lessonDB.find(bookId);
|
120
|
-
}
|
121
|
-
|
122
|
-
async getChildren(): Promise<LessonDoc[]> {
|
123
|
-
return await lessonDB.getChildren(this.entry.id);
|
124
|
-
}
|
125
|
-
|
126
|
-
async getDescendants(): Promise<LessonDoc[]> {
|
127
|
-
return await lessonDB.getDescendantDocs(this.entry.id);
|
128
|
-
}
|
129
|
-
|
130
|
-
override async toSidebarItem(): Promise<SidebarItemEntity> {
|
131
|
-
const debug = false;
|
132
|
-
|
133
|
-
const children = await this.getChildren();
|
134
|
-
let childItems = await Promise.all(
|
135
|
-
children.map((child) => child.toSidebarItem())
|
136
|
-
);
|
137
|
-
|
138
|
-
if (this.isBook()) {
|
139
|
-
childItems = [...childItems];
|
140
|
-
}
|
141
|
-
|
142
|
-
if (debug) {
|
143
|
-
cosyLogger.info(`${this.entry.id} 的侧边栏项目`);
|
144
|
-
console.log(childItems);
|
145
|
-
}
|
146
|
-
|
147
|
-
return new SidebarItemEntity({
|
148
|
-
text: this.getTitle(),
|
149
|
-
items: childItems,
|
150
|
-
link: this.getLink(),
|
151
|
-
});
|
152
|
-
}
|
153
|
-
}
|