sugar-high 0.2.2 → 0.3.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 +15 -0
- package/lib/index.mjs +40 -11
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -58,6 +58,21 @@ Then make your own theme with customized colors by token type and put in global
|
|
|
58
58
|
}
|
|
59
59
|
```
|
|
60
60
|
|
|
61
|
+
### Features
|
|
62
|
+
|
|
63
|
+
#### Line number
|
|
64
|
+
|
|
65
|
+
Sugar high provide `.sh_line` class name for each line. To display line number, define the `.sh_line::before` element with CSS will enable line numbers automatically.
|
|
66
|
+
|
|
67
|
+
```css
|
|
68
|
+
.sh__line::before {
|
|
69
|
+
content: attr(data-line-number);
|
|
70
|
+
margin-right: 24px;
|
|
71
|
+
text-align: right;
|
|
72
|
+
color: #a4a4a4;
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
61
76
|
### LICENSE
|
|
62
77
|
|
|
63
78
|
MIT
|
package/lib/index.mjs
CHANGED
|
@@ -123,8 +123,6 @@ function encode(str) {
|
|
|
123
123
|
.replace(/>/g, '>')
|
|
124
124
|
.replace(/"/g, '"')
|
|
125
125
|
.replace(/'/g, ''')
|
|
126
|
-
// matches space but not new line
|
|
127
|
-
.replace(/[^\S\r\n]/g, ' ')
|
|
128
126
|
}
|
|
129
127
|
|
|
130
128
|
function isWord(chr) {
|
|
@@ -349,16 +347,47 @@ export function tokenize(code) {
|
|
|
349
347
|
* @return {Array<string>}
|
|
350
348
|
*/
|
|
351
349
|
function generate(tokens) {
|
|
352
|
-
|
|
350
|
+
let lineNo = 1
|
|
351
|
+
const linesHtml = []
|
|
352
|
+
const createLine = (content) => `<span data-line-number="${lineNo++}" class="sh__line">${content}</span>`
|
|
353
|
+
|
|
354
|
+
function flushLine(tokens) {
|
|
355
|
+
linesHtml.push(createLine(
|
|
356
|
+
tokens.map(token => (
|
|
357
|
+
`<span class="sh__${types[token[0]]}">${encode(token[1])}</span>`
|
|
358
|
+
))
|
|
359
|
+
.join('')
|
|
360
|
+
))
|
|
361
|
+
}
|
|
362
|
+
const lineTokens = []
|
|
353
363
|
for (let i = 0; i < tokens.length; i++) {
|
|
354
|
-
const
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
364
|
+
const token = tokens[i]
|
|
365
|
+
const [type, value] = token
|
|
366
|
+
if (type !== T_BREAK) {
|
|
367
|
+
// Divide multi-line token into multi-line code
|
|
368
|
+
if (value.includes('\n')) {
|
|
369
|
+
const lines = value.split('\n')
|
|
370
|
+
for (let j = 0; j < lines.length; j++) {
|
|
371
|
+
lineTokens.push([type, lines[j]])
|
|
372
|
+
if (j < lines.length - 1) {
|
|
373
|
+
flushLine(lineTokens)
|
|
374
|
+
lineTokens.length = 0
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
} else {
|
|
378
|
+
lineTokens.push(token)
|
|
379
|
+
}
|
|
380
|
+
} else {
|
|
381
|
+
lineTokens.push([type, ''])
|
|
382
|
+
flushLine(lineTokens)
|
|
383
|
+
lineTokens.length = 0
|
|
384
|
+
}
|
|
360
385
|
}
|
|
361
|
-
|
|
386
|
+
|
|
387
|
+
if (lineTokens.length)
|
|
388
|
+
flushLine(lineTokens)
|
|
389
|
+
|
|
390
|
+
return linesHtml
|
|
362
391
|
}
|
|
363
392
|
|
|
364
393
|
/**
|
|
@@ -368,6 +397,6 @@ function generate(tokens) {
|
|
|
368
397
|
*/
|
|
369
398
|
export function highlight(code) {
|
|
370
399
|
const tokens = tokenize(code)
|
|
371
|
-
const output = generate(tokens).join('')
|
|
400
|
+
const output = generate(tokens).join('\n')
|
|
372
401
|
return output
|
|
373
402
|
}
|