sugar-high 2.2.0 → 2.2.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/core.js +1 -6
- package/lib/shared.js +80 -31
- package/package.json +3 -2
package/lib/core.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
|
-
assemble, generate,
|
|
4
|
+
assemble, generate, render, SugarHigh, T_BREAK, T_CLASS, T_COMMENT, T_IDENTIFIER,
|
|
5
5
|
T_KEYWORD, T_PROPERTY, T_SIGN, T_SPACE, T_STRING,
|
|
6
6
|
} from './shared.js'
|
|
7
7
|
|
|
@@ -147,11 +147,6 @@ function parse(code, options) {
|
|
|
147
147
|
return parsed
|
|
148
148
|
}
|
|
149
149
|
|
|
150
|
-
/** @param {ParsedCode} parsed @param {DisplayOptions | undefined} options */
|
|
151
|
-
function render(parsed, options) {
|
|
152
|
-
return toHtml(generate(parsed, options))
|
|
153
|
-
}
|
|
154
|
-
|
|
155
150
|
export { generate, parse, render, SugarHigh, tokenize }
|
|
156
151
|
|
|
157
152
|
/**
|
package/lib/shared.js
CHANGED
|
@@ -64,6 +64,42 @@ function assemble(value, tokens) {
|
|
|
64
64
|
return { value, lines }
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
/**
|
|
68
|
+
* @param {import('./core.js').ParsedLine} parsedLine
|
|
69
|
+
* @param {import('./core.js').DisplayOptions['markLine']} markLine
|
|
70
|
+
*/
|
|
71
|
+
function createLine(parsedLine, markLine) {
|
|
72
|
+
const line = {
|
|
73
|
+
index: parsedLine.index,
|
|
74
|
+
value: parsedLine.value,
|
|
75
|
+
tokens: parsedLine.tokens,
|
|
76
|
+
annotations: parsedLine.annotations,
|
|
77
|
+
className: `sh__line${parsedLine.annotations.map(annotation => ` sh__line--${annotation}`).join('')}`,
|
|
78
|
+
style: {},
|
|
79
|
+
properties: {},
|
|
80
|
+
}
|
|
81
|
+
markLine?.(line)
|
|
82
|
+
return line
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* @param {import('./core.js').ParsedToken} parsedToken
|
|
87
|
+
* @param {import('./core.js').DisplayOptions['cx']} cx
|
|
88
|
+
* @param {import('./core.js').DisplayOptions['mark']} mark
|
|
89
|
+
*/
|
|
90
|
+
function createToken({ type, value }, cx, mark) {
|
|
91
|
+
const extraClassName = cx?.[type]
|
|
92
|
+
const token = {
|
|
93
|
+
type,
|
|
94
|
+
value,
|
|
95
|
+
className: `sh__token--${type}${extraClassName ? ` ${extraClassName}` : ''}`,
|
|
96
|
+
style: { color: `var(--sh-${type})` },
|
|
97
|
+
properties: {},
|
|
98
|
+
}
|
|
99
|
+
mark?.(token)
|
|
100
|
+
return token
|
|
101
|
+
}
|
|
102
|
+
|
|
67
103
|
/**
|
|
68
104
|
* @param {import('./core.js').ParsedCode} parsed
|
|
69
105
|
* @param {import('./core.js').DisplayOptions | undefined} options
|
|
@@ -74,30 +110,13 @@ function generate(parsed, options) {
|
|
|
74
110
|
const markLine = options?.markLine
|
|
75
111
|
|
|
76
112
|
return parsed.lines.map((parsedLine) => {
|
|
77
|
-
const line =
|
|
78
|
-
index: parsedLine.index,
|
|
79
|
-
value: parsedLine.value,
|
|
80
|
-
tokens: parsedLine.tokens,
|
|
81
|
-
annotations: parsedLine.annotations,
|
|
82
|
-
className: `sh__line${parsedLine.annotations.map(annotation => ` sh__line--${annotation}`).join('')}`,
|
|
83
|
-
style: {},
|
|
84
|
-
properties: {},
|
|
85
|
-
}
|
|
86
|
-
markLine?.(line)
|
|
113
|
+
const line = createLine(parsedLine, markLine)
|
|
87
114
|
|
|
88
115
|
return {
|
|
89
116
|
type: 'element',
|
|
90
117
|
tagName: 'span',
|
|
91
|
-
children: parsedLine.tokens.map((
|
|
92
|
-
const
|
|
93
|
-
const token = {
|
|
94
|
-
type,
|
|
95
|
-
value,
|
|
96
|
-
className: `sh__token--${type}${extraClassName ? ` ${extraClassName}` : ''}`,
|
|
97
|
-
style: { color: `var(--sh-${type})` },
|
|
98
|
-
properties: {},
|
|
99
|
-
}
|
|
100
|
-
mark?.(token)
|
|
118
|
+
children: parsedLine.tokens.map((parsedToken) => {
|
|
119
|
+
const token = createToken(parsedToken, cx, mark)
|
|
101
120
|
return {
|
|
102
121
|
type: 'element',
|
|
103
122
|
tokenType: token.type,
|
|
@@ -119,6 +138,46 @@ function generate(parsed, options) {
|
|
|
119
138
|
})
|
|
120
139
|
}
|
|
121
140
|
|
|
141
|
+
/**
|
|
142
|
+
* Render parsed lines directly while preserving generate()'s markup and mutable display hooks.
|
|
143
|
+
* @param {import('./core.js').ParsedCode} parsed
|
|
144
|
+
* @param {import('./core.js').DisplayOptions | undefined} options
|
|
145
|
+
*/
|
|
146
|
+
function render(parsed, options) {
|
|
147
|
+
const cx = options?.cx
|
|
148
|
+
const mark = options?.mark
|
|
149
|
+
const markLine = options?.markLine
|
|
150
|
+
|
|
151
|
+
if (!cx && !mark && !markLine) {
|
|
152
|
+
return parsed.lines.map((line) => {
|
|
153
|
+
const className = `sh__line${line.annotations.map(annotation => ` sh__line--${annotation}`).join('')}`
|
|
154
|
+
const children = line.tokens.map(({ type, value }) => (
|
|
155
|
+
`<span class="sh__token--${type}" style="color:var(--sh-${type})">${encode(value)}</span>`
|
|
156
|
+
)).join('')
|
|
157
|
+
return `<span class="${encode(className)}">${children}</span>`
|
|
158
|
+
}).join('\n')
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return parsed.lines.map((parsedLine) => {
|
|
162
|
+
const line = createLine(parsedLine, markLine)
|
|
163
|
+
|
|
164
|
+
const children = parsedLine.tokens.map((parsedToken) => {
|
|
165
|
+
const token = createToken(parsedToken, cx, mark)
|
|
166
|
+
return `<span ${attributes({
|
|
167
|
+
...token.properties,
|
|
168
|
+
className: token.className,
|
|
169
|
+
style: token.style,
|
|
170
|
+
})}>${encode(token.value)}</span>`
|
|
171
|
+
}).join('')
|
|
172
|
+
|
|
173
|
+
return `<span ${attributes({
|
|
174
|
+
...line.properties,
|
|
175
|
+
className: line.className,
|
|
176
|
+
style: line.style,
|
|
177
|
+
})}>${children}</span>`
|
|
178
|
+
}).join('\n')
|
|
179
|
+
}
|
|
180
|
+
|
|
122
181
|
const entities = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }
|
|
123
182
|
/** @param {string} value */
|
|
124
183
|
const encode = (value) => value.replace(/[&<>"']/g, character => entities[character])
|
|
@@ -133,17 +192,7 @@ function attributes(values) {
|
|
|
133
192
|
return `class="${encode(values.className || '')}"${style ? ` style="${encode(style)}"` : ''}${properties ? ` ${properties}` : ''}`
|
|
134
193
|
}
|
|
135
194
|
|
|
136
|
-
/** @param {Array<any>} lines */
|
|
137
|
-
function toHtml(lines) {
|
|
138
|
-
return lines.map(line => {
|
|
139
|
-
const children = line.children.map(token => {
|
|
140
|
-
return `<${token.tagName} ${attributes(token.properties)}>${encode(token.children[0].value)}</${token.tagName}>`
|
|
141
|
-
}).join('')
|
|
142
|
-
return `<${line.tagName} ${attributes(line.properties)}>${children}</${line.tagName}>`
|
|
143
|
-
}).join('\n')
|
|
144
|
-
}
|
|
145
|
-
|
|
146
195
|
export {
|
|
147
|
-
assemble, encode, generate,
|
|
196
|
+
assemble, encode, generate, render, SugarHigh, TokenTypes, T_BREAK, T_CLASS, T_COMMENT, T_ENTITY, T_IDENTIFIER,
|
|
148
197
|
T_JSX_LITERALS, T_KEYWORD, T_PROPERTY, T_SIGN, T_SPACE, T_STRING,
|
|
149
198
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sugar-high",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.1",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/huozhi/sugar-high.git",
|
|
@@ -40,6 +40,7 @@
|
|
|
40
40
|
"scripts": {
|
|
41
41
|
"test": "vitest",
|
|
42
42
|
"build": "echo 'package requires no build'",
|
|
43
|
-
"benchmark": "node scripts/benchmark.mjs"
|
|
43
|
+
"benchmark": "node scripts/benchmark.mjs",
|
|
44
|
+
"benchmark:large": "node scripts/benchmark-large.mjs"
|
|
44
45
|
}
|
|
45
46
|
}
|