sugar-high 0.4.6 → 0.5.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/README.md +11 -1
- package/lib/index.mjs +30 -9
- package/package.json +7 -6
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
Super lightweight JSX syntax highlighter, around 1KB after minified and gzipped
|
|
10
10
|
|
|
11
|
-

|
|
12
12
|
|
|
13
13
|
### Usage
|
|
14
14
|
|
|
@@ -71,6 +71,16 @@ pre code {
|
|
|
71
71
|
}
|
|
72
72
|
```
|
|
73
73
|
|
|
74
|
+
#### CSS Class Names
|
|
75
|
+
|
|
76
|
+
You can use `.sh__token--<token type>` to customize the output node of each token.
|
|
77
|
+
|
|
78
|
+
```css
|
|
79
|
+
.sh__token--keyword {
|
|
80
|
+
background: #f47067;
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
74
84
|
### LICENSE
|
|
75
85
|
|
|
76
86
|
MIT
|
package/lib/index.mjs
CHANGED
|
@@ -76,7 +76,7 @@ const signs = new Set([
|
|
|
76
76
|
...jsxBrackets,
|
|
77
77
|
])
|
|
78
78
|
|
|
79
|
-
|
|
79
|
+
const types = [
|
|
80
80
|
'identifier',
|
|
81
81
|
'keyword',
|
|
82
82
|
'string',
|
|
@@ -174,9 +174,10 @@ function isRegexStart(str) {
|
|
|
174
174
|
* @param {string} code
|
|
175
175
|
* @return {Array<[number, string]>}
|
|
176
176
|
*/
|
|
177
|
-
|
|
177
|
+
function tokenize(code) {
|
|
178
178
|
let current = ''
|
|
179
179
|
let type = -1
|
|
180
|
+
/** @type {[number | null, string | null]} */
|
|
180
181
|
let last = [null, null]
|
|
181
182
|
/** @type {Array<[number, string]>} */
|
|
182
183
|
const tokens = []
|
|
@@ -197,10 +198,11 @@ export function tokenize(code) {
|
|
|
197
198
|
const inJsxTag = () => __jsxTag && !__jsxChild()
|
|
198
199
|
const inJsxLiterals = () => !__jsxTag && __jsxChild() && !__jsxExpr && __jsxStack > 0
|
|
199
200
|
|
|
200
|
-
/** @type {string |
|
|
201
|
-
let __strQuote =
|
|
201
|
+
/** @type {string | null} */
|
|
202
|
+
let __strQuote = null
|
|
202
203
|
let __strTemplateExprStack = 0
|
|
203
204
|
let __strTemplateQuoteStack = 0
|
|
205
|
+
const inStringQuotes = () => __strQuote !== null
|
|
204
206
|
const inStrTemplateLiterals = () => (__strTemplateQuoteStack > __strTemplateExprStack)
|
|
205
207
|
const inStrTemplateExpr = () => __strTemplateQuoteStack > 0 && (__strTemplateQuoteStack === __strTemplateExprStack)
|
|
206
208
|
|
|
@@ -210,6 +212,9 @@ export function tokenize(code) {
|
|
|
210
212
|
* @returns {number}
|
|
211
213
|
*/
|
|
212
214
|
function classify(token) {
|
|
215
|
+
if (inJsxTag() && inStringQuotes()) {
|
|
216
|
+
return T_STRING
|
|
217
|
+
}
|
|
213
218
|
const isJsxLiterals = inJsxLiterals()
|
|
214
219
|
if (isJsxLiterals) {
|
|
215
220
|
return T_JSX_LITERALS
|
|
@@ -224,7 +229,10 @@ export function tokenize(code) {
|
|
|
224
229
|
} else if (isCls(token)) {
|
|
225
230
|
return inJsxTag() ? T_IDENTIFIER : T_CLS_NUMBER
|
|
226
231
|
} else {
|
|
227
|
-
|
|
232
|
+
|
|
233
|
+
return isIdentifier(token) &&
|
|
234
|
+
!inStrTemplateLiterals() &&
|
|
235
|
+
(!isSingleQuotes(__strQuote)) ? T_IDENTIFIER : T_STRING
|
|
228
236
|
}
|
|
229
237
|
}
|
|
230
238
|
|
|
@@ -250,11 +258,13 @@ export function tokenize(code) {
|
|
|
250
258
|
const p_c = prev + curr // previous and current
|
|
251
259
|
const c_n = curr + next // current and next
|
|
252
260
|
|
|
253
|
-
|
|
261
|
+
// Determine string quotation outside of jsx literals.
|
|
262
|
+
// Inside jsx literals, string quotation is still part of it.
|
|
263
|
+
if (isSingleQuotes(curr) && !inJsxLiterals()) {
|
|
254
264
|
append()
|
|
255
265
|
if (prev !== `\\`) {
|
|
256
266
|
if (__strQuote && curr === __strQuote) {
|
|
257
|
-
__strQuote =
|
|
267
|
+
__strQuote = null
|
|
258
268
|
} else if (!__strQuote) {
|
|
259
269
|
__strQuote = curr
|
|
260
270
|
}
|
|
@@ -504,7 +514,7 @@ function generate(tokens) {
|
|
|
504
514
|
function flushLine(tokens) {
|
|
505
515
|
linesHtml.push(createLine(
|
|
506
516
|
tokens.map(([type, value]) => (
|
|
507
|
-
`<span style="color: var(--sh-${types[type]})">${encode(value)}</span>`
|
|
517
|
+
`<span class="sh__token--${types[type]}" style="color: var(--sh-${types[type]})">${encode(value)}</span>`
|
|
508
518
|
))
|
|
509
519
|
.join('')
|
|
510
520
|
))
|
|
@@ -545,8 +555,19 @@ function generate(tokens) {
|
|
|
545
555
|
* @param {string} code
|
|
546
556
|
* @returns {string}
|
|
547
557
|
*/
|
|
548
|
-
|
|
558
|
+
function highlight(code) {
|
|
549
559
|
const tokens = tokenize(code)
|
|
550
560
|
const output = generate(tokens).join('\n')
|
|
551
561
|
return output
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// namespace
|
|
565
|
+
const SugarHigh = {
|
|
566
|
+
TokenTypes: types,
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
export {
|
|
570
|
+
highlight,
|
|
571
|
+
tokenize,
|
|
572
|
+
SugarHigh,
|
|
552
573
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sugar-high",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": "./lib/index.mjs",
|
|
6
6
|
"description": "Super lightweight JSX syntax highlighter",
|
|
@@ -17,10 +17,11 @@
|
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
19
|
"codice": "^0.0.10",
|
|
20
|
-
"next": "canary",
|
|
21
|
-
"react": "^18.
|
|
22
|
-
"react-dom": "^18.
|
|
20
|
+
"next": "13.4.20-canary.23",
|
|
21
|
+
"react": "^18.2.0",
|
|
22
|
+
"react-dom": "^18.2.0",
|
|
23
23
|
"sugar-high": "link:./",
|
|
24
|
-
"vitest": "^0.
|
|
25
|
-
}
|
|
24
|
+
"vitest": "^0.34.4"
|
|
25
|
+
},
|
|
26
|
+
"packageManager": "pnpm@8.7.1"
|
|
26
27
|
}
|