c063 1.4.1 → 1.4.3
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 +121 -1
- package/dist/components/CodeBlock.js +3 -1
- package/dist/components/CodeLine.d.ts +1 -1
- package/dist/components/CodeToken.js +3 -12
- package/dist/libs/index.d.ts +18 -1
- package/dist/libs/index.js +30 -14
- package/package.json +4 -4
- package/src/components/CodeBlock.tsx +1 -0
- package/src/components/CodeLine.tsx +1 -1
- package/src/components/CodeToken.tsx +3 -15
- package/src/libs/index.tsx +39 -14
- package/src/utils/theme.tsx +0 -8
package/README.md
CHANGED
|
@@ -1 +1,121 @@
|
|
|
1
|
-
|
|
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)
|
|
@@ -11,7 +11,9 @@ import { CodeLine } from "./CodeLine";
|
|
|
11
11
|
* @returns JSX 元素,呈現語法高亮的程式碼區塊
|
|
12
12
|
*/
|
|
13
13
|
export const CodeBlock = ({ tokenLines, showLineNumbers = true, lineNumberStyle, theme, ...rest }) => {
|
|
14
|
-
return (_jsx("pre", { ...rest, children: tokenLines.map((line, index) => (
|
|
14
|
+
return (_jsx("pre", { ...rest, children: tokenLines.map((line, index) => (
|
|
15
|
+
// eslint-disable-next-line react/react-in-jsx-scope
|
|
16
|
+
_jsxs("div", { style: {
|
|
15
17
|
display: "flex",
|
|
16
18
|
flexWrap: "nowrap",
|
|
17
19
|
width: "100%",
|
|
@@ -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
|
};
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import {
|
|
3
|
-
import { loadTheme } from "../utils/theme";
|
|
2
|
+
import { themeMap } from "../libs/index";
|
|
4
3
|
/**
|
|
5
4
|
* 渲染單一語法 token(例如關鍵字、字串、註解等),可指定標籤與樣式。
|
|
6
5
|
*
|
|
@@ -14,16 +13,8 @@ import { loadTheme } from "../utils/theme";
|
|
|
14
13
|
*/
|
|
15
14
|
export const CodeToken = ({ as, style, children, type, theme, ...rest }) => {
|
|
16
15
|
const Tag = as || "span";
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
if (theme) {
|
|
20
|
-
loadTheme(theme).then((res) => setCurrTheme(res));
|
|
21
|
-
}
|
|
22
|
-
}, [theme]);
|
|
23
|
-
return (
|
|
24
|
-
// eslint-disable-next-line react/react-in-jsx-scope
|
|
25
|
-
_jsx(Tag, { ...rest, style: {
|
|
26
|
-
color: (currTheme === null || currTheme === void 0 ? void 0 : currTheme[type || "default"]) || undefined,
|
|
16
|
+
return (_jsx(Tag, { ...rest, style: {
|
|
17
|
+
color: themeMap[theme || "default-dark-modern"][type || "default"],
|
|
27
18
|
...style,
|
|
28
19
|
}, children: children }));
|
|
29
20
|
};
|
package/dist/libs/index.d.ts
CHANGED
|
@@ -1,2 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
import type { CodeTokenType, CodeTheme } from "../types";
|
|
2
|
+
declare const _themeRegistry: {
|
|
3
|
+
readonly "default-dark-modern": Record<CodeTokenType, import("csstype").Property.Color | undefined>;
|
|
4
|
+
readonly "default-dark": Record<CodeTokenType, import("csstype").Property.Color | undefined>;
|
|
5
|
+
readonly "default-dark-plus": Record<CodeTokenType, import("csstype").Property.Color | undefined>;
|
|
6
|
+
readonly "visual-studio-light": Record<CodeTokenType, import("csstype").Property.Color | undefined>;
|
|
7
|
+
readonly "default-light-plus": Record<CodeTokenType, import("csstype").Property.Color | undefined>;
|
|
8
|
+
readonly "default-light-modern": Record<CodeTokenType, import("csstype").Property.Color | undefined>;
|
|
9
|
+
readonly "github-light": Record<CodeTokenType, import("csstype").Property.Color | undefined>;
|
|
10
|
+
readonly "github-light-default": Record<CodeTokenType, import("csstype").Property.Color | undefined>;
|
|
11
|
+
readonly "github-light-colorblind": Record<CodeTokenType, import("csstype").Property.Color | undefined>;
|
|
12
|
+
readonly "github-dark": Record<CodeTokenType, import("csstype").Property.Color | undefined>;
|
|
13
|
+
readonly "github-dark-default": Record<CodeTokenType, import("csstype").Property.Color | undefined>;
|
|
14
|
+
readonly "github-dark-colorblind": Record<CodeTokenType, import("csstype").Property.Color | undefined>;
|
|
15
|
+
};
|
|
16
|
+
export declare const themes: (keyof typeof _themeRegistry)[];
|
|
17
|
+
export declare const themeMap: Record<CodeTheme, Record<CodeTokenType, React.CSSProperties["color"]>>;
|
|
2
18
|
export declare const parsableLanguages: readonly ["javascript"];
|
|
19
|
+
export {};
|
package/dist/libs/index.js
CHANGED
|
@@ -1,15 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
import { map as darkModern } from "./themes/default-dark-modern";
|
|
2
|
+
import { map as dark } from "./themes/default-dark";
|
|
3
|
+
import { map as darkPlus } from "./themes/default-dark-plus";
|
|
4
|
+
import { map as vsLight } from "./themes/visual-studio-light";
|
|
5
|
+
import { map as lightPlus } from "./themes/default-light-plus";
|
|
6
|
+
import { map as lightModern } from "./themes/default-light-modern";
|
|
7
|
+
import { map as ghLight } from "./themes/github-light";
|
|
8
|
+
import { map as ghLightDefault } from "./themes/github-light-default";
|
|
9
|
+
import { map as ghLightBilnd } from "./themes/github-light-colorblind";
|
|
10
|
+
import { map as ghDark } from "./themes/github-dark";
|
|
11
|
+
import { map as ghDarkDefault } from "./themes/github-dark-default";
|
|
12
|
+
import { map as ghDarkBilnd } from "./themes/github-dark-colorblind";
|
|
13
|
+
const _themeRegistry = {
|
|
14
|
+
"default-dark-modern": darkModern,
|
|
15
|
+
"default-dark": dark,
|
|
16
|
+
"default-dark-plus": darkPlus,
|
|
17
|
+
"visual-studio-light": vsLight,
|
|
18
|
+
"default-light-plus": lightPlus,
|
|
19
|
+
"default-light-modern": lightModern,
|
|
20
|
+
"github-light": ghLight,
|
|
21
|
+
"github-light-default": ghLightDefault,
|
|
22
|
+
"github-light-colorblind": ghLightBilnd,
|
|
23
|
+
"github-dark": ghDark,
|
|
24
|
+
"github-dark-default": ghDarkDefault,
|
|
25
|
+
"github-dark-colorblind": ghDarkBilnd,
|
|
26
|
+
};
|
|
27
|
+
// 自動推導主題名稱
|
|
28
|
+
export const themes = Object.keys(_themeRegistry);
|
|
29
|
+
// themeMap 保留給外部使用
|
|
30
|
+
export const themeMap = _themeRegistry;
|
|
15
31
|
export const parsableLanguages = ["javascript"];
|
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.
|
|
4
|
+
"version": "1.4.3",
|
|
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/
|
|
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,
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { CodeTokenProps
|
|
3
|
-
import { loadTheme } from "../utils/theme";
|
|
1
|
+
import { themeMap } from "../libs/index";
|
|
2
|
+
import { CodeTokenProps } from "../types/index";
|
|
4
3
|
|
|
5
4
|
/**
|
|
6
5
|
* 渲染單一語法 token(例如關鍵字、字串、註解等),可指定標籤與樣式。
|
|
@@ -22,23 +21,12 @@ export const CodeToken = <T extends React.ElementType = "span">({
|
|
|
22
21
|
...rest
|
|
23
22
|
}: CodeTokenProps<T>) => {
|
|
24
23
|
const Tag = as || "span";
|
|
25
|
-
const [currTheme, setCurrTheme] = useState<Record<
|
|
26
|
-
CodeTokenType,
|
|
27
|
-
React.CSSProperties["color"]
|
|
28
|
-
> | null>(null);
|
|
29
|
-
|
|
30
|
-
useLayoutEffect(() => {
|
|
31
|
-
if (theme) {
|
|
32
|
-
loadTheme(theme).then((res) => setCurrTheme(res));
|
|
33
|
-
}
|
|
34
|
-
}, [theme]);
|
|
35
24
|
|
|
36
25
|
return (
|
|
37
|
-
// eslint-disable-next-line react/react-in-jsx-scope
|
|
38
26
|
<Tag
|
|
39
27
|
{...rest}
|
|
40
28
|
style={{
|
|
41
|
-
color:
|
|
29
|
+
color: themeMap[theme || "default-dark-modern"][type || "default"],
|
|
42
30
|
...style,
|
|
43
31
|
}}
|
|
44
32
|
>
|
package/src/libs/index.tsx
CHANGED
|
@@ -1,16 +1,41 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
import type { CodeTokenType, CodeTheme } from "../types";
|
|
2
|
+
import { map as darkModern } from "./themes/default-dark-modern";
|
|
3
|
+
import { map as dark } from "./themes/default-dark";
|
|
4
|
+
import { map as darkPlus } from "./themes/default-dark-plus";
|
|
5
|
+
import { map as vsLight } from "./themes/visual-studio-light";
|
|
6
|
+
import { map as lightPlus } from "./themes/default-light-plus";
|
|
7
|
+
import { map as lightModern } from "./themes/default-light-modern";
|
|
8
|
+
import { map as ghLight } from "./themes/github-light";
|
|
9
|
+
import { map as ghLightDefault } from "./themes/github-light-default";
|
|
10
|
+
import { map as ghLightBilnd } from "./themes/github-light-colorblind";
|
|
11
|
+
import { map as ghDark } from "./themes/github-dark";
|
|
12
|
+
import { map as ghDarkDefault } from "./themes/github-dark-default";
|
|
13
|
+
import { map as ghDarkBilnd } from "./themes/github-dark-colorblind";
|
|
14
|
+
|
|
15
|
+
const _themeRegistry = {
|
|
16
|
+
"default-dark-modern": darkModern,
|
|
17
|
+
"default-dark": dark,
|
|
18
|
+
"default-dark-plus": darkPlus,
|
|
19
|
+
"visual-studio-light": vsLight,
|
|
20
|
+
"default-light-plus": lightPlus,
|
|
21
|
+
"default-light-modern": lightModern,
|
|
22
|
+
"github-light": ghLight,
|
|
23
|
+
"github-light-default": ghLightDefault,
|
|
24
|
+
"github-light-colorblind": ghLightBilnd,
|
|
25
|
+
"github-dark": ghDark,
|
|
26
|
+
"github-dark-default": ghDarkDefault,
|
|
27
|
+
"github-dark-colorblind": ghDarkBilnd,
|
|
28
|
+
} as const;
|
|
29
|
+
|
|
30
|
+
// 自動推導主題名稱
|
|
31
|
+
export const themes = Object.keys(
|
|
32
|
+
_themeRegistry
|
|
33
|
+
) as (keyof typeof _themeRegistry)[];
|
|
34
|
+
|
|
35
|
+
// themeMap 保留給外部使用
|
|
36
|
+
export const themeMap: Record<
|
|
37
|
+
CodeTheme,
|
|
38
|
+
Record<CodeTokenType, React.CSSProperties["color"]>
|
|
39
|
+
> = _themeRegistry;
|
|
15
40
|
|
|
16
41
|
export const parsableLanguages = ["javascript"] as const;
|
package/src/utils/theme.tsx
DELETED