@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
@@ -3,139 +3,141 @@
3
3
  * 提供链式API来创建和管理多语言文本
4
4
  */
5
5
  export class LangEntry {
6
- private translations: Record<string, string> = {};
7
-
8
- /**
9
- * 设置中文文本
10
- * @param text 中文文本
11
- */
12
- setZh(text: string): LangEntry {
13
- this.translations['zh-cn'] = text;
14
- return this;
15
- }
16
-
17
- /**
18
- * 设置英文文本
19
- * @param text 英文文本
20
- */
21
- setEn(text: string): LangEntry {
22
- this.translations['en'] = text;
23
- return this;
24
- }
25
-
26
- /**
27
- * 设置日文文本
28
- * @param text 日文文本
29
- */
30
- setJa(text: string): LangEntry {
31
- this.translations['ja'] = text;
32
- return this;
33
- }
34
-
35
- /**
36
- * 设置任意语言的文本
37
- * @param lang 语言代码
38
- * @param text 文本内容
39
- */
40
- set(lang: string, text: string): LangEntry {
41
- this.translations[lang] = text;
42
- return this;
43
- }
44
-
45
- /**
46
- * 设置所有语言为相同的文本值
47
- * 适用于不需要翻译的品牌名、专有名词等
48
- * @param text 所有语言共用的文本
49
- */
50
- setAll(text: string): LangEntry {
51
- // 如果已有语言,则更新它们
52
- if (Object.keys(this.translations).length > 0) {
53
- for (const lang of Object.keys(this.translations)) {
54
- this.translations[lang] = text;
55
- }
56
- } else {
57
- // 默认至少设置中英文
58
- this.translations['zh-cn'] = text;
59
- this.translations['en'] = text;
60
- }
61
- return this;
62
- }
63
-
64
- /**
65
- * 获取指定语言的文本
66
- * @param lang 语言代码
67
- * @param defaultValue 默认值,如果未找到翻译则返回此值
68
- */
69
- get(lang: string, defaultValue?: string): string {
70
- return this.translations[lang] || defaultValue || this.getFirst() || '';
71
- }
72
-
73
- /**
74
- * 获取第一个可用的翻译文本
75
- */
76
- getFirst(): string | undefined {
77
- const keys = Object.keys(this.translations);
78
- return keys.length > 0 ? this.translations[keys[0]] : undefined;
79
- }
80
-
81
- /**
82
- * 获取所有可用的语言代码
83
- */
84
- getLanguages(): string[] {
85
- return Object.keys(this.translations);
86
- }
87
-
88
- /**
89
- * 获取所有翻译
90
- */
91
- getAll(): Record<string, string> {
92
- return { ...this.translations };
93
- }
94
-
95
- /**
96
- * 合并另一个语言条目
97
- * @param other 要合并的语言条目
98
- */
99
- merge(other: LangEntry): LangEntry {
100
- Object.assign(this.translations, other.getAll());
101
- return this;
102
- }
103
-
104
- /**
105
- * 检查是否包含指定语言
106
- * @param lang 语言代码
107
- */
108
- has(lang: string): boolean {
109
- return lang in this.translations;
110
- }
111
-
112
- /**
113
- * 转换为普通对象
114
- */
115
- toObject(): Record<string, string> {
116
- return { ...this.translations };
117
- }
118
-
119
- /**
120
- * 允许以对象属性方式访问翻译
121
- * 例如: entry['zh-cn'] 或 entry.en
122
- */
123
- [key: string]: any;
6
+ private translations: Record<string, string> = {};
7
+
8
+ /**
9
+ * 设置中文文本
10
+ * @param text 中文文本
11
+ */
12
+ setZh(text: string): LangEntry {
13
+ this.translations['zh-cn'] = text;
14
+ return this;
15
+ }
16
+
17
+ /**
18
+ * 设置英文文本
19
+ * @param text 英文文本
20
+ */
21
+ setEn(text: string): LangEntry {
22
+ this.translations['en'] = text;
23
+ return this;
24
+ }
25
+
26
+ /**
27
+ * 设置日文文本
28
+ * @param text 日文文本
29
+ */
30
+ setJa(text: string): LangEntry {
31
+ this.translations['ja'] = text;
32
+ return this;
33
+ }
34
+
35
+ /**
36
+ * 设置任意语言的文本
37
+ * @param lang 语言代码
38
+ * @param text 文本内容
39
+ */
40
+ set(lang: string, text: string): LangEntry {
41
+ this.translations[lang] = text;
42
+ return this;
43
+ }
44
+
45
+ /**
46
+ * 设置所有语言为相同的文本值
47
+ * 适用于不需要翻译的品牌名、专有名词等
48
+ * @param text 所有语言共用的文本
49
+ */
50
+ setAll(text: string): LangEntry {
51
+ // 如果已有语言,则更新它们
52
+ if (Object.keys(this.translations).length > 0) {
53
+ for (const lang of Object.keys(this.translations)) {
54
+ this.translations[lang] = text;
55
+ }
56
+ } else {
57
+ // 默认至少设置中英文
58
+ this.translations['zh-cn'] = text;
59
+ this.translations['en'] = text;
60
+ }
61
+ return this;
62
+ }
63
+
64
+ /**
65
+ * 获取指定语言的文本
66
+ * @param lang 语言代码
67
+ * @param defaultValue 默认值,如果未找到翻译则返回此值
68
+ */
69
+ get(lang: string, defaultValue?: string): string {
70
+ return this.translations[lang] || defaultValue || this.getFirst() || '';
71
+ }
72
+
73
+ /**
74
+ * 获取第一个可用的翻译文本
75
+ */
76
+ getFirst(): string | undefined {
77
+ const keys = Object.keys(this.translations);
78
+ return keys.length > 0 ? this.translations[keys[0]] : undefined;
79
+ }
80
+
81
+ /**
82
+ * 获取所有可用的语言代码
83
+ */
84
+ getLanguages(): string[] {
85
+ return Object.keys(this.translations);
86
+ }
87
+
88
+ /**
89
+ * 获取所有翻译
90
+ */
91
+ getAll(): Record<string, string> {
92
+ return { ...this.translations };
93
+ }
94
+
95
+ /**
96
+ * 合并另一个语言条目
97
+ * @param other 要合并的语言条目
98
+ */
99
+ merge(other: LangEntry): LangEntry {
100
+ Object.assign(this.translations, other.getAll());
101
+ return this;
102
+ }
103
+
104
+ /**
105
+ * 检查是否包含指定语言
106
+ * @param lang 语言代码
107
+ */
108
+ has(lang: string): boolean {
109
+ return lang in this.translations;
110
+ }
111
+
112
+ /**
113
+ * 转换为普通对象
114
+ */
115
+ toObject(): Record<string, string> {
116
+ return { ...this.translations };
117
+ }
118
+
119
+ /**
120
+ * 允许以对象属性方式访问翻译
121
+ * 例如: entry['zh-cn'] 或 entry.en
122
+ */
123
+ [key: string]: any;
124
124
  }
