@coffic/cosy-ui 0.6.36 → 0.6.40

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.
@@ -1,6 +1,6 @@
1
1
  import { render, type RenderResult, type CollectionEntry, type DataEntryMap } from 'astro:content';
2
2
  import { SidebarItemEntity, type SidebarProvider } from './SidebarItem';
3
- import { logger } from '../utils/logger';
3
+ import { cosyLogger, ERROR_PREFIX } from '../cosy';
4
4
 
5
5
  /**
6
6
  * 文档基类,提供所有文档类型共享的基本功能
@@ -8,8 +8,7 @@ import { logger } from '../utils/logger';
8
8
  export abstract class BaseDoc<
9
9
  Collection extends keyof DataEntryMap,
10
10
  T extends CollectionEntry<Collection>,
11
- > implements SidebarProvider
12
- {
11
+ > implements SidebarProvider {
13
12
  protected entry: T;
14
13
 
15
14
  constructor(entry: T) {
@@ -54,6 +53,28 @@ export abstract class BaseDoc<
54
53
  */
55
54
  getAncestorId(level: number): string {
56
55
  const ancestorIds = this.getAncestorIds();
56
+
57
+ if (level < 1) {
58
+ cosyLogger.error('Level must be greater than 0');
59
+ throw new Error(ERROR_PREFIX + 'Level must be greater than 0');
60
+ }
61
+
62
+ if (level >= this.getLevel()) {
63
+ cosyLogger.debug("当前文档的ID:" + this.entry.id);
64
+ cosyLogger.debug("当前文档的Level:" + this.getLevel());
65
+ cosyLogger.debug("正在获取祖先ID,level:" + level);
66
+
67
+ throw new Error(ERROR_PREFIX + 'Level must be less than the document level');
68
+ }
69
+
70
+ if (ancestorIds.length < level) {
71
+ cosyLogger.debug("当前文档的ID\n" + this.entry.id);
72
+ cosyLogger.array("当前文档的祖先IDs", ancestorIds);
73
+ cosyLogger.debug("正在获取祖先ID,level:\n" + level);
74
+
75
+ throw new Error(ERROR_PREFIX + 'Level must be greater than 0');
76
+ }
77
+
57
78
  return ancestorIds[level - 1];
58
79
  }
59
80
 
@@ -93,6 +114,12 @@ export abstract class BaseDoc<
93
114
  * 子类可以根据需要覆盖此方法
94
115
  */
95
116
  getTopDocId(): string {
117
+ const level = this.getLevel();
118
+
119
+ if (level <= 2) {
120
+ return this.entry.id;
121
+ }
122
+
96
123
  return this.getAncestorId(2);
97
124
  }
98
125
 
@@ -155,7 +182,7 @@ export abstract class BaseDoc<
155
182
  const childItems = await Promise.all(children.map((child) => child.toSidebarItem()));
156
183
 
157
184
  if (debug) {
158
- logger.info(`${this.entry.id} 的侧边栏项目`);
185
+ cosyLogger.info(`${this.entry.id} 的侧边栏项目`);
159
186
  console.log(childItems);
160
187
  }
161
188
 
@@ -5,6 +5,46 @@ import { BaseDoc } from './BaseDoc';
5
5
  import { COLLECTION_BLOG } from '../database/BlogDB';
6
6
  import { blogDB } from '../database/BlogDB';
