@coffic/cosy-ui 0.5.8 → 0.5.14

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.
Files changed (41) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +14 -46
  3. package/dist/app.css +1 -1
  4. package/dist/collections/ArticleCollection.ts +19 -0
  5. package/dist/collections/BlogCollection.ts +28 -0
  6. package/dist/collections/CourseCollection.ts +11 -0
  7. package/dist/collections/ExperimentCollection.ts +18 -0
  8. package/dist/collections/LessonCollection.ts +25 -0
  9. package/dist/collections/MetaCollection.ts +17 -0
  10. package/dist/components/data-display/TeamMembers.astro +1 -1
  11. package/dist/components/display/Card.astro +0 -3
  12. package/dist/components/display/CodeBlock.astro +1 -2
  13. package/dist/components/display/Modal.astro +1 -2
  14. package/dist/components/icons/SearchIcon.astro +30 -34
  15. package/dist/components/icons/SunCloudyIcon.astro +35 -39
  16. package/dist/components/layouts/BaseLayout.astro +3 -2
  17. package/dist/components/navigation/TableOfContents.astro +6 -3
  18. package/dist/components/typography/Text.astro +1 -1
  19. package/dist/database/BlogDB.ts +199 -0
  20. package/dist/database/CourseDB.ts +85 -0
  21. package/dist/database/ExperimentDB.ts +103 -0
  22. package/dist/database/LessonDB.ts +103 -0
  23. package/dist/database/MetaDB.ts +75 -0
  24. package/dist/entities/BaseDoc.ts +170 -0
  25. package/dist/entities/BlogDoc.ts +53 -0
  26. package/dist/entities/CourseDoc.ts +56 -0
  27. package/dist/entities/ExperimentDoc.ts +117 -0
  28. package/dist/entities/Heading.ts +13 -0
  29. package/dist/entities/LessonDoc.ts +114 -0
  30. package/dist/entities/MetaDoc.ts +82 -0
  31. package/dist/entities/Tag.ts +42 -0
  32. package/dist/index.ts +9 -1
  33. package/dist/types/static-path.ts +8 -0
  34. package/dist/utils/image.ts +74 -70
  35. package/dist/utils/lang_package.ts +205 -206
  36. package/dist/utils/language.ts +0 -7
  37. package/dist/utils/link.ts +245 -239
  38. package/dist/utils/logger.ts +101 -126
  39. package/dist/utils/social.ts +1 -1
  40. package/package.json +24 -18
  41. package/dist/models/BaseDoc.ts +0 -164
