sugar-high 1.1.0 → 1.2.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 +2 -0
- package/lib/index.d.ts +2 -1
- package/lib/index.js +21 -6
- package/lib/presets/index.d.ts +2 -0
- package/lib/presets/index.js +1 -0
- package/lib/presets/lang/diff.js +11 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -33,6 +33,8 @@ import { rust } from 'sugar-high/presets'
|
|
|
33
33
|
const html = highlight(source, { ...rust })
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
+
Available presets: `c`, `css`, `diff`, `go`, `java`, `python`, and `rust`.
|
|
37
|
+
|
|
36
38
|
For more language presets and syntax color themes, see **[sugar-high.vercel.app](https://sugar-high.vercel.app/)**.
|
|
37
39
|
|
|
38
40
|
## Styling
|
package/lib/index.d.ts
CHANGED
|
@@ -11,11 +11,12 @@ type HighlightOptions = {
|
|
|
11
11
|
* or null/undefined or a number below 1 for default JS single-quoted string rules.
|
|
12
12
|
*/
|
|
13
13
|
onQuote?: (curr: string, i: number, code: string) => number | null | undefined
|
|
14
|
+
lineClassName?: (line: string, index: number) => string | null | undefined
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
export function highlight(code: string, options?: HighlightOptions): string
|
|
17
18
|
export function tokenize(code: string, options?: HighlightOptions): Array<[number, string]>
|
|
18
|
-
export function generate(tokens: Array<[number, string]>): Array<any>
|
|
19
|
+
export function generate(tokens: Array<[number, string]>, options?: Pick<HighlightOptions, 'lineClassName'>): Array<any>
|
|
19
20
|
export const SugarHigh: {
|
|
20
21
|
TokenTypes: {
|
|
21
22
|
[key: number]: string
|
package/lib/index.js
CHANGED
|
@@ -309,6 +309,7 @@ function isRegexStart(str) {
|
|
|
309
309
|
* onCommentStart?: (curr: string, next: string) => number | boolean
|
|
310
310
|
* onCommentEnd?: (prev: string, curr: string) => number | boolean
|
|
311
311
|
* onQuote?: (curr: string, i: number, code: string) => number | null | undefined
|
|
312
|
+
* lineClassName?: (line: string, index: number) => string | null | undefined
|
|
312
313
|
* } | undefined} options
|
|
313
314
|
* Optional `onQuote(curr, i, code)` at `code[i] === "'"`: return length to consume from `i` (>= 1),
|
|
314
315
|
* or null/undefined/below 1 for default JS single-quoted strings. No substring allocation.
|
|
@@ -813,29 +814,42 @@ function tokenize(code, options) {
|
|
|
813
814
|
|
|
814
815
|
/**
|
|
815
816
|
* @param {Array<[number, string]>} tokens
|
|
817
|
+
* @param {{
|
|
818
|
+
* lineClassName?: (line: string, index: number) => string | null | undefined
|
|
819
|
+
* } | undefined} options
|
|
816
820
|
* @return {Array<{type: string, tagName: string, children: any[], properties: Record<string, string>}>}
|
|
817
821
|
*/
|
|
818
|
-
function generate(tokens) {
|
|
822
|
+
function generate(tokens, options) {
|
|
819
823
|
const lines = []
|
|
824
|
+
const lineClassName = options && typeof options.lineClassName === 'function'
|
|
825
|
+
? options.lineClassName
|
|
826
|
+
: null
|
|
827
|
+
let lineIndex = 0
|
|
820
828
|
/**
|
|
821
829
|
* @param {any} children
|
|
830
|
+
* @param {string} text
|
|
822
831
|
* @return {{type: string, tagName: string, children: any[], properties: Record<string, string>}}
|
|
823
832
|
*/
|
|
824
|
-
const createLine = (children) =>
|
|
825
|
-
|
|
833
|
+
const createLine = (children, text) => {
|
|
834
|
+
const extraClassName = lineClassName ? lineClassName(text, lineIndex) : ''
|
|
835
|
+
lineIndex++
|
|
836
|
+
|
|
837
|
+
return ({
|
|
826
838
|
type: 'element',
|
|
827
839
|
tagName: 'span',
|
|
828
840
|
children,
|
|
829
841
|
properties: {
|
|
830
|
-
className: 'sh__line',
|
|
842
|
+
className: extraClassName ? `sh__line ${extraClassName}` : 'sh__line',
|
|
831
843
|
},
|
|
832
844
|
})
|
|
845
|
+
}
|
|
833
846
|
|
|
834
847
|
/**
|
|
835
848
|
* @param {Array<[number, string]>} tokens
|
|
836
849
|
* @returns {void}
|
|
837
850
|
*/
|
|
838
851
|
function flushLine(tokens) {
|
|
852
|
+
const lineText = tokens.map(([, value]) => value).join('')
|
|
839
853
|
/** @type {Array<any>} */
|
|
840
854
|
const lineTokens = (
|
|
841
855
|
tokens
|
|
@@ -855,7 +869,7 @@ function generate(tokens) {
|
|
|
855
869
|
}
|
|
856
870
|
})
|
|
857
871
|
)
|
|
858
|
-
lines.push(createLine(lineTokens))
|
|
872
|
+
lines.push(createLine(lineTokens, lineText))
|
|
859
873
|
}
|
|
860
874
|
/** @type {Array<[number, string]>} */
|
|
861
875
|
const lineTokens = []
|
|
@@ -945,13 +959,14 @@ function toHtml(lines) {
|
|
|
945
959
|
* onCommentStart?: (curr: string, next: string) => number | boolean
|
|
946
960
|
* onCommentEnd?: (curr: string, prev: string) => number | boolean
|
|
947
961
|
* onQuote?: (curr: string, i: number, code: string) => number | null | undefined
|
|
962
|
+
* lineClassName?: (line: string, index: number) => string | null | undefined
|
|
948
963
|
* } | undefined} options
|
|
949
964
|
* `onQuote` same as `tokenize`.
|
|
950
965
|
* @returns {string}
|
|
951
966
|
*/
|
|
952
967
|
function highlight(code, options) {
|
|
953
968
|
const tokens = tokenize(code, options)
|
|
954
|
-
const lines = generate(tokens)
|
|
969
|
+
const lines = generate(tokens, options)
|
|
955
970
|
const output = toHtml(lines)
|
|
956
971
|
return output
|
|
957
972
|
}
|
package/lib/presets/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ type LanguageConfig = {
|
|
|
4
4
|
onCommentStart?(curr: string, next: string): 0 | 1 | 2
|
|
5
5
|
onCommentEnd?(prev: string, curr: string): 0 | 1 | 2
|
|
6
6
|
onQuote?(curr: string, i: number, code: string): number | null | undefined
|
|
7
|
+
lineClassName?(line: string, index: number): string | null | undefined
|
|
7
8
|
}
|
|
8
9
|
|
|
9
10
|
export const css: LanguageConfig
|
|
@@ -12,3 +13,4 @@ export const python: LanguageConfig
|
|
|
12
13
|
export const c: LanguageConfig
|
|
13
14
|
export const go: LanguageConfig
|
|
14
15
|
export const java: LanguageConfig
|
|
16
|
+
export const diff: LanguageConfig
|
package/lib/presets/index.js
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
export const keywords = new Set([])
|
|
4
|
+
|
|
5
|
+
export const lineClassName = (line) => {
|
|
6
|
+
if (line.startsWith('+') && !line.startsWith('+++')) return 'sh__line--diff-add'
|
|
7
|
+
if (line.startsWith('-') && !line.startsWith('---')) return 'sh__line--diff-remove'
|
|
8
|
+
if (line.startsWith('@@')) return 'sh__line--diff-hunk'
|
|
9
|
+
if (/^(diff --git|index |--- |\+\+\+ )/.test(line)) return 'sh__line--diff-meta'
|
|
10
|
+
return undefined
|
|
11
|
+
}
|