c063 1.4.6 → 1.4.7
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/libs/index.d.ts +0 -1
- package/dist/libs/index.js +0 -1
- package/dist/types/index.d.ts +1 -2
- package/dist/utils/index.d.ts +0 -1
- package/dist/utils/index.js +0 -1
- package/package.json +1 -1
- package/src/libs/index.tsx +0 -2
- package/src/types/index.ts +1 -3
- package/src/utils/index.tsx +1 -2
- package/src/utils/parser.tsx +0 -103
package/dist/libs/index.d.ts
CHANGED
|
@@ -15,5 +15,4 @@ declare const _themeRegistry: {
|
|
|
15
15
|
};
|
|
16
16
|
export declare const themes: (keyof typeof _themeRegistry)[];
|
|
17
17
|
export declare const themeMap: Record<CodeTheme, Record<CodeTokenType, React.CSSProperties["color"]>>;
|
|
18
|
-
export declare const parsableLanguages: readonly ["javascript"];
|
|
19
18
|
export {};
|
package/dist/libs/index.js
CHANGED
package/dist/types/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { themes } from "../libs";
|
|
2
2
|
import { AsComponentProps, OverrideProps } from "./common";
|
|
3
3
|
/**
|
|
4
4
|
* 用於表示語法高亮中每個 token 的語意分類,對應於 `<CodeToken />` 中的 `type`。
|
|
@@ -114,4 +114,3 @@ export type CodeBlockProps<T extends React.ElementType> = OverrideProps<React.HT
|
|
|
114
114
|
*/
|
|
115
115
|
theme?: CodeTheme;
|
|
116
116
|
}>;
|
|
117
|
-
export type ParsableLanguage = (typeof parsableLanguages)[number];
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -29,4 +29,3 @@ declare const c063: Record<CodeTokenType, CodeTokenBuilder>;
|
|
|
29
29
|
export declare const whiteSpace: (count?: number) => CodeTokenProps<"span">;
|
|
30
30
|
export declare const extractTokenContent: <T extends React.ElementType>(token: CodeTokenProps<T>) => string;
|
|
31
31
|
export default c063;
|
|
32
|
-
export * from "./parser";
|
package/dist/utils/index.js
CHANGED
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.7",
|
|
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",
|
package/src/libs/index.tsx
CHANGED
package/src/types/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { themes } from "../libs";
|
|
2
2
|
import { AsComponentProps, OverrideProps } from "./common";
|
|
3
3
|
/**
|
|
4
4
|
* 用於表示語法高亮中每個 token 的語意分類,對應於 `<CodeToken />` 中的 `type`。
|
|
@@ -143,5 +143,3 @@ export type CodeBlockProps<T extends React.ElementType> = OverrideProps<
|
|
|
143
143
|
theme?: CodeTheme;
|
|
144
144
|
}
|
|
145
145
|
>;
|
|
146
|
-
|
|
147
|
-
export type ParsableLanguage = (typeof parsableLanguages)[number];
|
package/src/utils/index.tsx
CHANGED
package/src/utils/parser.tsx
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { parsableLanguages } from "../libs";
|
|
2
|
-
import {
|
|
3
|
-
CodeTokenProps,
|
|
4
|
-
CodeTokenType,
|
|
5
|
-
ParsableLanguage,
|
|
6
|
-
} from "../types/index";
|
|
7
|
-
|
|
8
|
-
export const isParsableLanguage = (lang: string): lang is ParsableLanguage => {
|
|
9
|
-
return parsableLanguages.includes(lang as ParsableLanguage);
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
const regexMap: Record<
|
|
13
|
-
ParsableLanguage,
|
|
14
|
-
{ type: CodeTokenType; regex: RegExp }[]
|
|
15
|
-
> = {
|
|
16
|
-
javascript: [
|
|
17
|
-
{ type: "comment", regex: /^\/\/.*/ },
|
|
18
|
-
{ type: "string", regex: /^(['"])(?:\\.|[^\\])*?\1/ },
|
|
19
|
-
{ type: "number", regex: /^\d+(\.\d+)?/ },
|
|
20
|
-
{
|
|
21
|
-
type: "keyword1",
|
|
22
|
-
regex:
|
|
23
|
-
/^(?:const|let|var|function|class|interface|type|enum|extends|implements|new|this|super)\b/,
|
|
24
|
-
},
|
|
25
|
-
{
|
|
26
|
-
type: "keyword2",
|
|
27
|
-
regex:
|
|
28
|
-
/^(?:return|import|from|for|while|as|await|async|export|if|else|switch|case|break|continue)\b/,
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
type: "type",
|
|
32
|
-
regex: /^(?:string|number|boolean|void|any|unknown|never|Record|Array)\b/,
|
|
33
|
-
},
|
|
34
|
-
{ type: "operator", regex: /^(===|!==|==|!=|<=|>=|=>|<|>|\+|-|\*|\/|=)/ },
|
|
35
|
-
{ type: "constant", regex: /^[A-Z_][A-Z0-9_]*/ },
|
|
36
|
-
{ type: "variable", regex: /^[a-zA-Z_$][a-zA-Z0-9_$]*/ },
|
|
37
|
-
{ type: "default", regex: /^[:;,.?]/ },
|
|
38
|
-
],
|
|
39
|
-
};
|
|
40
|
-
// 解析單行 CodeTokenProps
|
|
41
|
-
const _parseTokenLine = (
|
|
42
|
-
line: string,
|
|
43
|
-
lang: ParsableLanguage
|
|
44
|
-
): CodeTokenProps<"span">[] => {
|
|
45
|
-
if (!isParsableLanguage(lang)) return [];
|
|
46
|
-
|
|
47
|
-
const result: CodeTokenProps<"span">[] = [];
|
|
48
|
-
let remaining = line;
|
|
49
|
-
let bracketDepth = 0;
|
|
50
|
-
|
|
51
|
-
const bracketLevels = { "(": 1, "{": 1, "[": 1, ")": -1, "}": -1, "]": -1 };
|
|
52
|
-
const bracketRegex = /^[\[\]{}()]/;
|
|
53
|
-
const rules = regexMap[lang];
|
|
54
|
-
|
|
55
|
-
outer: while (remaining.length > 0) {
|
|
56
|
-
const whitespaceMatch = remaining.match(/^\s+/);
|
|
57
|
-
if (whitespaceMatch) {
|
|
58
|
-
result.push({ type: "default", children: whitespaceMatch[0] });
|
|
59
|
-
remaining = remaining.slice(whitespaceMatch[0].length);
|
|
60
|
-
continue outer;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// 括號處理(根據深度標記 brackets1 / 2 / 3)
|
|
64
|
-
const bracketMatch = remaining.match(bracketRegex);
|
|
65
|
-
if (bracketMatch) {
|
|
66
|
-
const bracket = bracketMatch[0];
|
|
67
|
-
bracketDepth += bracketLevels[bracket as keyof typeof bracketLevels] || 0;
|
|
68
|
-
const depth = Math.max(1, Math.min(3, Math.abs(bracketDepth)));
|
|
69
|
-
result.push({
|
|
70
|
-
type: `brackets${depth}` as CodeTokenType,
|
|
71
|
-
children: bracket,
|
|
72
|
-
});
|
|
73
|
-
remaining = remaining.slice(1);
|
|
74
|
-
continue outer;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// 用 regexMap 中的每個語法規則嘗試匹配
|
|
78
|
-
for (const { type, regex } of rules) {
|
|
79
|
-
const match = remaining.match(regex);
|
|
80
|
-
if (match) {
|
|
81
|
-
result.push({ type, children: match[0] });
|
|
82
|
-
remaining = remaining.slice(match[0].length);
|
|
83
|
-
continue outer;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// 如果沒有任何規則匹配,就當作 default 一個字元
|
|
88
|
-
result.push({ type: "default", children: remaining[0] });
|
|
89
|
-
remaining = remaining.slice(1);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return result;
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
const parseTokenLines = new Proxy(
|
|
96
|
-
{},
|
|
97
|
-
{
|
|
98
|
-
get:
|
|
99
|
-
(_, prop: ParsableLanguage) =>
|
|
100
|
-
(content: string): CodeTokenProps<"span">[][] =>
|
|
101
|
-
content.split("\n").map((line) => _parseTokenLine(line, prop)),
|
|
102
|
-
}
|
|
103
|
-
) as Record<ParsableLanguage, (content: string) => CodeTokenProps<"span">[][]>;
|