sugar-high 2.3.1 → 2.4.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/README.md +33 -3
- package/lib/core.js +3 -1
- package/lib/gpu.d.ts +7 -0
- package/lib/gpu.js +85 -0
- package/lib/presets/javascript-runtime.js +35 -52
- package/package.json +14 -1
package/README.md
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
[![version][npm-version-badge]][npm]
|
|
4
4
|
[![downloads][npm-downloads-badge]][npm]
|
|
5
5
|
|
|
6
|
-
Lightweight
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
Lightweight syntax highlighting for JavaScript, popular programming languages, and formats
|
|
7
|
+
commonly generated by coding agents. It runs in browsers and JavaScript runtimes and returns HTML
|
|
8
|
+
without requiring a DOM.
|
|
9
9
|
|
|
10
10
|

|
|
11
11
|
|
|
@@ -41,6 +41,14 @@ See [benchmark methodology and options](../../docs/BENCHMARK.md).
|
|
|
41
41
|
npm install sugar-high
|
|
42
42
|
```
|
|
43
43
|
|
|
44
|
+
## Agent skill
|
|
45
|
+
|
|
46
|
+
Install Sugar High guidance for an AI coding agent with the Skills CLI:
|
|
47
|
+
|
|
48
|
+
```sh
|
|
49
|
+
npx skills add huozhi/sugar-high --skill sugar-high
|
|
50
|
+
```
|
|
51
|
+
|
|
44
52
|
## Highlight code
|
|
45
53
|
|
|
46
54
|
```js
|
|
@@ -49,6 +57,28 @@ import { highlight } from 'sugar-high'
|
|
|
49
57
|
const html = highlight('const ready = true')
|
|
50
58
|
```
|
|
51
59
|
|
|
60
|
+
## Experimental WebGPU highlighting
|
|
61
|
+
|
|
62
|
+
Use the opt-in `sugar-high/gpu` entry for asynchronous, language-agnostic highlighting powered by
|
|
63
|
+
[`gpu-lexer`](https://gpu-lexer.vercel.app). It requires WebGPU and rejects when WebGPU is
|
|
64
|
+
unavailable; importing any other Sugar High entry does not load the GPU model.
|
|
65
|
+
|
|
66
|
+
Install the experimental lexer separately when using this entry:
|
|
67
|
+
|
|
68
|
+
```sh
|
|
69
|
+
npm install sugar-high gpu-lexer
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
import { highlight, parse } from 'sugar-high/gpu'
|
|
74
|
+
|
|
75
|
+
const parsed = await parse(source)
|
|
76
|
+
const html = await highlight(source)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
GPU labels map onto Sugar High's existing token classes, themes, `cx`, `mark`, and `markLine`
|
|
80
|
+
hooks. The model infers syntax without a language option and may differ from grammar-based output.
|
|
81
|
+
|
|
52
82
|
JavaScript, including JSX, is the default. Pass a canonical name for another built-in language:
|
|
53
83
|
|
|
54
84
|
```js
|
package/lib/core.js
CHANGED
package/lib/gpu.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { DisplayOptions, ParsedCode } from './core.js'
|
|
2
|
+
|
|
3
|
+
/** Parse source code asynchronously with gpu-lexer and return Sugar High's structured format. */
|
|
4
|
+
export function parse(code: string): Promise<ParsedCode>
|
|
5
|
+
|
|
6
|
+
/** Highlight source code asynchronously with WebGPU. */
|
|
7
|
+
export function highlight(code: string, options?: DisplayOptions): Promise<string>
|
package/lib/gpu.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { parse as parseWithGpu } from 'gpu-lexer'
|
|
4
|
+
import {
|
|
5
|
+
assemble,
|
|
6
|
+
render,
|
|
7
|
+
T_CLASS,
|
|
8
|
+
T_COMMENT,
|
|
9
|
+
T_ENTITY,
|
|
10
|
+
T_IDENTIFIER,
|
|
11
|
+
T_KEYWORD,
|
|
12
|
+
T_SIGN,
|
|
13
|
+
T_SPACE,
|
|
14
|
+
T_STRING,
|
|
15
|
+
T_BREAK,
|
|
16
|
+
} from './shared.js'
|
|
17
|
+
|
|
18
|
+
const tokenTypes = Object.freeze({
|
|
19
|
+
plain: T_IDENTIFIER,
|
|
20
|
+
comment: T_COMMENT,
|
|
21
|
+
string: T_STRING,
|
|
22
|
+
number: T_CLASS,
|
|
23
|
+
keyword: T_KEYWORD,
|
|
24
|
+
type: T_CLASS,
|
|
25
|
+
function: T_ENTITY,
|
|
26
|
+
constant: T_CLASS,
|
|
27
|
+
operator: T_SIGN,
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Preserve plain whitespace as Sugar High space and break tokens while mapping
|
|
32
|
+
* the GPU lexer's semantic labels onto the existing theme vocabulary.
|
|
33
|
+
* @param {Array<[number, string]>} tokens
|
|
34
|
+
* @param {string} value
|
|
35
|
+
*/
|
|
36
|
+
function appendPlain(tokens, value) {
|
|
37
|
+
for (const part of value.match(/\r\n|\r|\n|[^\S\r\n]+|[^\s\r\n]+/g) || []) {
|
|
38
|
+
const type = part === '\n' || part === '\r' || part === '\r\n'
|
|
39
|
+
? T_BREAK
|
|
40
|
+
: /^\s/.test(part)
|
|
41
|
+
? T_SPACE
|
|
42
|
+
: T_IDENTIFIER
|
|
43
|
+
tokens.push([type, part])
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Parse source code with gpu-lexer and return Sugar High's structured format.
|
|
49
|
+
* GPU labels are adapted to the existing token and theme types.
|
|
50
|
+
* @param {string} code
|
|
51
|
+
* @returns {Promise<import('./core.js').ParsedCode>}
|
|
52
|
+
*/
|
|
53
|
+
async function parse(code) {
|
|
54
|
+
const spans = await parseWithGpu(code)
|
|
55
|
+
/** @type {Array<[number, string]>} */
|
|
56
|
+
const tokens = []
|
|
57
|
+
let cursor = 0
|
|
58
|
+
|
|
59
|
+
for (const span of spans) {
|
|
60
|
+
const start = Math.max(cursor, Math.min(code.length, span.start))
|
|
61
|
+
const end = Math.max(start, Math.min(code.length, span.end))
|
|
62
|
+
if (start > cursor) appendPlain(tokens, code.slice(cursor, start))
|
|
63
|
+
cursor = start
|
|
64
|
+
if (end === start) continue
|
|
65
|
+
|
|
66
|
+
const value = code.slice(start, end)
|
|
67
|
+
if (span.type === 'plain') appendPlain(tokens, value)
|
|
68
|
+
else tokens.push([tokenTypes[span.type] ?? T_IDENTIFIER, value])
|
|
69
|
+
cursor = end
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (cursor < code.length) appendPlain(tokens, code.slice(cursor))
|
|
73
|
+
return assemble(code, tokens)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Highlight source code asynchronously with WebGPU.
|
|
78
|
+
* @param {string} code
|
|
79
|
+
* @param {import('./core.js').DisplayOptions | undefined} options
|
|
80
|
+
*/
|
|
81
|
+
async function highlight(code, options) {
|
|
82
|
+
return render(await parse(code), options)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export { highlight, parse }
|
|
@@ -168,18 +168,11 @@ function isSign(ch) {
|
|
|
168
168
|
}
|
|
169
169
|
|
|
170
170
|
function isWord(chr) {
|
|
171
|
-
return /^[\
|
|
171
|
+
return /^[\w$\u0080-\uffff]+$/.test(chr || '')
|
|
172
172
|
}
|
|
173
173
|
|
|
174
174
|
function isCls(str) {
|
|
175
|
-
|
|
176
|
-
return isWord(chr0) &&
|
|
177
|
-
chr0 === chr0.toUpperCase() ||
|
|
178
|
-
str === 'null'
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
function hasUnicode(s) {
|
|
182
|
-
return /[^\u0000-\u007f]/.test(s);
|
|
175
|
+
return /^[0-9A-Z\p{Lu}]/u.test(str) || str === 'null'
|
|
183
176
|
}
|
|
184
177
|
|
|
185
178
|
function isAlpha(chr) {
|
|
@@ -187,11 +180,11 @@ function isAlpha(chr) {
|
|
|
187
180
|
}
|
|
188
181
|
|
|
189
182
|
function isIdentifierChar(chr) {
|
|
190
|
-
return
|
|
183
|
+
return /^[$_A-Za-z\u0080-\uffff]$/.test(chr || '')
|
|
191
184
|
}
|
|
192
185
|
|
|
193
186
|
function isIdentifier(str) {
|
|
194
|
-
return isIdentifierChar(str[0]) &&
|
|
187
|
+
return isIdentifierChar(str[0]) && isWord(str)
|
|
195
188
|
}
|
|
196
189
|
|
|
197
190
|
function isStrTemplateChr(chr) {
|
|
@@ -282,17 +275,17 @@ function tokenize(code, options) {
|
|
|
282
275
|
let __jsxEnter = false
|
|
283
276
|
/** @type {0 | 1 | 2} 0 = none; 1 = inside `<open`; 2 = inside `</close` */
|
|
284
277
|
let __jsxTag = 0
|
|
285
|
-
let
|
|
278
|
+
let __jsxExprDepth = 0
|
|
286
279
|
let __jsxTagExpr = 0
|
|
287
280
|
|
|
288
281
|
/** Nested `<open>…</open>` depth (content between tags, including nested elements). */
|
|
289
282
|
let __jsxStack = 0
|
|
290
283
|
|
|
291
|
-
const __jsxChild = () => __jsxEnter && !
|
|
284
|
+
const __jsxChild = () => __jsxEnter && !__jsxExprDepth && !__jsxTag
|
|
292
285
|
// < __content__ >
|
|
293
|
-
const inJsxTag = () => __jsxTag
|
|
286
|
+
const inJsxTag = () => __jsxTag
|
|
294
287
|
// {'__content__'}
|
|
295
|
-
const inJsxLiterals = () =>
|
|
288
|
+
const inJsxLiterals = () => __jsxChild() && __jsxStack
|
|
296
289
|
|
|
297
290
|
/** @type {string | null} */
|
|
298
291
|
let __strQuote = null
|
|
@@ -419,14 +412,12 @@ function tokenize(code, options) {
|
|
|
419
412
|
if (isSingleQuotes(curr) && !inJsxLiterals() && !inStrTemplateLiterals()) {
|
|
420
413
|
append()
|
|
421
414
|
let isStringClose = false
|
|
422
|
-
if (
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
__strTokenStart = tokens.length
|
|
429
|
-
}
|
|
415
|
+
if (__strQuote && curr === __strQuote) {
|
|
416
|
+
__strQuote = null
|
|
417
|
+
isStringClose = true
|
|
418
|
+
} else if (!__strQuote) {
|
|
419
|
+
__strQuote = curr
|
|
420
|
+
__strTokenStart = tokens.length
|
|
430
421
|
}
|
|
431
422
|
|
|
432
423
|
append(T_STRING, curr)
|
|
@@ -438,25 +429,14 @@ function tokenize(code, options) {
|
|
|
438
429
|
continue
|
|
439
430
|
}
|
|
440
431
|
|
|
441
|
-
if (
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
continue
|
|
447
|
-
}
|
|
432
|
+
if (isTemplateQuote(curr)) {
|
|
433
|
+
append()
|
|
434
|
+
__strTemplateQuoteStack += inStrTemplateLiterals() ? -1 : 1
|
|
435
|
+
append(T_STRING, curr)
|
|
436
|
+
continue
|
|
448
437
|
}
|
|
449
438
|
|
|
450
439
|
if (inStrTemplateLiterals()) {
|
|
451
|
-
if (prev !== '\\n' && isTemplateQuote(curr)) {
|
|
452
|
-
if (__strTemplateQuoteStack > 0) {
|
|
453
|
-
append()
|
|
454
|
-
__strTemplateQuoteStack--
|
|
455
|
-
append(T_STRING, curr)
|
|
456
|
-
continue
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
|
|
460
440
|
if (c_n === '${') {
|
|
461
441
|
__strTemplateExprStack++
|
|
462
442
|
append(T_STRING)
|
|
@@ -477,7 +457,7 @@ function tokenize(code, options) {
|
|
|
477
457
|
if (curr === '{') {
|
|
478
458
|
append()
|
|
479
459
|
append(T_SIGN, curr)
|
|
480
|
-
|
|
460
|
+
__jsxExprDepth = 1
|
|
481
461
|
continue
|
|
482
462
|
}
|
|
483
463
|
}
|
|
@@ -634,6 +614,8 @@ function tokenize(code, options) {
|
|
|
634
614
|
// string quotation
|
|
635
615
|
if (isQuotationChar || isStringTemplateLiterals || isSingleQuotes(__strQuote)) {
|
|
636
616
|
current += curr
|
|
617
|
+
// Consume the escaped character before the next delimiter check.
|
|
618
|
+
if (curr === '\\') current += code[++i] || ''
|
|
637
619
|
} else if (isRegexChar) {
|
|
638
620
|
append()
|
|
639
621
|
const [lastType, lastToken] = last
|
|
@@ -656,25 +638,25 @@ function tokenize(code, options) {
|
|
|
656
638
|
__regexQuoteStart = true
|
|
657
639
|
const start = i++
|
|
658
640
|
|
|
659
|
-
// end of line of end of file
|
|
660
|
-
const isEof = () => i >= code.length
|
|
661
|
-
const isEol = () => isEof() || code[i] === '\n'
|
|
662
|
-
|
|
663
641
|
let foundClose = false
|
|
664
642
|
|
|
665
643
|
// `/` is literal inside regex character classes, e.g. `[/]`.
|
|
666
644
|
let inCharClass = false
|
|
667
645
|
|
|
668
646
|
// traverse to find closing regex slash
|
|
669
|
-
for (;
|
|
647
|
+
for (; i < code.length && code[i] !== '\n'; i++) {
|
|
670
648
|
const ch = code[i]
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
649
|
+
if (ch === '\\') {
|
|
650
|
+
if (code[i + 1] === '\n') break
|
|
651
|
+
i++
|
|
652
|
+
continue
|
|
653
|
+
}
|
|
654
|
+
if (ch === '[') inCharClass = true
|
|
655
|
+
if (ch === ']') inCharClass = false
|
|
656
|
+
if (ch === '/' && !inCharClass) {
|
|
675
657
|
foundClose = true
|
|
676
658
|
// end of regex, append regex flags
|
|
677
|
-
while (
|
|
659
|
+
while (/^[a-z]$/.test(code[i + 1])) {
|
|
678
660
|
i++
|
|
679
661
|
}
|
|
680
662
|
break
|
|
@@ -730,11 +712,11 @@ function tokenize(code, options) {
|
|
|
730
712
|
append()
|
|
731
713
|
}
|
|
732
714
|
} else {
|
|
733
|
-
if (
|
|
715
|
+
if (__jsxExprDepth && curr === '}') {
|
|
734
716
|
append()
|
|
735
717
|
current = curr
|
|
736
718
|
append()
|
|
737
|
-
|
|
719
|
+
__jsxExprDepth--
|
|
738
720
|
} else if (
|
|
739
721
|
// it's jsx literals and is not a jsx bracket
|
|
740
722
|
(isJsxLiterals && !JSXBrackets.has(curr)) ||
|
|
@@ -761,6 +743,7 @@ function tokenize(code, options) {
|
|
|
761
743
|
}
|
|
762
744
|
else if (JSXBrackets.has(curr)) append()
|
|
763
745
|
}
|
|
746
|
+
if (__jsxExprDepth && curr === '{') __jsxExprDepth++
|
|
764
747
|
}
|
|
765
748
|
}
|
|
766
749
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sugar-high",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.1",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/huozhi/sugar-high.git",
|
|
@@ -18,6 +18,10 @@
|
|
|
18
18
|
"types": "./lib/core.d.ts",
|
|
19
19
|
"default": "./lib/core.js"
|
|
20
20
|
},
|
|
21
|
+
"./gpu": {
|
|
22
|
+
"types": "./lib/gpu.d.ts",
|
|
23
|
+
"default": "./lib/gpu.js"
|
|
24
|
+
},
|
|
21
25
|
"./lang": {
|
|
22
26
|
"types": "./lib/lang.d.ts",
|
|
23
27
|
"default": "./lib/lang.js"
|
|
@@ -34,9 +38,18 @@
|
|
|
34
38
|
"license": "MIT",
|
|
35
39
|
"devDependencies": {
|
|
36
40
|
"@types/node": "22.12.0",
|
|
41
|
+
"gpu-lexer": "0.0.2",
|
|
37
42
|
"typescript": "6.0.2",
|
|
38
43
|
"vitest": "^3.0.2"
|
|
39
44
|
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"gpu-lexer": ">=0.0.2"
|
|
47
|
+
},
|
|
48
|
+
"peerDependenciesMeta": {
|
|
49
|
+
"gpu-lexer": {
|
|
50
|
+
"optional": true
|
|
51
|
+
}
|
|
52
|
+
},
|
|
40
53
|
"scripts": {
|
|
41
54
|
"test": "vitest",
|
|
42
55
|
"build": "echo 'package requires no build'",
|