c063 1.5.0 → 1.6.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.
- package/dist/components/CodeBlock.d.ts +1 -1
- package/dist/components/CodeBlock.js +10 -9
- package/dist/components/CodeLine.d.ts +2 -1
- package/dist/components/CodeLine.js +3 -2
- package/dist/types/index.d.ts +10 -0
- package/package.json +1 -1
- package/src/components/CodeBlock.tsx +29 -22
- package/src/components/CodeLine.tsx +4 -2
- package/src/types/index.tsx +12 -0
|
@@ -11,6 +11,6 @@ import { CodeBlockProps } from "../types/index";
|
|
|
11
11
|
* @returns JSX 元素,呈現語法高亮的程式碼區塊
|
|
12
12
|
*/
|
|
13
13
|
export declare const CodeBlock: {
|
|
14
|
-
<T extends React.ElementType = "span">({ tokenLines, showLineNumbers, lineNumberStyle, theme, ...rest }: CodeBlockProps<T>): import("react/jsx-runtime").JSX.Element;
|
|
14
|
+
<T extends React.ElementType = "span">({ tokenLines, showLineNumbers, lineNumberStyle, autoWrap, theme, ...rest }: CodeBlockProps<T>): import("react/jsx-runtime").JSX.Element;
|
|
15
15
|
displayName: string;
|
|
16
16
|
};
|
|
@@ -11,14 +11,15 @@ import { CodeLine } from "./CodeLine";
|
|
|
11
11
|
* @param rest 其他傳遞給 <pre> 的屬性
|
|
12
12
|
* @returns JSX 元素,呈現語法高亮的程式碼區塊
|
|
13
13
|
*/
|
|
14
|
-
export const CodeBlock = ({ tokenLines, showLineNumbers = true, lineNumberStyle, theme, ...rest }) => {
|
|
15
|
-
return (_jsx("pre", { ...rest, children: tokenLines.map((line, index) => (
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
14
|
+
export const CodeBlock = ({ tokenLines, showLineNumbers = true, lineNumberStyle, autoWrap, theme, ...rest }) => {
|
|
15
|
+
return (_jsx("pre", { ...rest, style: { margin: 0, padding: 0, overflowX: "auto" }, children: _jsx("table", { style: { borderCollapse: "collapse", width: "100%" }, children: _jsx("tbody", { children: tokenLines.map((line, index) => (_jsxs("tr", { style: { verticalAlign: "top" }, children: [showLineNumbers && (_jsx("td", { style: {
|
|
16
|
+
paddingInline: "0.5rem",
|
|
17
|
+
textAlign: "right",
|
|
18
|
+
whiteSpace: "pre",
|
|
19
|
+
fontVariantNumeric: "tabular-nums",
|
|
20
|
+
color: "#888",
|
|
21
|
+
userSelect: "none",
|
|
22
|
+
...lineNumberStyle,
|
|
23
|
+
}, children: index + 1 })), _jsx("td", { style: { width: "100%" }, children: _jsx(CodeLine, { theme: theme, tokens: line, autoWrap: autoWrap }) })] }, index))) }) }) }));
|
|
23
24
|
};
|
|
24
25
|
CodeBlock.displayName = "CodeBlock";
|
|
@@ -6,10 +6,11 @@ import { CodeLineProps } from "../types/index";
|
|
|
6
6
|
* @param props.tokens 該行所包含的語法 token 陣列
|
|
7
7
|
* @param props.style 自訂樣式,會與 whiteSpace: pre-wrap 合併
|
|
8
8
|
* @param props.theme 主題
|
|
9
|
+
* @param prop.autoWrap 是否自動換行
|
|
9
10
|
* @param rest 其他 HTMLAttributes
|
|
10
11
|
* @returns JSX 元素,呈現語法 token 的單行程式碼
|
|
11
12
|
*/
|
|
12
13
|
export declare const CodeLine: {
|
|
13
|
-
<T extends React.ElementType = "span">({ style, tokens, theme, ...rest }: CodeLineProps<T>): import("react/jsx-runtime").JSX.Element;
|
|
14
|
+
<T extends React.ElementType = "span">({ style, tokens, theme, autoWrap, ...rest }: CodeLineProps<T>): import("react/jsx-runtime").JSX.Element;
|
|
14
15
|
displayName: string;
|
|
15
16
|
};
|
|
@@ -7,12 +7,13 @@ import { CodeToken } from "./CodeToken";
|
|
|
7
7
|
* @param props.tokens 該行所包含的語法 token 陣列
|
|
8
8
|
* @param props.style 自訂樣式,會與 whiteSpace: pre-wrap 合併
|
|
9
9
|
* @param props.theme 主題
|
|
10
|
+
* @param prop.autoWrap 是否自動換行
|
|
10
11
|
* @param rest 其他 HTMLAttributes
|
|
11
12
|
* @returns JSX 元素,呈現語法 token 的單行程式碼
|
|
12
13
|
*/
|
|
13
|
-
export const CodeLine = ({ style, tokens, theme, ...rest }) => {
|
|
14
|
+
export const CodeLine = ({ style, tokens, theme, autoWrap = true, ...rest }) => {
|
|
14
15
|
return (_jsx("code", { ...rest, style: {
|
|
15
|
-
whiteSpace: "pre-wrap",
|
|
16
|
+
whiteSpace: autoWrap ? "pre-wrap" : "nowrap",
|
|
16
17
|
...style,
|
|
17
18
|
}, children: tokens.map((token, index) => (_jsx(CodeToken, { theme: theme, ...token }, index))) }));
|
|
18
19
|
};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -71,6 +71,11 @@ export type CodeLineProps<T extends React.ElementType> = OverrideProps<React.HTM
|
|
|
71
71
|
* @default "vscode-dark"
|
|
72
72
|
*/
|
|
73
73
|
theme?: CodeTheme;
|
|
74
|
+
/**
|
|
75
|
+
* 是否自動換行
|
|
76
|
+
* @default true
|
|
77
|
+
*/
|
|
78
|
+
autoWrap?: boolean;
|
|
74
79
|
}>;
|
|
75
80
|
export type CodeBlockProps<T extends React.ElementType> = OverrideProps<React.HTMLAttributes<HTMLPreElement>, {
|
|
76
81
|
/**
|
|
@@ -112,5 +117,10 @@ export type CodeBlockProps<T extends React.ElementType> = OverrideProps<React.HT
|
|
|
112
117
|
* @default "default-dark-modern"
|
|
113
118
|
*/
|
|
114
119
|
theme?: CodeTheme;
|
|
120
|
+
/**
|
|
121
|
+
* 是否自動換行
|
|
122
|
+
* @default true
|
|
123
|
+
*/
|
|
124
|
+
autoWrap?: boolean;
|
|
115
125
|
}>;
|
|
116
126
|
export type ParsableLanguage = (typeof parsableLanguages)[number];
|
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
|
+
"version": "1.6.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,5 +1,6 @@
|
|
|
1
1
|
import { CodeBlockProps } from "../types/index";
|
|
2
2
|
import { CodeLine } from "./CodeLine";
|
|
3
|
+
|
|
3
4
|
/**
|
|
4
5
|
* 顯示完整程式碼區塊,支援多行語法 token 與行號顯示。
|
|
5
6
|
*
|
|
@@ -15,32 +16,38 @@ export const CodeBlock = <T extends React.ElementType = "span">({
|
|
|
15
16
|
tokenLines,
|
|
16
17
|
showLineNumbers = true,
|
|
17
18
|
lineNumberStyle,
|
|
19
|
+
autoWrap,
|
|
18
20
|
theme,
|
|
19
21
|
...rest
|
|
20
22
|
}: CodeBlockProps<T>) => {
|
|
21
23
|
return (
|
|
22
|
-
<pre {...rest}>
|
|
23
|
-
{
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
24
|
+
<pre {...rest} style={{ margin: 0, padding: 0, overflowX: "auto" }}>
|
|
25
|
+
<table style={{ borderCollapse: "collapse", width: "100%" }}>
|
|
26
|
+
<tbody>
|
|
27
|
+
{tokenLines.map((line, index) => (
|
|
28
|
+
<tr key={index} style={{ verticalAlign: "top" }}>
|
|
29
|
+
{showLineNumbers && (
|
|
30
|
+
<td
|
|
31
|
+
style={{
|
|
32
|
+
paddingInline: "0.5rem",
|
|
33
|
+
textAlign: "right",
|
|
34
|
+
whiteSpace: "pre",
|
|
35
|
+
fontVariantNumeric: "tabular-nums",
|
|
36
|
+
color: "#888",
|
|
37
|
+
userSelect: "none",
|
|
38
|
+
...lineNumberStyle,
|
|
39
|
+
}}
|
|
40
|
+
>
|
|
41
|
+
{index + 1}
|
|
42
|
+
</td>
|
|
43
|
+
)}
|
|
44
|
+
<td style={{ width: "100%" }}>
|
|
45
|
+
<CodeLine theme={theme} tokens={line} autoWrap={autoWrap} />
|
|
46
|
+
</td>
|
|
47
|
+
</tr>
|
|
48
|
+
))}
|
|
49
|
+
</tbody>
|
|
50
|
+
</table>
|
|
44
51
|
</pre>
|
|
45
52
|
);
|
|
46
53
|
};
|
|
@@ -6,7 +6,8 @@ import { CodeToken } from "./CodeToken";
|
|
|
6
6
|
* @template T 元件渲染類型,例如 <code>、<span> 等
|
|
7
7
|
* @param props.tokens 該行所包含的語法 token 陣列
|
|
8
8
|
* @param props.style 自訂樣式,會與 whiteSpace: pre-wrap 合併
|
|
9
|
-
* @param props.theme 主題
|
|
9
|
+
* @param props.theme 主題
|
|
10
|
+
* @param prop.autoWrap 是否自動換行
|
|
10
11
|
* @param rest 其他 HTMLAttributes
|
|
11
12
|
* @returns JSX 元素,呈現語法 token 的單行程式碼
|
|
12
13
|
*/
|
|
@@ -14,13 +15,14 @@ export const CodeLine = <T extends React.ElementType = "span">({
|
|
|
14
15
|
style,
|
|
15
16
|
tokens,
|
|
16
17
|
theme,
|
|
18
|
+
autoWrap = true,
|
|
17
19
|
...rest
|
|
18
20
|
}: CodeLineProps<T>) => {
|
|
19
21
|
return (
|
|
20
22
|
<code
|
|
21
23
|
{...rest}
|
|
22
24
|
style={{
|
|
23
|
-
whiteSpace: "pre-wrap",
|
|
25
|
+
whiteSpace: autoWrap ? "pre-wrap" : "nowrap",
|
|
24
26
|
...style,
|
|
25
27
|
}}
|
|
26
28
|
>
|
package/src/types/index.tsx
CHANGED
|
@@ -94,6 +94,12 @@ export type CodeLineProps<T extends React.ElementType> = OverrideProps<
|
|
|
94
94
|
* @default "vscode-dark"
|
|
95
95
|
*/
|
|
96
96
|
theme?: CodeTheme;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 是否自動換行
|
|
100
|
+
* @default true
|
|
101
|
+
*/
|
|
102
|
+
autoWrap?: boolean;
|
|
97
103
|
}
|
|
98
104
|
>;
|
|
99
105
|
|
|
@@ -140,6 +146,12 @@ export type CodeBlockProps<T extends React.ElementType> = OverrideProps<
|
|
|
140
146
|
* @default "default-dark-modern"
|
|
141
147
|
*/
|
|
142
148
|
theme?: CodeTheme;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* 是否自動換行
|
|
152
|
+
* @default true
|
|
153
|
+
*/
|
|
154
|
+
autoWrap?: boolean;
|
|
143
155
|
}
|
|
144
156
|
>;
|
|
145
157
|
|