sugar-high 0.7.5 → 0.8.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/lib/index.d.ts +14 -2
- package/lib/index.js +46 -26
- package/package.json +4 -4
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
type HighlightOptions = {
|
|
2
|
+
keywords?: Set<string>
|
|
3
|
+
onCommentStart?: (curr: string, next: string) => number | boolean
|
|
4
|
+
onCommentEnd?: (curr: string, prev: string) => number | boolean
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function highlight(code: string, options?: HighlightOptions): string
|
|
8
|
+
export function tokenize(code: string, options?: HighlightOptions): Array<[number, string]>
|
|
3
9
|
export function generate(tokens: Array<[number, string]>): Array<any>
|
|
10
|
+
export const SugarHigh: {
|
|
11
|
+
TokenTypes: {
|
|
12
|
+
[key: number]: string
|
|
13
|
+
}
|
|
14
|
+
TokenMap: Map<string, number>
|
|
15
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const
|
|
3
|
+
const JSXBrackets = new Set(['<', '>', '{', '}', '[', ']'])
|
|
4
|
+
const Keywords_Js = new Set([
|
|
5
5
|
'for',
|
|
6
6
|
'do',
|
|
7
7
|
'while',
|
|
@@ -45,7 +45,7 @@ const keywords = new Set([
|
|
|
45
45
|
'static',
|
|
46
46
|
])
|
|
47
47
|
|
|
48
|
-
const
|
|
48
|
+
const Signs = new Set([
|
|
49
49
|
'+',
|
|
50
50
|
'-',
|
|
51
51
|
'*',
|
|
@@ -73,9 +73,14 @@ const signs = new Set([
|
|
|
73
73
|
'#',
|
|
74
74
|
'@',
|
|
75
75
|
'\\',
|
|
76
|
-
...
|
|
76
|
+
...JSXBrackets,
|
|
77
77
|
])
|
|
78
78
|
|
|
79
|
+
const DefaultOptions = {
|
|
80
|
+
keywords: Keywords_Js,
|
|
81
|
+
onCommentStart: isCommentStart_Js,
|
|
82
|
+
onCommentEnd: isCommentEnd_Js,
|
|
83
|
+
}
|
|
79
84
|
|
|
80
85
|
/**
|
|
81
86
|
*
|
|
@@ -92,7 +97,7 @@ const signs = new Set([
|
|
|
92
97
|
* 10 - space
|
|
93
98
|
*
|
|
94
99
|
*/
|
|
95
|
-
const
|
|
100
|
+
const TokenTypes = /** @type {const} */ ([
|
|
96
101
|
'identifier',
|
|
97
102
|
'keyword',
|
|
98
103
|
'string',
|
|
@@ -117,14 +122,14 @@ const [
|
|
|
117
122
|
T_COMMENT,
|
|
118
123
|
T_BREAK,
|
|
119
124
|
T_SPACE,
|
|
120
|
-
] = /** @types {const} */
|
|
125
|
+
] = /** @types {const} */ TokenTypes.map((_, i) => i)
|
|
121
126
|
|
|
122
127
|
function isSpaces(str) {
|
|
123
128
|
return /^[^\S\r\n]+$/g.test(str)
|
|
124
129
|
}
|
|
125
130
|
|
|
126
131
|
function isSign(ch) {
|
|
127
|
-
return
|
|
132
|
+
return Signs.has(ch)
|
|
128
133
|
}
|
|
129
134
|
|
|
130
135
|
function encode(str) {
|
|
@@ -175,20 +180,31 @@ function isStringQuotation(chr) {
|
|
|
175
180
|
return isSingleQuotes(chr) || isStrTemplateChr(chr)
|
|
176
181
|
}
|
|
177
182
|
|
|
178
|
-
function
|
|
179
|
-
str =
|
|
183
|
+
function isCommentStart_Js(curr, next) {
|
|
184
|
+
const str = curr + next
|
|
180
185
|
return str === '//' || str === '/*'
|
|
181
186
|
}
|
|
182
187
|
|
|
188
|
+
function isCommentEnd_Js(prev, curr) {
|
|
189
|
+
return curr === '\n' || (prev + curr) === '*/'
|
|
190
|
+
}
|
|
191
|
+
|
|
183
192
|
function isRegexStart(str) {
|
|
184
|
-
return str[0] === '/' && !
|
|
193
|
+
return str[0] === '/' && !isCommentStart_Js(str[0], str[1])
|
|
185
194
|
}
|
|
186
195
|
|
|
187
196
|
/**
|
|
188
197
|
* @param {string} code
|
|
198
|
+
* @param {{ keywords: Set<string> }} options
|
|
189
199
|
* @return {Array<[number, string]>}
|
|
190
200
|
*/
|
|
191
|
-
function tokenize(code) {
|
|
201
|
+
function tokenize(code, options) {
|
|
202
|
+
const {
|
|
203
|
+
keywords,
|
|
204
|
+
onCommentStart,
|
|
205
|
+
onCommentEnd,
|
|
206
|
+
} = { ...DefaultOptions, ...options }
|
|
207
|
+
|
|
192
208
|
let current = ''
|
|
193
209
|
let type = -1
|
|
194
210
|
/** @type {[number, string]} */
|
|
@@ -284,7 +300,7 @@ function tokenize(code) {
|
|
|
284
300
|
*/
|
|
285
301
|
const append = (type_, token_) => {
|
|
286
302
|
if (type_ || token_) {
|
|
287
|
-
const nType =
|
|
303
|
+
const nType = TokenTypes[type_]
|
|
288
304
|
}
|
|
289
305
|
if (token_) {
|
|
290
306
|
current = token_
|
|
@@ -536,13 +552,16 @@ function tokenize(code) {
|
|
|
536
552
|
append()
|
|
537
553
|
i = start
|
|
538
554
|
}
|
|
539
|
-
} else if (
|
|
555
|
+
} else if (onCommentStart(curr, next)) {
|
|
540
556
|
append()
|
|
541
557
|
const start = i
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
558
|
+
const commentType = onCommentStart(curr, next)
|
|
559
|
+
|
|
560
|
+
// just match the comment, commentType === true
|
|
561
|
+
// inline comment, commentType === 1
|
|
562
|
+
// block comment, commentType === 2
|
|
563
|
+
if (commentType) {
|
|
564
|
+
for (; i < code.length && !onCommentEnd(code[i - 1], code[i]); i++);
|
|
546
565
|
}
|
|
547
566
|
current = code.slice(start, i + 1)
|
|
548
567
|
append(T_COMMENT)
|
|
@@ -571,9 +590,9 @@ function tokenize(code) {
|
|
|
571
590
|
__jsxExpr = false
|
|
572
591
|
} else if (
|
|
573
592
|
// it's jsx literals and is not a jsx bracket
|
|
574
|
-
(isJsxLiterals && !
|
|
593
|
+
(isJsxLiterals && !JSXBrackets.has(curr)) ||
|
|
575
594
|
// same type char as previous one in current token
|
|
576
|
-
((isWord(curr) === isWord(current[current.length - 1]) || __jsxChild()) && !
|
|
595
|
+
((isWord(curr) === isWord(current[current.length - 1]) || __jsxChild()) && !Signs.has(curr))
|
|
577
596
|
) {
|
|
578
597
|
current += curr
|
|
579
598
|
} else {
|
|
@@ -591,7 +610,7 @@ function tokenize(code) {
|
|
|
591
610
|
append()
|
|
592
611
|
i++
|
|
593
612
|
}
|
|
594
|
-
else if (
|
|
613
|
+
else if (JSXBrackets.has(curr)) append()
|
|
595
614
|
}
|
|
596
615
|
}
|
|
597
616
|
}
|
|
@@ -638,8 +657,8 @@ function generate(tokens) {
|
|
|
638
657
|
value: value, // to encode
|
|
639
658
|
}],
|
|
640
659
|
properties: {
|
|
641
|
-
className: `sh__token--${
|
|
642
|
-
style: `color: var(--sh-${
|
|
660
|
+
className: `sh__token--${TokenTypes[type]}`,
|
|
661
|
+
style: `color: var(--sh-${TokenTypes[type]})`,
|
|
643
662
|
},
|
|
644
663
|
}
|
|
645
664
|
))
|
|
@@ -700,10 +719,11 @@ function toHtml(lines) {
|
|
|
700
719
|
/**
|
|
701
720
|
*
|
|
702
721
|
* @param {string} code
|
|
722
|
+
* @param {{ keywords: Set<string> } | undefined} options
|
|
703
723
|
* @returns {string}
|
|
704
724
|
*/
|
|
705
|
-
function highlight(code) {
|
|
706
|
-
const tokens = tokenize(code)
|
|
725
|
+
function highlight(code, options) {
|
|
726
|
+
const tokens = tokenize(code, options)
|
|
707
727
|
const lines = generate(tokens)
|
|
708
728
|
const output = toHtml(lines)
|
|
709
729
|
return output
|
|
@@ -711,8 +731,8 @@ function highlight(code) {
|
|
|
711
731
|
|
|
712
732
|
// namespace
|
|
713
733
|
const SugarHigh = /** @type {const} */ {
|
|
714
|
-
TokenTypes
|
|
715
|
-
TokenMap: new Map(
|
|
734
|
+
TokenTypes,
|
|
735
|
+
TokenMap: new Map(TokenTypes.map((type, i) => [type, i])),
|
|
716
736
|
}
|
|
717
737
|
|
|
718
738
|
export {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sugar-high",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"types": "./lib/index.d.ts",
|
|
6
6
|
"exports": {
|
|
@@ -19,9 +19,9 @@
|
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"codice": "^0.2.0",
|
|
22
|
-
"next": "15.
|
|
23
|
-
"react": "^
|
|
24
|
-
"react-dom": "^
|
|
22
|
+
"next": "15.1.3",
|
|
23
|
+
"react": "^19.0.0",
|
|
24
|
+
"react-dom": "^19.0.0",
|
|
25
25
|
"sugar-high": "link:./",
|
|
26
26
|
"vitest": "^2.1.3"
|
|
27
27
|
},
|