125
125
 
126
126
  // 创建索引访问器的Proxy处理程序
127
127
  const langEntryHandler: ProxyHandler<LangEntry> = {
128
- get(target: LangEntry, prop: string | symbol): any {
129
- // 如果是字符串属性并且不是LangEntry的方法,则尝试作为语言代码获取翻译
130
- if (typeof prop === 'string' &&
131
- !(prop in Object.getPrototypeOf(target)) &&
132
- prop !== 'translations') {
133
- return target.get(prop);
134
- }
135
-
136
- // 否则返回原始属性
137
- return (target as any)[prop];
138
- }
128
+ get(target: LangEntry, prop: string | symbol): any {
129
+ // 如果是字符串属性并且不是LangEntry的方法,则尝试作为语言代码获取翻译
130
+ if (
131
+ typeof prop === 'string' &&
132
+ !(prop in Object.getPrototypeOf(target)) &&
133
+ prop !== 'translations'
134
+ ) {
135
+ return target.get(prop);
136
+ }
137
+
138
+ // 否则返回原始属性
139
+ return (target as any)[prop];
140
+ },
139
141
  };
140
142
 
141
143
  /**
@@ -143,80 +145,77 @@ const langEntryHandler: ProxyHandler<LangEntry> = {
143
145
  * 可以直接通过静态方法创建和配置语言条目
144
146
  */