@@ -0,0 +1,103 @@
1
+ import { type CollectionEntry } from 'astro:content';
2
+ import { BaseDB } from './BaseDB';
3
+ import ExperimentDoc from '../entities/ExperimentDoc';
4
+ import { logger } from '../utils/logger';
5
+
6
+ export const COLLECTION_NAME = 'experiments' as const;
7
+ export type ExperimentEntry = CollectionEntry<typeof COLLECTION_NAME>;
8
+
9
+ /**
10
+ * 实验数据库类,用于管理实验内容集合
11
+ *
12
+ * 目录结构:
13
+ * ```
14
+ * experiments/
15
+ * ├── safari_itp/ # 实验目录
16
+ * │ ├── images/ # 实验图片资源
17
+ * │ ├── components/ # 课程组件
18
+ * │ ├── en/ # 英文版本
19
+ * │ │ ├── index.mdx # 课程首页
20
+ * │ │ ├── 1.mdx # 第一章
21
+ * │ │ └── 2.mdx # 第二章
22
+ * │ └── zh-cn/ # 中文版本
23
+ * │ ├── index.mdx # 课程首页
24
+ * │ ├── 1.mdx # 第一章
25
+ * │ └── 2.mdx # 第二章
26
+ * └── learn_astro/ # 另一个课程
27
+ * ├── en/
28
+ * │ ├── index.mdx
29
+ * │ ├── 1.mdx
30
+ * │ └── 2.mdx
31
+ * └── zh-cn/
32
+ * ├── index.mdx
33
+ * ├── 1.mdx
34
+ * └── 2.mdx
35
+ * ```
36
+ *
37
+ * 说明:
38
+ * - 每个课程(如 build_your_own_web_toolbox)是一个独立的目录
39
+ * - 课程目录可以包含多语言版本(en, zh-cn 等)
40
+ * - 每个语言版本包含完整的课程内容
41
+ * - 课程目录可以作为 git 子模块独立管理
42
+ */
43
+ class ExperimentDB extends BaseDB<typeof COLLECTION_NAME, ExperimentEntry, ExperimentDoc> {
44
+ protected collectionName = COLLECTION_NAME;
45
+
46
+ protected createDoc(entry: ExperimentEntry): ExperimentDoc {
47
+ return new ExperimentDoc(entry);
48
+ }
49
+
50
+ /**
51
+ * 获取指定语言的所有课程
52
+ *
53
+ * @param {string} lang - 语言代码
54
+ * @returns {Promise<ExperimentDoc[]>} 返回指定语言的所有课程
55
+ */
56
+ async allExperiments(lang: string): Promise<ExperimentDoc[]> {
57
+ const docs = await this.getDocsByDepth(2);
58
+ return docs.filter((doc) => doc.getId().endsWith(lang));
59
+ }
60
+
61
+ /**
62
+ * 获取用于 Astro 静态路由生成的路径参数
63
+ *
64
+ * @returns 返回路径参数数组
65
+ */
66
+ async getStaticPaths() {
67
+ const debug = false;
68
+ const docs = await this.getEntries();
69
+
70
+ if (debug) {
71
+ logger.array('所有文档', docs);
72
+ }
73
+
74
+ const paths = docs.map((doc) => {
75
+ const id = doc.id;
76
+ const lang = id.split('/')[1];
77
+
78
+ let slug = '';
79
+ if (id.endsWith(lang)) {
80
+ slug = id.replace(`${lang}`, '');
81
+ } else {
82
+ slug = id.replace(`${lang}/`, '');
83
+ }
84
+
85
+ return {
86
+ params: {
87
+ lang: lang,
88
+ slug: slug,
89
+ },
90
+ };
91
+ });
92
+
93
+ if (debug) {
94
+ logger.array('所有文档的路径', paths);
95
+ }
96
+
97
+ return paths;
98
+ }
99
+ }
100
+
101
+ // 创建并导出单例实例
102
+ const experimentDB = new ExperimentDB();
103
+ export default experimentDB;
@@ -0,0 +1,103 @@
1
+ import { type CollectionEntry } from 'astro:content';
2
+ import { BaseDB } from './BaseDB';
3
+ import LessonDoc from '../entities/LessonDoc';
4
+ import { logger } from '../utils/logger';
5
+
6
+ export const COLLECTION_NAME = 'lessons' as const;
7
+ export type LessonEntry = CollectionEntry<typeof COLLECTION_NAME>;
8
+
9
+ /**
10
+ * 课程数据库类,用于管理课程内容集合
11
+ *
12
+ * 目录结构:
13
+ * ```
14
+ * lessons/
15
+ * ├── build_your_own_web_toolbox/ # 课程目录
16
+ * │ ├── images/ # 课程图片资源
17
+ * │ ├── components/ # 课程组件
18
+ * │ ├── en/ # 英文版本
19
+ * │ │ ├── index.mdx # 课程首页
20
+ * │ │ ├── 1.mdx # 第一章
21
+ * │ │ └── 2.mdx # 第二章
22
+ * │ └── zh-cn/ # 中文版本
23
+ * │ ├── index.mdx # 课程首页
24
+ * │ ├── 1.mdx # 第一章
25
+ * │ └── 2.mdx # 第二章
26
+ * └── learn_astro/ # 另一个课程
27
+ * ├── en/
28
+ * │ ├── index.mdx
29
+ * │ ├── 1.mdx
30
+ * │ └── 2.mdx
31
+ * └── zh-cn/
32
+ * ├── index.mdx
33
+ * ├── 1.mdx
34
+ * └── 2.mdx
35
+ * ```
36
+ *
37
+ * 说明:
38
+ * - 每个课程(如 build_your_own_web_toolbox)是一个独立的目录
39
+ * - 课程目录可以包含多语言版本(en, zh-cn 等)
40
+ * - 每个语言版本包含完整的课程内容
41
+ * - 课程目录可以作为 git 子模块独立管理
42
+ */
43
+ class LessonDB extends BaseDB<typeof COLLECTION_NAME, LessonEntry, LessonDoc> {
44
+ protected collectionName = COLLECTION_NAME;
45
+
46
+ protected createDoc(entry: LessonEntry): LessonDoc {
47
+ return new LessonDoc(entry);
48
+ }
49
+
50
+ /**
51
+ * 获取指定语言的所有课程
52
+ *
53
+ * @param {string} lang - 语言代码
54
+ * @returns {Promise<LessonDoc[]>} 返回指定语言的所有课程
55
+ */
56
+ async allLessons(lang: string): Promise<LessonDoc[]> {
57
+ const docs = await this.getDocsByDepth(2);
58
+ return docs.filter((doc) => doc.getId().endsWith(lang));
59
+ }
60
+
61
+ /**
62
+ * 获取用于 Astro 静态路由生成的路径参数
63
+ *
64
+ * @returns 返回路径参数数组
65
+ */
66
+ async getStaticPaths() {
67
+ const debug = false;
68
+ const docs = await this.getEntries();
69
+
70
+ if (debug) {
71
+ logger.array('所有文档', docs);
72
+ }
73
+
74
+ const paths = docs.map((doc) => {
75
+ const id = doc.id;
76
+ const lang = id.split('/')[1];
77
+
78
+ let slug = '';
79
+ if (id.endsWith(lang)) {
80
+ slug = id.replace(`${lang}`, '');
81
+ } else {
82
+ slug = id.replace(`${lang}/`, '');
83
+ }
84
+
85
+ return {
86
+ params: {
87
+ lang: lang,
88
+ slug: slug,
89
+ },
90
+ };
91
+ });
92
+
93
+ if (debug) {
94
+ logger.array('所有文档的路径', paths);
95
+ }
96
+
97
+ return paths;
98
+ }
99
+ }
100
+
101
+ // 创建并导出单例实例
102
+ const lessonDB = new LessonDB();
103
+ export default lessonDB;
@@ -0,0 +1,75 @@
1
+ import MetaDoc from '../entities/MetaDoc';
2
+ import { logger } from '../utils/logger';
3
+ import { type CollectionEntry } from 'astro:content';
4
+ import { BaseDB } from './BaseDB';
5
+
6
+ export const COLLECTION_NAME = 'meta' as const;
7
+ export type MetaEntry = CollectionEntry<typeof COLLECTION_NAME>;
8
+
9
+ /**
10
+ * 元数据数据库类,用于管理网站的元数据内容集合(如"关于我们"等页面)
11
+ *
12
+ * 目录结构:
13
+ * ```
14
+ * meta/
15
+ * ├── zh-cn/ # 中文内容
16
+ * │ ├── about.md # 关于我们
17
+ * │ ├── advice.md # 建议
18
+ * └── en/ # 英文内容
19
+ * ├── about.md
20
+ * ├── privacy.md
21
+ * └── terms.md
22
+ * ```
23
+ */
24
+ class MetaDB extends BaseDB<typeof COLLECTION_NAME, MetaEntry, MetaDoc> {
25
+ protected collectionName = COLLECTION_NAME;
26
+
27
+ protected createDoc(entry: MetaEntry): MetaDoc {
28
+ return new MetaDoc(entry);
29
+ }
30
+
31
+ /**
32
+ * 获取指定文档的兄弟文档
33
+ * 例如:对于 'zh-cn/about',会返回 'zh-cn' 下的文档
34
+ *
35
+ * @param targetId - 目标文档ID
36
+ * @returns 返回兄弟文档数组(包括目标文档本身)
37
+ */
38
+ async getSiblings(targetId: string): Promise<MetaDoc[]> {
39
+ const target = await this.find(targetId);
40
+ if (!target) {
41
+ return [];
42
+ }
43
+ const docs = await this.getDocsByDepth(2);
44
+ return docs.filter((doc) => doc.getLang() === target.getLang());
45
+ }
46
+
47
+ /**
48
+ * 获取用于 Astro 静态路由生成的路径参数,专门配合 [lang]/meta/[slug].astro 使用
49
+ *
50
+ * @returns 返回路径参数数组
51
+ */
52
+ async getStaticPaths() {
53
+ const debug = false;
54
+ const docs = await this.getDescendantDocs('');
55
+
56
+ const paths = docs.map((doc) => {
57
+ return {
58
+ params: {
59
+ lang: doc.getLang(),
60
+ slug: doc.getSlug(),
61
+ },
62
+ };
63
+ });
64
+
65
+ if (debug) {
66
+ logger.array('所有元数据文档的路径', paths);
67
+ }
68
+
69
+ return paths;
70
+ }
71
+ }
72
+
73
+ // 创建并导出单例实例
74
+ const metaDB = new MetaDB();
75
+ export default metaDB;
@@ -0,0 +1,170 @@
1
+ import { render, type RenderResult, type CollectionEntry, type DataEntryMap } from 'astro:content';
2
+ import { SidebarItemEntity, type SidebarProvider } from './SidebarItem';
3
+ import { logger } from '../utils/logger';
4
+
5
+ /**
6
+ * 文档基类,提供所有文档类型共享的基本功能
7
+ */
8
+ export abstract class BaseDoc<
9
+ Collection extends keyof DataEntryMap,
10
+ T extends CollectionEntry<Collection>,
11
+ > implements SidebarProvider
12
+ {
13
+ protected entry: T;
14
+
15
+ constructor(entry: T) {
16
+ this.entry = entry;
17
+ }
18
+
19
+ /**
20
+ * 获取文档ID
21
+ */
22
+ getId(): string {
23
+ return this.entry.id;
24
+ }
25
+
26
+ /**
27
+ * 获取文档标题
28
+ */
29
+ getTitle(): string {
30
+ return this.entry.data.title as string;
31
+ }
32
+
33
+ /**
34
+ * 获取文档语言
35
+ */
36
+ getLang(): string {
37
+ return this.entry.id.split('/')[0];
38
+ }
39
+
40
+ /**
41
+ * 获取文档slug
42
+ */
43
+ getSlug(): string {
44
+ return this.getId().split('/').slice(1).join('/');
45
+ }
46
+
47
+ /**
48
+ * 获取文档描述
49
+ */
50
+ getDescription(): string {
51
+ return this.entry.data.description as string;
52
+ }
53
+
54
+ /**
55
+ * 获取文档链接
56
+ * 每个子类必须实现此方法以提供正确的链接
57
+ */
58
+ abstract getLink(): string;
59
+
60
+ /**
61
+ * 渲染文档内容
62
+ */
63
+ async render(): Promise<RenderResult> {
64
+ return await render(this.entry);
65
+ }
66
+
67
+ /**
68
+ * 获取文档的层级深度
69
+ * 例如:对于 ID 为 "zh-cn/blog/typescript" 的文档,深度为3
70
+ */
71
+ getLevel(): number {
72
+ return this.entry.id.split('/').length;
73
+ }
74
+
75
+ /**
76
+ * 转换为侧边栏项目
77
+ * 基本实现,只包含当前文档
78
+ */
79
+ async toSidebarItem(): Promise<SidebarItemEntity> {
80
+ return new SidebarItemEntity({
81
+ text: this.getTitle(),
82
+ link: this.getLink(),
83
+ });
84
+ }
85
+ }
86
+
87
+ /**
88
+ * 层级文档基类,为有层级结构的文档类型提供额外功能
89
+ * 例如课程等有父子关系的文档
90
+ */
91
+ export abstract class HierarchicalDoc<
92
+ Collection extends keyof DataEntryMap,
93
+ T extends CollectionEntry<Collection>,
94
+ > extends BaseDoc<Collection, T> {
95
+ /**
96
+ * 获取父文档ID
97
+ * 例如:对于 ID 为 "zh-cn/blog/typescript" 的文档,父ID为 "zh-cn/blog"
98
+ *
99
+ * @returns 父文档ID,如果没有父文档则返回null
100
+ */
101
+ getParentId(): string | null {
102
+ const parts = this.entry.id.split('/');
103
+ return parts.length > 1 ? parts.slice(0, -1).join('/') : null;
104
+ }
105
+
106
+ /**
107
+ * 获取顶级文档的ID
108
+ * 例如:对于 ID 为 "zh-cn/blog/typescript" 的文档,顶级ID为 "zh-cn/blog"
109
+ *
110
+ * 默认实现假设顶级ID是前两部分
111
+ * 子类可以根据需要覆盖此方法
112
+ */
113
+ async getTopDocId(): Promise<string> {
114
+ const id = this.entry.id;
115
+ const parts = id.split('/');
116
+ return parts[0] + '/' + parts[1];
117
+ }
118
+
119
+ /**
120
+ * 获取顶级文档
121
+ * 子类应该实现此方法以提供正确的顶级文档
122
+ */
123
+ abstract getTopDoc(): Promise<HierarchicalDoc<Collection, T> | null>;
124
+
125
+ /**
126
+ * 获取子文档
127
+ * 子类应该实现此方法以提供正确的子文档列表
128
+ */
129
+ abstract getChildren(): Promise<HierarchicalDoc<Collection, T>[]>;
130
+
131
+ /**
132
+ * 转换为侧边栏项目
133
+ * 如果文档有子文档,会包含子文档的侧边栏项目
134
+ */
135
+ override async toSidebarItem(): Promise<SidebarItemEntity> {
136
+ const debug = false;
137
+
138
+ const children = await this.getChildren();
139
+ const childItems = await Promise.all(children.map((child) => child.toSidebarItem()));
140
+
141
+ if (debug) {
142
+ logger.info(`${this.entry.id} 的侧边栏项目`);
143
+ console.log(childItems);
144
+ }
145
+
146
+ return new SidebarItemEntity({
147
+ text: this.getTitle(),
148
+ items: childItems,
149
+ link: this.getLink(),
150
+ });
151
+ }
152
+
153
+ /**
154
+ * 获取顶级侧边栏项目
155
+ * 如果有顶级文档,返回顶级文档的侧边栏项目
156
+ * 否则返回当前文档的侧边栏项目
157
+ */
158
+ async getTopSidebarItem(): Promise<SidebarItemEntity> {
159
+ const topDoc = await this.getTopDoc();
160
+ if (topDoc) {
161
+ return await topDoc.toSidebarItem();
162
+ }
163
+
164
+ return new SidebarItemEntity({
165
+ text: this.getTitle(),
166
+ items: [],
167
+ link: this.getLink(),
168
+ });
169
+ }
170
+ }
@@ -0,0 +1,53 @@
1
+ import type { BlogEntry } from '../database/BlogDB';
2
+ import { LinkUtil } from '../utils/link';
3
+ import Tag from './Tag';
4
+ import { BaseDoc } from './BaseDoc';
5
+ import { COLLECTION_NAME } from '../database/BlogDB';
6
+
7
+ export default class BlogDoc extends BaseDoc<typeof COLLECTION_NAME, BlogEntry> {
8
+ private constructor(entry: BlogEntry) {
9
+ super(entry);
10
+ }
11
+
12
+ static fromEntry(entry: BlogEntry) {
13
+ return new BlogDoc(entry);
14
+ }
15
+
16
+ getLink(): string {
17
+ return LinkUtil.getBlogLink(this.entry.id, this.getLang());
18
+ }
19
+
20
+ getTags(): Tag[] {
21
+ const tags = this.entry.data.tags as string[];
22
+
23
+ if (!tags || tags.length === 0) {
24
+ return [];
25
+ }
26
+
27
+ return tags.map((tag) => new Tag(tag, 0, this.getLang()));
28
+ }
29
+
30
+ getDate(): Date {
31
+ return new Date(this.entry.data.date as Date);
32
+ }
33
+
34
+ getDateForDisplay() {
35
+ try {
36
+ const dateObj = new Date(this.entry.data.date as Date);
37
+
38
+ // Check if date is valid
39
+ if (isNaN(dateObj.getTime())) {
40
+ console.warn(`Invalid date format: ${this.entry.data.date}`);
41
+ return 'Date unavailable: ' + this.getTitle() + ' ' + this.getLink();
42
+ }
43
+ return dateObj.toLocaleDateString('zh-CN', {
44
+ year: 'numeric',
45
+ month: 'long',
46
+ day: 'numeric',
47
+ });
48
+ } catch (error) {
49
+ console.error(`Error formatting date: ${this.entry.data.date}`, error);
50
+ return 'Date unavailable';
51
+ }
52
+ }
53
+ }
@@ -0,0 +1,56 @@
1
+ import { logger } from '@/utils/logger';
2
+ import { SidebarItemEntity } from './SidebarItem';
3
+ import type { CourseEntry } from '../database/CourseDB';
4
+ import courseDB from '../database/CourseDB';
5
+ import { LinkUtil } from '../utils/link';
6
+ import { HierarchicalDoc } from './BaseDoc';
7
+ import { COLLECTION_NAME } from '../database/CourseDB';
8
+
9
+ export default class CourseDoc extends HierarchicalDoc<typeof COLLECTION_NAME, CourseEntry> {
10
+ constructor(entry: CourseEntry) {
11
+ super(entry);
12
+ }
13
+
14
+ static fromEntry(entry: CourseEntry) {
15
+ return new CourseDoc(entry);
16
+ }
17
+
18
+ getLink(): string {
19
+ return LinkUtil.getCourseLink(this.entry.id);
20
+ }
21
+
22
+ async getTopDoc(): Promise<CourseDoc | null> {
23
+ const id = await this.getTopDocId();
24
+ const doc = await courseDB.find(id);
25
+ return doc;
26
+ }
27
+
28
+ async getChildren(): Promise<CourseDoc[]> {
29
+ return await courseDB.getChildren(this.entry.id);
30
+ }
31
+
32
+ override async toSidebarItem(): Promise<SidebarItemEntity> {
33
+ const debug = false;
34
+ const children = await this.getChildren();
35
+ let childItems = await Promise.all(children.map((child) => child.toSidebarItem()));
36
+
37
+ if (this.isBook()) {
38
+ childItems = [...childItems];
39
+ }
40
+
41
+ if (debug) {
42
+ logger.info(`${this.entry.id} 的侧边栏项目`);
43
+ console.log(childItems);
44
+ }
45
+
46
+ return new SidebarItemEntity({
47
+ text: this.getTitle(),
48
+ items: childItems,
49
+ link: this.getLink(),
50
+ });
51
+ }
52
+
53
+ isBook(): boolean {
54
+ return this.entry.id.split('/').length === 2;
55
+ }
56
+ }
@@ -0,0 +1,117 @@
1
+ import type { ExperimentEntry } from '../database/ExperimentDB';
2
+ import experimentDB from '../database/ExperimentDB';
3
+ import { logger } from '../utils/logger';
4
+ import { SidebarItemEntity } from './SidebarItem';
5
+ import type { Heading } from './Heading';
6
+ import { LinkUtil } from '../utils/link';
7
+ import { HierarchicalDoc } from './BaseDoc';
8
+ import { COLLECTION_NAME } from '../database/ExperimentDB';
9
+
10
+ export default class ExperimentDoc extends HierarchicalDoc<
11
+ typeof COLLECTION_NAME,
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 await 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
+ logger.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
+ logger.info(`获取 ${this.entry.id} 的语言: ${lang}`);
58
+ }
59
+
60
+ return lang;
61
+ }
62
+
63
+ getHTML(): string {
64
+ const debug = false;
65
+
66
+ if (debug) {
67
+ logger.info(`获取 ${this.entry.id} 的 HTML`);
68
+ }
69
+
70
+ return this.entry.rendered?.html || '';
71
+ }
72
+
73
+ getHeadings(): Heading[] {
74
+ const debug = false;
75
+
76
+ if (debug) {
77
+ logger.info(`获取 ${this.entry.id} 的 headings`);
78
+ }
79
+
80
+ return (this.entry.rendered?.metadata?.headings as Heading[]) || [];
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(children.map((child) => child.toSidebarItem()));
101
+
102
+ if (this.isBook()) {
103
+ childItems = [...childItems];
104
+ }
105
+
106
+ if (debug) {
107
+ logger.info(`${this.entry.id} 的侧边栏项目`);
108
+ console.log(childItems);
109
+ }
110
+
111
+ return new SidebarItemEntity({
112
+ text: this.getTitle(),
113
+ items: childItems,
114
+ link: this.getLink(),
115
+ });
116
+ }
117
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * 表示文档中的标题结构
3
+ */
4
+ export interface Heading {
5
+ /** 标题深度,如 h1=1, h2=2, h3=3 等 */
6
+ depth: number;
7
+
8
+ /** 标题的唯一标识符,用于锚点链接 */
9
+ slug: string;
10
+
11
+ /** 标题文本内容 */
12
+ text: string;
13
+ }