c063 1.1.7 → 1.3.0

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 @@
1
- # Code.js
1
+ # c063
@@ -1,5 +1,15 @@
1
1
  import { CodeBlockProps } from "../types/props";
2
+ /**
3
+ * 顯示完整程式碼區塊,支援多行語法 token 與行號顯示。
4
+ *
5
+ * @template T 元件渲染類型,預設為 <span>
6
+ * @param props.tokenLines 所有程式碼行的 token 陣列
7
+ * @param props.showLineNumbers 是否顯示行號,預設為 true
8
+ * @param props.lineNumberStyle 行號的自訂樣式
9
+ * @param rest 其他傳遞給 <pre> 的屬性
10
+ * @returns JSX 元素,呈現語法高亮的程式碼區塊
11
+ */
2
12
  export declare const CodeBlock: {
3
- ({ tokenLines, showLineNumbers, lineNumberStyle, ...rest }: CodeBlockProps): import("react/jsx-runtime").JSX.Element;
13
+ <T extends React.ElementType = "span">({ tokenLines, showLineNumbers, lineNumberStyle, theme, ...rest }: CodeBlockProps<T>): import("react/jsx-runtime").JSX.Element;
4
14
  displayName: string;
5
15
  };
@@ -1,11 +1,21 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { CodeLine } from "./CodeLine";
3
- export const CodeBlock = ({ tokenLines, showLineNumbers = true, lineNumberStyle, ...rest }) => {
3
+ /**
4
+ * 顯示完整程式碼區塊,支援多行語法 token 與行號顯示。
5
+ *
6
+ * @template T 元件渲染類型,預設為 <span>
7
+ * @param props.tokenLines 所有程式碼行的 token 陣列
8
+ * @param props.showLineNumbers 是否顯示行號,預設為 true
9
+ * @param props.lineNumberStyle 行號的自訂樣式
10
+ * @param rest 其他傳遞給 <pre> 的屬性
11
+ * @returns JSX 元素,呈現語法高亮的程式碼區塊
12
+ */
13
+ export const CodeBlock = ({ tokenLines, showLineNumbers = true, lineNumberStyle, theme, ...rest }) => {
4
14
  return (_jsx("pre", { ...rest, children: tokenLines.map((line, index) => (_jsxs("div", { style: {
5
15
  display: "flex",
6
16
  flexWrap: "nowrap",
7
17
  width: "100%",
8
18
  gap: "0.5rem",
9
- }, children: [showLineNumbers && (_jsx("span", { style: { color: "#888", userSelect: "none", ...lineNumberStyle }, children: index + 1 })), _jsx(CodeLine, { tokens: line })] }, index))) }));
19
+ }, children: [showLineNumbers && (_jsx("span", { style: { color: "#888", userSelect: "none", ...lineNumberStyle }, children: index + 1 })), _jsx(CodeLine, { theme: theme, tokens: line })] }, index))) }));
10
20
  };
11
21
  CodeBlock.displayName = "CodeBlock";
@@ -1,5 +1,14 @@
1
1
  import { CodeLineProps } from "../types/props";
2
+ /**
3
+ * 渲染單一程式碼行,包含多個語法 token。
4
+ *
5
+ * @template T 元件渲染類型,例如 <code>、<span> 等
6
+ * @param props.tokens 該行所包含的語法 token 陣列
7
+ * @param props.style 自訂樣式,會與 whiteSpace: pre-wrap 合併
8
+ * @param rest 其他 HTMLAttributes
9
+ * @returns JSX 元素,呈現語法 token 的單行程式碼
10
+ */
2
11
  export declare const CodeLine: {
3
- ({ style, tokens, ...rest }: CodeLineProps): import("react/jsx-runtime").JSX.Element;
12
+ <T extends React.ElementType>({ style, tokens, theme, ...rest }: CodeLineProps<T>): import("react/jsx-runtime").JSX.Element;
4
13
  displayName: string;
5
14
  };
@@ -1,9 +1,18 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { CodeToken } from "./CodeToken";
3
- export const CodeLine = ({ style, tokens, ...rest }) => {
3
+ /**
4
+ * 渲染單一程式碼行,包含多個語法 token。
5
+ *
6
+ * @template T 元件渲染類型,例如 <code>、<span> 等
7
+ * @param props.tokens 該行所包含的語法 token 陣列
8
+ * @param props.style 自訂樣式,會與 whiteSpace: pre-wrap 合併
9
+ * @param rest 其他 HTMLAttributes
10
+ * @returns JSX 元素,呈現語法 token 的單行程式碼
11
+ */
12
+ export const CodeLine = ({ style, tokens, theme, ...rest }) => {
4
13
  return (_jsx("code", { ...rest, style: {
5
14
  whiteSpace: "pre-wrap",
6
15
  ...style,
7
- }, children: tokens.map((token, index) => (_jsx(CodeToken, { ...token }, `${token.type}-${index}`))) }));
16
+ }, children: tokens.map((token, index) => (_jsx(CodeToken, { theme: theme, ...token }, `${token.type}-${index}`))) }));
8
17
  };