7
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
+
8
48
  export default class BlogDoc extends BaseDoc<typeof COLLECTION_BLOG, BlogEntry> {
9
49
  private constructor(entry: BlogEntry) {
10
50
  super(entry);
@@ -1,4 +1,4 @@
1
- import { logger } from '../utils/logger';
1
+ import { cosyLogger } from '../cosy';
2
2
  import { SidebarItemEntity } from './SidebarItem';
3
3
  import type { CourseEntry } from '../database/CourseDB';
4
4
  import { courseDB } from '../database/CourseDB';
@@ -36,12 +36,12 @@ export default class CourseDoc extends BaseDoc<typeof COLLECTION_COURSE, CourseE
36
36
  async getAncestor(level: number): Promise<CourseDoc | null> {
37
37
  const debug = false;
38
38
  if (debug) {
39
- logger.info(`获取 ${this.entry.id} 的祖先文档,level: ${level}`);
39
+ cosyLogger.info(`获取 ${this.entry.id} 的祖先文档,level: ${level}`);
40
40
  }
41
41
 
42
42
  if (level >= this.getLevel()) {
43
43
  if (debug) {
44
- logger.info(`祖先文档为自身`);
44
+ cosyLogger.info(`祖先文档为自身`);
45
45
  }
46
46
  return this;
47
47
  }
@@ -61,7 +61,7 @@ export default class CourseDoc extends BaseDoc<typeof COLLECTION_COURSE, CourseE
61
61
  (a, b) => a.getOrder() - b.getOrder()
62
62
  );
63
63
  if (debug && children.length > 0) {
64
- logger.array(
64
+ cosyLogger.array(
65
65
  `${this.entry.id} 的子文档(${children.length})`,
66
66
  children.map((child) => `#${child.getOrder()} ${child.entry.id}`)
67
67
  );
@@ -79,7 +79,7 @@ export default class CourseDoc extends BaseDoc<typeof COLLECTION_COURSE, CourseE
79
79
  }
80
80
 
81
81
  if (debug) {
82
- logger.info(`${this.entry.id} 的侧边栏项目`);
82
+ cosyLogger.info(`${this.entry.id} 的侧边栏项目`);
83
83
  console.log(childItems);
84
84
  }
85
85
 
@@ -1,6 +1,6 @@
1
1
  import type { ExperimentEntry } from '../database/ExperimentDB';
2
2
  import { experimentDB } from '../database/ExperimentDB';
3
- import { logger } from '../utils/logger';
3
+ import { cosyLogger } from '../cosy';
4
4
  import { SidebarItemEntity } from './SidebarItem';
5
5
  import { LinkUtil } from '../utils/link';
6
6
  import { COLLECTION_EXPERIMENT } from '../database/ExperimentDB';
@@ -31,7 +31,7 @@ export default class ExperimentDoc extends BaseDoc<typeof COLLECTION_EXPERIMENT,
31
31
  const link = LinkUtil.getExperimentLink(lang, this.getId());
32
32
 
33
33
  if (debug) {
34
- logger.info(`获取 ${this.entry.id} 的链接: ${link}`);
34
+ cosyLogger.info(`获取 ${this.entry.id} 的链接: ${link}`);
35
35
  }
36
36
 
37
37
  return link;
@@ -51,7 +51,7 @@ export default class ExperimentDoc extends BaseDoc<typeof COLLECTION_EXPERIMENT,
51
51
  const lang = parts[1];
52
52
 
53
53
  if (debug) {
54
- logger.info(`获取 ${this.entry.id} 的语言: ${lang}`);
54
+ cosyLogger.info(`获取 ${this.entry.id} 的语言: ${lang}`);
55
55
  }
56
56
 
57
57
  return lang;
@@ -61,7 +61,7 @@ export default class ExperimentDoc extends BaseDoc<typeof COLLECTION_EXPERIMENT,
61
61
  const debug = false;
62
62
 
63
63
  if (debug) {
64
- logger.info(`获取 ${this.entry.id} 的 HTML`);
64
+ cosyLogger.info(`获取 ${this.entry.id} 的 HTML`);
65
65
  }
66
66
 
67
67
  return this.entry.rendered?.html || '';
@@ -71,7 +71,7 @@ export default class ExperimentDoc extends BaseDoc<typeof COLLECTION_EXPERIMENT,
71
71
  const debug = false;
72
72
 
73
73
  if (debug) {
74
- logger.info(`获取 ${this.entry.id} 的 headings`);
74
+ cosyLogger.info(`获取 ${this.entry.id} 的 headings`);
75
75
  }
76
76
 
77
77
  return (this.entry.rendered?.metadata?.headings as IHeadingType[]) || [];
@@ -101,7 +101,7 @@ export default class ExperimentDoc extends BaseDoc<typeof COLLECTION_EXPERIMENT,
101
101
  }
102
102
 
103
103
  if (debug) {
104
- logger.info(`${this.entry.id} 的侧边栏项目`);
104
+ cosyLogger.info(`${this.entry.id} 的侧边栏项目`);
105
105
  console.log(childItems);
106
106
  }
107
107
 
@@ -1,12 +1,46 @@
1
1
  import type { LessonEntry } from '../database/LessonDB';
2
2
  import { lessonDB } from '../database/LessonDB';
3
- import { logger } from '../utils/logger';
3
+ import { cosyLogger } from '../cosy';
4
4
  import { SidebarItemEntity } from './SidebarItem';
5
5
  import { LinkUtil } from '../utils/link';
6
6
  import { COLLECTION_LESSON } from '../database/LessonDB';
7
7
  import { BaseDoc } from './BaseDoc';
8
8
  import type { IHeadingType } from '../types/heading';
9
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
+ */
10
44
  export default class LessonDoc extends BaseDoc<typeof COLLECTION_LESSON, LessonEntry> {
11
45
  constructor(entry: LessonEntry) {
12
46
  super(entry);
@@ -31,7 +65,7 @@ export default class LessonDoc extends BaseDoc<typeof COLLECTION_LESSON, LessonE
31
65
  const link = LinkUtil.getLessonLink(lang, this.getId());
32
66
 
33
67
  if (debug) {
34
- logger.info(`获取 ${this.entry.id} 的链接: ${link}`);
68
+ cosyLogger.info(`获取 ${this.entry.id} 的链接: ${link}`);
35
69
  }
36
70
 
37
71
  return link;
@@ -51,7 +85,7 @@ export default class LessonDoc extends BaseDoc<typeof COLLECTION_LESSON, LessonE
51
85
  const lang = parts[1];
52
86
 
53
87
  if (debug) {
54
- logger.info(`获取 ${this.entry.id} 的语言: ${lang}`);
88
+ cosyLogger.info(`获取 ${this.entry.id} 的语言: ${lang}`);
55
89
  }
56
90
 
57
91
  return lang;
@@ -61,7 +95,7 @@ export default class LessonDoc extends BaseDoc<typeof COLLECTION_LESSON, LessonE
61
95
  const debug = false;
62
96
 
63
97
  if (debug) {
64
- logger.info(`获取 ${this.entry.id} 的 HTML`);
98
+ cosyLogger.info(`获取 ${this.entry.id} 的 HTML`);
65
99
  }
66
100
 
67
101
  return this.entry.rendered?.html || '';
@@ -71,7 +105,7 @@ export default class LessonDoc extends BaseDoc<typeof COLLECTION_LESSON, LessonE
71
105
  const debug = false;
72
106
 
73
107
  if (debug) {
74
- logger.info(`获取 ${this.entry.id} 的 headings`);
108
+ cosyLogger.info(`获取 ${this.entry.id} 的 headings`);
75
109
  }
76
110
 
77
111
  return (this.entry.rendered?.metadata?.headings as IHeadingType[]) || [];
@@ -101,7 +135,7 @@ export default class LessonDoc extends BaseDoc<typeof COLLECTION_LESSON, LessonE
101
135
  }
102
136
 
103
137
  if (debug) {
104
- logger.info(`${this.entry.id} 的侧边栏项目`);
138
+ cosyLogger.info(`${this.entry.id} 的侧边栏项目`);
105
139
  console.log(childItems);
106
140
  }
107
141
 
package/dist/index.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  export * from './index_astro';
2
2
  export * from './index_utils';
3
- export * from './index_icons';
3
+ export * from './index_icons';
@@ -2,103 +2,103 @@ import { defineCollection, z } from 'astro:content';
2
2
  import { glob } from 'astro/loaders';
3
3
 
4
4
  export const makeArticleCollection = (base: string) => {
5
- return defineCollection({
6
- loader: glob({
7
- pattern: '**/*.{md,mdx}',
8
- base,
9
- }),
10
- schema: z.object({
11
- title: z.string(),
12
- folder: z.boolean(),
13
- order: z.number(),
14
- description: z.string().optional(),
15
- }),
16
- });
5
+ return defineCollection({
6
+ loader: glob({
7
+ pattern: '**/*.{md,mdx}',
8
+ base,
9
+ }),
10
+ schema: z.object({
11
+ title: z.string().optional(),
12
+ folder: z.boolean().optional(),
13
+ order: z.number().optional(),
14
+ description: z.string().optional(),
15
+ }),
16
+ });
17
17
  };
18
18
 
19
19
  export const makeBlogCollection = (base: string) => {
20
- return defineCollection({
21
- loader: glob({
22
- pattern: '**/*.{md,mdx}',
23
- base,
24
- }),
25
- schema: z.object({
26
- title: z.string(),
27
- description: z.string().optional(),
28
- tags: z.array(z.string()).optional(),
29
- date: z.date().optional(),
30
- authors: z
31
- .array(
32
- z.object({
33
- name: z.string(),
34
- picture: z.string().optional(),
35
- url: z.string().optional(),
36
- })
37
- )
38
- .optional(),
39
- }),
40
- });
20
+ return defineCollection({
21
+ loader: glob({
22
+ pattern: '**/*.{md,mdx}',
23
+ base,
24
+ }),
25
+ schema: z.object({
26
+ title: z.string(),
27
+ description: z.string().optional(),
28
+ tags: z.array(z.string()).optional(),
29
+ date: z.date().optional(),
30
+ authors: z
31
+ .array(
32
+ z.object({
33
+ name: z.string(),
34
+ picture: z.string().optional(),
35
+ url: z.string().optional(),
36
+ })
37
+ )
38
+ .optional(),
39
+ }),
40
+ });
41
41
  };
42
42
 
43
43
  export const makeCourseCollection = (base: string) => {
44
- return defineCollection({
45
- loader: glob({
46
- pattern: '**/*.{md,mdx}',
47
- base,
48
- }),
49
- schema: z.object({
50
- title: z.string(),
51
- description: z.string().optional(),
52
- folder: z.boolean(),
53
- order: z.number(),
54
- }),
55
- });
44
+ return defineCollection({
45
+ loader: glob({
46
+ pattern: '**/*.{md,mdx}',
47
+ base,
48
+ }),
49
+ schema: z.object({
50
+ title: z.string().optional(),
51
+ description: z.string().optional(),
52
+ folder: z.boolean().optional(),
53
+ order: z.number().optional(),
54
+ }),
55
+ });
56
56
  };
57
57
 
58
58
  export const makeExperimentCollection = (base: string) => {
59
- return defineCollection({
60
- loader: glob({
61
- pattern: '**/*.{md,mdx}',
62
- base,
63
- }),
64
- schema: z.object({
65
- title: z.string(),
66
- description: z.string().optional(),
67
- pubDate: z.date().optional(),
68
- }),
69
- });
59
+ return defineCollection({
60
+ loader: glob({
61
+ pattern: '**/*.{md,mdx}',
62
+ base,
63
+ }),
64
+ schema: z.object({
65
+ title: z.string().optional(),
66
+ description: z.string().optional(),
67
+ pubDate: z.date().optional(),
68
+ }),
69
+ });
70
70
  };
71
71
 
72
72
  export const makeLessonCollection = (base: string) => {
73
- return defineCollection({
74
- loader: glob({
75
- pattern: '**/*.{md,mdx}',
76
- base,
77
- }),
78
- schema: z.object({
79
- title: z.string(),
80
- description: z.string().optional(),
81
- authors: z
82
- .array(
83
- z.object({
84
- name: z.string(),
85
- picture: z.string().optional(),
86
- })
87
- )
88
- .optional(),
89
- }),
90
- });
73
+ return defineCollection({
74
+ loader: glob({
75
+ pattern: '**/*.{md,mdx}',
76
+ base,
77
+ }),
78
+ schema: z.object({
79
+ title: z.string().optional(),
80
+ description: z.string().optional(),
81
+ authors: z
82
+ .array(
83
+ z.object({
84
+ name: z.string(),
85
+ picture: z.string().optional(),
86
+ })
87
+ )
88
+ .optional(),
89
+ }),
90
+ });
91
91
  };
92
92
 
93
93
  export const makeMetaCollection = (base: string) => {
94
- return defineCollection({
95
- loader: glob({
96
- pattern: '**/*.{md,mdx}',
97
- base,
98
- }),
99
- schema: z.object({
100
- title: z.string(),
101
- description: z.string().optional(),
102
- }),
103
- });
94
+ return defineCollection({
95
+ loader: glob({
96
+ pattern: '**/*.{md,mdx}',
97
+ base,
98
+ }),
99
+ schema: z.object({
100
+ title: z.string().optional(),
101
+ description: z.string().optional(),
102
+ }),
103
+ });
104
104
  };