c063 1.2.0 → 1.3.1

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 (58) hide show
  1. package/README.md +1 -1
  2. package/dist/components/CodeBlock.d.ts +11 -1
  3. package/dist/components/CodeBlock.js +12 -2
  4. package/dist/components/CodeLine.d.ts +10 -1
  5. package/dist/components/CodeLine.js +11 -2
  6. package/dist/components/CodeToken.d.ts +12 -1
  7. package/dist/components/CodeToken.js +14 -3
  8. package/dist/libs/default-dark-modern.d.ts +2 -0
  9. package/dist/libs/default-dark-modern.js +16 -0
  10. package/dist/libs/default-dark-plus.d.ts +2 -0
  11. package/dist/libs/default-dark-plus.js +16 -0
  12. package/dist/libs/default-dark.d.ts +2 -0
  13. package/dist/libs/default-dark.js +16 -0
  14. package/dist/libs/default-light-modern.d.ts +2 -0
  15. package/dist/libs/default-light-modern.js +16 -0
  16. package/dist/libs/default-light-plus.d.ts +2 -0
  17. package/dist/libs/default-light-plus.js +16 -0
  18. package/dist/libs/github-dark-colorblind.d.ts +2 -0
  19. package/dist/libs/github-dark-colorblind.js +16 -0
  20. package/dist/libs/github-dark-default.d.ts +2 -0
  21. package/dist/libs/github-dark-default.js +17 -0
  22. package/dist/libs/github-dark.d.ts +2 -0
  23. package/dist/libs/github-dark.js +16 -0
  24. package/dist/libs/github-light-colorblind.d.ts +2 -0
  25. package/dist/libs/github-light-colorblind.js +16 -0
  26. package/dist/libs/github-light-default.d.ts +2 -0
  27. package/dist/libs/github-light-default.js +16 -0
  28. package/dist/libs/github-light.d.ts +2 -0
  29. package/dist/libs/github-light.js +16 -0
  30. package/dist/libs/index.d.ts +18 -1
  31. package/dist/libs/index.js +30 -1
  32. package/dist/libs/visual-studio-light.d.ts +2 -0
  33. package/dist/libs/visual-studio-light.js +16 -0
  34. package/dist/types/props.d.ts +48 -2
  35. package/dist/utils/index.d.ts +26 -1
  36. package/dist/utils/index.js +27 -5
  37. package/package.json +1 -1
  38. package/src/components/CodeBlock.tsx +12 -3
  39. package/src/components/CodeLine.tsx +11 -2
  40. package/src/components/CodeToken.tsx +14 -3
  41. package/src/libs/default-dark-modern.tsx +18 -0
  42. package/src/libs/default-dark-plus.tsx +18 -0
  43. package/src/libs/default-dark.tsx +18 -0
  44. package/src/libs/default-light-modern.tsx +18 -0
  45. package/src/libs/default-light-plus.tsx +18 -0
  46. package/src/libs/github-dark-colorblind.tsx +18 -0
  47. package/src/libs/github-dark-default.tsx +19 -0
  48. package/src/libs/github-dark.tsx +18 -0
  49. package/src/libs/github-light-colorblind.tsx +18 -0
  50. package/src/libs/github-light-default.tsx +18 -0
  51. package/src/libs/github-light.tsx +18 -0
  52. package/src/libs/index.tsx +39 -0
  53. package/src/libs/visual-studio-light.tsx +18 -0
  54. package/src/types/index.ts +1 -1
  55. package/src/types/props.tsx +60 -16
  56. package/src/utils/index.ts +29 -7
  57. package/src/libs/codeColorMap.tsx +0 -20
  58. package/src/libs/index.ts +0 -1
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
- <T extends React.ElementType = "span">({ tokenLines, showLineNumbers, lineNumberStyle, ...rest }: CodeBlockProps<T>): 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
- <T extends React.ElementType>({ style, tokens, ...rest }: CodeLineProps<T>): 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,16 @@
1
+ export const map = {
2
+ function: "#dcdcaa",
3
+ keyword1: "#569cd6",
4
+ keyword2: "#c586c0",
5
+ string: "#ce9178",
6
+ number: "#b5cea8",
7
+ comment: "#6a9955",
8
+ variable: "#9cdcfe",
9
+ constant: "#4fc1ff",
10
+ type: "#4ec9b0",
11
+ brackets1: "#ffd700",
12
+ brackets2: "#da70d6",
13
+ brackets3: "#179fff",
14
+ operator: "#d4d4d4",
15
+ default: "#d4d4d4",
16
+ };
@@ -0,0 +1,2 @@
1
+ import { CodeTokenType } from "../types";
2
+ export declare const map: Record<CodeTokenType, React.CSSProperties["color"]>;
@@ -0,0 +1,16 @@
1
+ export const map = {
2
+ function: "#dcdcaa",
3
+ keyword1: "#569cd6",
4
+ keyword2: "#c586c0",
5
+ string: "#ce9178",
6
+ number: "#b5cea8",
7
+ comment: "#6a9955",
8
+ variable: "#9cdcfe",
9
+ constant: "#4fc1ff",
10
+ type: "#4ec9b0",
11
+ brackets1: "#ffd700",
12
+ brackets2: "#da70d6",
13
+ brackets3: "#179fff",
14
+ operator: "#d4d4d4",
15
+ default: "#d4d4d4",
16
+ };
@@ -0,0 +1,2 @@
1
+ import { CodeTokenType } from "../types";
2
+ export declare const map: Record<CodeTokenType, React.CSSProperties["color"]>;
@@ -0,0 +1,16 @@
1
+ export const map = {
2
+ function: "#d4d4d4",
3
+ keyword1: "#569cd6",
4
+ keyword2: "#569cd6",
5
+ string: "#ce9178",
6
+ number: "#b5cea8",
7
+ comment: "#6a9955",
8
+ variable: "#d4d4d4",
9
+ constant: "#d4d4d4",
10
+ type: "#d4d4d4",
11
+ brackets1: "#ffd700",
12
+ brackets2: "#da70d6",
13
+ brackets3: "#179fff",
14
+ operator: "#d4d4d4",
15
+ default: "#d4d4d4",
16
+ };
@@ -0,0 +1,2 @@
1
+ import { CodeTokenType } from "../types";
2
+ export declare const map: Record<CodeTokenType, React.CSSProperties["color"]>;
@@ -0,0 +1,16 @@
1
+ export const map = {
2
+ function: "rgb(121, 94, 38)",
3
+ keyword1: "rgb(0, 0, 255)",
4
+ keyword2: "rgb(175, 0, 219)",
5
+ string: "rgb(163, 21, 21)",
6
+ number: "rgb(9, 134, 88)",
7
+ comment: "rgb(0, 128, 0)",
8
+ variable: "rgb(0, 16, 128)",
9
+ constant: "rgb(0, 112, 193)",
10
+ type: "rgb(0, 0, 255)",
11
+ brackets1: "rgb(4, 49, 250)",
12
+ brackets2: "rgb(49, 147, 49)",
13
+ brackets3: "rgb(123, 56, 20)",
14
+ operator: "rgb(0, 0, 0)",
15
+ default: "rgb(0, 0, 0)",
16
+ };
@@ -0,0 +1,2 @@
1
+ import { CodeTokenType } from "../types";
2
+ export declare const map: Record<CodeTokenType, React.CSSProperties["color"]>;
@@ -0,0 +1,16 @@
1
+ export const map = {
2
+ function: "#795e26",
3
+ keyword1: "#0000ff",
4
+ keyword2: "#af00db",
5
+ string: "#a31515",
6
+ number: "#098658",
7
+ comment: "#008000",
8
+ variable: "#001080",
9
+ constant: "#0070c1",
10
+ type: "#267f99",
11
+ brackets1: "#0431fa",
12
+ brackets2: "#319331",
13
+ brackets3: "#7b3814",
14
+ operator: "#000000",
15
+ default: "#000000",
16
+ };
@@ -0,0 +1,2 @@
1
+ import { CodeTokenType } from "../types";
2
+ export declare const map: Record<CodeTokenType, React.CSSProperties["color"]>;
@@ -0,0 +1,16 @@
1
+ export const map = {
2
+ function: "rgb(210, 168, 255)",
3
+ keyword1: "rgb(236, 142, 44)",
4
+ keyword2: "rgb(236, 142, 44)",
5
+ string: "rgb(165, 214, 255)",
6
+ number: "rgb(121, 192, 255)",
7
+ comment: "#rgb(139, 148, 158)",
8
+ variable: "rgb(201, 209, 217)",
9
+ constant: "rgb(121, 192, 255)",
10
+ type: "rgb(236, 142, 44)",
11
+ brackets1: "rgb(121, 192, 255)",
12
+ brackets2: "rgb(121, 192, 255)",
13
+ brackets3: "rgb(227, 179, 65)",
14
+ operator: "rgb(236, 142, 44)",
15
+ default: "rgb(201, 209, 217)",
16
+ };
@@ -0,0 +1,2 @@
1
+ import { CodeTokenType } from "../types";
2
+ export declare const map: Record<CodeTokenType, React.CSSProperties["color"]>;
@@ -0,0 +1,17 @@
1
+ export const map = {
2
+ function: "#d2a8ff",
3
+ keyword1: "#ff7b72",
4
+ keyword2: "#ff7b72",
5
+ string: "#a5d6ff",
6
+ number: "#79c0ff",
7
+ comment: "##6a737d",
8
+ variable: "#e6edf3",
9
+ constant: "#79c0ff",
10
+ type: "#ff7b72",
11
+ brackets1: "#79c0ff",
12
+ brackets2: "#56d364",
13
+ brackets3: "#e3b341",
14
+ brackets4: "#ffa198",
15
+ operator: "#ff7b72",
16
+ default: "#e6edf3",
17
+ };
@@ -0,0 +1,2 @@
1
+ import { CodeTokenType } from "../types";
2
+ export declare const map: Record<CodeTokenType, React.CSSProperties["color"]>;
@@ -0,0 +1,16 @@
1
+ export const map = {
2
+ function: "#b392f0",
3
+ keyword1: "#f97583",
4
+ keyword2: "#f97583",
5
+ string: "#9ecbff",
6
+ number: "#79b8ff",
7
+ comment: "##6a737d",
8
+ variable: "#e1e4e8",
9
+ constant: "#79b8ff",
10
+ type: "#f97583",
11
+ brackets1: "#79b8ff",
12
+ brackets2: "#ffab70",
13
+ brackets3: "#b392f0",
14
+ operator: "#f97583",
15
+ default: "#e1e4e8",
16
+ };
@@ -0,0 +1,2 @@
1
+ import { CodeTokenType } from "../types";
2
+ export declare const map: Record<CodeTokenType, React.CSSProperties["color"]>;
@@ -0,0 +1,16 @@
1
+ export const map = {
2
+ function: "#8250df",
3
+ keyword1: "#b35900",
4
+ keyword2: "#b35900",
5
+ string: "#0a3069",
6
+ number: "#0550ae",
7
+ comment: "#6e7781",
8
+ variable: "#24292f",
9
+ constant: "#0550ae",
10
+ type: "#b35900",
11
+ brackets1: "#0969da",
12
+ brackets2: "#0969da",
13
+ brackets3: "#9a6700",
14
+ operator: "#b35900",
15
+ default: "#24292f",
16
+ };
@@ -0,0 +1,2 @@
1
+ import { CodeTokenType } from "../types";
2
+ export declare const map: Record<CodeTokenType, React.CSSProperties["color"]>;
@@ -0,0 +1,16 @@
1
+ export const map = {
2
+ function: "#8250df",
3
+ keyword1: "#cf222e",
4
+ keyword2: "#cf222e",
5
+ string: "#0a3069",
6
+ number: "##0550ae",
7
+ comment: "#6e7781",
8
+ variable: "#1f2328",
9
+ constant: "#0550ae",
10
+ type: "#cf222e",
11
+ brackets1: "#0969da",
12
+ brackets2: "#1a7f37",
13
+ brackets3: "#9a6700",
14
+ operator: "#cf222e",
15
+ default: "#24292e",
16
+ };
@@ -0,0 +1,2 @@
1
+ import { CodeTokenType } from "../types";
2
+ export declare const map: Record<CodeTokenType, React.CSSProperties["color"]>;
@@ -0,0 +1,16 @@
1
+ export const map = {
2
+ function: "#6f42c1",
3
+ keyword1: "#d73a49",
4
+ keyword2: "#d73a49",
5
+ string: "#032f62",
6
+ number: "#005cc5",
7
+ comment: "#6a737d",
8
+ variable: "#24292e",
9
+ constant: "#0070c1",
10
+ type: "#6f42c1",
11
+ brackets1: "#005cc5",
12
+ brackets2: "#e36209",
13
+ brackets3: "#5a32a3",
14
+ operator: "#d73a49",
15
+ default: "#24292e",
16
+ };
@@ -1 +1,18 @@
1
- export { codeColoreMap } from "./codeColorMap";
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"]>>;
18
+ export {};
@@ -1 +1,30 @@
1
- export { codeColoreMap } from "./codeColorMap";
1
+ import { map as darkModern } from "./default-dark-modern";
2
+ import { map as dark } from "./default-dark";
3
+ import { map as darkPlus } from "./default-dark-plus";
4
+ import { map as vsLight } from "./visual-studio-light";
5
+ import { map as lightPlus } from "./default-light-plus";
6
+ import { map as lightModern } from "./default-light-modern";
7
+ import { map as ghLight } from "./github-light";
8
+ import { map as ghLightDefault } from "./github-light-default";
9
+ import { map as ghLightBilnd } from "./github-light-colorblind";
10
+ import { map as ghDark } from "./github-dark";
11
+ import { map as ghDarkDefault } from "./github-dark-default";
12
+ import { map as ghDarkBilnd } from "./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;
@@ -0,0 +1,2 @@
1
+ import { CodeTokenType } from "../types";
2
+ export declare const map: Record<CodeTokenType, React.CSSProperties["color"]>;
@@ -0,0 +1,16 @@
1
+ export const map = {
2
+ function: "#000000",
3
+ keyword1: "#0000ff",
4
+ keyword2: "#0000ff",
5
+ string: "#a31515",
6
+ number: "#098658",
7
+ comment: "#008000",
8
+ variable: "#000000",
9
+ constant: "#000000",
10
+ type: "#000000",
11
+ brackets1: "#0431fa",
12
+ brackets2: "#319331",
13
+ brackets3: "#7b3814",
14
+ operator: "#000000",
15
+ default: "#800000",
16
+ };
@@ -1,6 +1,37 @@
1
+ import { themes } from "../libs";
1
2
  import { AsComponentProps, 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";
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${number}` | "function" | "string" | "number" | "comment" | "type" | "variable" | "constant" | `brackets${number}` | "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
  *
@@ -11,6 +42,11 @@ export type CodeTokenProps<T extends React.ElementType> = AsComponentProps<T, {
11
42
  * 語法 token 的語意類型,用於指定樣式顏色。
12
43
  */
13
44
  type?: CodeTokenType;
45
+ /**
46
+ * 語法主題名稱。
47
+ * @default "vscode-dark"
48
+ */
49
+ theme?: CodeTheme;
14
50
  }>;
15
51
  export type CodeTokenBuilder = <T extends React.ElementType>(children: CodeTokenProps<T>["children"], props?: CodeTokenProps<T>) => CodeTokenProps<T>;
16
52
  /**
@@ -31,6 +67,11 @@ export type CodeLineProps<T extends React.ElementType> = OverrideProps<React.HTM
31
67
  * ```
32
68
  */
33
69
  tokens: CodeTokenProps<T>[];
70
+ /**
71
+ * 語法主題名稱。
72
+ * @default "vscode-dark"
73
+ */
74
+ theme?: CodeTheme;
34
75
  }>;