9
18
  CodeLine.displayName = "CodeLine";
@@ -1,5 +1,16 @@
1
1
  import { CodeTokenProps } from "../types/props";
2
+ /**
3
+ * 渲染單一語法 token(例如關鍵字、字串、註解等),可指定標籤與樣式。
4
+ *
5
+ * @template T 元件渲染類型,預設為 <span>
6
+ * @param props.as 指定要渲染的 HTML 標籤或客製元件
7
+ * @param props.type 語法類型,用於對應不同顏色
8
+ * @param props.style 額外樣式,會與語法顏色合併
9
+ * @param props.children 顯示的程式碼字串
10
+ * @param rest 其他 HTML 屬性
11
+ * @returns JSX 元素,顯示帶有語法顏色的 token
12
+ */
2
13
  export declare const CodeToken: {
3
- <T extends React.ElementType = "span">({ as, style, children, type, ...rest }: CodeTokenProps<T>): import("react/jsx-runtime").JSX.Element;
14
+ <T extends React.ElementType = "span">({ as, style, children, type, theme, ...rest }: CodeTokenProps<T>): import("react/jsx-runtime").JSX.Element;
4
15
  displayName: string;
5
16
  };
@@ -1,9 +1,20 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
- import { codeColoreMap } from "../libs/codeColorMap";
3
- export const CodeToken = ({ as, style, children, type, ...rest }) => {
2
+ import { themeMap } from "../libs";
3
+ /**
4
+ * 渲染單一語法 token(例如關鍵字、字串、註解等),可指定標籤與樣式。
5
+ *
6
+ * @template T 元件渲染類型,預設為 <span>
7
+ * @param props.as 指定要渲染的 HTML 標籤或客製元件
8
+ * @param props.type 語法類型,用於對應不同顏色
9
+ * @param props.style 額外樣式,會與語法顏色合併
10
+ * @param props.children 顯示的程式碼字串
11
+ * @param rest 其他 HTML 屬性
12
+ * @returns JSX 元素,顯示帶有語法顏色的 token
13
+ */
14
+ export const CodeToken = ({ as, style, children, type, theme, ...rest }) => {
4
15
  const Tag = as || "span";
5
16
  return (_jsx(Tag, { ...rest, style: {
6
- color: codeColoreMap[type || "default"],
17
+ color: themeMap[theme || "default-dark-modern"][type || "default"],
7
18
  ...style,
8
19
  }, children: children }));
9
20
  };
@@ -0,0 +1,2 @@
1
+ import { CodeTokenType } from "../types";
2
+ export declare const map: Record<CodeTokenType, React.CSSProperties["color"]>;
@@ -0,0 +1,15 @@
1
+ export const map = {
2
+ keyword1: "#569cd6", // 深藍色關鍵字
3
+ keyword2: "#c586c0", // 紫色關鍵字
4
+ string: "#ce9178", // 淺棕色字串
5
+ number: "#b5cea8", // 淺綠色數字
6
+ comment: "#6a9955", // 綠色註解
7
+ variable: "#9cdcfe", // 藍色變數
8
+ constant: "#4fc1ff", // 藍色常數
9
+ type: "#4ec9b0", // 青綠色類型
10
+ brackets1: "#ffd700", // 金黃色括號層級1
11
+ brackets2: "#da70d6", // 紫羅蘭色括號層級2
12
+ brackets3: "#179fff", // 藍色括號層級3
13
+ operator: "#d4d4d4", // 淺灰色運算符
14
+ default: "#d4d4d4", // 淺灰色預設文字
15
+ };
@@ -1 +1,7 @@
1
- export { codeColoreMap } from "./codeColorMap";
1
+ import type { CodeTokenType, CodeTheme } from "../types";
2
+ export declare const themeRegistry: {
3
+ readonly "default-dark-modern": Record<CodeTokenType, import("csstype").Property.Color | undefined>;
4
+ readonly "visual-studio-light": Record<CodeTokenType, import("csstype").Property.Color | undefined>;
5
+ };
6
+ export declare const themes: (keyof typeof themeRegistry)[];
7
+ export declare const themeMap: Record<CodeTheme, Record<CodeTokenType, React.CSSProperties["color"]>>;
@@ -1 +1,10 @@
1
- export { codeColoreMap } from "./codeColorMap";
1
+ import { map as darkModern } from "./default-dark-modern";
2
+ import { map as vsLight } from "./visual-studio-light";
3
+ export const themeRegistry = {
4
+ "default-dark-modern": darkModern,
5
+ "visual-studio-light": vsLight,
6
+ };
7
+ // 自動推導主題名稱
8
+ export const themes = Object.keys(themeRegistry);
9
+ // themeMap 保留給外部使用
10
+ export const themeMap = themeRegistry;
@@ -0,0 +1,2 @@
1
+ import { CodeTokenType } from "../types";
2
+ export declare const map: Record<CodeTokenType, React.CSSProperties["color"]>;
@@ -0,0 +1,15 @@
1
+ export const map = {
2
+ keyword1: "#0000ff",
3
+ keyword2: "#0000ff",
4
+ string: "#000000",
5
+ number: "#098658",
6
+ comment: "#008000",
7
+ variable: "#000000",
8
+ constant: "#000000",
9
+ type: "#000000",
10
+ brackets1: "#0431fa",
11
+ brackets2: "#319331",
12
+ brackets3: "#7b3814",
13
+ operator: "#000000",
14
+ default: "#000000",
15
+ };
@@ -3,7 +3,7 @@ export type DistributiveOmit<T, K extends keyof any> = T extends any ? Omit<T, K
3
3
  /** 用於重載同名屬性(用於覆蓋類型 T 中與類型 P 相同名稱的屬性,並保留其餘屬性*/
4
4
  export type OverrideProps<T, P> = DistributiveOmit<T, keyof P> & P;
5
5
  /** 支援元件型別 (可複寫屬性)*/
6
- export type AsComponentProps<As extends React.ElementType, PermanentProps extends object = {}> = OverrideProps<React.ComponentPropsWithRef<As>, PermanentProps> & {
6
+ export type AsComponentProps<T extends React.ElementType, P = {}> = OverrideProps<React.ComponentPropsWithRef<T>, P> & {
7
7
  /** 指定用於渲染的 React 元件(可為任意元件*/
8
- as?: As;
8
+ as?: T;
9
9
  };
@@ -1,22 +1,58 @@
1
- import { AsComponentProps, DistributiveOmit, OverrideProps } from "./common";
2
- export type KeywordColorType = "blue" | "purple";
3
- export type CodeTokenType = `keyword-${KeywordColorType}` | "string" | "number" | "comment" | "type" | "variable" | "constant" | `brackets-${1 | 2 | 3}` | "operator" | "default";
1
+ import { themes } from "../libs";
2
+ import { AsComponentProps, OverrideProps } from "./common";
3
+ /**
4
+ * 用於表示語法高亮中每個 token 的語意分類,對應於 `<CodeToken />` 中的 `type`。
5
+ *
6
+ * 每個類型會對應特定的顏色與用途,例如關鍵字、數字、字串、註解等,
7
+ * 可配合 `codeColoreMap` 指定顯示樣式。s
8
+ *
9
+ * 類型分為以下幾大類:
10
+ *
11
+ * - `keyword1` / `keyword2`: 關鍵字,如 `const`、`return`、`import` 等,分顏色類別。
12
+ * - `string`: 字串常值,如 `'text'`、`"value"`。
13
+ * - `number`: 數字常值,如 `123`、`3.14`。
14
+ * - `comment`: 註解內容,如 `//`。
15
+ * - `type`: 類型定義,如 `interface`、`enum`、`type`。
16
+ * - `variable`: 識別符號,如變數名、函式名、類別名。
17
+ * - `constant`: 常數或靜態值,如 `PI`、`MAX_VALUE`。
18
+ * - `brackets1`, `brackets2`, `brackets3`: 括號配對,區分不同層級的括號。
19
+ * - `operator`: 運算符,如 `=`, `+`, `===`, `<`, `>=`。
20
+ * - `default`: 其他符號,如 `;`, `,`, `.`, `?`, `"`, `'`。
21
+ *
22
+ * @example
23
+ * const token: CodeTokenType = "keyword1";
24
+ * const token2: CodeTokenType = "string";
25
+ */
26
+ export type CodeTokenType = `keyword${1 | 2}` | "string" | "number" | "comment" | "type" | "variable" | "constant" | `brackets${1 | 2 | 3}` | "operator" | "default";
27
+ /**
28
+ * 表示可用的語法高亮主題名稱。
29
+ * 對應 `themes` 陣列中定義的名稱,例如 `"vscode-dark"`。
30
+ *
31
+ * @example
32
+ * const theme: CodeTheme = "vscode-dark";
33
+ */
34
+ export type CodeTheme = (typeof themes)[number];
4
35
  /**
5
36
  * 單一語法 token 的屬性,用於 <CodeToken /> 元件。
6
37
  *
7
38
  * @template T HTML 或客製元素,例如 span、a、Link 等
8
39
  */
9
- export type CodeTokenProps<T extends React.ElementType = React.ElementType> = AsComponentProps<T, {
40
+ export type CodeTokenProps<T extends React.ElementType> = AsComponentProps<T, {
10
41
  /**
11
42
  * 語法 token 的語意類型,用於指定樣式顏色。
12
43
  */
13
44
  type?: CodeTokenType;
45
+ /**
46
+ * 語法主題名稱。
47
+ * @default "vscode-dark"
48
+ */
49
+ theme?: CodeTheme;
14
50
  }>;
15
- export type CodeTokenBuilder = <T extends React.ElementType>(children: CodeTokenProps<T>["children"] extends undefined ? React.ReactNode : CodeTokenProps<T>["children"], props?: DistributiveOmit<CodeTokenProps<T>, "type">) => CodeTokenProps<T>;
51
+ export type CodeTokenBuilder = <T extends React.ElementType>(children: CodeTokenProps<T>["children"], props?: CodeTokenProps<T>) => CodeTokenProps<T>;
16
52
  /**
17
53
  * 用於單一程式碼行的屬性,用在 <CodeLine /> 或類似元件中。
18
54
  */
19
- export type CodeLineProps = OverrideProps<React.HTMLAttributes<HTMLElement>, {
55
+ export type CodeLineProps<T extends React.ElementType> = OverrideProps<React.HTMLAttributes<HTMLElement>, {
20
56
  /**
21
57
  * 該行所包含的語法 token。
22
58
  *
@@ -30,9 +66,14 @@ export type CodeLineProps = OverrideProps<React.HTMLAttributes<HTMLElement>, {
30
66
  * ]} />
31
67
  * ```
32
68
  */
33
- tokens: CodeTokenProps[];
69
+ tokens: CodeTokenProps<T>[];
70
+ /**
71
+ * 語法主題名稱。
72
+ * @default "vscode-dark"
73
+ */
74
+ theme?: CodeTheme;
34
75
  }>;
35
- export type CodeBlockProps = OverrideProps<React.HTMLAttributes<HTMLPreElement>, {
76
+ export type CodeBlockProps<T extends React.ElementType> = OverrideProps<React.HTMLAttributes<HTMLPreElement>, {
36
77
  /**
37
78
  * 所有程式碼行的 token 陣列。
38
79
  *
@@ -52,7 +93,7 @@ export type CodeBlockProps = OverrideProps<React.HTMLAttributes<HTMLPreElement>,
52
93
  * ]} />
53
94
  * ```
54
95
  */
55
- tokenLines: CodeTokenProps[][];
96
+ tokenLines: CodeTokenProps<T>[][];
56
97
  /**
57
98
  * 是否顯示行號。
58
99
  * @default true
@@ -67,4 +108,9 @@ export type CodeBlockProps = OverrideProps<React.HTMLAttributes<HTMLPreElement>,
67
108
  * ```
68
109
  * */
69
110
  lineNumberStyle?: React.CSSProperties;
111
+ /**
112
+ * 語法主題名稱。
113
+ * @default "vscode-dark"
114
+ */
115
+ theme?: CodeTheme;
70
116
  }>;
@@ -1,3 +1,28 @@
1
1
  import { CodeTokenBuilder, CodeTokenProps, CodeTokenType } from "../types";
2
- export declare const createToken: Record<CodeTokenType, CodeTokenBuilder>;
3
- export declare const whiteSpace: (count?: number) => CodeTokenProps;
2
+ /**
3
+ * 語法 token 的建構器集合,每個 key 對應一種語法類型(如 `keyword-blue`, `string`, `comment` 等),
4
+ * 透過 Proxy 生成對應的 `CodeTokenBuilder`。
5
+ *
6
+ * 使用方式:
7
+ * ```tsx
8
+ * c063["keyword-blue"]("const") // -> { type: "keyword-blue", children: "const" }
9
+ * c063["string"]("'hello'", { as: "code" }) // 可自訂 as 或其他 props
10
+ * ```
11
+ *
12
+ * @example
13
+ * tokens.push(c063["keyword-blue"]("const"));
14
+ * tokens.push(c063.string("'Hello'"));
15
+ *
16
+ * @returns 一個以 `CodeTokenType` 為 key 的建構器函式集合
17
+ */
18
+ export declare const c063: Record<CodeTokenType, CodeTokenBuilder>;
19
+ /**
20
+ * 產生指定空白數量的 CodeToken,用於程式碼中的縮排或空格。
21
+ *
22
+ * @param count 空白字元數,預設為 1
23
+ * @returns `CodeTokenProps` 物件,type 為 "default",children 為空白字串
24
+ *
25
+ * @example
26
+ * tokens.push(whiteSpace(2)); // -> { type: "default", children: " " }
27
+ */
28
+ export declare const whiteSpace: (count?: number) => CodeTokenProps<"span">;
@@ -1,16 +1,38 @@
1
- export const createToken = new Proxy({}, {
1
+ /**
2
+ * 語法 token 的建構器集合,每個 key 對應一種語法類型(如 `keyword-blue`, `string`, `comment` 等),
3
+ * 透過 Proxy 生成對應的 `CodeTokenBuilder`。
4
+ *
5
+ * 使用方式:
6
+ * ```tsx
7
+ * c063["keyword-blue"]("const") // -> { type: "keyword-blue", children: "const" }
8
+ * c063["string"]("'hello'", { as: "code" }) // 可自訂 as 或其他 props
9
+ * ```
10
+ *
11
+ * @example
12
+ * tokens.push(c063["keyword-blue"]("const"));
13
+ * tokens.push(c063.string("'Hello'"));
14
+ *
15
+ * @returns 一個以 `CodeTokenType` 為 key 的建構器函式集合
16
+ */
17
+ export const c063 = new Proxy({}, {
2
18
  get: (_, prop) => {
3
19
  const builder = (children, props) => {
4
20
  return {
5
- type: prop,
6
21
  children,
22
+ type: prop,
7
23
  ...props,
8
24
  };
9
25
  };
10
26
  return builder;
11
27
  },
12
28
  });
13
- export const whiteSpace = (count = 1) => ({
14
- type: "default",
15
- children: " ".repeat(count),
16
- });
29
+ /**
30
+ * 產生指定空白數量的 CodeToken,用於程式碼中的縮排或空格。
31
+ *
32
+ * @param count 空白字元數,預設為 1
33
+ * @returns `CodeTokenProps` 物件,type 為 "default",children 為空白字串
34
+ *
35
+ * @example
36
+ * tokens.push(whiteSpace(2)); // -> { type: "default", children: " " }
37
+ */
38
+ export const whiteSpace = (count = 1) => c063.default(" ".repeat(count));
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.1.7",
4
+ "version": "1.3.0",
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",
@@ -1,12 +1,22 @@
1
1
  import { CodeBlockProps } from "../types/props";
2
2
  import { CodeLine } from "./CodeLine";
3
-
4
- export const CodeBlock = ({
3
+ /**
4
+ * 顯示完整程式碼區塊,支援多行語法 token 與行號顯示。
5
+ *
6
+ * @template T 元件渲染類型,預設為 <span>
7
+ * @param props.tokenLines 所有程式碼行的 token 陣列
8
+ * @param props.showLineNumbers 是否顯示行號,預設為 true
9
+ * @param props.lineNumberStyle 行號的自訂樣式
10
+ * @param rest 其他傳遞給 <pre> 的屬性
11
+ * @returns JSX 元素,呈現語法高亮的程式碼區塊
12
+ */
13
+ export const CodeBlock = <T extends React.ElementType = "span">({
5
14
  tokenLines,
6
15
  showLineNumbers = true,
7
16
  lineNumberStyle,
17
+ theme,
8
18
  ...rest
9
- }: CodeBlockProps) => {
19
+ }: CodeBlockProps<T>) => {
10
20
  return (
11
21
  <pre {...rest}>
12
22
  {tokenLines.map((line, index) => (
@@ -19,7 +29,6 @@ export const CodeBlock = ({
19
29
  gap: "0.5rem",
20
30
  }}
21
31
  >
22
- {/* 如果需要顯示行號,則在每行前添加行號 */}
23
32
  {showLineNumbers && (
24
33
  <span
25
34
  style={{ color: "#888", userSelect: "none", ...lineNumberStyle }}
@@ -27,7 +36,7 @@ export const CodeBlock = ({
27
36
  {index + 1}
28
37
  </span>
29
38
  )}
30
- <CodeLine tokens={line} />
39
+ <CodeLine theme={theme} tokens={line} />
31
40
  </div>
32
41
  ))}
33
42
  </pre>
@@ -1,7 +1,20 @@
1
1
  import { CodeLineProps } from "../types/props";
2
2
  import { CodeToken } from "./CodeToken";
3
-
4
- export const CodeLine = ({ style, tokens, ...rest }: CodeLineProps) => {
3
+ /**
4
+ * 渲染單一程式碼行,包含多個語法 token。
5
+ *
6
+ * @template T 元件渲染類型,例如 <code>、<span> 等
7
+ * @param props.tokens 該行所包含的語法 token 陣列
8
+ * @param props.style 自訂樣式,會與 whiteSpace: pre-wrap 合併
9
+ * @param rest 其他 HTMLAttributes
10
+ * @returns JSX 元素,呈現語法 token 的單行程式碼
11
+ */
12
+ export const CodeLine = <T extends React.ElementType>({
13
+ style,
14
+ tokens,
15
+ theme,
16
+ ...rest
17
+ }: CodeLineProps<T>) => {
5
18
  return (
6
19
  <code
7
20
  {...rest}
@@ -11,7 +24,7 @@ export const CodeLine = ({ style, tokens, ...rest }: CodeLineProps) => {
11
24
  }}
12
25
  >
13
26
  {tokens.map((token, index) => (
14
- <CodeToken key={`${token.type}-${index}`} {...token} />
27
+ <CodeToken key={`${token.type}-${index}`} theme={theme} {...token} />
15
28
  ))}
16
29
  </code>
17
30
  );
@@ -1,11 +1,22 @@
1
- import { codeColoreMap } from "../libs/codeColorMap";
1
+ import { themeMap } from "../libs";
2
2
  import { CodeTokenProps } from "../types/props";
3
-
3
+ /**
4
+ * 渲染單一語法 token(例如關鍵字、字串、註解等),可指定標籤與樣式。
5
+ *
6
+ * @template T 元件渲染類型,預設為 <span>
7
+ * @param props.as 指定要渲染的 HTML 標籤或客製元件
8
+ * @param props.type 語法類型,用於對應不同顏色
9
+ * @param props.style 額外樣式,會與語法顏色合併
10
+ * @param props.children 顯示的程式碼字串
11
+ * @param rest 其他 HTML 屬性
12
+ * @returns JSX 元素,顯示帶有語法顏色的 token
13
+ */
4
14
  export const CodeToken = <T extends React.ElementType = "span">({
5
15
  as,
6
16
  style,
7
17
  children,
8
18
  type,
19
+ theme,
9
20
  ...rest
10
21
  }: CodeTokenProps<T>) => {
11
22
  const Tag = as || "span";
@@ -14,7 +25,7 @@ export const CodeToken = <T extends React.ElementType = "span">({
14
25
  <Tag
15
26
  {...rest}
16
27
  style={{
17
- color: codeColoreMap[type || "default"],
28
+ color: themeMap[theme || "default-dark-modern"][type || "default"],
18
29
  ...style,
19
30
  }}
20
31
  >
@@ -0,0 +1,17 @@
1
+ import { CodeTokenType } from "../types";
2
+
3
+ export const map: Record<CodeTokenType, React.CSSProperties["color"]> = {
4
+ keyword1: "#569cd6", // 深藍色關鍵字
5
+ keyword2: "#c586c0", // 紫色關鍵字
6
+ string: "#ce9178", // 淺棕色字串
7
+ number: "#b5cea8", // 淺綠色數字
8
+ comment: "#6a9955", // 綠色註解
9
+ variable: "#9cdcfe", // 藍色變數
10
+ constant: "#4fc1ff", // 藍色常數
11
+ type: "#4ec9b0", // 青綠色類型
12
+ brackets1: "#ffd700", // 金黃色括號層級1
13
+ brackets2: "#da70d6", // 紫羅蘭色括號層級2
14
+ brackets3: "#179fff", // 藍色括號層級3
15
+ operator: "#d4d4d4", // 淺灰色運算符
16
+ default: "#d4d4d4", // 淺灰色預設文字
17
+ };
@@ -0,0 +1,19 @@
1
+ import type { CodeTokenType, CodeTheme } from "../types";
2
+ import { map as darkModern } from "./default-dark-modern";
3
+ import { map as vsLight } from "./visual-studio-light";
4
+
5
+ export const themeRegistry = {
6
+ "default-dark-modern": darkModern,
7
+ "visual-studio-light": vsLight,
8
+ } as const;
9
+
10
+ // 自動推導主題名稱
11
+ export const themes = Object.keys(
12
+ themeRegistry
13
+ ) as (keyof typeof themeRegistry)[];
14
+
15
+ // themeMap 保留給外部使用
16
+ export const themeMap: Record<
17
+ CodeTheme,
18
+ Record<CodeTokenType, React.CSSProperties["color"]>
19
+ > = themeRegistry;
@@ -0,0 +1,17 @@
1
+ import { CodeTokenType } from "../types";
2
+
3
+ export const map: Record<CodeTokenType, React.CSSProperties["color"]> = {
4
+ keyword1: "#0000ff",
5
+ keyword2: "#0000ff",
6
+ string: "#000000",
7
+ number: "#098658",
8
+ comment: "#008000",
9
+ variable: "#000000",
10
+ constant: "#000000",
11
+ type: "#000000",
12
+ brackets1: "#0431fa",
13
+ brackets2: "#319331",
14
+ brackets3: "#7b3814",
15
+ operator: "#000000",
16
+ default: "#000000",
17
+ };
@@ -8,9 +8,9 @@ export type OverrideProps<T, P> = DistributiveOmit<T, keyof P> & P;
8
8
 
9
9
  /** 支援元件型別 (可複寫屬性)*/
10
10
  export type AsComponentProps<
11
- As extends React.ElementType,
12
- PermanentProps extends object = {}
13
- > = OverrideProps<React.ComponentPropsWithRef<As>, PermanentProps> & {
11
+ T extends React.ElementType,
12
+ P = {}
13
+ > = OverrideProps<React.ComponentPropsWithRef<T>, P> & {
14
14
  /** 指定用於渲染的 React 元件(可為任意元件*/
15
- as?: As;
15
+ as?: T;
16
16
  };
@@ -1,2 +1,2 @@
1
1
  export * from "./common";
2
- export * from "./props";
2
+ export * from "./props";
@@ -1,48 +1,78 @@
1
- import { AsComponentProps, DistributiveOmit, OverrideProps } from "./common";
2
-
3
- export type KeywordColorType =
4
- | "blue" // const, let, function, if, else class type
5
- | "purple"; // import, export, from, as ,return
6
-
1
+ import { themes } from "../libs";
2
+ import { AsComponentProps, OverrideProps } from "./common";
3
+ /**
4
+ * 用於表示語法高亮中每個 token 的語意分類,對應於 `<CodeToken />` 中的 `type`。
5
+ *
6
+ * 每個類型會對應特定的顏色與用途,例如關鍵字、數字、字串、註解等,
7
+ * 可配合 `codeColoreMap` 指定顯示樣式。s
8
+ *
9
+ * 類型分為以下幾大類:
10
+ *
11
+ * - `keyword1` / `keyword2`: 關鍵字,如 `const`、`return`、`import` 等,分顏色類別。
12
+ * - `string`: 字串常值,如 `'text'`、`"value"`。
13
+ * - `number`: 數字常值,如 `123`、`3.14`。
14
+ * - `comment`: 註解內容,如 `//`。
15
+ * - `type`: 類型定義,如 `interface`、`enum`、`type`。
16
+ * - `variable`: 識別符號,如變數名、函式名、類別名。
17
+ * - `constant`: 常數或靜態值,如 `PI`、`MAX_VALUE`。
18
+ * - `brackets1`, `brackets2`, `brackets3`: 括號配對,區分不同層級的括號。
19
+ * - `operator`: 運算符,如 `=`, `+`, `===`, `<`, `>=`。
20
+ * - `default`: 其他符號,如 `;`, `,`, `.`, `?`, `"`, `'`。
21
+ *
22
+ * @example
23
+ * const token: CodeTokenType = "keyword1";
24
+ * const token2: CodeTokenType = "string";
25
+ */
7
26
  export type CodeTokenType =
8
- | `keyword-${KeywordColorType}`
9
- | "string" // string
10
- | "number" // number
11
- | "comment" // comment ex: //, /* */, /** */
12
- | "type" // type, interface, enum
13
- | "variable" // variable, function name, class name, method name
14
- | "constant" // constant, enum value, static property
15
- | `brackets-${1 | 2 | 3}` // (), [], {}, <>, (), [], {}, <
16
- | "operator" // +, -, *, /, %, =, ==, ===, !=, !==, <, >, <=, >=
17
- | "default"; // ., ,, ;, :, ?, !, @, #, $, %, ^, &, *, (, ), [, ], {, }, <, >, /, \, |, \", ', `;
27
+ | `keyword${1 | 2}` // 關鍵字,分兩種樣式層級
28
+ | "string" // 字串常值:'abc'、"hello"
29
+ | "number" // 數值常量:123、3.14
30
+ | "comment" // 註解內容:// /* */
31
+ | "type" // 類型定義:typeinterfaceenum
32
+ | "variable" // 變數名、函式名、類別名等識別符號
33
+ | "constant" // 常數值:例如 enum 值、靜態屬性
34
+ | `brackets${1 | 2 | 3}` // 括號配對,三層不同樣式:(), [], {}
35
+ | "operator" // 運算符號:=、+、*、===、<、>=
36
+ | "default"; // 其他符號:, ; . ? !
37
+
38
+ /**
39
+ * 表示可用的語法高亮主題名稱。
40
+ * 對應 `themes` 陣列中定義的名稱,例如 `"vscode-dark"`。
41
+ *
42
+ * @example
43
+ * const theme: CodeTheme = "vscode-dark";
44
+ */
45
+ export type CodeTheme = (typeof themes)[number];
18
46
 
19
47
  /**
20
48
  * 單一語法 token 的屬性,用於 <CodeToken /> 元件。
21
49
  *
22
50
  * @template T HTML 或客製元素,例如 span、a、Link 等
23
51
  */
24
- export type CodeTokenProps<T extends React.ElementType = React.ElementType> =
25
- AsComponentProps<
26
- T,
27
- {
28
- /**
29
- * 語法 token 的語意類型,用於指定樣式顏色。
30
- */
31
- type?: CodeTokenType;
32
- }
33
- >;
52
+ export type CodeTokenProps<T extends React.ElementType> = AsComponentProps<
53
+ T,
54
+ {
55
+ /**
56
+ * 語法 token 的語意類型,用於指定樣式顏色。
57
+ */
58
+ type?: CodeTokenType;
59
+ /**
60
+ * 語法主題名稱。
61
+ * @default "vscode-dark"
62
+ */
63
+ theme?: CodeTheme;
64
+ }
65
+ >;
34
66
 
35
67
  export type CodeTokenBuilder = <T extends React.ElementType>(
36
- children: CodeTokenProps<T>["children"] extends undefined
37
- ? React.ReactNode
38
- : CodeTokenProps<T>["children"],
39
- props?: DistributiveOmit<CodeTokenProps<T>, "type">
68
+ children: CodeTokenProps<T>["children"],
69
+ props?: CodeTokenProps<T>
40
70
  ) => CodeTokenProps<T>;
41
71
 
42
72
  /**
43
73
  * 用於單一程式碼行的屬性,用在 <CodeLine /> 或類似元件中。
44
74
  */
45
- export type CodeLineProps = OverrideProps<
75
+ export type CodeLineProps<T extends React.ElementType> = OverrideProps<
46
76
  React.HTMLAttributes<HTMLElement>,
47
77
  {
48
78
  /**
@@ -58,11 +88,16 @@ export type CodeLineProps = OverrideProps<
58
88
  * ]} />
59
89
  * ```
60
90
  */
61
- tokens: CodeTokenProps[];
91
+ tokens: CodeTokenProps<T>[];
92
+ /**
93
+ * 語法主題名稱。
94
+ * @default "vscode-dark"
95
+ */
96
+ theme?: CodeTheme;
62
97
  }
63
98
  >;
64
99
 
65
- export type CodeBlockProps = OverrideProps<
100
+ export type CodeBlockProps<T extends React.ElementType> = OverrideProps<
66
101
  React.HTMLAttributes<HTMLPreElement>,
67
102
  {
68
103
  /**
@@ -84,7 +119,7 @@ export type CodeBlockProps = OverrideProps<
84
119
  * ]} />
85
120
  * ```
86
121
  */
87
- tokenLines: CodeTokenProps[][];
122
+ tokenLines: CodeTokenProps<T>[][];
88
123
 
89
124
  /**
90
125
  * 是否顯示行號。
@@ -100,5 +135,10 @@ export type CodeBlockProps = OverrideProps<
100
135
  * ```
101
136
  * */
102
137
  lineNumberStyle?: React.CSSProperties;
138
+ /**
139
+ * 語法主題名稱。
140
+ * @default "vscode-dark"
141
+ */
142
+ theme?: CodeTheme;
103
143
  }
104
144
  >;
@@ -1,31 +1,47 @@
1
- import {
2
- CodeTokenBuilder,
3
- CodeTokenProps,
4
- CodeTokenType,
5
- DistributiveOmit,
6
- } from "../types";
7
-
8
- export const createToken = new Proxy(
1
+ import { CodeTokenBuilder, CodeTokenProps, CodeTokenType } from "../types";
2
+ /**
3
+ * 語法 token 的建構器集合,每個 key 對應一種語法類型(如 `keyword-blue`, `string`, `comment` 等),
4
+ * 透過 Proxy 生成對應的 `CodeTokenBuilder`。
5
+ *
6
+ * 使用方式:
7
+ * ```tsx
8
+ * c063["keyword-blue"]("const") // -> { type: "keyword-blue", children: "const" }
9
+ * c063["string"]("'hello'", { as: "code" }) // 可自訂 as 或其他 props
10
+ * ```
11
+ *
12
+ * @example
13
+ * tokens.push(c063["keyword-blue"]("const"));
14
+ * tokens.push(c063.string("'Hello'"));
15
+ *
16
+ * @returns 一個以 `CodeTokenType` 為 key 的建構器函式集合
17
+ */
18
+ export const c063 = new Proxy(
9
19
  {},
10
20
  {
11
21
  get: (_, prop: CodeTokenType) => {
12
- const builder = <T extends React.ElementType = React.ElementType>(
13
- children: CodeTokenProps<T>["children"],
14
- props: DistributiveOmit<CodeTokenProps<T>, "type">
15
- ): CodeTokenProps<T> =>
16
- {
17
- return {
18
- type: prop,
19
- children,
20
- ...props,
21
- };
22
+ const builder = <T extends React.ElementType = "span">(
23
+ children: React.ReactNode,
24
+ props: CodeTokenProps<T>
25
+ ) => {
26
+ return {
27
+ children,
28
+ type: prop,
29
+ ...props,
22
30
  };
23
- return builder;
31
+ };
32
+ return builder as CodeTokenBuilder;
24
33
  },
25
34
  }
26
35
  ) as Record<CodeTokenType, CodeTokenBuilder>;
27
36
 
28
- export const whiteSpace = (count: number = 1): CodeTokenProps => ({
29
- type: "default",
30
- children: " ".repeat(count),
31
- });
37
+ /**
38
+ * 產生指定空白數量的 CodeToken,用於程式碼中的縮排或空格。
39
+ *
40
+ * @param count 空白字元數,預設為 1
41
+ * @returns `CodeTokenProps` 物件,type 為 "default",children 為空白字串
42
+ *
43
+ * @example
44
+ * tokens.push(whiteSpace(2)); // -> { type: "default", children: " " }
45
+ */
46
+ export const whiteSpace = (count: number = 1): CodeTokenProps<"span"> =>
47
+ c063.default(" ".repeat(count));
@@ -1,20 +0,0 @@
1
- import { CodeTokenType } from "../types/props";
2
-
3
- export const codeColoreMap: Record<
4
- CodeTokenType,
5
- React.HTMLAttributes<React.ElementType>["className"]
6
- > = {
7
- "keyword-blue": "#569cd6",
8
- "keyword-purple": "#c586c0",
9
- string: "#ce9178",
10
- number: "#b5cea8",
11
- comment: "#6a9955",
12
- variable: "#9cdcfe",
13
- constant: "#4fc1ff",
14
- type: "#4ec9b0",
15
- "brackets-1": "#ffd700",
16
- "brackets-2": "#da70d6",
17
- "brackets-3": "#179fff",
18
- operator: "#d4d4d4",
19
- default: "#d4d4d4",
20
- };
package/src/libs/index.ts DELETED
@@ -1 +0,0 @@
1
- export { codeColoreMap } from "./codeColorMap";