c063 1.4.2 → 1.4.4

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/README.md CHANGED
@@ -1 +1,121 @@
1
- # c063
1
+ <div align="center">
2
+ <h1>c063</h1>
3
+
4
+ <div style="display: flex; justify-content: center">
5
+ <a href="https://www.npmjs.com/package/c063"><img alt="npm version" src="https://img.shields.io/npm/v/c063"></a>
6
+ <a href="./LICENSE"><img alt="license" src="https://img.shields.io/npm/l/c063.svg"></a></div>
7
+ </div>
8
+
9
+ ---
10
+
11
+ A highly customizable React component library for displaying syntax-highlighted code snippets. Supports multiple languages and themes, making it ideal for documentation, blogs, or educational platforms.
12
+
13
+ ## ✨ Project Overview
14
+
15
+ **c063** is a syntax highlighting component built with React and TypeScript. It offers flexible theming and a modular architecture that makes it easy for developers to embed code blocks into their applications.
16
+
17
+ ## ⚡️ Features
18
+
19
+ - ✏️ Display code snippets with syntax highlighting.
20
+ - 🌟 Multiple theme support: GitHub, Visual Studio, Light/Dark, etc.
21
+ - 🔄 Modular design for easy integration and customization.
22
+ - 📖 Perfect for tutorials, blog posts, and educational content.
23
+
24
+ ## 🚀 Installation & Usage
25
+
26
+ ### Installation
27
+
28
+ ```bash
29
+ npm install c063
30
+ ```
31
+
32
+ ### Usage Example
33
+
34
+ ```tsx
35
+ import { CodeBlock } from "c063";
36
+
37
+ const tokens = [
38
+ [
39
+ { type: "keyword1", children: "const" },
40
+ { type: "default", children: " " },
41
+ { type: "variable", children: "x" },
42
+ { type: "default", children: " = " },
43
+ { type: "number", children: "42" },
44
+ ],
45
+ ];
46
+
47
+ <CodeBlock tokenLines={tokens} theme="github-light" />;
48
+ ```
49
+
50
+ ## 📂 Project Structure
51
+
52
+ ```txt
53
+ src
54
+ ├── components/ # Component files
55
+ │ ├── CodeBlock.tsx # Code block container
56
+ │ ├── CodeLine.tsx # Single line component
57
+ │ ├── CodeToken.tsx # Individual token component
58
+ ├── libs/ # Theme and configuration
59
+ │ └── themes/ # Color themes
60
+ ├── types/ # Type definitions
61
+ ├── utils/ # Utility functions and parsers
62
+ └── index.ts # Module export entry
63
+ ```
64
+
65
+ ## 🔍 API / Props Reference
66
+
67
+ ### `CodeBlock<T>`
68
+
69
+ ```ts
70
+ interface CodeBlockProps<T extends React.ElementType> {
71
+ tokenLines: CodeTokenProps<T>[][];
72
+ showLineNumbers?: boolean; // Default true
73
+ lineNumberStyle?: React.CSSProperties;
74
+ theme?: CodeTheme; // e.g. "github-dark"
75
+ }
76
+ ```
77
+
78
+ ### `CodeLine<T>`
79
+
80
+ ```ts
81
+ interface CodeLineProps<T extends React.ElementType> {
82
+ tokens: CodeTokenProps<T>[];
83
+ theme?: CodeTheme;
84
+ }
85
+ ```
86
+
87
+ ### `CodeToken<T>`
88
+
89
+ ```ts
90
+ interface CodeTokenProps<T extends React.ElementType> {
91
+ type?: CodeTokenType;
92
+ theme?: CodeTheme;
93
+ children: React.ReactNode;
94
+ as?: T; // Custom rendering tag
95
+ }
96
+ ```
97
+
98
+ ### Utilities
99
+
100
+ - `c063.<type>()`: Quickly create tokens, e.g., `c063.keyword1("const")`.
101
+ - `whiteSpace(count)`: Insert a specific number of whitespace tokens.
102
+
103
+ ## ✍️ Contributing
104
+
105
+ All contributions are welcome, including but not limited to:
106
+
107
+ - Bug fixes or performance improvements
108
+ - New theme submissions
109
+ - Support for more programming languages
110
+
111
+ Before submitting a PR, please fork the repository and create a new branch for your changes.
112
+
113
+ ## 📄 License
114
+
115
+ This project is licensed under the [MIT](./LICENSE) license.
116
+
117
+ ---
118
+
119
+ Repository:
120
+
121
+ [https://github.com/fanyuuu2006/c063](https://github.com/fanyuuu2006/c063)
@@ -9,6 +9,6 @@ import { CodeLineProps } from "../types/index";
9
9
  * @returns JSX 元素,呈現語法 token 的單行程式碼
10
10
  */
11
11
  export declare const CodeLine: {
12
- <T extends React.ElementType>({ style, tokens, theme, ...rest }: CodeLineProps<T>): import("react/jsx-runtime").JSX.Element;
12
+ <T extends React.ElementType = "span">({ style, tokens, theme, ...rest }: CodeLineProps<T>): import("react/jsx-runtime").JSX.Element;
13
13
  displayName: string;
14
14
  };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- export * from './types';
2
- export * from './components';
3
- import c063, { whiteSpace } from './utils';
1
+ export * from "./types";
2
+ export * from "./components";
3
+ import c063, { whiteSpace, extractTokenContent } from "./utils";
4
4
  export default c063;
5
- export { whiteSpace };
5
+ export { whiteSpace, extractTokenContent };
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- export * from './types';
2
- export * from './components';
3
- import c063, { whiteSpace } from './utils';
1
+ export * from "./types";
2
+ export * from "./components";
3
+ import c063, { whiteSpace, extractTokenContent } from "./utils";
4
4
  export default c063;
5
- export { whiteSpace };
5
+ export { whiteSpace, extractTokenContent };
@@ -1,3 +1,4 @@
1
+ import React from "react";
1
2
  import { CodeTokenBuilder, CodeTokenProps, CodeTokenType } from "../types";
2
3
  /**
3
4
  * 語法 token 的建構器集合,每個 key 對應一種語法類型(如 `keyword-blue`, `string`, `comment` 等),
@@ -26,4 +27,5 @@ declare const c063: Record<CodeTokenType, CodeTokenBuilder>;
26
27
  * tokens.push(whiteSpace(2)); // -> { type: "default", children: " " }
27
28
  */
28
29
  export declare const whiteSpace: (count?: number) => CodeTokenProps<"span">;
30
+ export declare const extractTokenContent: <T extends React.ElementType>(token: CodeTokenProps<T>) => string;
29
31
  export default c063;
@@ -1,3 +1,4 @@
1
+ import React from "react";
1
2
  /**
2
3
  * 語法 token 的建構器集合,每個 key 對應一種語法類型(如 `keyword-blue`, `string`, `comment` 等),
3
4
  * 透過 Proxy 生成對應的 `CodeTokenBuilder`。
@@ -36,4 +37,19 @@ const c063 = new Proxy({}, {
36
37
  * tokens.push(whiteSpace(2)); // -> { type: "default", children: " " }
37
38
  */
38
39
  export const whiteSpace = (count = 1) => c063.default(" ".repeat(count));
40
+ export const extractTokenContent = (token) => {
41
+ const _extract = (children) => {
42
+ if (typeof children === "string")
43
+ return children;
44
+ if (typeof children === "number")
45
+ return children.toString();
46
+ if (Array.isArray(children))
47
+ return children.map(_extract).join("");
48
+ if (React.isValidElement(children)) {
49
+ return _extract(children.props.children);
50
+ }
51
+ return ""; // 如果 children 是 null 或 undefined,則返回空字串
52
+ };
53
+ return _extract(token.children);
54
+ };
39
55
  export default c063;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "c063",
4
- "version": "1.4.2",
4
+ "version": "1.4.4",
5
5
  "description": "A React component for displaying code snippets with syntax highlighting.",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -9,11 +9,11 @@
9
9
  "build": "tsc"
10
10
  },
