arche-highlight 0.6.2 → 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.
@@ -6,12 +6,6 @@ function testOperatorSymbol(c) {
6
6
  return c === "+" || c === "-" || c === "*" || c === "/" || c === "=" || c === "&" || c === "|" || c === "<" || c === ">" || c === "~" || c === "^" || c === "!" || c === "?" || c === ":"
7
7
  }
8
8
 
9
- // testComment(term string) -> boolean
10
- function testComment(term) {
11
- return (term[0] === "/" && term[1] === "/")
12
- || (term[0] === "/" && term[1] === "*")
13
- }
14
-
15
9
  // pushWordTerm(parsedElements Array<object|string>, term string, index number, text string) -> ()
16
10
  function pushWordTerm(parsedElements, term, index, text) {
17
11
  if (term == "process" || term == "require" || term == "console" || term == "window" || term == "global" || term == "globalThis" || term == "isNaN") {
@@ -99,21 +93,21 @@ function parseJavaScript(text) {
99
93
  parsedElements.push(Span({ class: "operator" }, term))
100
94
  }
101
95
  term = ""
102
- } else if (isStringSingleQuote && c === "'") {
96
+ } else if (isStringSingleQuote && c === "'" && term[term.length - 1] !== "\\") {
103
97
  isStringSingleQuote = false
104
98
  term += c
105
99
  index += 1
106
100
  c = text[index]
107
101
  parsedElements.push(Span({ class: "string" }, term))
108
102
  term = ""
109
- } else if (isStringDoubleQuote && c === "\"") {
103
+ } else if (isStringDoubleQuote && c === "\"" && term[term.length - 1] !== "\\") {
110
104
  isStringDoubleQuote = false
111
105
  term += c
112
106
  index += 1
113
107
  c = text[index]
114
108
  parsedElements.push(Span({ class: "string" }, term))
115
109
  term = ""
116
- } else if (isTemplateLiteral && c === "`") {
110
+ } else if (isTemplateLiteral && c === "`" && term[term.length - 1] !== "\\") {
117
111
  isTemplateLiteral = false
118
112
  term += c
119
113
  index += 1
@@ -122,6 +116,10 @@ function parseJavaScript(text) {
122
116
  term = ""
123
117
  }
124
118
 
119
+ if (index == text.length) {
120
+ break
121
+ }
122
+
125
123
  if (isSingleLineComment) {
126
124
  term += c
127
125
  index += 1
@@ -157,6 +155,7 @@ function parseJavaScript(text) {
157
155
  term = ""
158
156
  index += 1
159
157
  } else {
158
+ term += templateVariable
160
159
  term += c
161
160
  index += 1
162
161
  }
@@ -187,16 +186,6 @@ function parseJavaScript(text) {
187
186
  isOperator = true
188
187
  term = c
189
188
  index += 1
190
- } else if (c === ":") {
191
- parsedElements.push(term)
192
- if (isTernaryOperator) {
193
- parsedElements.push(Span({ class: "operator" }, term))
194
- isTernaryOperator = false
195
- } else {
196
- parsedElements.push(Span({ class: "object-colon" }, c))
197
- }
198
- term = ""
199
- index += 1
200
189
  } else if (term.length == 0 && c === "'") {
201
190
  isStringSingleQuote = true
202
191
  term += c
@@ -218,7 +207,7 @@ function parseJavaScript(text) {
218
207
  parsedElements.push(c)
219
208
  term = ""
220
209
  index += 1
221
- } else if (term == "import" || term == "new" || term == "async" || term == "await" || term == "for" || term == "while" || term == "of") {
210
+ } else if (term == "import" || term == "new" || term == "async" || term == "await" || term == "for" || term == "while" || term == "of" || term == "from") {
222
211
  parsedElements.push(Span({ class: "keyword" }, term))
223
212
  parsedElements.push(c)
224
213
  term = ""
@@ -229,20 +218,13 @@ function parseJavaScript(text) {
229
218
  parsedElements.push(c)
230
219
  term = ""
231
220
  index += 1
232
- } else if (/^\d+$/.test(term)) {
233
- parsedElements.push(Span({ class: "number" }, term))
234
- parsedElements.push(c)
235
- term = ""
236
- index += 1
237
- }
238
- else if (isFunctionDeclaration) {
221
+ } else if (isFunctionDeclaration) {
239
222
  isFunctionDeclaration = false
240
223
  parsedElements.push(Span({ class: "function-name" }, term))
241
224
  parsedElements.push(c)
242
225
  term = ""
243
226
  index += 1
244
- }
245
- else {
227
+ } else {
246
228
  if (term.length > 0) {
247
229
  index = pushWordTerm(parsedElements, term, index, text)
248
230
  c = text[index]
@@ -273,8 +255,7 @@ function parseJavaScript(text) {
273
255
  parsedElements.push(Span({ class: "spread-operator" }, term))
274
256
  }
275
257
  term = ""
276
- }
277
- else if (c === "(") {
258
+ } else if (c === "(") {
278
259
  if (term === "") {
279
260
  // skip
280
261
  } else if (term == "require" || term == "isNaN") {
@@ -339,6 +320,14 @@ function parseJavaScript(text) {
339
320
  if (term.length > 0) {
340
321
  if (isSingleLineComment) {
341
322
  parsedElements.push(Span({ class: "comment" }, term))
323
+ } else if (isNumber) {
324
+ if (term[term.length - 1] === "n") {
325
+ parsedElements.push(Span({ class: "bigint" }, term))
326
+ } else if (term.includes("n")) {
327
+ parsedElements.push(term)
328
+ } else {
329
+ parsedElements.push(Span({ class: "number" }, term))
330
+ }
342
331
  } else {
343
332
  pushWordTerm(parsedElements, term, index, text)
344
333
  }
package/ArcheHighlight.js CHANGED
@@ -4,7 +4,6 @@
4
4
 
5
5
  (function (root, ArcheHighlight) {
6
6
  if (typeof module == 'object') (module.exports = ArcheHighlight) // CommonJS
7
- else if (typeof define == 'function') define(() => ArcheHighlight) // AMD
8
7
  else (root.ArcheHighlight = ArcheHighlight) // Browser
9
8
  }(typeof globalThis == 'object' ? globalThis : this, (function () { 'use strict'
10
9
 
@@ -47,6 +46,7 @@ function parseJavaScript(text) {
47
46
 
48
47
  let index = 0
49
48
  let term = ""
49
+ let isShebangInterpreterDirective = false
50
50
  let isSingleLineComment = false
51
51
  let isMultiLineComment = false
52
52
  let isNumber = false
@@ -60,7 +60,11 @@ function parseJavaScript(text) {
60
60
  while (index < text.length) {
61
61
  let c = text[index]
62
62
 
63
- if (isSingleLineComment && c === "\n") {
63
+ if (isShebangInterpreterDirective && c === "\n") {
64
+ isShebangInterpreterDirective = false
65
+ parsedElements.push(Span({ class: "shebang-interpreter-directive" }, term))
66
+ term = ""
67
+ } else if (isSingleLineComment && c === "\n") {
64
68
  isSingleLineComment = false
65
69
  parsedElements.push(Span({ class: "comment" }, term))
66
70
  term = ""
@@ -126,7 +130,10 @@ function parseJavaScript(text) {
126
130
  break
127
131
  }
128
132
 
129
- if (isSingleLineComment) {
133
+ if (isShebangInterpreterDirective) {
134
+ term += c
135
+ index += 1
136
+ } else if (isSingleLineComment) {
130
137
  term += c
131
138
  index += 1
132
139
  } else if (isMultiLineComment) {
@@ -169,6 +176,10 @@ function parseJavaScript(text) {
169
176
  term += c
170
177
  index += 1
171
178
  }
179
+ } else if (c === "#" && text[index + 1] === "!") {
180
+ isShebangInterpreterDirective = true
181
+ term += c
182
+ index += 1
172
183
  } else if (c === "/" && text[index + 1] === "/") {
173
184
  isSingleLineComment = true
174
185
  term += c
@@ -213,7 +224,7 @@ function parseJavaScript(text) {
213
224
  parsedElements.push(c)
214
225
  term = ""
215
226
  index += 1
216
- } else if (term == "import" || term == "new" || term == "async" || term == "await" || term == "for" || term == "while" || term == "of") {
227
+ } else if (term == "import" || term == "new" || term == "async" || term == "await" || term == "for" || term == "while" || term == "of" || term == "from") {
217
228
  parsedElements.push(Span({ class: "keyword" }, term))
218
229
  parsedElements.push(c)
219
230
  term = ""
@@ -326,6 +337,8 @@ function parseJavaScript(text) {
326
337
  if (term.length > 0) {
327
338
  if (isSingleLineComment) {
328
339
  parsedElements.push(Span({ class: "comment" }, term))
340
+ } else if (isShebangInterpreterDirective) {
341
+ parsedElements.push(Span({ class: "shebang-interpreter-directive" }, term))
329
342
  } else if (isNumber) {
330
343
  if (term[term.length - 1] === "n") {
331
344
  parsedElements.push(Span({ class: "bigint" }, term))
@@ -6,12 +6,6 @@ function testOperatorSymbol(c) {
6
6
  return c === "+" || c === "-" || c === "*" || c === "/" || c === "=" || c === "&" || c === "|" || c === "<" || c === ">" || c === "~" || c === "^" || c === "!" || c === "?" || c === ":"
7
7
  }
8
8
 
9
- // testComment(term string) -> boolean
10
- function testComment(term) {
11
- return (term[0] === "/" && term[1] === "/")
12
- || (term[0] === "/" && term[1] === "*")
13
- }
14
-
15
9
  // pushWordTerm(parsedElements Array<object|string>, term string, index number, text string) -> ()
16
10
  function pushWordTerm(parsedElements, term, index, text) {
17
11
  if (term == "process" || term == "require" || term == "console" || term == "window" || term == "global" || term == "globalThis" || term == "isNaN") {
@@ -99,21 +93,21 @@ function parseJavaScript(text) {
99
93
  parsedElements.push(Span({ class: "operator" }, term))
100
94
  }
101
95
  term = ""
102
- } else if (isStringSingleQuote && c === "'") {
96
+ } else if (isStringSingleQuote && c === "'" && term[term.length - 1] !== "\\") {
103
97
  isStringSingleQuote = false
104
98
  term += c
105
99
  index += 1
106
100
  c = text[index]
107
101
  parsedElements.push(Span({ class: "string" }, term))
108
102
  term = ""
109
- } else if (isStringDoubleQuote && c === "\"") {
103
+ } else if (isStringDoubleQuote && c === "\"" && term[term.length - 1] !== "\\") {
110
104
  isStringDoubleQuote = false
111
105
  term += c
112
106
  index += 1
113
107
  c = text[index]
114
108
  parsedElements.push(Span({ class: "string" }, term))
115
109
  term = ""
116
- } else if (isTemplateLiteral && c === "`") {
110
+ } else if (isTemplateLiteral && c === "`" && term[term.length - 1] !== "\\") {
117
111
  isTemplateLiteral = false
118
112
  term += c
119
113
  index += 1
@@ -122,6 +116,10 @@ function parseJavaScript(text) {
122
116
  term = ""
123
117
  }
124
118
 
119
+ if (index == text.length) {
120
+ break
121
+ }
122
+
125
123
  if (isSingleLineComment) {
126
124
  term += c
127
125
  index += 1
@@ -157,6 +155,7 @@ function parseJavaScript(text) {
157
155
  term = ""
158
156
  index += 1
159
157
  } else {
158
+ term += templateVariable
160
159
  term += c
161
160
  index += 1
162
161
  }
@@ -187,16 +186,6 @@ function parseJavaScript(text) {
187
186
  isOperator = true
188
187
  term = c
189
188
  index += 1
190
- } else if (c === ":") {
191
- parsedElements.push(term)
192
- if (isTernaryOperator) {
193
- parsedElements.push(Span({ class: "operator" }, term))
194
- isTernaryOperator = false
195
- } else {
196
- parsedElements.push(Span({ class: "object-colon" }, c))
197
- }
198
- term = ""
199
- index += 1
200
189
  } else if (term.length == 0 && c === "'") {
201
190
  isStringSingleQuote = true
202
191
  term += c
@@ -218,7 +207,7 @@ function parseJavaScript(text) {
218
207
  parsedElements.push(c)
219
208
  term = ""
220
209
  index += 1
221
- } else if (term == "import" || term == "new" || term == "async" || term == "await" || term == "for" || term == "while" || term == "of") {
210
+ } else if (term == "import" || term == "new" || term == "async" || term == "await" || term == "for" || term == "while" || term == "of" || term == "from") {
222
211
  parsedElements.push(Span({ class: "keyword" }, term))
223
212
  parsedElements.push(c)
224
213
  term = ""
@@ -229,20 +218,13 @@ function parseJavaScript(text) {
229
218
  parsedElements.push(c)
230
219
  term = ""
231
220
  index += 1
232
- } else if (/^\d+$/.test(term)) {
233
- parsedElements.push(Span({ class: "number" }, term))
234
- parsedElements.push(c)
235
- term = ""
236
- index += 1
237
- }
238
- else if (isFunctionDeclaration) {
221
+ } else if (isFunctionDeclaration) {
239
222
  isFunctionDeclaration = false
240
223
  parsedElements.push(Span({ class: "function-name" }, term))
241
224
  parsedElements.push(c)
242
225
  term = ""
243
226
  index += 1
244
- }
245
- else {
227
+ } else {
246
228
  if (term.length > 0) {
247
229
  index = pushWordTerm(parsedElements, term, index, text)
248
230
  c = text[index]
@@ -273,8 +255,7 @@ function parseJavaScript(text) {
273
255
  parsedElements.push(Span({ class: "spread-operator" }, term))
274
256
  }
275
257
  term = ""
276
- }
277
- else if (c === "(") {
258
+ } else if (c === "(") {
278
259
  if (term === "") {
279
260
  // skip
280
261
  } else if (term == "require" || term == "isNaN") {
@@ -339,6 +320,14 @@ function parseJavaScript(text) {
339
320
  if (term.length > 0) {
340
321
  if (isSingleLineComment) {
341
322
  parsedElements.push(Span({ class: "comment" }, term))
323
+ } else if (isNumber) {
324
+ if (term[term.length - 1] === "n") {
325
+ parsedElements.push(Span({ class: "bigint" }, term))
326
+ } else if (term.includes("n")) {
327
+ parsedElements.push(term)
328
+ } else {
329
+ parsedElements.push(Span({ class: "number" }, term))
330
+ }
342
331
  } else {
343
332
  pushWordTerm(parsedElements, term, index, text)
344
333
  }
package/README.md CHANGED
@@ -1,6 +1,14 @@
1
1
  # ArcheHighlight
2
2
  Syntax highlighting for [vanilla microfrontends](https://arche.gallery/). Converts JavaScript code into a React Element or HTML.
3
3
 
4
+ Source code: [GitHub](https://github.com/richytong/ArcheHighlight) |
5
+ License: [CFOSS](https://cloutsworld.com/en-us/legal/license/cfoss)
6
+
7
+ ![Node.js CI](https://github.com/richytong/ArcheHighlight/workflows/Node.js%20CI/badge.svg)
8
+ [![codecov](https://codecov.io/gh/richytong/arche-highlight/graph/badge.svg?token=1IMQYQOMDM)](https://codecov.io/gh/richytong/arche-highlight)
9
+ [![npm version](https://img.shields.io/npm/v/arche-highlight.svg?style=flat)](https://www.npmjs.com/package/arche-highlight)
10
+
11
+
4
12
  ```javascript
5
13
  const ReactElement = Arche(React)
6
14
  window.Span = ReactElement.Span
@@ -65,6 +73,7 @@ Return:
65
73
 
66
74
  ### JavaScript Classes
67
75
  The following JavaScript classes are added to the code as `span` elements of the parsed React Element or HTML:
76
+ * `shebang-interpreter-directive` - an interpreter directive at the beginning of a script starting with the shebang (`#!`) that instructs the loader of the script to execute the specified interpreter, for example `#!/usr/bin/env node`.
68
77
  * `comment` - single line (`//`) or multi-line comments (`/* */`).
69
78
  * `number` - numbers, for example `1`, `0`, `2.5`, `-1`, `100`.
70
79
  * `string` - strings, for example `"a"`, `'a'`, `"This is a string."`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arche-highlight",
3
- "version": "0.6.2",
3
+ "version": "0.7.0",
4
4
  "description": "Syntax highlighting for vanilla microfrontends",
5
5
  "author": "Richard Tong",
6
6
  "license": "CFOSS",