@codady/coax 0.0.3 → 0.0.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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2025 AXUI
3
+ Copyright (c) 2026 AXUI
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,236 +1,401 @@
1
+ # Coax - Code Display Web Component
1
2
 
2
- # Coax: A Code Syntax Highlighter for Web Components
3
+ 一个优雅的代码高亮和显示 Web 组件。
3
4
 
4
- ## Overview
5
+ An elegant code highlighting and display web component.
5
6
 
6
- **Coax** is a custom web component that enables syntax highlighting for various programming languages inside your HTML documents. It allows you to easily embed highlighted code using web standards (Web Components) and modern CSS. This plugin automatically highlights code using regular expressions and supports dynamic theming and customization.
7
+ ## 🌟 Features / 功能特性
7
8
 
8
- ---
9
-
10
- ## Features
11
-
12
- - **Custom Web Component**: Easily use `<ax-code>` to render syntax-highlighted code.
13
- - **Language Support**: Register custom language configurations for syntax highlighting (e.g., `HTML`, `CSS`, `JavaScript`, `TypeScript`).
14
- - **Custom Themes**: Add language-specific themes using CSS variables for customization.
15
- - **Shadow DOM**: Isolated styling and functionality using the Shadow DOM for better encapsulation.
16
- - **Dynamic CSS**: Dynamically generate styles for each language based on rules.
17
-
18
- ---
19
-
20
- ## Installation
21
-
22
- Simply include the `Coax` JavaScript file in your project, or use the module system if you're using a build tool like Webpack or Rollup.
23
-
24
- ### Direct inclusion (via script tag)
9
+ - **Syntax Highlighting** - Built-in support for HTML, CSS, JavaScript, TypeScript, Markdown
10
+ - **语法高亮** - 内置 HTML、CSS、JavaScript、TypeScript、Markdown 支持
11
+ - **Typewriter Effect** - Real-time streaming with adjustable speed
12
+ - **打字机效果** - 实时流式显示,速度可调
13
+ - **Dark/Light Mode** - Automatic theme switching based on system preference
14
+ - **明暗模式** - 基于系统偏好自动切换主题
15
+ - **Interactive Tools** - Copy button with visual feedback
16
+ - **交互工具** - 带视觉反馈的复制按钮
17
+ - **Customizable** - Extensive CSS custom properties for styling
18
+ - **高度可定制** - 丰富的 CSS 自定义属性
19
+ - **Line Features** - Line numbers, zebra striping, hover effects
20
+ - **行功能** - 行号、斑马纹、悬停效果
25
21
 
26
- ```html
27
- <script src="path/to/coax.js"></script>
28
- ````
29
-
30
- ---
31
-
32
- ## Usage
22
+ ## 📦 Installation / 安装
33
23
 
34
- To use the Coax code highlighting component, follow these steps:
35
-
36
- ### 1. Define the `<ax-code>` element
24
+ ### CDN Usage / CDN 使用
37
25
 
38
26
  ```html
39
- <ax-code lang="js">
40
- // Your JavaScript code here
41
- </ax-code>
42
-
43
- <ax-code lang="css">
44
- /* Your CSS code here */
45
- </ax-code>
27
+ <!-- 直接引入 UMD 版本 -->
28
+ <script src="https://unpkg.com/@codady/coax@latest"></script>
46
29
 
47
- <ax-code lang="html">
48
- <!-- Your HTML code here -->
49
- </ax-code>
30
+ <!-- 或者使用模块化方式 -->
31
+ <script type="module">
32
+ import 'https://unpkg.com/@codady/coax@latest';
33
+ </script>
50
34
  ```
51
35
 
52
- ### 2. Register Language Configurations
36
+ ### NPM Installation / NPM 安装
53
37
 
54
- You can register languages with their syntax rules and themes using the `Coax.register` method.
38
+ ```bash
39
+ npm install @codady/coax
40
+ # 或者
41
+ yarn add @codady/coax
42
+ ```
55
43
 
56
44
  ```javascript