145
147
  export class LangPackage {
146
- // 内部实例,用于静态方法直接操作
147
- private static _instance = new Proxy(new LangEntry(), langEntryHandler);
148
-
149
- /**
150
- * 创建一个新的语言条目
151
- */
152
- static make(): LangEntry {
153
- return new Proxy(new LangEntry(), langEntryHandler);
154
- }
155
-
156
- /**
157
- * 从对象创建语言条目
158
- * @param obj 包含语言翻译的对象
159
- */
160
- static from(obj: Record<string, string>): LangEntry {
161
- const entry = new LangEntry();
162
- Object.entries(obj).forEach(([lang, text]) => {
163
- entry.set(lang, text);
164
- });
165
- return new Proxy(entry, langEntryHandler);
166
- }
167
-
168
- /**
169
- * 创建只包含中文和英文的语言条目
170
- * @param zh 中文文本
171
- * @param en 英文文本
172
- */
173
- static zhEn(zh: string, en: string): LangEntry {
174
- return new Proxy(new LangEntry().setZh(zh).setEn(en), langEntryHandler);
175
- }
176
-
177
- /**
178
- * 创建所有语言都使用相同文本的语言条目
179
- * 适用于品牌名、产品名等不需要翻译的文本
180
- * @param text 所有语言共用的文本
181
- */
182
- static common(text: string): LangEntry {
183
- return new Proxy(new LangEntry().setAll(text), langEntryHandler);
184
- }
185
-
186
- /**
187
- * 设置中文文本
188
- * 快捷方法,直接返回一个新的LangEntry实例
189
- * @param text 中文文本
190
- */
191
- static setZh(text: string): LangEntry {
192
- return new Proxy(new LangEntry().setZh(text), langEntryHandler);
193
- }
194
-
195
- /**
196
- * 设置英文文本
197
- * 快捷方法,直接返回一个新的LangEntry实例
198
- * @param text 英文文本
199
- */
200
- static setEn(text: string): LangEntry {
201
- return new Proxy(new LangEntry().setEn(text), langEntryHandler);
202
- }
203
-
204
- /**
205
- * 设置日文文本
206
- * 快捷方法,直接返回一个新的LangEntry实例
207
- * @param text 日文文本
208
- */
209
- static setJa(text: string): LangEntry {
210
- return new Proxy(new LangEntry().setJa(text), langEntryHandler);
211
- }
212
-
213
- /**
214
- * 设置任意语言的文本
215
- * 快捷方法,直接返回一个新的LangEntry实例
216
- * @param lang 语言代码
217
- * @param text 文本内容
218
- */
219
- static set(lang: string, text: string): LangEntry {
220
- return new Proxy(new LangEntry().set(lang, text), langEntryHandler);
221
- }
222
- }
148
+ /**
149
+ * 创建一个新的语言条目
150
+ */
151
+ static make(): LangEntry {
152
+ return new Proxy(new LangEntry(), langEntryHandler);
153
+ }
154
+
155
+ /**
156
+ * 从对象创建语言条目
157
+ * @param obj 包含语言翻译的对象
158
+ */
159
+ static from(obj: Record<string, string>): LangEntry {
160
+ const entry = new LangEntry();
161
+ Object.entries(obj).forEach(([lang, text]) => {
162
+ entry.set(lang, text);
163
+ });
164
+ return new Proxy(entry, langEntryHandler);
165
+ }
166
+
167
+ /**
168
+ * 创建只包含中文和英文的语言条目
169
+ * @param zh 中文文本
170
+ * @param en 英文文本
171
+ */
172
+ static zhEn(zh: string, en: string): LangEntry {
173
+ return new Proxy(new LangEntry().setZh(zh).setEn(en), langEntryHandler);
174
+ }
175
+
176
+ /**
177
+ * 创建所有语言都使用相同文本的语言条目
178
+ * 适用于品牌名、产品名等不需要翻译的文本
179
+ * @param text 所有语言共用的文本
180
+ */
181
+ static common(text: string): LangEntry {
182
+ return new Proxy(new LangEntry().setAll(text), langEntryHandler);
183
+ }
184
+
185
+ /**
186
+ * 设置中文文本
187
+ * 快捷方法,直接返回一个新的LangEntry实例
188
+ * @param text 中文文本
189
+ */
190
+ static setZh(text: string): LangEntry {
191
+ return new Proxy(new LangEntry().setZh(text), langEntryHandler);
192
+ }
193
+
194
+ /**
195
+ * 设置英文文本
196
+ * 快捷方法,直接返回一个新的LangEntry实例
197
+ * @param text 英文文本
198
+ */
199
+ static setEn(text: string): LangEntry {
200
+ return new Proxy(new LangEntry().setEn(text), langEntryHandler);
201
+ }
202
+
203
+ /**
204
+ * 设置日文文本
205
+ * 快捷方法,直接返回一个新的LangEntry实例
206
+ * @param text 日文文本
207
+ */
208
+ static setJa(text: string): LangEntry {
209
+ return new Proxy(new LangEntry().setJa(text), langEntryHandler);
210
+ }
211
+
212
+ /**
213
+ * 设置任意语言的文本
214
+ * 快捷方法,直接返回一个新的LangEntry实例
215
+ * @param lang 语言代码
216
+ * @param text 文本内容
217
+ */
218
+ static set(lang: string, text: string): LangEntry {
219
+ return new Proxy(new LangEntry().set(lang, text), langEntryHandler);
220
+ }
221
+ }
@@ -94,13 +94,6 @@ export class LanguageUtil {
94
94
  return pathMatch[1];
95
95
  }
96
96
 
97
- // 如果网站运行在二级目录,则从路径中提取语言代码
98
- // 例如: /docs/zh-cn/components/button
99
- const pathMatch2 = currentUrl.match(/^\/([^\/]+)\/([\w-]+)\//);
100
- if (pathMatch2) {
101
- return pathMatch2[2];
102
- }
103
-
104
97
  // 尝试从查询参数中提取语言代码
105
98
  // 例如: ?lang=zh-cn
106
99
  const urlParams = new URLSearchParams(currentUrl.split('?')[1]);