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.
@@ -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 {};
@@ -28,4 +28,3 @@ const _themeRegistry = {
28
28
  export const themes = Object.keys(_themeRegistry);
29
29
  // themeMap 保留給外部使用
30
30
  export const themeMap = _themeRegistry;
31
- export const parsableLanguages = ["javascript"];
@@ -1,4 +1,4 @@
1
- import { parsableLanguages, themes } from "../libs";
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];
@@ -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";
@@ -53,4 +53,3 @@ export const extractTokenContent = (token) => {
53
53
  return _extract(token.children);
54
54
  };
55
55
  export default c063;
56
- export * from "./parser";
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.6",
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",
@@ -37,5 +37,3 @@ export const themeMap: Record<
37
37
  CodeTheme,
38
38
  Record<CodeTokenType, React.CSSProperties["color"]>
39
39
  > = _themeRegistry;
40
-
41
- export const parsableLanguages = ["javascript"] as const;
@@ -1,4 +1,4 @@
1
- import { parsableLanguages, themes } from "../libs";
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];
@@ -64,5 +64,4 @@ export const extractTokenContent = <T extends React.ElementType>(
64
64
  return _extract(token.children);
65
65
  };
66
66
 
67
- export default c063;
68
- export * from "./parser"
67
+ export default c063;
@@ -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">[][]>;