rn-shiki 0.0.37-19 → 0.0.37-20
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +1 -1
- package/src/utils/style-transformer.ts +72 -22
package/package.json
CHANGED
@@ -7,37 +7,72 @@ export interface HighlighterStyleSheet {
|
|
7
7
|
|
8
8
|
export type ReactStyle = Record<string, TextStyle>
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
// Helper function to convert hex color to RGB
|
11
|
+
function hexToRgb(hex: string): { r: number, g: number, b: number } | null {
|
12
|
+
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
|
13
|
+
return result
|
14
|
+
? {
|
15
|
+
r: Number.parseInt(result[1]!, 16),
|
16
|
+
g: Number.parseInt(result[2]!, 16),
|
17
|
+
b: Number.parseInt(result[3]!, 16),
|
18
|
+
}
|
19
|
+
: null
|
20
|
+
}
|
21
|
+
|
22
|
+
// Helper function to normalize color values
|
23
|
+
function normalizeColor(color: string | undefined): string {
|
24
|
+
if (!color)
|
25
|
+
return 'transparent'
|
26
|
+
|
27
|
+
// Type guard to ensure color is string before we call string methods
|
28
|
+
if (typeof color === 'string' && color.startsWith('#')) {
|
29
|
+
const rgb = hexToRgb(color)
|
30
|
+
if (rgb) {
|
31
|
+
return `rgb(${rgb.r}, ${rgb.g}, ${rgb.b})`
|
32
|
+
}
|
33
|
+
}
|
34
|
+
return color
|
15
35
|
}
|
16
36
|
|
17
37
|
export function convertTokenColorsToReactStyle(tokenColors: any[]): ReactStyle {
|
18
38
|
const styleMap: ReactStyle = {}
|
19
39
|
|
40
|
+
// Add default styles
|
41
|
+
styleMap.default = {
|
42
|
+
color: '#e1e4e8',
|
43
|
+
backgroundColor: 'transparent',
|
44
|
+
}
|
45
|
+
|
20
46
|
tokenColors.forEach((token) => {
|
21
|
-
|
47
|
+
if (!token.settings || !token.scope)
|
48
|
+
return
|
49
|
+
|
50
|
+
const scopes = Array.isArray(token.scope) ? token.scope : [token.scope]
|
22
51
|
const styleEntries: Array<[string, string]> = []
|
23
52
|
|
24
|
-
if
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
if (settings.
|
29
|
-
|
53
|
+
// Only add styles if they exist
|
54
|
+
if (token.settings.foreground) {
|
55
|
+
styleEntries.push(['color', normalizeColor(token.settings.foreground)])
|
56
|
+
}
|
57
|
+
if (token.settings.background) {
|
58
|
+
styleEntries.push(['backgroundColor', normalizeColor(token.settings.background)])
|
59
|
+
}
|
60
|
+
if (token.settings.fontStyle) {
|
61
|
+
if (token.settings.fontStyle.includes('italic')) {
|
30
62
|
styleEntries.push(['fontStyle', 'italic'])
|
31
|
-
|
63
|
+
}
|
64
|
+
if (token.settings.fontStyle.includes('bold')) {
|
32
65
|
styleEntries.push(['fontWeight', 'bold'])
|
66
|
+
}
|
33
67
|
}
|
34
68
|
|
35
69
|
if (styleEntries.length > 0) {
|
36
|
-
const transformedStyle = transform(styleEntries
|
70
|
+
const transformedStyle = transform(styleEntries)
|
37
71
|
|
38
|
-
|
39
|
-
|
40
|
-
|
72
|
+
scopes.forEach((scope: string) => {
|
73
|
+
if (typeof scope === 'string') {
|
74
|
+
styleMap[scope] = { ...(styleMap[scope] || {}), ...transformedStyle }
|
75
|
+
}
|
41
76
|
})
|
42
77
|
}
|
43
78
|
})
|
@@ -46,12 +81,27 @@ export function convertTokenColorsToReactStyle(tokenColors: any[]): ReactStyle {
|
|
46
81
|
}
|
47
82
|
|
48
83
|
export function getRNStylesFromShikiStyle(theme: any): HighlighterStyleSheet {
|
49
|
-
|
50
|
-
if (!theme.tokenColors) {
|
84
|
+
if (!theme || !theme.tokenColors) {
|
51
85
|
console.error('Provided theme does not contain \'tokenColors\'.')
|
52
86
|
return {}
|
53
87
|
}
|
54
|
-
|
55
|
-
|
56
|
-
|
88
|
+
|
89
|
+
try {
|
90
|
+
// Convert theme colors to React Native styles
|
91
|
+
const styles = convertTokenColorsToReactStyle(theme.tokenColors)
|
92
|
+
|
93
|
+
// Add theme-specific base styles if they exist
|
94
|
+
if (theme.colors) {
|
95
|
+
styles.base = {
|
96
|
+
color: normalizeColor(theme.colors['editor.foreground']),
|
97
|
+
backgroundColor: normalizeColor(theme.colors['editor.background']),
|
98
|
+
}
|
99
|
+
}
|
100
|
+
|
101
|
+
return styles
|
102
|
+
}
|
103
|
+
catch {
|
104
|
+
console.error('Error converting theme styles')
|
105
|
+
return {}
|
106
|
+
}
|
57
107
|
}
|