sugar-high 0.4.5 → 0.4.7

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.
Files changed (3) hide show
  1. package/README.md +19 -1
  2. package/lib/index.mjs +23 -8
  3. package/package.json +7 -6
package/README.md CHANGED
@@ -1,9 +1,14 @@
1
1
  # Sugar High
2
+
3
+ [![Build][build-badge]][build]
4
+ [![Coverage][coverage-badge]][coverage]
5
+ [![Downloads][downloads-badge]][downloads]
6
+
2
7
  ### Introduction
3
8
 
4
9
  Super lightweight JSX syntax highlighter, around 1KB after minified and gzipped
5
10
 
6
- ![img](https://repository-images.githubusercontent.com/453236442/1d63a6ff-aa11-422c-a36c-ca16fc102f18)
11
+ ![img](https://repository-images.githubusercontent.com/453236442/aa0db684-bad3-4cd3-a420-f4e53b8c6757)
7
12
 
8
13
  ### Usage
9
14
 
@@ -70,3 +75,16 @@ pre code {
70
75
 
71
76
  MIT
72
77
 
78
+ <!-- Definitions -->
79
+
80
+ [build-badge]: https://github.com/huozhi/sugar-high/workflows/Test/badge.svg
81
+
82
+ [build]: https://github.com/huozhi/sugar-high/actions
83
+
84
+ [coverage-badge]: https://badge.fury.io/js/sugar-high.svg
85
+
86
+ [coverage]: https://codecov.io/github/huozhi/sugar-high
87
+
88
+ [downloads-badge]: https://img.shields.io/npm/dm/sugar-high.svg
89
+
90
+ [downloads]: https://www.npmjs.com/package/sugar-high
package/lib/index.mjs CHANGED
@@ -3,6 +3,7 @@
3
3
  const jsxBrackets = new Set(['<', '>', '{', '}', '[', ']'])
4
4
  const keywords = new Set([
5
5
  'for',
6
+ 'do',
6
7
  'while',
7
8
  'if',
8
9
  'else',
@@ -52,7 +53,6 @@ const signs = new Set([
52
53
  '%',
53
54
  '=',
54
55
  '!',
55
- ...jsxBrackets,
56
56
  '&',
57
57
  '|',
58
58
  '^',
@@ -73,6 +73,7 @@ const signs = new Set([
73
73
  '#',
74
74
  '@',
75
75
  '\\',
76
+ ...jsxBrackets,
76
77
  ])
77
78
 
78
79
  export const types = [
@@ -126,7 +127,7 @@ function encode(str) {
126
127
  }
127
128
 
128
129
  function isWord(chr) {
129
- return /^[\w_]+$/.test(chr)
130
+ return /^[\w_]+$/.test(chr) || hasUnicode(chr)
130
131
  }
131
132
 
132
133
  function isCls(str) {
@@ -136,8 +137,12 @@ function isCls(str) {
136
137
  str === 'null'
137
138
  }
138
139
 
140
+ function hasUnicode(s) {
141
+ return /[^\u0000-\u007f]/.test(s);
142
+ }
143
+
139
144
  function isIdentifierChar(chr) {
140
- return /^[a-zA-Z_$]$/.test(chr)
145
+ return /^[a-zA-Z_$]$/.test(chr) || hasUnicode(chr)
141
146
  }
142
147
 
143
148
  function isIdentifier(str) {
@@ -172,6 +177,7 @@ function isRegexStart(str) {
172
177
  export function tokenize(code) {
173
178
  let current = ''
174
179
  let type = -1
180
+ /** @type {[number | null, string | null]} */
175
181
  let last = [null, null]
176
182
  /** @type {Array<[number, string]>} */
177
183
  const tokens = []
@@ -192,10 +198,11 @@ export function tokenize(code) {
192
198
  const inJsxTag = () => __jsxTag && !__jsxChild()
193
199
  const inJsxLiterals = () => !__jsxTag && __jsxChild() && !__jsxExpr && __jsxStack > 0
194
200
 
195
- /** @type {string | 0} */
196
- let __strQuote = 0
201
+ /** @type {string | null} */
202
+ let __strQuote = null
197
203
  let __strTemplateExprStack = 0
198
204
  let __strTemplateQuoteStack = 0
205
+ const inStringQuotes = () => __strQuote !== null
199
206
  const inStrTemplateLiterals = () => (__strTemplateQuoteStack > __strTemplateExprStack)
200
207
  const inStrTemplateExpr = () => __strTemplateQuoteStack > 0 && (__strTemplateQuoteStack === __strTemplateExprStack)
201
208
 
@@ -205,6 +212,9 @@ export function tokenize(code) {
205
212
  * @returns {number}
206
213
  */
207
214
  function classify(token) {
215
+ if (inJsxTag() && inStringQuotes()) {
216
+ return T_STRING
217
+ }
208
218
  const isJsxLiterals = inJsxLiterals()
209
219
  if (isJsxLiterals) {
210
220
  return T_JSX_LITERALS
@@ -219,7 +229,10 @@ export function tokenize(code) {
219
229
  } else if (isCls(token)) {
220
230
  return inJsxTag() ? T_IDENTIFIER : T_CLS_NUMBER
221
231
  } else {
222
- return isIdentifier(token) && !inStrTemplateLiterals() && (__strQuote !== `"` && __strQuote !== `'`) ? T_IDENTIFIER : T_STRING
232
+
233
+ return isIdentifier(token) &&
234
+ !inStrTemplateLiterals() &&
235
+ (!isSingleQuotes(__strQuote)) ? T_IDENTIFIER : T_STRING
223
236
  }
224
237
  }
225
238
 
@@ -245,11 +258,13 @@ export function tokenize(code) {
245
258
  const p_c = prev + curr // previous and current
246
259
  const c_n = curr + next // current and next
247
260
 
248
- if (isSingleQuotes(curr)) {
261
+ // Determine string quotation outside of jsx literals.
262
+ // Inside jsx literals, string quotation is still part of it.
263
+ if (isSingleQuotes(curr) && !inJsxLiterals()) {
249
264
  append()
250
265
  if (prev !== `\\`) {
251
266
  if (__strQuote && curr === __strQuote) {
252
- __strQuote = 0
267
+ __strQuote = null
253
268
  } else if (!__strQuote) {
254
269
  __strQuote = curr
255
270
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sugar-high",
3
- "version": "0.4.5",
3
+ "version": "0.4.7",
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": "12.1.6-canary.6",
21
- "react": "18.0.0",
22
- "react-dom": "18.0.0",
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.6.0"
25
- }
24
+ "vitest": "^0.34.4"
25
+ },
26
+ "packageManager": "pnpm@8.7.1"
26
27
  }