11
11
  "keywords": [
12
+ "c063",
13
+ "code",
12
14
  "React",
13
15
  "Component",
14
- "Code",
15
16
  "Language",
16
- "Code.js",
17
17
  "Fanyu",
18
18
  "Syntax Highlighting",
19
19
  "Code Display",
@@ -31,7 +31,7 @@
31
31
  ],
32
32
  "repository": {
33
33
  "type": "git",
34
- "url": "git+https://github.com/fanyuuu2006/fanyu-components.git"
34
+ "url": "git+https://github.com/fanyuuu2006/c063.git"
35
35
  },
36
36
  "author": "",
37
37
  "license": "MIT",
@@ -9,7 +9,7 @@ import { CodeToken } from "./CodeToken";
9
9
  * @param rest 其他 HTMLAttributes
10
10
  * @returns JSX 元素,呈現語法 token 的單行程式碼
11
11
  */
12
- export const CodeLine = <T extends React.ElementType>({
12
+ export const CodeLine = <T extends React.ElementType = "span">({
13
13
  style,
14
14
  tokens,
15
15
  theme,
package/src/index.ts CHANGED
@@ -1,8 +1,6 @@
1
- export * from './types';
2
- export * from './components';
1
+ export * from "./types";
2
+ export * from "./components";
3
3
 
4
- import c063, { whiteSpace }from './utils';
4
+ import c063, { whiteSpace, extractTokenContent } from "./utils";
5
5
  export default c063;
6
- export {
7
- whiteSpace
8
- }
6
+ export { whiteSpace, extractTokenContent };
@@ -1,3 +1,4 @@
1
+ import React from "react";
1
2
  import { CodeTokenBuilder, CodeTokenProps, CodeTokenType } from "../types";
2
3
  /**
3
4
  * 語法 token 的建構器集合,每個 key 對應一種語法類型(如 `keyword-blue`, `string`, `comment` 等),
@@ -46,4 +47,19 @@ const c063 = new Proxy(
46
47
  export const whiteSpace = (count: number = 1): CodeTokenProps<"span"> =>
47
48
  c063.default(" ".repeat(count));
48
49
 
50
+ export const extractTokenContent = <T extends React.ElementType>(
51
+ token: CodeTokenProps<T>
52
+ ): string => {
53
+ const _extract = (children: React.ReactNode): string => {
54
+ if (typeof children === "string") return children;
55
+ if (typeof children === "number") return children.toString();
56
+ if (Array.isArray(children)) return children.map(_extract).join("");
57
+ if (React.isValidElement(children)) {
58
+ return _extract((children as React.JSX.Element).props.children);
59
+ }
60
+ return ""; // 如果 children 是 null 或 undefined,則返回空字串
61
+ };
62
+ return _extract(token.children);
63
+ };
64
+
49
65
  export default c063;