c063 1.4.5 → 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/index.d.ts +2 -2
- package/dist/index.js +2 -2
- 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/parser.d.ts +2 -1
- package/dist/utils/parser.js +1 -2
- package/package.json +1 -1
- package/src/index.ts +4 -4
- package/src/libs/index.tsx +0 -2
- package/src/types/index.ts +1 -3
- package/src/utils/index.tsx +3 -1
- package/src/utils/parser.tsx +0 -104
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
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/parser.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { ParsableLanguage } from "../types/index";
|
|
2
|
+
export declare const isParsableLanguage: (lang: string) => lang is ParsableLanguage;
|
package/dist/utils/parser.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { parsableLanguages } from "../libs";
|
|
2
|
-
const isParsableLanguage = (lang) => {
|
|
2
|
+
export const isParsableLanguage = (lang) => {
|
|
3
3
|
return parsableLanguages.includes(lang);
|
|
4
4
|
};
|
|
5
5
|
const regexMap = {
|
|
@@ -25,7 +25,6 @@ const regexMap = {
|
|
|
25
25
|
{ type: "default", regex: /^[:;,.?]/ },
|
|
26
26
|
],
|
|
27
27
|
};
|
|
28
|
-
const bracketLevels = { "(": 1, "{": 1, "[": 1, ")": -1, "}": -1, "]": -1 };
|
|
29
28
|
// 解析單行 CodeTokenProps
|
|
30
29
|
const _parseTokenLine = (line, lang) => {
|
|
31
30
|
if (!isParsableLanguage(lang))
|
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/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import c063 from "./utils";
|
|
2
|
+
|
|
1
3
|
export * from "./types";
|
|
2
4
|
export * from "./components";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export default c063;
|
|
6
|
-
export { whiteSpace, extractTokenContent };
|
|
5
|
+
export * from "./utils";
|
|
6
|
+
export default c063
|
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
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
import { CodeTokenBuilder, CodeTokenProps, CodeTokenType } from "../types";
|
|
3
|
+
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
6
|
* 語法 token 的建構器集合,每個 key 對應一種語法類型(如 `keyword-blue`, `string`, `comment` 等),
|
|
5
7
|
* 透過 Proxy 生成對應的 `CodeTokenBuilder`。
|
|
@@ -62,4 +64,4 @@ export const extractTokenContent = <T extends React.ElementType>(
|
|
|
62
64
|
return _extract(token.children);
|
|
63
65
|
};
|
|
64
66
|
|
|
65
|
-
export default c063;
|
|
67
|
+
export default c063;
|
package/src/utils/parser.tsx
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import { parsableLanguages } from "../libs";
|
|
2
|
-
import {
|
|
3
|
-
CodeTokenProps,
|
|
4
|
-
CodeTokenType,
|
|
5
|
-
ParsableLanguage,
|
|
6
|
-
} from "../types/index";
|
|
7
|
-
|
|
8
|
-
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
|
-
const bracketLevels = { "(": 1, "{": 1, "[": 1, ")": -1, "}": -1, "]": -1 };
|
|
41
|
-
// 解析單行 CodeTokenProps
|
|
42
|
-
const _parseTokenLine = (
|
|
43
|
-
line: string,
|
|
44
|
-
lang: ParsableLanguage
|
|
45
|
-
): CodeTokenProps<"span">[] => {
|
|
46
|
-
if (!isParsableLanguage(lang)) return [];
|
|
47
|
-
|
|
48
|
-
const result: CodeTokenProps<"span">[] = [];
|
|
49
|
-
let remaining = line;
|
|
50
|
-
let bracketDepth = 0;
|
|
51
|
-
|
|
52
|
-
const bracketLevels = { "(": 1, "{": 1, "[": 1, ")": -1, "}": -1, "]": -1 };
|
|
53
|
-
const bracketRegex = /^[\[\]{}()]/;
|
|
54
|
-
const rules = regexMap[lang];
|
|
55
|
-
|
|
56
|
-
outer: while (remaining.length > 0) {
|
|
57
|
-
const whitespaceMatch = remaining.match(/^\s+/);
|
|
58
|
-
if (whitespaceMatch) {
|
|
59
|
-
result.push({ type: "default", children: whitespaceMatch[0] });
|
|
60
|
-
remaining = remaining.slice(whitespaceMatch[0].length);
|
|
61
|
-
continue outer;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// 括號處理(根據深度標記 brackets1 / 2 / 3)
|
|
65
|
-
const bracketMatch = remaining.match(bracketRegex);
|
|
66
|
-
if (bracketMatch) {
|
|
67
|
-
const bracket = bracketMatch[0];
|
|
68
|
-
bracketDepth += bracketLevels[bracket as keyof typeof bracketLevels] || 0;
|
|
69
|
-
const depth = Math.max(1, Math.min(3, Math.abs(bracketDepth)));
|
|
70
|
-
result.push({
|
|
71
|
-
type: `brackets${depth}` as CodeTokenType,
|
|
72
|
-
children: bracket,
|
|
73
|
-
});
|
|
74
|
-
remaining = remaining.slice(1);
|
|
75
|
-
continue outer;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// 用 regexMap 中的每個語法規則嘗試匹配
|
|
79
|
-
for (const { type, regex } of rules) {
|
|
80
|
-
const match = remaining.match(regex);
|
|
81
|
-
if (match) {
|
|
82
|
-
result.push({ type, children: match[0] });
|
|
83
|
-
remaining = remaining.slice(match[0].length);
|
|
84
|
-
continue outer;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// 如果沒有任何規則匹配,就當作 default 一個字元
|
|
89
|
-
result.push({ type: "default", children: remaining[0] });
|
|
90
|
-
remaining = remaining.slice(1);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return result;
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
const parseTokenLines = new Proxy(
|
|
97
|
-
{},
|
|
98
|
-
{
|
|
99
|
-
get:
|
|
100
|
-
(_, prop: ParsableLanguage) =>
|
|
101
|
-
(content: string): CodeTokenProps<"span">[][] =>
|
|
102
|
-
content.split("\n").map((line) => _parseTokenLine(line, prop)),
|
|
103
|
-
}
|
|
104
|
-
) as Record<ParsableLanguage, (content: string) => CodeTokenProps<"span">[][]>;
|