57
- Coax.register('html', {
58
- rules: [
59
- { name: 'comment', reg: /&lt;!--[\s\S]*?--&gt;/ },
60
- { name: 'tag', reg: /&lt;\/?[a-zA-Z0-9]+/ },
61
- { name: 'attr', reg: /[a-zA-Z-]+(?=\s*=\s*)/ },
62
- { name: 'string', reg: /(['"])(?:\\.|[^\\])*?\1/ },
63
- ],
64
- theme: {
65
- 'tag': '#e06c75', // red
66
- 'attr': '#d19a66', // orange
67
- 'string': '#98c379', // green
68
- }
69
- });
45
+ // 在您的模块中导入
46
+ import '@codady/coax';
70
47
  ```
71
48
 
72
- ### 3. Customization
49
+ ## 🚀 Quick Start / 快速开始
73
50
 
74
- You can customize the code highlighting by modifying the language's rules, theme colors, or internal CSS using the `theme`, `internalCss`, and `cssPrefix` properties.
51
+ ### Basic Usage / 基本使用
75
52
 
76
- Example:
77
-
78
- ```javascript
79
- Coax.register('css', {
80
- rules: [
81
- { name: 'comment', reg: /\/\*[\s\S]*?\*\// },
82
- { name: 'property', reg: /[a-z-]+(?=\s*:)/ },
83
- { name: 'selector', reg: /[.#a-z0-9, \n\t>:+()_-]+(?=\s*\{)/i },
84
- { name: 'unit', reg: /(?<=\d)(px|em|rem|%|vh|vw|ms|s|deg)/ },
85
- ],
86
- theme: {
87
- 'keyword': '#61afef', // blue
88
- 'string': '#98c379', // green
89
- 'op': '#abb2bf', // gray
90
- },
91
- internalCss: `
92
- .ax-css-string { color: var(--ax-code-string); }
93
- `
94
- });
53
+ ```html
54
+ <!-- 注册组件后使用 -->
55
+ <ax-code lang="js">
56
+ function greet(name) {
57
+ console.log(`Hello, ${name}!`);
58
+ return true;
59
+ }
60
+ </ax-code>
95
61
  ```
96
62
 
97
- ### 4. Optional Attributes
98
-
99
- You can customize the code block's height and max height:
63
+ ### All Features / 全部功能
100
64
 
101
65
  ```html
102
- <ax-code lang="js" height="200px" max-height="400px">
103
- const x = 10;
66
+ <ax-code
67
+ lang="ts"
68
+ speed="5"
69
+ indexed
70
+ striped
71
+ hoverable
72
+ wrapped
73
+ scheme="dark"
74
+ tools="copy"
75
+ height="300px"
76
+ max-height="500px"
77
+ >
78
+ interface User {
79
+ id: number;
80
+ name: string;
81
+ email: string;
82
+
83
+ greet(): string;
84
+ }
85
+
86
+ const user: User = {
87
+ id: 1,
88
+ name: "John",
89
+ email: "john@example.com",
90
+ greet() {
91
+ return `Hello, ${this.name}!`;
92
+ }
93
+ };
104
94
  </ax-code>
105
95
  ```
106
96
 
107
- ---
97
+ ## 📚 API Reference / API 参考
108
98
 
109
- ## Development
99
+ ### Attributes / 属性
110
100
 
111
- If you'd like to extend or modify the plugin, the source code is available for you to contribute or adapt.
101
+ | Attribute | Type | Default | Description | 描述 |
102
+ |-----------|------|---------|-------------|------|
103
+ | `lang` | string | `'plain'` | Programming language | 编程语言 |
104
+ | `speed` | number | `5` | Typing speed (higher = slower) | 打字速度(值越高越慢) |
105
+ | `indexed` | boolean | `false` | Show line numbers | 显示行号 |
106
+ | `striped` | boolean | `false` | Zebra striping | 斑马纹背景 |
107
+ | `hoverable` | boolean | `false` | Highlight line on hover | 鼠标悬停高亮 |
108
+ | `wrapped` | boolean | `false` | Line wrapping | 自动换行 |
109
+ | `unnamed` | boolean | `false` | Hide language name | 隐藏语言名称 |
110
+ | `sanitized` | boolean | `false` | Remove empty lines | 移除空行 |
111
+ | `scheme` | string | `'auto'` | Color scheme: `light`, `dark`, `auto` | 配色方案 |
112
+ | `height` | string | `'auto'` | Container height | 容器高度 |
113
+ | `max-height` | string | `'500px'` | Max container height | 最大容器高度 |
114
+ | `tools` | string | `''` | Comma-separated tools | 逗号分隔的工具 |
112
115
 
113
- * **Clone the repository**: `git clone https://github.com/your-repository`
114
- * **Run build tools** (e.g., Webpack, Babel) for production optimization.
116
+ ### Supported Languages / 支持的语言
115
117
 
116
- ---
118
+ | Language | Alias | Key |
119
+ |----------|-------|-----|
120
+ | JavaScript | Javascript | `js` |
121
+ | TypeScript | Typescript | `ts` |
122
+ | HTML | HTML | `html` |
123
+ | CSS | CSS | `css` |
124
+ | Markdown | Markdown | `md` |
125
+ | Plain Text | Plain Text | `plain` |
117
126
 
118
- ## License
127
+ ### Methods / 方法
119
128
 
120
- MIT License. Feel free to use and modify it for your projects.
129
+ #### Static Methods / 静态方法
121
130
 
122
- ---
131
+ ```javascript
132
+ // Register a new language
133
+ Coax.register('python', {
134
+ alias: 'Python',
135
+ rules: [
136
+ { token: 'keyword', pattern: /\b(def|class|if|else|for|while)\b/, light: '#d73a49', dark: '#ff7b72' },
137
+ // ... more rules
138
+ ]
139
+ });
123
140
 
124
- # 中文文档
141
+ // Register custom tools
142
+ Coax.addTools([
143
+ {
144
+ name: 'download',
145
+ icon: '<svg>...</svg>',
146
+ action: function() {
147
+ // Download code as file
148
+ }
149
+ }
150
+ ]);
151
+ ```
125
152
 
126
- ## 概述
153
+ #### Instance Methods / 实例方法
127
154
 
128
- **Coax** 是一个自定义 Web 组件,可以在 HTML 文档中嵌入高亮的代码。它允许您使用现代 Web 标准(Web 组件)和 CSS 来轻松实现代码高亮。该插件通过正则表达式自动高亮代码,并支持动态主题和自定义设置。
155
+ ```javascript
156
+ const codeElement = document.querySelector('ax-code');
129
157
 
130
- ---
158
+ // Replace all code
159
+ codeElement.replace('const x = 10;');
131
160
 
132
- ## 特性
161
+ // Append new code
162
+ codeElement.append('\nconsole.log(x);');
133
163
 
134
- * **自定义 Web 组件**:使用 `<ax-code>` 标签轻松渲染代码高亮。
135
- * **语言支持**:为每种编程语言注册自定义语法高亮配置(例如,`HTML`,`CSS`,`JavaScript`,`TypeScript`)。
136
- * **自定义主题**:使用 CSS 变量添加语言特定的主题,支持个性化定制。
137
- * **Shadow DOM**:使用 Shadow DOM 实现样式和功能的隔离。
138
- * **动态 CSS**:基于规则动态生成每种语言的样式。
164
+ // Stream code with typewriter effect
165
+ codeElement.stream('function test() {\n', true);
139
166
 
140
- ---
167
+ // Clear all code
168
+ codeElement.clear();
141
169
 
142
- ## 安装
170
+ // Get current source
171
+ console.log(codeElement.source);
143
172
 
144
- 只需将 `Coax` 的 JavaScript 文件包含到您的项目中,或使用构建工具(如 Webpack 或 Rollup)进行模块化引入。
173
+ // Control auto-scroll
174
+ codeElement.autoScroll = false;
175
+ ```
145
176
 
146
- ### 直接引入(通过 `<script>` 标签)
177
+ ## 🎨 Customization / 自定义
178
+
179
+ ### CSS Custom Properties / CSS 自定义属性
180
+
181
+ ```css
182
+ /* 全局样式覆盖 */
183
+ ax-code {
184
+ /* 布局 */
185
+ --ax-code-font-size: 14px;
186
+ --ax-code-line-height: 1.6;
187
+ --ax-code-padding: 1rem;
188
+ --ax-code-radius: 8px;
189
+
190
+ /* 语言特定颜色 */
191
+ --ax-js-keyword: #d73a49;
192
+ --ax-js-string: #032f62;
193
+
194
+ /* 亮色主题 */
195
+ --ax-code-background-color: #f8f9fa;
196
+ --ax-code-color: #212529;
197
+ --ax-code-border-color: #dee2e6;
198
+
199
+ }
200
+ ax-code[scheme]{
201
+ /* 暗色主题 */
202
+ --ax-code-dark-background: #1a1d23;
203
+ --ax-code-dark-color: #e9ecef;
204
+ --ax-code-dark-border: #495057;
205
+ }
147
206
 
148
- ```html
149
- <script src="path/to/coax.js"></script>
150
207
  ```
151
208
 
152
- ---
209
+ ### Register Custom Language / 注册自定义语言
153
210
 
154
- ## 使用方法
211
+ ```javascript
212
+ // 定义新的语言规则
213
+ const pythonRules = [
214
+ {
215
+ token: 'keyword',
216
+ pattern: /\b(def|class|if|elif|else|for|while|try|except|finally|with|import|from|as)\b/,
217
+ light: '#d73a49',
218
+ dark: '#ff7b72'
219
+ },
220
+ {
221
+ token: 'string',
222
+ pattern: /(['"`])(?:\\.|[^\\'"\b])*?(?:['"`])/,
223
+ light: '#032f62',
224
+ dark: '#98c379'
225
+ },
226
+ {
227
+ token: 'comment',
228
+ pattern: /#.*/,
229
+ light: '#6a737d',
230
+ dark: '#8b949e'
231
+ },
232
+ {
233
+ token: 'decorator',
234
+ pattern: /@\w+/,
235
+ light: '#6f42c1',
236
+ dark: '#d2a8ff'
237
+ }
238
+ ];
239
+
240
+ // 注册到 Coax
241
+ Coax.register('python', {
242
+ alias: 'Python 3',
243
+ rules: pythonRules,
244
+ themeStyles: `
245
+ /* 额外样式 */
246
+ .ax-python-decorator {
247
+ font-style: italic;
248
+ }
249
+ `
250
+ });
251
+ ```
155
252
 
156
- 要使用 Coax 代码高亮组件,请按照以下步骤进行操作:
253
+ ### Create Custom Tools / 创建自定义工具
157
254
 
158
- ### 1. 定义 `<ax-code>` 元素
255
+ ```javascript
256
+ // 创建格式化工具
257
+ const formatTool = {
258
+ name: 'format',
259
+ icon: `<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor">
260
+ <polygon points="3 11 22 2 13 21 11 13 3 11"></polygon>
261
+ </svg>`,
262
+ title: 'Format Code',
263
+ action: function() {
264
+ // 使用 Prettier 或其他格式化工具
265
+ const formatted = prettier.format(this.source, {
266
+ parser: 'babel',
267
+ semi: true,
268
+ singleQuote: true
269
+ });
270
+ this.replace(formatted);
271
+ }
272
+ };
273
+
274
+ // 创建分享工具
275
+ const shareTool = {
276
+ name: 'share',
277
+ icon: `<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor">
278
+ <path d="M4 12v8a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-8"></path>
279
+ <polyline points="16 6 12 2 8 6"></polyline><line x1="12" y1="2" x2="12" y2="15"></line>
280
+ </svg>`,
281
+ title: 'Share Code',
282
+ action: function() {
283
+ // 创建分享链接
284
+ const code = encodeURIComponent(this.source);
285
+ const url = `${window.location.origin}/share?code=${code}`;
286
+ navigator.clipboard.writeText(url);
287
+ alert('Share link copied to clipboard!');
288
+ }
289
+ };
290
+
291
+ // 注册工具
292
+ Coax.addTools([formatTool, shareTool]);
293
+ ```
159
294
 
160
- ```html
161
- <ax-code lang="js">
162
- // 这里是您的 JavaScript 代码
163
- </ax-code>
295
+ ## 📝 Examples / 示例
164
296
 
165
- <ax-code lang="css">
166
- /* 这里是您的 CSS 代码 */
167
- </ax-code>
297
+ ### Interactive Demo / 交互式演示
168
298
 
169
- <ax-code lang="html">
170
- <!-- 这里是您的 HTML 代码 -->
171
- </ax-code>
299
+ ```html
300
+ <!DOCTYPE html>
301
+ <html>
302
+
303
+ <head>
304
+ <meta charset="UTF-8">
305
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
306
+ <title>Coax</title>
307
+ <style>
308
+ </style>
309
+
310
+ </head>
311
+
312
+ <body>
313
+ <ax-code streaming tools="copy" indexed lang="js">
314
+ <code>
315
+ /**
316
+ * Simple animation function
317
+ */
318
+ async function animate(element, duration) {
319
+ const start = Date.now();
320
+
321
+ if (!element) return false;
322
+
323
+ console.log("Animation started...");
324
+
325
+ while (Date.now() - start < duration) {
326
+ let progress = (Date.now() - start) / duration;
327
+ element.style.opacity = Math.min(progress, 1);
328
+ await Promise.resolve();
329
+ }
330
+
331
+ return true;
332
+ }
333
+ </code>
334
+ </ax-code>
335
+
336
+ <script src="https://unpkg.com/@codady/coax@latest/dist/coax.umd.js" type='text/javascript'></script>
337
+ </body>
338
+
339
+ </html>
172
340
  ```
173
341
 
174
- ### 2. 注册语言配置
175
-
176
- 您可以使用 `Coax.register` 方法注册语言的语法规则和主题。
342
+ ### Advanced Usage / 高级用法
177
343
 
178
344
  ```javascript
179
- Coax.register('html', {
180
- rules: [
181
- { name: 'comment', reg: /&lt;!--[\s\S]*?--&gt;/ },
182
- { name: 'tag', reg: /&lt;\/?[a-zA-Z0-9]+/ },
183
- { name: 'attr', reg: /[a-zA-Z-]+(?=\s*=\s*)/ },
184
- { name: 'string', reg: /(['"])(?:\\.|[^\\])*?\1/ },
185
- ],
186
- theme: {
187
- 'tag': '#e06c75', // 红色
188
- 'attr': '#d19a66', // 橙色
189
- 'string': '#98c379', // 绿色
190
- }
345
+ // 动态创建代码块
346
+ const createCodeBlock = (lang, code) => {
347
+ const codeBlock = document.createElement('ax-code');
348
+ codeBlock.setAttribute('lang', lang);
349
+ codeBlock.textContent = code;
350
+ return codeBlock;
351
+ };
352
+
353
+ // 添加到页面
354
+ document.body.appendChild(createCodeBlock('js', 'const x = 10;'));
355
+
356
+ // 响应式监听
357
+ const codeElement = document.querySelector('ax-code');
358
+ const observer = new MutationObserver((mutations) => {
359
+ mutations.forEach((mutation) => {
360
+ if (mutation.attributeName === 'lang') {
361
+ console.log('Language changed to:', codeElement.lang);
362
+ }
363
+ });
191
364
  });
192
- ```
193
365
 
194
- ### 3. 自定义设置
366
+ observer.observe(codeElement, { attributes: true });
195
367
 
196
- 您可以通过修改语言的规则、主题颜色或内部 CSS 来定制代码高亮,具体方法如下:
197
-
198
- ```javascript
199
- Coax.register('css', {
200
- rules: [
201
- { name: 'comment', reg: /\/\*[\s\S]*?\*\// },
202
- { name: 'property', reg: /[a-z-]+(?=\s*:)/ },
203
- { name: 'selector', reg: /[.#a-z0-9, \n\t>:+()_-]+(?=\s*\{)/i },
204
- { name: 'unit', reg: /(?<=\d)(px|em|rem|%|vh|vw|ms|s|deg)/ },
205
- ],
206
- theme: {
207
- 'keyword': '#61afef', // 蓝色
208
- 'string': '#98c379', // 绿色
209
- 'op': '#abb2bf', // 灰色
210
- },
211
- internalCss: `
212
- .ax-css-string { color: var(--ax-code-string); }
213
- `
368
+ // 键盘快捷键
369
+ document.addEventListener('keydown', (e) => {
370
+ if (e.ctrlKey && e.key === 'c' && document.activeElement === codeElement) {
371
+ e.preventDefault();
372
+ navigator.clipboard.writeText(codeElement.source);
373
+ }
214
374
  });
215
375
  ```
216
376
 
217
- ### 4. 可选属性
218
377
 
219
- 您可以通过 `height` `max-height` 属性自定义代码块的高度和最大高度:
378
+ ## 📄 License / 许可证
220
379
 
221
- ```html
222
- <ax-code lang="js" height="200px" max-height="400px">
223
- const x = 10;
224
- </ax-code>
225
- ```
380
+ MIT License. See LICENSE file for details.
226
381
 
227
- ---
382
+ This software supports the MIT License, allowing free learning and commercial use, but please retain the terms 'coax', 'Coax' and 'COAX' within the software.
228
383
 
229
- ## 许可证
384
+ MIT 许可证。详见 LICENSE 文件。
230
385
 
231
- MIT 许可证。可以自由使用和修改代码以适应您的项目需求。
386
+ 本软件支持 MIT 许可证,允许自由学习和商业使用,但请保留软件中的 'coax'、'Coax' 和 'COAX' 字样。
232
387
 
233
- ```
388
+
389
+ ### Contact / 联系
390
+
391
+ - **Email**: 3217728223@qq.com
392
+ - **Author**: AXUI development team
234
393
 
235
394
  ---
236
395
 
396
+ **Made with ❤️ by the AXUI Development Team**
397
+
398
+ **由 AXUI 开发团队用心打造**
399
+
400
+ > "Elegant code deserves elegant display."
401
+ > "优雅的代码值得优雅的展示。"