35
76
  export type CodeBlockProps<T extends React.ElementType> = OverrideProps<React.HTMLAttributes<HTMLPreElement>, {
36
77
  /**
@@ -67,4 +108,9 @@ export type CodeBlockProps<T extends React.ElementType> = OverrideProps<React.HT
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>;
2
+ /**
3
+ * 語法 token 的建構器集合,每個 key 對應一種語法類型(如 `keyword-blue`, `string`, `comment` 等),
4
+ * 透過 Proxy 生成對應的 `CodeTokenBuilder`。
5
+ *
6
+ * 使用方式:
7
+ * ```tsx
8
+ * c063.keyword1("const") // -> { type: "keyword1", children: "const" }
9
+ * c063.string("'hello'", { as: "code" }) // 可自訂 as 或其他 props
10
+ * ```
11
+ *
12
+ * @example
13
+ * tokens.push(c063.keyword1("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
+ */
3
28
  export declare const whiteSpace: (count?: number) => CodeTokenProps<"span">;
@@ -1,4 +1,20 @@
1
- export const createToken = new Proxy({}, {
1
+ /**
2
+ * 語法 token 的建構器集合,每個 key 對應一種語法類型(如 `keyword-blue`, `string`, `comment` 等),
3
+ * 透過 Proxy 生成對應的 `CodeTokenBuilder`。
4
+ *
5
+ * 使用方式:
6
+ * ```tsx
7
+ * c063.keyword1("const") // -> { type: "keyword1", children: "const" }
8
+ * c063.string("'hello'", { as: "code" }) // 可自訂 as 或其他 props
9
+ * ```
10
+ *
11
+ * @example
12
+ * tokens.push(c063.keyword1("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 {
@@ -10,7 +26,13 @@ export const createToken = new Proxy({}, {
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.2.0",
4
+ "version": "1.3.1",
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,10 +1,20 @@
1
1
  import { CodeBlockProps } from "../types/props";
2
2
  import { CodeLine } from "./CodeLine";
3
-
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
+ */
4
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
19
  }: CodeBlockProps<T>) => {
10
20
  return (
@@ -19,7 +29,6 @@ export const CodeBlock = <T extends React.ElementType = "span">({
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 = <T extends React.ElementType = "span">({
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,9 +1,18 @@
1
1
  import { CodeLineProps } from "../types/props";
2
2
  import { CodeToken } from "./CodeToken";
3
-
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
+ */
4
12
  export const CodeLine = <T extends React.ElementType>({
5
13
  style,
6
14
  tokens,
15
+ theme,
7
16
  ...rest
8
17
  }: CodeLineProps<T>) => {
9
18
  return (
@@ -15,7 +24,7 @@ export const CodeLine = <T extends React.ElementType>({
15
24
  }}
16
25
  >
17
26
  {tokens.map((token, index) => (
18
- <CodeToken key={`${token.type}-${index}`} {...token} />
27
+ <CodeToken key={`${token.type}-${index}`} theme={theme} {...token} />
19
28
  ))}
20
29
  </code>
21
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,18 @@
1
+ import { CodeTokenType } from "../types";
2
+
3
+ export const map: Record<CodeTokenType, React.CSSProperties["color"]> = {
4
+ function: "#dcdcaa",
5
+ keyword1: "#569cd6",
6
+ keyword2: "#c586c0",
7
+ string: "#ce9178",
8
+ number: "#b5cea8",
9
+ comment: "#6a9955",
10
+ variable: "#9cdcfe",
11
+ constant: "#4fc1ff",
12
+ type: "#4ec9b0",
13
+ brackets1: "#ffd700",
14
+ brackets2: "#da70d6",
15
+ brackets3: "#179fff",
16
+ operator: "#d4d4d4",
17
+ default: "#d4d4d4",
18
+ };
@@ -0,0 +1,18 @@
1
+ import { CodeTokenType } from "../types";
2
+
3
+ export const map: Record<CodeTokenType, React.CSSProperties["color"]> = {
4
+ function: "#dcdcaa",
5
+ keyword1: "#569cd6",
6
+ keyword2: "#c586c0",
7
+ string: "#ce9178",
8
+ number: "#b5cea8",
9
+ comment: "#6a9955",
10
+ variable: "#9cdcfe",
11
+ constant: "#4fc1ff",
12
+ type: "#4ec9b0",
13
+ brackets1: "#ffd700",
14
+ brackets2: "#da70d6",
15
+ brackets3: "#179fff",
16
+ operator: "#d4d4d4",
17
+ default: "#d4d4d4",
18
+ };
@@ -0,0 +1,18 @@
1
+ import { CodeTokenType } from "../types";
2
+
3
+ export const map: Record<CodeTokenType, React.CSSProperties["color"]> = {
4
+ function: "#d4d4d4",
5
+ keyword1: "#569cd6",
6
+ keyword2: "#569cd6",
7
+ string: "#ce9178",
8
+ number: "#b5cea8",
9
+ comment: "#6a9955",
10
+ variable: "#d4d4d4",
11
+ constant: "#d4d4d4",
12
+ type: "#d4d4d4",
13
+ brackets1: "#ffd700",
14
+ brackets2: "#da70d6",
15
+ brackets3: "#179fff",
16
+ operator: "#d4d4d4",
17
+ default: "#d4d4d4",
18
+ };
@@ -0,0 +1,18 @@
1
+ import { CodeTokenType } from "../types";
2
+
3
+ export const map: Record<CodeTokenType, React.CSSProperties["color"]> = {
4
+ function: "rgb(121, 94, 38)",
5
+ keyword1: "rgb(0, 0, 255)",
6
+ keyword2: "rgb(175, 0, 219)",
7
+ string: "rgb(163, 21, 21)",
8
+ number: "rgb(9, 134, 88)",
9
+ comment: "rgb(0, 128, 0)",
10
+ variable: "rgb(0, 16, 128)",
11
+ constant: "rgb(0, 112, 193)",
12
+ type: "rgb(0, 0, 255)",
13
+ brackets1: "rgb(4, 49, 250)",
14
+ brackets2: "rgb(49, 147, 49)",
15
+ brackets3: "rgb(123, 56, 20)",
16
+ operator: "rgb(0, 0, 0)",
17
+ default: "rgb(0, 0, 0)",
18
+ };
@@ -0,0 +1,18 @@
1
+ import { CodeTokenType } from "../types";
2
+
3
+ export const map: Record<CodeTokenType, React.CSSProperties["color"]> = {
4
+ function: "#795e26",
5
+ keyword1: "#0000ff",
6
+ keyword2: "#af00db",
7
+ string: "#a31515",
8
+ number: "#098658",
9
+ comment: "#008000",
10
+ variable: "#001080",
11
+ constant: "#0070c1",
12
+ type: "#267f99",
13
+ brackets1: "#0431fa",
14
+ brackets2: "#319331",
15
+ brackets3: "#7b3814",
16
+ operator: "#000000",
17
+ default: "#000000",
18
+ };
@@ -0,0 +1,18 @@
1
+ import { CodeTokenType } from "../types";
2
+
3
+ export const map: Record<CodeTokenType, React.CSSProperties["color"]> = {
4
+ function: "rgb(210, 168, 255)",
5
+ keyword1: "rgb(236, 142, 44)",
6
+ keyword2: "rgb(236, 142, 44)",
7
+ string: "rgb(165, 214, 255)",
8
+ number: "rgb(121, 192, 255)",
9
+ comment: "#rgb(139, 148, 158)",
10
+ variable: "rgb(201, 209, 217)",
11
+ constant: "rgb(121, 192, 255)",
12
+ type: "rgb(236, 142, 44)",
13
+ brackets1: "rgb(121, 192, 255)",
14
+ brackets2: "rgb(121, 192, 255)",
15
+ brackets3: "rgb(227, 179, 65)",
16
+ operator: "rgb(236, 142, 44)",
17
+ default: "rgb(201, 209, 217)",
18
+ };
@@ -0,0 +1,19 @@
1
+ import { CodeTokenType } from "../types";
2
+
3
+ export const map: Record<CodeTokenType, React.CSSProperties["color"]> = {
4
+ function: "#d2a8ff",
5
+ keyword1: "#ff7b72",
6
+ keyword2: "#ff7b72",
7
+ string: "#a5d6ff",
8
+ number: "#79c0ff",
9
+ comment: "##6a737d",
10
+ variable: "#e6edf3",
11
+ constant: "#79c0ff",
12
+ type: "#ff7b72",
13
+ brackets1: "#79c0ff",
14
+ brackets2: "#56d364",
15
+ brackets3: "#e3b341",
16
+ brackets4: "#ffa198",
17
+ operator: "#ff7b72",
18
+ default: "#e6edf3",
19
+ };
@@ -0,0 +1,18 @@
1
+ import { CodeTokenType } from "../types";
2
+
3
+ export const map: Record<CodeTokenType, React.CSSProperties["color"]> = {
4
+ function: "#b392f0",
5
+ keyword1: "#f97583",
6
+ keyword2: "#f97583",
7
+ string: "#9ecbff",
8
+ number: "#79b8ff",
9
+ comment: "##6a737d",
10
+ variable: "#e1e4e8",
11
+ constant: "#79b8ff",
12
+ type: "#f97583",
13
+ brackets1: "#79b8ff",
14
+ brackets2: "#ffab70",
15
+ brackets3: "#b392f0",
16
+ operator: "#f97583",
17
+ default: "#e1e4e8",
18
+ };
@@ -0,0 +1,18 @@
1
+ import { CodeTokenType } from "../types";
2
+
3
+ export const map: Record<CodeTokenType, React.CSSProperties["color"]> = {
4
+ function: "#8250df",
5
+ keyword1: "#b35900",
6
+ keyword2: "#b35900",
7
+ string: "#0a3069",
8
+ number: "#0550ae",
9
+ comment: "#6e7781",
10
+ variable: "#24292f",
11
+ constant: "#0550ae",
12
+ type: "#b35900",
13
+ brackets1: "#0969da",
14
+ brackets2: "#0969da",
15
+ brackets3: "#9a6700",
16
+ operator: "#b35900",
17
+ default: "#24292f",
18
+ };
@@ -0,0 +1,18 @@
1
+ import { CodeTokenType } from "../types";
2
+
3
+ export const map: Record<CodeTokenType, React.CSSProperties["color"]> = {
4
+ function: "#8250df",
5
+ keyword1: "#cf222e",
6
+ keyword2: "#cf222e",
7
+ string: "#0a3069",
8
+ number: "##0550ae",
9
+ comment: "#6e7781",
10
+ variable: "#1f2328",
11
+ constant: "#0550ae",
12
+ type: "#cf222e",
13
+ brackets1: "#0969da",
14
+ brackets2: "#1a7f37",
15
+ brackets3: "#9a6700",
16
+ operator: "#cf222e",
17
+ default: "#24292e",
18
+ };
@@ -0,0 +1,18 @@
1
+ import { CodeTokenType } from "../types";
2
+
3
+ export const map: Record<CodeTokenType, React.CSSProperties["color"]> = {
4
+ function: "#6f42c1",
5
+ keyword1: "#d73a49",
6
+ keyword2: "#d73a49",
7
+ string: "#032f62",
8
+ number: "#005cc5",
9
+ comment: "#6a737d",
10
+ variable: "#24292e",
11
+ constant: "#0070c1",
12
+ type: "#6f42c1",
13
+ brackets1: "#005cc5",
14
+ brackets2: "#e36209",
15
+ brackets3: "#5a32a3",
16
+ operator: "#d73a49",
17
+ default: "#24292e",
18
+ };
@@ -0,0 +1,39 @@
1
+ import type { CodeTokenType, CodeTheme } from "../types";
2
+ import { map as darkModern } from "./default-dark-modern";
3
+ import { map as dark } from "./default-dark";
4
+ import { map as darkPlus } from "./default-dark-plus";
5
+ import { map as vsLight } from "./visual-studio-light";
6
+ import { map as lightPlus } from "./default-light-plus";
7
+ import { map as lightModern } from "./default-light-modern";
8
+ import { map as ghLight } from "./github-light";
9
+ import { map as ghLightDefault } from "./github-light-default";
10
+ import { map as ghLightBilnd } from "./github-light-colorblind";
11
+ import { map as ghDark } from "./github-dark";
12
+ import { map as ghDarkDefault } from "./github-dark-default";
13
+ import { map as ghDarkBilnd } from "./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;
@@ -0,0 +1,18 @@
1
+ import { CodeTokenType } from "../types";
2
+
3
+ export const map: Record<CodeTokenType, React.CSSProperties["color"]> = {
4
+ function: "#000000",
5
+ keyword1: "#0000ff",
6
+ keyword2: "#0000ff",
7
+ string: "#a31515",
8
+ number: "#098658",
9
+ comment: "#008000",
10
+ variable: "#000000",
11
+ constant: "#000000",
12
+ type: "#000000",
13
+ brackets1: "#0431fa",
14
+ brackets2: "#319331",
15
+ brackets3: "#7b3814",
16
+ operator: "#000000",
17
+ default: "#800000",
18
+ };
@@ -1,2 +1,2 @@
1
1
  export * from "./common";
2
- export * from "./props";
2
+ export * from "./props";
@@ -1,20 +1,49 @@
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${number}` // 關鍵字,分兩種樣式層級
28
+ | "function" // 函式名
29
+ | "string" // 字串常值:'abc'、"hello"
30
+ | "number" // 數值常量:123、3.14
31
+ | "comment" // 註解內容:// /* */
32
+ | "type" // 類型定義:type、interface、enum
33
+ | "variable" // 變數名、函式名、類別名等識別符號
34
+ | "constant" // 常數值:例如 enum 值、靜態屬性
35
+ | `brackets${number}` // 括號配對,多層不同樣式:(), [], {}
36
+ | "operator" // 運算符號:=、+、*、===、<、>=
37
+ | "default"; // 其他符號:, ; . ? ! 等
38
+
39
+ /**
40
+ * 表示可用的語法高亮主題名稱。
41
+ * 對應 `themes` 陣列中定義的名稱,例如 `"vscode-dark"`。
42
+ *
43
+ * @example
44
+ * const theme: CodeTheme = "vscode-dark";
45
+ */
46
+ export type CodeTheme = (typeof themes)[number];
18
47
 
19
48
  /**
20
49
  * 單一語法 token 的屬性,用於 <CodeToken /> 元件。
@@ -28,6 +57,11 @@ export type CodeTokenProps<T extends React.ElementType> = AsComponentProps<
28
57
  * 語法 token 的語意類型,用於指定樣式顏色。
29
58
  */
30
59
  type?: CodeTokenType;
60
+ /**
61
+ * 語法主題名稱。
62
+ * @default "vscode-dark"
63
+ */
64
+ theme?: CodeTheme;
31
65
  }
32
66
  >;
33
67
 
@@ -56,6 +90,11 @@ export type CodeLineProps<T extends React.ElementType> = OverrideProps<
56
90
  * ```
57
91
  */
58
92
  tokens: CodeTokenProps<T>[];
93
+ /**
94
+ * 語法主題名稱。
95
+ * @default "vscode-dark"
96
+ */
97
+ theme?: CodeTheme;
59
98
  }
60
99
  >;
61
100
 
@@ -97,5 +136,10 @@ export type CodeBlockProps<T extends React.ElementType> = OverrideProps<
97
136
  * ```
98
137
  * */
99
138
  lineNumberStyle?: React.CSSProperties;
139
+ /**
140
+ * 語法主題名稱。
141
+ * @default "vscode-dark"
142
+ */
143
+ theme?: CodeTheme;
100
144
  }
101
145
  >;
@@ -1,6 +1,21 @@
1
1
  import { CodeTokenBuilder, CodeTokenProps, CodeTokenType } from "../types";
2
-
3
- export const createToken = new Proxy(
2
+ /**
3
+ * 語法 token 的建構器集合,每個 key 對應一種語法類型(如 `keyword-blue`, `string`, `comment` 等),
4
+ * 透過 Proxy 生成對應的 `CodeTokenBuilder`。
5
+ *
6
+ * 使用方式:
7
+ * ```tsx
8
+ * c063.keyword1("const") // -> { type: "keyword1", children: "const" }
9
+ * c063.string("'hello'", { as: "code" }) // 可自訂 as 或其他 props
10
+ * ```
11
+ *
12
+ * @example
13
+ * tokens.push(c063.keyword1("const"));
14
+ * tokens.push(c063.string("'Hello'"));
15
+ *
16
+ * @returns 一個以 `CodeTokenType` 為 key 的建構器函式集合
17
+ */
18
+ export const c063 = new Proxy(
4
19
  {},
5
20
  {
6
21
  get: (_, prop: CodeTokenType) => {
@@ -14,12 +29,19 @@ export const createToken = new Proxy(
14
29
  ...props,
15
30
  };
16
31
  };
17
- return builder;
32
+ return builder as CodeTokenBuilder;
18
33
  },
19
34
  }
20
35
  ) as Record<CodeTokenType, CodeTokenBuilder>;
21
36
 
22
- export const whiteSpace = (count: number = 1): CodeTokenProps<"span"> => ({
23
- type: "default",
24
- children: " ".repeat(count),
25
- });
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";