c063 1.0.0 → 1.1.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.
@@ -1,6 +1,6 @@
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, lineNumberStyle, ...rest }) => {
3
+ export const CodeBlock = ({ tokenLines, showLineNumbers = true, lineNumberStyle, ...rest }) => {
4
4
  return (_jsx("pre", { ...rest, children: tokenLines.map((line, index) => (_jsxs("div", { style: {
5
5
  display: "flex",
6
6
  flexWrap: "nowrap",
@@ -1,16 +1,18 @@
1
- import { AsComponentProps, OverrideProps } from "./common";
2
- export type CodeTokenType = "keyword-blue" | "keyword-purple" | "string" | "number" | "comment" | "type" | "variable" | "constant" | `brackets-${1 | 2 | 3}` | "operator" | "default";
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";
3
4
  /**
4
5
  * 單一語法 token 的屬性,用於 <CodeToken /> 元件。
5
6
  *
6
7
  * @template T HTML 或客製元素,例如 span、a、Link 等
7
8
  */
8
- export type CodeTokenProps<T extends React.ElementType = React.ElementType> = AsComponentProps<T, {
9
+ export type CodeTokenProps<T extends React.ElementType = React.ElementType<React.HTMLAttributes<HTMLElement>>> = AsComponentProps<T, {
9
10
  /**
10
11
  * 語法 token 的語意類型,用於指定樣式顏色。
11
12
  */
12
13
  type?: CodeTokenType;
13
14
  }>;
15
+ export type CodeTokenBuilder = (children: CodeTokenProps["children"], props: DistributiveOmit<CodeTokenProps, "type">) => CodeTokenProps;
14
16
  /**
15
17
  * 用於單一程式碼行的屬性,用在 <CodeLine /> 或類似元件中。
16
18
  */
@@ -53,7 +55,7 @@ export type CodeBlockProps = OverrideProps<React.HTMLAttributes<HTMLPreElement>,
53
55
  tokenLines: CodeTokenProps[][];
54
56
  /**
55
57
  * 是否顯示行號。
56
- * @default false
58
+ * @default true
57
59
  */
58
60
  showLineNumbers?: boolean;
59
61
  /**
@@ -1 +1,3 @@
1
- export {};
1
+ import { CodeTokenBuilder, CodeTokenProps, CodeTokenType } from "../types";
2
+ export declare const createToken: Record<CodeTokenType, CodeTokenBuilder>;
3
+ export declare const whiteSpace: (count?: number) => CodeTokenProps;
@@ -1 +1,14 @@
1
- export {};
1
+ export const createToken = new Proxy({}, {
2
+ get: (_, prop) => {
3
+ const builder = (children, props) => ({
4
+ type: prop,
5
+ children,
6
+ ...props,
7
+ });
8
+ return builder;
9
+ },
10
+ });
11
+ export const whiteSpace = (count = 1) => ({
12
+ type: "default",
13
+ children: " ".repeat(count),
14
+ });
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.0.0",
4
+ "version": "1.1.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",
@@ -3,7 +3,7 @@ import { CodeLine } from "./CodeLine";
3
3
 
4
4
  export const CodeBlock = ({
5
5
  tokenLines,
6
- showLineNumbers,
6
+ showLineNumbers = true,
7
7
  lineNumberStyle,
8
8
  ...rest
9
9
  }: CodeBlockProps) => {
@@ -1,8 +1,11 @@
1
- import { AsComponentProps, OverrideProps } from "./common";
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
2
6
 
3
7
  export type CodeTokenType =
4
- | "keyword-blue" // const, let, function, if, else class type
5
- | "keyword-purple" // import, export, from, as ,return
8
+ | `keyword-${KeywordColorType}`
6
9
  | "string" // string
7
10
  | "number" // number
8
11
  | "comment" // comment ex: //, /* */, /** */
@@ -18,16 +21,24 @@ export type CodeTokenType =
18
21
  *
19
22
  * @template T HTML 或客製元素,例如 span、a、Link 等
20
23
  */
21
- export type CodeTokenProps<T extends React.ElementType = React.ElementType> =
22
- AsComponentProps<
23
- T,
24
- {
25
- /**
26
- * 語法 token 的語意類型,用於指定樣式顏色。
27
- */
28
- type?: CodeTokenType;
29
- }
30
- >;
24
+ export type CodeTokenProps<
25
+ T extends React.ElementType = React.ElementType<
26
+ React.HTMLAttributes<HTMLElement>
27
+ >
28
+ > = AsComponentProps<
29
+ T,
30
+ {
31
+ /**
32
+ * 語法 token 的語意類型,用於指定樣式顏色。
33
+ */
34
+ type?: CodeTokenType;
35
+ }
36
+ >;
37
+
38
+ export type CodeTokenBuilder = (
39
+ children: CodeTokenProps["children"],
40
+ props: DistributiveOmit<CodeTokenProps, "type">
41
+ ) => CodeTokenProps;
31
42
 
32
43
  /**
33
44
  * 用於單一程式碼行的屬性,用在 <CodeLine /> 或類似元件中。
@@ -78,7 +89,7 @@ export type CodeBlockProps = OverrideProps<
78
89
 
79
90
  /**
80
91
  * 是否顯示行號。
81
- * @default false
92
+ * @default true
82
93
  */
83
94
  showLineNumbers?: boolean;
84
95
  /**
@@ -1 +1,20 @@
1
- export {}
1
+ import { CodeTokenBuilder, CodeTokenProps, CodeTokenType } from "../types";
2
+
3
+ export const createToken = new Proxy(
4
+ {},
5
+ {
6
+ get: (_, prop: CodeTokenType) => {
7
+ const builder: CodeTokenBuilder = (children, props?) => ({
8
+ type: prop,
9
+ children,
10
+ ...props,
11
+ });
12
+ return builder;
13
+ },
14
+ }
15
+ ) as Record<CodeTokenType, CodeTokenBuilder>;
16
+
17
+ export const whiteSpace = (count: number = 1): CodeTokenProps => ({
18
+ type: "default",
19
+ children: " ".repeat(count),
20
+ });