sugar-high 0.6.0 → 0.7.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 +1 -1
- package/lib/index.js +67 -13
- package/package.json +11 -11
package/README.md
CHANGED
|
@@ -25,7 +25,7 @@ document.querySelector('pre > code').innerHTML = codeHTML
|
|
|
25
25
|
|
|
26
26
|
### Highlight with CSS
|
|
27
27
|
|
|
28
|
-
Then make your own theme with customized colors by token type and put in global CSS. The corresponding class names
|
|
28
|
+
Then make your own theme with customized colors by token type and put in global CSS. The corresponding class names start with `--sh-` prefix.
|
|
29
29
|
|
|
30
30
|
```css
|
|
31
31
|
/**
|
package/lib/index.js
CHANGED
|
@@ -434,7 +434,7 @@ function tokenize(code) {
|
|
|
434
434
|
}
|
|
435
435
|
}
|
|
436
436
|
// `=` in property=<value>
|
|
437
|
-
if (next === '=') {
|
|
437
|
+
if (next === '=' && !inStringContent()) {
|
|
438
438
|
const prop = current ? (current + curr) : curr
|
|
439
439
|
if (isIdentifier(prop)) {
|
|
440
440
|
current = prop
|
|
@@ -578,20 +578,52 @@ function tokenize(code) {
|
|
|
578
578
|
|
|
579
579
|
/**
|
|
580
580
|
* @param {Array<[number, string]>} tokens
|
|
581
|
-
* @return {Array<
|
|
581
|
+
* @return {Array<any>}
|
|
582
582
|
*/
|
|
583
583
|
function generate(tokens) {
|
|
584
|
-
const
|
|
585
|
-
|
|
584
|
+
const lines = []
|
|
585
|
+
/**
|
|
586
|
+
* @param {any} children
|
|
587
|
+
*
|
|
588
|
+
*/
|
|
589
|
+
const createLine = (children) =>
|
|
590
|
+
// generateType === 'html'
|
|
591
|
+
// ? `<span class="sh__line">${content}</span>`
|
|
592
|
+
({
|
|
593
|
+
type: 'element',
|
|
594
|
+
tagName: 'span',
|
|
595
|
+
children,
|
|
596
|
+
properties: {
|
|
597
|
+
className: 'sh__line',
|
|
598
|
+
},
|
|
599
|
+
})
|
|
586
600
|
|
|
601
|
+
/**
|
|
602
|
+
* @param {Array<[number, string]>} tokens
|
|
603
|
+
* @returns {void}
|
|
604
|
+
*/
|
|
587
605
|
function flushLine(tokens) {
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
606
|
+
/** @type {Array<any>} */
|
|
607
|
+
const lineTokens = (
|
|
608
|
+
tokens
|
|
609
|
+
.map(([type, value]) => (
|
|
610
|
+
{
|
|
611
|
+
type: 'element',
|
|
612
|
+
tagName: 'span',
|
|
613
|
+
children: [{
|
|
614
|
+
type: 'text',
|
|
615
|
+
value: value, // to encode
|
|
616
|
+
}],
|
|
617
|
+
properties: {
|
|
618
|
+
className: `sh__token--${types[type]}`,
|
|
619
|
+
style: `color: var(--sh-${types[type]})`,
|
|
620
|
+
},
|
|
621
|
+
}
|
|
622
|
+
))
|
|
623
|
+
)
|
|
624
|
+
lines.push(createLine(lineTokens))
|
|
594
625
|
}
|
|
626
|
+
/** @type {Array<[number, string]>} */
|
|
595
627
|
const lineTokens = []
|
|
596
628
|
for (let i = 0; i < tokens.length; i++) {
|
|
597
629
|
const token = tokens[i]
|
|
@@ -620,7 +652,26 @@ function generate(tokens) {
|
|
|
620
652
|
if (lineTokens.length)
|
|
621
653
|
flushLine(lineTokens)
|
|
622
654
|
|
|
623
|
-
return
|
|
655
|
+
return lines
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
function toHtml(lines) {
|
|
659
|
+
return lines
|
|
660
|
+
.map(line => {
|
|
661
|
+
const { tagName: lineTag } = line
|
|
662
|
+
const tokens = line.children
|
|
663
|
+
.map(child => {
|
|
664
|
+
const { tagName, children, properties } = child
|
|
665
|
+
return `<${tagName} class="${
|
|
666
|
+
properties.className
|
|
667
|
+
}" style="${
|
|
668
|
+
properties.style
|
|
669
|
+
}">${encode(children[0].value)}</${tagName}>`
|
|
670
|
+
})
|
|
671
|
+
.join('')
|
|
672
|
+
return `<${lineTag} class="${line.properties.className}">${tokens}</${lineTag}>`
|
|
673
|
+
})
|
|
674
|
+
.join('\n')
|
|
624
675
|
}
|
|
625
676
|
|
|
626
677
|
/**
|
|
@@ -630,17 +681,20 @@ function generate(tokens) {
|
|
|
630
681
|
*/
|
|
631
682
|
function highlight(code) {
|
|
632
683
|
const tokens = tokenize(code)
|
|
633
|
-
const
|
|
684
|
+
const lines = generate(tokens)
|
|
685
|
+
const output = toHtml(lines)
|
|
634
686
|
return output
|
|
635
687
|
}
|
|
636
688
|
|
|
637
689
|
// namespace
|
|
638
690
|
const SugarHigh = /** @type {const} */ {
|
|
639
691
|
TokenTypes: types,
|
|
692
|
+
TokenMap: new Map(types.map((type, i) => [type, i])),
|
|
640
693
|
}
|
|
641
694
|
|
|
642
695
|
export {
|
|
643
696
|
highlight,
|
|
644
697
|
tokenize,
|
|
698
|
+
generate,
|
|
645
699
|
SugarHigh,
|
|
646
|
-
}
|
|
700
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sugar-high",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"types": "./lib/index.d.ts",
|
|
6
6
|
"exports": {
|
|
@@ -12,18 +12,18 @@
|
|
|
12
12
|
"lib"
|
|
13
13
|
],
|
|
14
14
|
"license": "MIT",
|
|
15
|
+
"scripts": {
|
|
16
|
+
"test": "vitest run",
|
|
17
|
+
"dev": "next dev docs",
|
|
18
|
+
"build": "next build docs"
|
|
19
|
+
},
|
|
15
20
|
"devDependencies": {
|
|
16
21
|
"codice": "^0.2.0",
|
|
17
|
-
"next": "14.
|
|
18
|
-
"react": "^18.
|
|
19
|
-
"react-dom": "^18.
|
|
22
|
+
"next": "14.2.3",
|
|
23
|
+
"react": "^18.3.1",
|
|
24
|
+
"react-dom": "^18.3.1",
|
|
20
25
|
"sugar-high": "link:./",
|
|
21
26
|
"vitest": "^1.2.2"
|
|
22
27
|
},
|
|
23
|
-
"packageManager": "pnpm@8.7.1"
|
|
24
|
-
|
|
25
|
-
"test": "vitest run",
|
|
26
|
-
"dev": "next dev docs",
|
|
27
|
-
"build": "next build docs"
|
|
28
|
-
}
|
|
29
|
-
}
|
|
28
|
+
"packageManager": "pnpm@8.7.1"
|
|
29
|
+
}
|