sugar-high 0.7.5 → 0.8.0
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 +13 -2
- package/lib/index.js +33 -22
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
type HighlightOptions = {
|
|
2
|
+
keywords?: Set<string>
|
|
3
|
+
onCommentStart?: (curr: string, next: string) => boolean
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export function highlight(code: string, options?: HighlightOptions): string
|
|
7
|
+
export function tokenize(code: string, options?: HighlightOptions): Array<[number, string]>
|
|
3
8
|
export function generate(tokens: Array<[number, string]>): Array<any>
|
|
9
|
+
export const SugarHigh: {
|
|
10
|
+
TokenTypes: {
|
|
11
|
+
[key: number]: string
|
|
12
|
+
}
|
|
13
|
+
TokenMap: Map<string, number>
|
|
14
|
+
}
|
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,13 @@ 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
|
+
}
|
|
79
83
|
|
|
80
84
|
/**
|
|
81
85
|
*
|
|
@@ -92,7 +96,7 @@ const signs = new Set([
|
|
|
92
96
|
* 10 - space
|
|
93
97
|
*
|
|
94
98
|
*/
|
|
95
|
-
const
|
|
99
|
+
const TokenTypes = /** @type {const} */ ([
|
|
96
100
|
'identifier',
|
|
97
101
|
'keyword',
|
|
98
102
|
'string',
|
|
@@ -117,14 +121,14 @@ const [
|
|
|
117
121
|
T_COMMENT,
|
|
118
122
|
T_BREAK,
|
|
119
123
|
T_SPACE,
|
|
120
|
-
] = /** @types {const} */
|
|
124
|
+
] = /** @types {const} */ TokenTypes.map((_, i) => i)
|
|
121
125
|
|
|
122
126
|
function isSpaces(str) {
|
|
123
127
|
return /^[^\S\r\n]+$/g.test(str)
|
|
124
128
|
}
|
|
125
129
|
|
|
126
130
|
function isSign(ch) {
|
|
127
|
-
return
|
|
131
|
+
return Signs.has(ch)
|
|
128
132
|
}
|
|
129
133
|
|
|
130
134
|
function encode(str) {
|
|
@@ -175,20 +179,26 @@ function isStringQuotation(chr) {
|
|
|
175
179
|
return isSingleQuotes(chr) || isStrTemplateChr(chr)
|
|
176
180
|
}
|
|
177
181
|
|
|
178
|
-
function
|
|
179
|
-
str =
|
|
182
|
+
function isCommentStart_Js(curr, next) {
|
|
183
|
+
const str = curr + next
|
|
180
184
|
return str === '//' || str === '/*'
|
|
181
185
|
}
|
|
182
186
|
|
|
183
187
|
function isRegexStart(str) {
|
|
184
|
-
return str[0] === '/' && !
|
|
188
|
+
return str[0] === '/' && !isCommentStart_Js(str[0], str[1])
|
|
185
189
|
}
|
|
186
190
|
|
|
187
191
|
/**
|
|
188
192
|
* @param {string} code
|
|
193
|
+
* @param {{ keywords: Set<string> }} options
|
|
189
194
|
* @return {Array<[number, string]>}
|
|
190
195
|
*/
|
|
191
|
-
function tokenize(code) {
|
|
196
|
+
function tokenize(code, options) {
|
|
197
|
+
const {
|
|
198
|
+
keywords,
|
|
199
|
+
onCommentStart,
|
|
200
|
+
} = { ...DefaultOptions, ...options }
|
|
201
|
+
|
|
192
202
|
let current = ''
|
|
193
203
|
let type = -1
|
|
194
204
|
/** @type {[number, string]} */
|
|
@@ -284,7 +294,7 @@ function tokenize(code) {
|
|
|
284
294
|
*/
|
|
285
295
|
const append = (type_, token_) => {
|
|
286
296
|
if (type_ || token_) {
|
|
287
|
-
const nType =
|
|
297
|
+
const nType = TokenTypes[type_]
|
|
288
298
|
}
|
|
289
299
|
if (token_) {
|
|
290
300
|
current = token_
|
|
@@ -536,7 +546,7 @@ function tokenize(code) {
|
|
|
536
546
|
append()
|
|
537
547
|
i = start
|
|
538
548
|
}
|
|
539
|
-
} else if (
|
|
549
|
+
} else if (onCommentStart(curr, next)) {
|
|
540
550
|
append()
|
|
541
551
|
const start = i
|
|
542
552
|
if (next === '/') {
|
|
@@ -571,9 +581,9 @@ function tokenize(code) {
|
|
|
571
581
|
__jsxExpr = false
|
|
572
582
|
} else if (
|
|
573
583
|
// it's jsx literals and is not a jsx bracket
|
|
574
|
-
(isJsxLiterals && !
|
|
584
|
+
(isJsxLiterals && !JSXBrackets.has(curr)) ||
|
|
575
585
|
// same type char as previous one in current token
|
|
576
|
-
((isWord(curr) === isWord(current[current.length - 1]) || __jsxChild()) && !
|
|
586
|
+
((isWord(curr) === isWord(current[current.length - 1]) || __jsxChild()) && !Signs.has(curr))
|
|
577
587
|
) {
|
|
578
588
|
current += curr
|
|
579
589
|
} else {
|
|
@@ -591,7 +601,7 @@ function tokenize(code) {
|
|
|
591
601
|
append()
|
|
592
602
|
i++
|
|
593
603
|
}
|
|
594
|
-
else if (
|
|
604
|
+
else if (JSXBrackets.has(curr)) append()
|
|
595
605
|
}
|
|
596
606
|
}
|
|
597
607
|
}
|
|
@@ -638,8 +648,8 @@ function generate(tokens) {
|
|
|
638
648
|
value: value, // to encode
|
|
639
649
|
}],
|
|
640
650
|
properties: {
|
|
641
|
-
className: `sh__token--${
|
|
642
|
-
style: `color: var(--sh-${
|
|
651
|
+
className: `sh__token--${TokenTypes[type]}`,
|
|
652
|
+
style: `color: var(--sh-${TokenTypes[type]})`,
|
|
643
653
|
},
|
|
644
654
|
}
|
|
645
655
|
))
|
|
@@ -700,10 +710,11 @@ function toHtml(lines) {
|
|
|
700
710
|
/**
|
|
701
711
|
*
|
|
702
712
|
* @param {string} code
|
|
713
|
+
* @param {{ keywords: Set<string> } | undefined} options
|
|
703
714
|
* @returns {string}
|
|
704
715
|
*/
|
|
705
|
-
function highlight(code) {
|
|
706
|
-
const tokens = tokenize(code)
|
|
716
|
+
function highlight(code, options) {
|
|
717
|
+
const tokens = tokenize(code, options)
|
|
707
718
|
const lines = generate(tokens)
|
|
708
719
|
const output = toHtml(lines)
|
|
709
720
|
return output
|
|
@@ -711,8 +722,8 @@ function highlight(code) {
|
|
|
711
722
|
|
|
712
723
|
// namespace
|
|
713
724
|
const SugarHigh = /** @type {const} */ {
|
|
714
|
-
TokenTypes
|
|
715
|
-
TokenMap: new Map(
|
|
725
|
+
TokenTypes,
|
|
726
|
+
TokenMap: new Map(TokenTypes.map((type, i) => [type, i])),
|
|
716
727
|
}
|
|
717
728
|
|
|
718
729
|
export {
|